home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 16 / pascal / dump.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-05-13  |  3.5 KB  |  85 lines

  1. (* dump - Show the contents of a file in hexadecimal and ASCII.  This is not a
  2.     very sophisticated dump program, but it does further illustrate file access
  3.     with Pascal, and it also demonstrates columnar output.  Possible extensions
  4.     are:  GEM interface, ability to divert output to a file or the printer,
  5.     starting position within file, dump in ASCII or hex only, etc. *)
  6.  
  7. PROGRAM dump;
  8.  
  9.   CONST
  10.     chunk_size = 16;    (* Number of bytes to display per line.  16 is almost  *)
  11.                         (* maximum for 80 character lines, since we need 3     *)
  12.                         (* characters for the hexadecimal value (with a space) *)
  13.                         (* and one character for the ASCII.  16*3+16 = 64, and *)
  14.                         (* 8 characters for the file position gives 72.        *)
  15.  
  16.   TYPE
  17.     (* For each line we display, we have to hold all the bytes in a buffer, so
  18.       we can print out the hex and then the ASCII. *)
  19.     chunk_range = 1..chunk_size;
  20.     chunk_buf = PACKED ARRAY[ chunk_range ] OF byte;
  21.  
  22.   VAR
  23.     chunk : chunk_buf;          (* Buffer for bytes of current display line *)
  24.     offset : long_integer;      (* Current file offset position *)
  25.     n : 0..chunk_size;          (* Number of bytes in current chunk *)
  26.     f : PACKED FILE OF byte;    (* Our input file-- it's binary! *)
  27.     name : STRING;              (* Finally, the name of our input file *)
  28.  
  29.   (* dump_chunk - Read a chunk from the input file and display it on the current
  30.       line.  'chunk_size' bytes are always read, except possibly for the last
  31.       chunk, which may be shorter if end-of-file is encountered. *)
  32.  
  33.   PROCEDURE dump_chunk;
  34.  
  35.     VAR
  36.       i : chunk_range;          (* Used to index into the current chunk *)
  37.  
  38.     BEGIN
  39.       (* While we don't yet have a full chunk, and we haven't reached the end of
  40.         the file, read one byte and add it to the chunk.  The variable 'n' is
  41.         used to keep track of how many bytes we have added. *)
  42.       n := 0;
  43.       WHILE (n < chunk_size) AND NOT eof(f) DO
  44.         BEGIN
  45.           n := n + 1;
  46.           chunk[n] := f^;
  47.           get( f );
  48.         END;
  49.       (* OK, we have a chunk of 'n' bytes.  Write the current file offset and
  50.         the hexadecimal byte values. *)
  51.       write( offset:6:h, ': ' );
  52.       FOR i := 1 TO n DO
  53.         write( chunk[i]:2:h, ' ' );
  54.       (* If we have fewer than 'chunk_size' bytes in this chunk, we must put out
  55.         some spaces so the ASCII data lines up with that of the previous line *)
  56.       FOR i := n+1 TO chunk_size DO
  57.         write( '   ' );
  58.       (* Now put out the ASCII data.  If the character we are putting out isn't
  59.         printable, use a period, instead.  We aren't checking for characters
  60.         over 127, since they print OK on the console screen, but if you modify
  61.         this program to go to a printer, you might want to change those to
  62.         periods, also. *)
  63.       write( '"' );
  64.       FOR i := 1 TO n DO
  65.         IF ord(chunk[i]) < ord(' ') THEN
  66.           write( '.' )
  67.         ELSE
  68.           write( chr(chunk[i]) );
  69.       writeln( '"' );
  70.       offset := offset + n;
  71.     END;
  72.  
  73.   (* main routine - Ask the user for the name of the file to dump, open it, and
  74.       dump chunks until end-of-file is reached. *)
  75.  
  76.   BEGIN
  77.     write( 'File to dump: ' );
  78.     readln( name );
  79.     reset( f, name );
  80.  
  81.     offset := 0;
  82.     WHILE NOT eof(f) DO
  83.       dump_chunk;
  84.   END.
  85. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!