home *** CD-ROM | disk | FTP | other *** search
- { -----------------------------------------------------------------------------
-
- NOTICE:
-
- THESE MATERIALS are UNSUPPORTED by OSS! If you do not understand how to
- use them do not contact OSS for help! We will not teach you how to
- program in Pascal. If you find an error in these materials, feel free
- to SEND US A LETTER explaining the error, and how to fix it.
-
- THE BOTTOM LINE:
-
- Use it, enjoy it, but you are on your own when using these materials!
-
-
- DISCLAIMER:
-
- OSS makes no representations or warranties with respect to the contents
- hereof and specifically disclaim all warranties of merchantability or
- fitness for any particular purpose. This document is subject to change
- without notice.
-
- OSS provides these materials for use with Personal Pascal. Use them in
- any way you wish.
-
- -------------------------------------------------------------------------- }
-
-
- { 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.
-