home *** CD-ROM | disk | FTP | other *** search
- (* dump - Show the contents of a file in hexadecimal and ASCII. This is not a
- very sophisticated dump program, but it does further illustrate file access
- with Pascal, and it also demonstrates columnar output. Possible extensions
- are: GEM interface, ability to divert output to a file or the printer,
- starting position within file, dump in ASCII or hex only, etc. *)
-
- PROGRAM dump;
-
- CONST
- chunk_size = 16; (* Number of bytes to display per line. 16 is almost *)
- (* maximum for 80 character lines, since we need 3 *)
- (* characters for the hexadecimal value (with a space) *)
- (* and one character for the ASCII. 16*3+16 = 64, and *)
- (* 8 characters for the file position gives 72. *)
-
- TYPE
- (* For each line we display, we have to hold all the bytes in a buffer, so
- we can print out the hex and then the ASCII. *)
- chunk_range = 1..chunk_size;
- chunk_buf = PACKED ARRAY[ chunk_range ] OF byte;
-
- VAR
- chunk : chunk_buf; (* Buffer for bytes of current display line *)
- offset : long_integer; (* Current file offset position *)
- n : 0..chunk_size; (* Number of bytes in current chunk *)
- f : PACKED FILE OF byte; (* Our input file-- it's binary! *)
- name : STRING; (* Finally, the name of our input file *)
-
- (* dump_chunk - Read a chunk from the input file and display it on the current
- line. 'chunk_size' bytes are always read, except possibly for the last
- chunk, which may be shorter if end-of-file is encountered. *)
-
- PROCEDURE dump_chunk;
-
- VAR
- i : chunk_range; (* Used to index into the current chunk *)
-
- BEGIN
- (* While we don't yet have a full chunk, and we haven't reached the end of
- the file, read one byte and add it to the chunk. The variable 'n' is
- used to keep track of how many bytes we have added. *)
- n := 0;
- WHILE (n < chunk_size) AND NOT eof(f) DO
- BEGIN
- n := n + 1;
- chunk[n] := f^;
- get( f );
- END;
- (* OK, we have a chunk of 'n' bytes. Write the current file offset and
- the hexadecimal byte values. *)
- write( offset:6:h, ': ' );
- FOR i := 1 TO n DO
- write( chunk[i]:2:h, ' ' );
- (* If we have fewer than 'chunk_size' bytes in this chunk, we must put out
- some spaces so the ASCII data lines up with that of the previous line *)
- FOR i := n+1 TO chunk_size DO
- write( ' ' );
- (* Now put out the ASCII data. If the character we are putting out isn't
- printable, use a period, instead. We aren't checking for characters
- over 127, since they print OK on the console screen, but if you modify
- this program to go to a printer, you might want to change those to
- periods, also. *)
- write( '"' );
- FOR i := 1 TO n DO
- IF ord(chunk[i]) < ord(' ') THEN
- write( '.' )
- ELSE
- write( chr(chunk[i]) );
- writeln( '"' );
- offset := offset + n;
- END;
-
- (* main routine - Ask the user for the name of the file to dump, open it, and
- dump chunks until end-of-file is reached. *)
-
- BEGIN
- write( 'File to dump: ' );
- readln( name );
- reset( f, name );
-
- offset := 0;
- WHILE NOT eof(f) DO
- dump_chunk;
- END.
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!