home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / dump.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-11-18  |  4.4 KB  |  112 lines

  1. { -----------------------------------------------------------------------------
  2.  
  3.                                  NOTICE:
  4.  
  5.       THESE MATERIALS are UNSUPPORTED by OSS!  If you do not understand how to
  6.       use them do not contact OSS for help!  We will not teach you how to 
  7.       program in Pascal.  If you find an error in these materials, feel free
  8.       to SEND US A LETTER explaining the error, and how to fix it.
  9.  
  10.       THE BOTTOM LINE:
  11.  
  12.          Use it, enjoy it, but you are on your own when using these materials!
  13.  
  14.  
  15.                                DISCLAIMER:
  16.  
  17.       OSS makes no representations or warranties with respect to the contents
  18.       hereof and specifically disclaim all warranties of merchantability or
  19.       fitness for any particular purpose.   This document is subject to change
  20.       without notice.
  21.       
  22.       OSS provides these materials for use with Personal Pascal.  Use them in
  23.       any way you wish.
  24.  
  25.    -------------------------------------------------------------------------- }
  26.  
  27.  
  28. { dump - Show the contents of a file in hexadecimal and ASCII.  This is not a
  29.     very sophisticated dump program, but it does further illustrate file access
  30.     with Pascal, and it also demonstrates columnar output.  Possible extensions
  31.     are:  GEM interface, ability to divert output to a file or the printer,
  32.     starting position within file, dump in ASCII or hex only, etc. }
  33.  
  34. PROGRAM dump;
  35.  
  36.   CONST
  37.     chunk_size = 16;    { Number of bytes to display per line.  16 is almost  }
  38.                         { maximum for 80 character lines, since we need 3     }
  39.                         { characters for the hexadecimal value (with a space) }
  40.                         { and one character for the ASCII.  16*3+16 = 64, and }
  41.                         { 8 characters for the file position gives 72.        }
  42.  
  43.   TYPE
  44.     { For each line we display, we have to hold all the bytes in a buffer, so
  45.       we can print out the hex and then the ASCII. }
  46.     chunk_range = 1..chunk_size;
  47.     chunk_buf = PACKED ARRAY[ chunk_range ] OF byte;
  48.  
  49.   VAR
  50.     chunk : chunk_buf;          { Buffer for bytes of current display line }
  51.     offset : long_integer;      { Current file offset position }
  52.     n : 0..chunk_size;          { Number of bytes in current chunk }
  53.     f : PACKED FILE OF byte;    { Our input file-- it's binary! }
  54.     name : STRING;              { Finally, the name of our input file }
  55.  
  56.   { dump_chunk - Read a chunk from the input file and display it on the current
  57.       line.  'chunk_size' bytes are always read, except possibly for the last
  58.       chunk, which may be shorter if end-of-file is encountered. }
  59.  
  60.   PROCEDURE dump_chunk;
  61.  
  62.     VAR
  63.       i : chunk_range;          { Used to index into the current chunk }
  64.  
  65.     BEGIN
  66.       { While we don't yet have a full chunk, and we haven't reached the end of
  67.         the file, read one byte and add it to the chunk.  The variable 'n' is
  68.         used to keep track of how many bytes we have added. }
  69.       n := 0;
  70.       WHILE (n < chunk_size) AND NOT eof(f) DO
  71.         BEGIN
  72.           n := n + 1;
  73.           chunk[n] := f^;
  74.           get( f );
  75.         END;
  76.       { OK, we have a chunk of 'n' bytes.  Write the current file offset and
  77.         the hexadecimal byte values. }
  78.       write( offset:6:h, ': ' );
  79.       FOR i := 1 TO n DO
  80.         write( chunk[i]:2:h, ' ' );
  81.       { If we have fewer than 'chunk_size' bytes in this chunk, we must put out
  82.         some spaces so the ASCII data lines up with that of the previous line }
  83.       FOR i := n+1 TO chunk_size DO
  84.         write( '   ' );
  85.       { Now put out the ASCII data.  If the character we are putting out isn't
  86.         printable, use a period, instead.  We aren't checking for characters
  87.         over 127, since they print OK on the console screen, but if you modify
  88.         this program to go to a printer, you might want to change those to
  89.         periods, also. }
  90.       write( '"' );
  91.       FOR i := 1 TO n DO
  92.         IF ord(chunk[i]) < ord(' ') THEN
  93.           write( '.' )
  94.         ELSE
  95.           write( chr(chunk[i]) );
  96.       writeln( '"' );
  97.       offset := offset + n;
  98.     END;
  99.  
  100.   { main routine - Ask the user for the name of the file to dump, open it, and
  101.       dump chunks until end-of-file is reached. }
  102.  
  103.   BEGIN
  104.     write( 'File to dump: ' );
  105.     readln( name );
  106.     reset( f, name );
  107.  
  108.     offset := 0;
  109.     WHILE NOT eof(f) DO
  110.       dump_chunk;
  111.   END.
  112.