home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / l / lzw4p12.zip / TEST_LZW.PAS < prev    next >
Pascal/Delphi Source File  |  1993-02-21  |  6KB  |  205 lines

  1. (*
  2. **   TEST_LZW.PAS      Copyright (C) 1992 by MarshallSoft Computing, Inc.
  3. **
  4. **   This program is used to compress, expand, and verify each specified
  5. **   file. It's purpose is for you to test the LZW4P library on your own
  6. **   files. Your files are never modified. However, you should NOT have a
  7. **   file named "XXX.XXX" or "YYY.YYY".  Compression ratios are printed
  8. **   for each file compressed. For example, to compress all files ending
  9. **   in *.PAS in your current directory, type:
  10. **
  11. **        TEST_LZW *.PAS
  12. *)
  13.  
  14.  
  15. program TEST_LZW;
  16. uses dos, crt, memory, rw_io, hex_io, lzw_errs, LZW4P;
  17.  
  18. type
  19.   String12 = String[12];
  20.   AllocMemoryType = function(Size : Word) : Pointer;
  21.   FreeMemoryType  = function(P : Pointer; Size : Word) : Integer;
  22.  
  23. Var
  24.   FileName     : String12;
  25.   InpFileName  : String12;
  26.   OutFileName  : String12;
  27.   Inp1FileName : String12;
  28.   Inp2FileName : String12;
  29.   MemoryP      : Pointer;
  30.   AllocMemoryP : Pointer;
  31.   FreeMemoryP  : Pointer;
  32.   ReaderP      : Pointer;
  33.   WriterP      : Pointer;
  34.   Size         : Integer;
  35.   Code         : Integer;
  36.   i, x         : Integer;
  37.   DirInfo      : SearchRec;
  38.   F1, F2       : file;
  39.   Buffer1      : array [1..1024] of Byte;
  40.   Buffer2      : array [1..1024] of Byte;
  41.   NumRead1     : Integer;
  42.   NumRead2     : Integer;
  43.   Index        : LongInt;
  44.   Ratio        : Real;
  45.   ReaderCnt    : Real;
  46.   WriterCnt    : Real;
  47.   Count        : Integer;
  48. begin
  49.   (* get file specs *)
  50.   if ParamCount <> 1 then
  51.     begin
  52.       writeln('Usage: TEST_LZW <filespec>');
  53.       halt;
  54.     end;
  55.   (* sign on *)
  56.   writeln('TEST_LZW 1.0: Type any key to abort...');
  57.   writeln;
  58.   Count := 0;
  59.   (* get pointers *)
  60.   AllocMemoryP := @AllocMemory;
  61.   FreeMemoryP  := @FreeMemory;
  62.   ReaderP := @Reader;
  63.   WriterP := @Writer;
  64.   (* Initialize LZW *)
  65.   Code :=  InitLZW(AllocMemoryP);
  66.   writeln;
  67.   (* consider each file in FileSpec *)
  68.   FindFirst(ParamStr(1),0,DirInfo);
  69.   while DosError = 0 do
  70.   begin (* while *)
  71.     FileName := DirInfo.Name;
  72.     (*writeln('<',FileName,'>');*)
  73.     if (FileName<>'XXX.XXX') and (FileName<>'YYY.YYY') then
  74.       begin (* process file *)
  75.         if KeyPressed then
  76.           begin
  77.             writeln;
  78.             writeln('Aborted by USER');
  79.             Halt;
  80.           end;
  81.         Count := Count + 1;
  82.         InpFileName := FileName;
  83.         OutFileName := 'XXX.XXX';
  84.         (***** COMPRESSION *****)
  85.         (* open input file for compress *)
  86.         Code := ReaderOpen(InpFileName);
  87.         if Code <> 0 then
  88.           begin
  89.             writeln('Cannot open ',InpFileName,' for input. IOResult = ',Code);
  90.             halt;
  91.           end;
  92.         (* open output *)
  93.         Code := WriterOpen(OutFileName);
  94.         if Code <> 0 then
  95.           begin
  96.             writeln('Cannot open ',OutFileName,' for output. IOResult = ',Code);
  97.             halt;
  98.           end;
  99.         (* compress *)
  100.         write('COMPRESSING ',FileName:12,' ');
  101.         Code := Compress(ReaderP,WriterP);
  102.         if Code < 0 then
  103.           begin
  104.             SayError(Code);
  105.           end;
  106.         (* report compression ratio *)
  107.         if ReaderCount > 0 then
  108.           begin
  109.             ReaderCnt := ReaderCount;
  110.             WriterCnt := WriterCount;
  111.             Ratio := WriterCnt / ReaderCnt;
  112.             writeln('OK',Ratio:6:2);
  113.           end
  114.         else writeln('???');
  115.         (* close input & output *)
  116.         Code := ReaderClose;
  117.         Code := WriterClose;
  118.         (***** EXPANSION *****)
  119.         InpFileName := 'XXX.XXX';
  120.         OutFileName := 'YYY.YYY';
  121.         (* open input file for expansion *)
  122.         Code := ReaderOpen(InpFileName);
  123.         if Code <> 0 then
  124.           begin
  125.             writeln('Cannot open ',InpFileName,' for input. IOResult = ',Code);
  126.             halt;
  127.           end;
  128.         (* open output *)
  129.         Code := WriterOpen(OutFileName);
  130.         if Code <> 0 then
  131.           begin
  132.             writeln('Cannot open ',OutFileName,' for output. IOResult = ',Code);
  133.             halt;
  134.           end;
  135.         (* expand *)
  136.         write('  EXPANDING ',FileName:12,' ');
  137.         Code := Expand(ReaderP,WriterP);
  138.         if Code < 0 then
  139.           begin
  140.             SayError(Code);
  141.           end;
  142.         (* close input & output *)
  143.         Code := ReaderClose;
  144.         Code := WriterClose;
  145.         writeln('OK');
  146.         (*** COMPARING ***)
  147.         Inp1FileName := DirInfo.Name;
  148.         Inp2FileName := 'YYY.YYY';
  149.         (* open 1st input *)
  150.         Assign(F1,Inp1FileName);
  151. {$I-}
  152.         Reset(F1,1);
  153. {$I+}
  154.         if IOResult <> 0 then
  155.           begin
  156.             writeln('Cannot open ',Inp1FileName,' for input. IOResult = ',IOResult);
  157.             halt;
  158.           end;
  159.         (* open 2nd input *)
  160.         Assign(F2,Inp2FileName);
  161. {$I-}
  162.         Reset(F2,1);
  163. {$I+}
  164.         if IOResult <> 0 then
  165.           begin
  166.             writeln('Cannot open ',Inp2FileName,' for input. IOResult = ',IOResult);
  167.             halt;
  168.           end;
  169.         (* compare file byte for byte *)
  170.         write('  COMPARING ',FileName:12,' ');
  171.         Index := 0;
  172.         repeat
  173.           (* input 1st buffer *)
  174.           BlockRead(F1,Buffer1,Sizeof(Buffer1),NumRead1);
  175.           BlockRead(F2,Buffer2,Sizeof(Buffer2),NumRead2);
  176.           if NumRead1 <> NumRead2 then
  177.             begin
  178.               writeln('Error comparing files');
  179.               Halt;
  180.             end;
  181.           for i:= 1 to NumRead1 do
  182.             begin
  183.               Index := Index + 1;
  184.               if Buffer1[i] <> Buffer2[i] then
  185.                 begin
  186.                   writeln('Mismatch: Index=',Index,',Byte1=');
  187.                   WriteHexByte(Buffer1[i]);
  188.                   writeln(',Byte2=');
  189.                   WriteHexByte(Buffer2[i]);
  190.                   Halt;
  191.                 end;
  192.             end;
  193.         until (NumRead1=0) or (NumRead2=0);
  194.         writeln('OK');
  195.         writeln;
  196.         close(F1);
  197.         close(F2);
  198.       end; (* process file *)
  199.     (* get next filename *)
  200.     FindNext(DirInfo);
  201.   end; (* while *)
  202.   (* Terminate LZW *)
  203.   writeln(Count,' files processed.');
  204.   Code := TermLZW(FreeMemoryP);
  205. end.