home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / compare.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-11-18  |  4.5 KB  |  110 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. { compare - Binary file comparison program.  This program is just a demon-
  29.     stration of file access in Personal Pascal.  In the current version, single
  30.     byte access to a file is not that fast, so to increase the speed of this
  31.     program, you might replace the single-byte accesses with block-oriented
  32.     GEMDOS reads into a buffer.  The algorithm for comparison is very simple;
  33.     keep track of the current offset position within the two files, get a pair
  34.     of bytes to compare, and print any that don't match.  If one file is
  35.     shorter than the other, report that fact, too.  Since we are just showing
  36.     binary file access here, there is no GEM interface (if we were selling the
  37.     program, you can bet there would be!). }
  38.  
  39. PROGRAM Compare ;
  40.  
  41.   CONST
  42.     max_fn = 80 ;       { Maximum length of a TOS file name }
  43.  
  44.   TYPE
  45.     file_name = STRING [ max_fn ] ;     { A string big enough to hold a name }
  46.     byte_file = PACKED FILE OF byte ;   { The declaration for a binary file }
  47.  
  48.   VAR
  49.     f1, f2 : byte_file ;        { The buffer variables for the two files we }
  50.                                 { will be accessing. }
  51.     name1, name2 : file_name ;  { And their names }
  52.  
  53.   { compare_files - This routine does the actual byte-for-byte compare,
  54.       assuming that the two file buffer variable parameters have already been
  55.       opened to the two files to compare. }
  56.  
  57.   PROCEDURE compare_files( VAR f1, f2 : byte_file ) ;
  58.  
  59.     VAR
  60.       c1, c2 : byte ;           { These will hold a pair of bytes to compare }
  61.       offset : long_integer ;   { Current offset within the two files }
  62.       errors : boolean ;        { True if non-matching bytes have been found }
  63.  
  64.     BEGIN
  65.       offset := 0 ;             { We start at the beginning with no errors }
  66.       errors := false ;
  67.       { While we aren't at the end of either file, get a pair of bytes and
  68.         compare them.  If they are not equal, print out the current offset,
  69.         and the two values.  Also set the 'errors' flag to true, so we will
  70.         say at the end that the files don't match. }
  71.       WHILE NOT eof(f1) AND NOT eof(f2) DO
  72.         BEGIN
  73.           c1 := f1^ ; get( f1 ) ;
  74.           c2 := f2^ ; get( f2 ) ;
  75.           IF c1 <> c2 THEN
  76.             BEGIN
  77.               errors := true ;
  78.               writeln( offset:6:h, ': ', c1:2:h, ' ', c2:2:h ) ;
  79.             END ;
  80.           offset := offset + 1 ;
  81.         END ;
  82.       { If we reached the end of one file, but not the other, tell the user
  83.         which file is shorter. }
  84.       IF eof(f1) AND NOT eof(f2) THEN
  85.         writeln( 'File 1 is shorter' )
  86.       ELSE IF eof(f2) AND NOT eof(f1) THEN
  87.         writeln( 'File 2 is shorter' )
  88.       { If neither is shorter, and we never got a non-matching pair, tell the
  89.         user the files are the same. }
  90.       ELSE IF NOT errors THEN
  91.         writeln( 'Files compare exactly' ) ;
  92.     END ;
  93.  
  94.   { main routine - Ask the user for the names of the two files to compare, open
  95.       the files, and call 'compare_files' to do the real work.  We are not
  96.       getting the parameters from a command line so the program can be called
  97.       from the desktop or the Pascal manager.  Those of you using command-line
  98.       interfaces might want to pull the parameters from the command line,
  99.       instead. }
  100.  
  101.   BEGIN
  102.     write( 'File 1: ' ) ;
  103.     readln( name1 ) ;
  104.     reset( f1, name1 ) ;
  105.     write( 'File 2: ' ) ;
  106.     readln( name2 ) ;
  107.     reset( f2, name2 ) ;
  108.     compare_files( f1, f2 ) ;
  109.   END.
  110.