home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0044_Unit for Large Text Files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-26  |  3.3 KB  |  136 lines

  1. {
  2. > Unfortunately this won't work, for some reason BP ignores the FileMode
  3. > variable when dealing with a file of type Text.  I modified one of the
  4. > RTL files to correct this problem
  5.  
  6.    This is not directed specifically to you, but to anyone that's interested in
  7. a unit that can read big textfiles (like loggfiles, nodelists and so on) in a
  8. fast way. It can only handle one file at a time and with later versions of TP/
  9. BP the speed increase isn't as dramatic as it was in older versions, that
  10. hadn't stuff like buffered textfiles. I use it a lot (even in network
  11. environments -- with FileMode set to $40).
  12. }
  13.  
  14. unit FileUtil; { unit for fast reading of large textfiles }
  15.  
  16. { Donated to the PD by Björn Felten @ 2:203/208 -- Nov 1994 }
  17. { From the package BF_UTIL.ZIP }
  18.  
  19. interface
  20.  
  21. var
  22.     Line    :string; { where the read line is returned }
  23.                      { a global is used for higher speed }
  24.     XEof    :boolean;
  25.  
  26. procedure XReset;
  27. procedure XReadLn(var FileHandle: file);
  28. procedure XOpen(var FileHandle: file; FileName: string);
  29. procedure XClose(var FileHandle: file);
  30.  
  31. implementation
  32.  
  33. const
  34.     BufSiz = $e000;
  35.  
  36. type
  37.     Buffer = array[1..BufSiz+2] of char;
  38.     BufPtr = ^Buffer;
  39.  
  40. var
  41.     TempLine    :string;
  42.     Buff        :BufPtr;
  43.     Segm,Offs,
  44.     NumRead,
  45.     LastPos,
  46.     FirstPos    : Word;
  47.  
  48. procedure XReset;
  49. begin
  50.    FirstPos:=0;
  51.    LastPos:=FirstPos;
  52.    XEof:=false
  53. end
  54. ;
  55.  
  56. procedure XReadLn(var FileHandle: file);
  57. begin
  58.     asm
  59.       mov  es,Segm      { just to be sure }
  60.       mov  bx,es
  61.       mov  dx,ds
  62.       cld
  63.       mov  di,LastPos   { start within file }
  64.       add  di,Offs
  65.       mov  al,13        { set up a search for CR }
  66.       mov  cx,257       { max 255 char plus overhead }
  67.       mov  si,di
  68.       repnz scasb       { look for CR }
  69.       mov  di,offset Line   { put it into string }
  70.       neg  cl           { get string length }
  71.       mov  [di],cl      { put it in first position }
  72.       je   @empty
  73.       inc  di
  74.       mov  ds,bx        { swap es and ds}
  75.       mov  es,dx
  76.       rep  movsb        { move it }
  77. @empty:
  78.       lodsw             { skip CR/LF }
  79.       mov  ds,dx        { restore ds }
  80.       sub  si,Offs
  81.       mov  LastPos,si   { save last position }
  82.  end
  83.  ;
  84.  if LastPos>=BufSiz then
  85.  begin
  86.    TempLine:=Line; { handle partial line at block boundary }
  87.    blockread(FileHandle,Buff^,BufSiz,NumRead);
  88.    XReset;
  89.    XReadLn(FileHandle);
  90.    Line:=TempLine+Line
  91.  end
  92.  ;
  93.  XEof:=(LastPos>=NumRead)
  94. end;
  95.  
  96. procedure XOpen(var FileHandle: file; FileName: string);
  97. begin
  98.   assign(FileHandle,FileName);
  99.   (*$I-*) reset(FileHandle,1) (*$I+*)
  100.   ;
  101.   if IoResult<>0 then
  102.   begin
  103.      writeln;
  104.      writeln('Can''t find ',FileName,' in this directory!');
  105.      writeln;
  106.      halt(2)
  107.   end
  108.   ;
  109.   if memavail<BufSiz+$400 then
  110.   begin
  111.      writeln;
  112.      writeln('Need at least ',BufSiz shr 10,'kb RAM!');
  113.      writeln;
  114.      halt(3)
  115.   end
  116.   ;
  117.   new(Buff);
  118.   Segm:=seg(Buff^);
  119.   Offs:=ofs(Buff^);
  120.   Buff^[BufSiz+1]:=#13;  { stuff some CR's in to avoid running }
  121.   Buff^[BufSiz+2]:=#13;  { off limits if unlucky w/ boundaries }
  122.   blockread(FileHandle,Buff^,BufSiz,NumRead);
  123.   Buff^[NumRead+1]:=#13;
  124.   Buff^[NumRead+2]:=#13;
  125.   XReset
  126. end
  127. ;
  128. procedure XClose(var FileHandle: file);
  129. begin
  130.    close(FileHandle);
  131.    dispose(Buff)
  132. end
  133. ;
  134.  
  135. end.
  136.