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

  1. (* compare - Binary file comparison program.  This program is just a demon-
  2.     stration of file access in Personal Pascal.  In the current version, single
  3.     byte access to a file is not that fast, so to increase the speed of this
  4.     program, you might replace the single-byte accesses with block-oriented
  5.     GEMDOS reads into a buffer.  The algorithm for comparison is very simple;
  6.     keep track of the current offset position within the two files, get a pair
  7.     of bytes to compare, and print any that don't match.  If one file is
  8.     shorter than the other, report that fact, too.  Since we are just showing
  9.     binary file access here, there is no GEM interface (if we were selling the
  10.     program, you can bet there would be!). *)
  11.  
  12. PROGRAM Compare ;
  13.  
  14.   CONST
  15.     max_fn = 80 ;       (* Maximum length of a TOS file name *)
  16.  
  17.   TYPE
  18.     file_name = STRING [ max_fn ] ;     (* A string big enough to hold a name *)
  19.     byte_file = PACKED FILE OF byte ;   (* The declaration for a binary file *)
  20.  
  21.   VAR
  22.     f1, f2 : byte_file ;        (* The buffer variables for the two files we *)
  23.                                 (* will be accessing. *)
  24.     name1, name2 : file_name ;  (* And their names *)
  25.  
  26.   (* compare_files - This routine does the actual byte-for-byte compare,
  27.       assuming that the two file buffer variable parameters have already been
  28.       opened to the two files to compare. *)
  29.  
  30.   PROCEDURE compare_files( VAR f1, f2 : byte_file ) ;
  31.  
  32.     VAR
  33.       c1, c2 : byte ;           (* These will hold a pair of bytes to compare *)
  34.       offset : long_integer ;   (* Current offset within the two files *)
  35.       errors : boolean ;        (* True if non-matching bytes have been found *)
  36.  
  37.     BEGIN
  38.       offset := 0 ;             (* We start at the beginning with no errors *)
  39.       errors := false ;
  40.       (* While we aren't at the end of either file, get a pair of bytes and
  41.         compare them.  If they are not equal, print out the current offset,
  42.         and the two values.  Also set the 'errors' flag to true, so we will
  43.         say at the end that the files don't match. *)
  44.       WHILE NOT eof(f1) AND NOT eof(f2) DO
  45.         BEGIN
  46.           c1 := f1^ ; get( f1 ) ;
  47.           c2 := f2^ ; get( f2 ) ;
  48.           IF c1 <> c2 THEN
  49.             BEGIN
  50.               errors := true ;
  51.               writeln( offset:6:h, ': ', c1:2:h, ' ', c2:2:h ) ;
  52.             END ;
  53.           offset := offset + 1 ;
  54.         END ;
  55.       (* If we reached the end of one file, but not the other, tell the user
  56.         which file is shorter. *)
  57.       IF eof(f1) AND NOT eof(f2) THEN
  58.         writeln( 'File 1 is shorter' )
  59.       ELSE IF eof(f2) AND NOT eof(f1) THEN
  60.         writeln( 'File 2 is shorter' )
  61.       (* If neither is shorter, and we never got a non-matching pair, tell the
  62.         user the files are the same. *)
  63.       ELSE IF NOT errors THEN
  64.         writeln( 'Files compare exactly' ) ;
  65.     END ;
  66.  
  67.   (* main routine - Ask the user for the names of the two files to compare, open
  68.       the files, and call 'compare_files' to do the real work.  We are not
  69.       getting the parameters from a command line so the program can be called
  70.       from the desktop or the Pascal manager.  Those of you using command-line
  71.       interfaces might want to pull the parameters from the command line,
  72.       instead. *)
  73.  
  74.   BEGIN
  75.     write( 'File 1: ' ) ;
  76.     readln( name1 ) ;
  77.     reset( f1, name1 ) ;
  78.     write( 'File 2: ' ) ;
  79.     readln( name2 ) ;
  80.     reset( f2, name2 ) ;
  81.     compare_files( f1, f2 ) ;
  82.   END.
  83. {{{{{