home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / pascal / lzw4p12.zip / RW_IO.PAS < prev    next >
Pascal/Delphi Source File  |  1993-02-22  |  4KB  |  145 lines

  1. (*   RW_IO.PAS
  2. **
  3. **   Reader/Writer Buffered I/O
  4. **
  5. **   Reader() and Writer() are called directly by the LZW4PLIB.ASM code.
  6. **   They should never be called by your application code.
  7. **
  8. **   The other functions are never called by the LZW4PLIB.ASM code, but are
  9. *    called only by your application routines.
  10. **
  11. **   Note that only a Reader() and Writer() function is required by the
  12. **   LZW4PLIB.ASM code. This means that you have complete control over data
  13. **   coming into and out of the compression/expansion code. Instead of
  14. **   reading or writing to disk, you can just as easily read/write to a
  15. **   buffer, serial port, etc. You just have to write the Reader() and
  16. **   Writer() code.
  17. *)
  18.  
  19. unit RW_IO;
  20.  
  21. interface
  22.  
  23. type
  24.   String40 = String[40];
  25.   String12 = String[12];
  26.  
  27. function ReaderOpen(Filename : String12) : Integer;
  28. function Reader : Integer;
  29. function ReaderClose : Integer;
  30. function ReaderCount : LongInt;
  31.  
  32. function WriterOpen(Filename : String12) : Integer;
  33. function Writer(TheByte : Byte) : Integer;
  34. function WriterClose : Integer;
  35. function WriterCount : LongInt;
  36.  
  37. implementation
  38.  
  39. const
  40.    BUFFER_SIZE = 2048;
  41.  
  42. type
  43.    IOstruct = record
  44.       Filename : String12;
  45.       Handle   : File;
  46.       Left     : Integer;
  47.       Right    : Integer;
  48.       Count    : LongInt;
  49.       Buffer   : array[0..BUFFER_SIZE-1] of Byte;
  50.    end;
  51.  
  52. var
  53.    InpCtrl : IOstruct;
  54.    OutCtrl : IOstruct;
  55.  
  56. function ReaderOpen(Filename : String12) : Integer;
  57. begin
  58.    InpCtrl.Filename := Filename;
  59. {$I-}
  60.    Assign(InpCtrl.Handle,InpCtrl.Filename);
  61.    Reset(InpCtrl.Handle,1);
  62. {$I+}
  63.    InpCtrl.Left := 0;
  64.    InpCtrl.Right := 0;
  65.    InpCtrl.Count := 0;
  66.    ReaderOpen := IOResult;
  67. end;
  68.  
  69. function Reader : Integer;
  70. label 999;
  71. var
  72.   TheByte : Byte;
  73. begin
  74.   if InpCtrl.Left=InpCtrl.Right then
  75.      begin
  76.         (* read next buffer *)
  77.         InpCtrl.Left := 0;
  78.         BlockRead(InpCtrl.Handle,InpCtrl.Buffer,BUFFER_SIZE,InpCtrl.Right);
  79.         if InpCtrl.Right <= 0 then
  80.            begin
  81.              Reader := -1;
  82.              goto 999;
  83.            end;
  84.      end;
  85.   (* return next byte in buffer *)
  86.   TheByte := InpCtrl.Buffer[InpCtrl.Left];
  87.   InpCtrl.Left := InpCtrl.Left + 1;
  88.   InpCtrl.Count := InpCtrl.Count + 1;
  89.   Reader := TheByte;
  90. 999 : end;
  91.  
  92. function ReaderClose : Integer;
  93. begin
  94.   close(InpCtrl.Handle);
  95. end;
  96.  
  97. function ReaderCount : LongInt;
  98. begin
  99.   ReaderCount := InpCtrl.Count;
  100. end;
  101.  
  102. function WriterOpen(Filename : String12) : Integer;
  103. begin
  104.    OutCtrl.Filename := Filename;
  105. {$I-}
  106.    Assign(OutCtrl.Handle,OutCtrl.Filename);
  107.    Rewrite(OutCtrl.Handle,1);
  108. {$I+}
  109.    OutCtrl.Left := 0;
  110.    OutCtrl.Right := 0;
  111.    OutCtrl.Count := 0;
  112.    WriterOpen := IOResult;
  113. end;
  114.  
  115. function Writer(TheByte : Byte) : Integer;
  116. begin
  117.   OutCtrl.Count := OutCtrl.Count + 1;
  118.   if (OutCtrl.Count and $0fff) = 0 then write('.');
  119.   OutCtrl.Buffer[OutCtrl.Right] := TheByte;
  120.   OutCtrl.Right := OutCtrl.Right + 1;
  121.   if OutCtrl.Right = BUFFER_SIZE then
  122.     begin
  123.       (* write buffer to disk *)
  124.        BlockWrite(OutCtrl.Handle,OutCtrl.Buffer,BUFFER_SIZE);
  125.        OutCtrl.Right := 0;
  126.     end;
  127.   Writer := 0;
  128. end;
  129.  
  130. function WriterClose : Integer;
  131. begin
  132.   if OutCtrl.Right > 0 then
  133.     begin
  134.        BlockWrite(OutCtrl.Handle,OutCtrl.Buffer,OutCtrl.Right);
  135.        OutCtrl.Right := 0;
  136.     end;
  137.   close(OutCtrl.Handle);
  138. end;
  139.  
  140. function WriterCount : LongInt;
  141. begin
  142.   WriterCount := OutCtrl.Count;
  143. end;
  144.  
  145. end.