home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / TPLZH019 / LZ.PAS < prev    next >
Pascal/Delphi Source File  |  1992-12-25  |  3KB  |  92 lines

  1. {$A+,B-,D+,E+,F+,I-,L-,N-,O-,R-,S-,V-}
  2. {$M 1024,60000,60000}
  3. program LZH_Test;
  4. uses
  5.   LZH;
  6. var
  7.   infile,outfile: file;
  8.   s: String[60];
  9.   Hufmode : boolean;
  10.  
  11.   procedure Error (msg: String);
  12.   begin
  13.     writeln(msg);
  14.     HALT(1)
  15.   end;
  16.  
  17. {$F+}
  18.  
  19.   procedure ReadNextBlock; {This routine handles reading of input data}
  20.  
  21.   begin
  22.     If Hufmode then write(LZHMem^.textsize,#13);
  23.     LZHMem^.inptr:= 0;
  24.     BlockRead(infile,LZHMem^.inbuf,sizeof(LZHMem^.inbuf),LZHMem^.inend);
  25.     if IoResult>0 then Error('! Error reading input file');
  26.   end;
  27.  
  28.   procedure WriteNextBlock; {This routine handles reading of output data}
  29.   var
  30.     wr: word;
  31.   begin
  32.     BlockWrite(outfile,LZHMem^.outbuf,LZHMem^.outptr,wr);
  33.     If Not Hufmode then write(LZHMem^.count,#13);
  34.     if (IoResult>0) or (wr<LZHMem^.outptr) then
  35.        Error('! Error writing output file');
  36.     LZHMem^.outptr:= 0;
  37.   end;
  38.  
  39.   procedure OpenInput (fn: String);
  40.   begin
  41.     assign(infile,fn); reset(infile,1);
  42.     if IoResult>0 then Error('! Can''t open input file');
  43.   end;
  44.  
  45.   procedure OpenOutput (fn: String);
  46.   begin
  47.  
  48.     assign(outfile,fn); rewrite(outfile,1);
  49.     if IoResult>0 then Error('! Can''t open output file');
  50.     LZHMem^.outend:= sizeof(LZHMem^.Outbuf);
  51.     LZHMem^.outptr:= 0;
  52.   end;
  53.  
  54. Var
  55.    test : Word;
  56.    RData  : Byte;
  57.  
  58. begin
  59.      Hufmode := false;
  60.      Writeln ('Huffman Compression Engine v',EngineVer);
  61.      if ParamCount<>3 then begin
  62.         writeln('Usage: lz e(compression)|d(uncompression) infile outfile');
  63.         HALT(1);
  64.      end;
  65.      WriteFromBuffer:= WriteNextBlock;
  66.      ReadToBuffer:= ReadNextBlock;
  67.      InitLZH;    {This routine should be called before any LZHMem calls}
  68.      OpenInput(ParamStr(2));
  69.      OpenOutput(ParamStr(3));
  70.      s:= ParamStr(1);
  71.      case s[1] of
  72.           'e','E': Begin
  73.                         Hufmode := true;
  74.                         LZHMem^.Ebytes:= filesize(infile);
  75.                         Writeln (LZHMem^.Ebytes);
  76.                         Encode;     {Call black box routine to compress}
  77.                         writeln('input:  ',LZHMem^.textsize,' bytes');
  78.                         writeln('output: ',LZHMem^.codesize,' bytes');
  79.                         writeln('compression: ',LZHMem^.textsize*100 DIV LZHMem^.codesize,'%');
  80.  
  81.                         End;
  82.      'd','D': Decode   {Call routine to decompress}
  83.    else
  84.      Error('! Use [D] for Decode or [E] for Encode')
  85.    end;
  86.    close(infile); if IoResult>0 then Error('! Error closing input file');
  87.    close(outfile); if IoResult>0 then Error('! Error closing output file');
  88.    DInitLZH;
  89.  
  90.  
  91.    end.
  92.