Advertisement
Guest User

FDCTextFileTools.pas

a guest
Dec 11th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.70 KB | None | 0 0
  1. unit FDCTextFileTools;
  2. interface
  3. uses
  4.   SysUtils;
  5.  
  6. const
  7.   UserDataSize = {$IFNDEF Win32}16 {$else} 32 {$ENDIF};
  8.   MaxTxtBufSize      = 1024*1024*1024;
  9.   FastTextFlushTreshold  = 512;
  10.  
  11.  
  12. TYPE
  13.   pFile = ^File; {Pointer to a binary file}
  14.   pFastTextRec = ^RFastTextRec; {TTextRec.UserData type cast}
  15.   RFastTextRec = PACKED RECORD
  16.     DataFile : pFile;    {Pointer to the "text" file}
  17.     Unused : ARRAY[SizeOf(pFile)+1..UserDataSize] OF Byte; {not used}
  18.   END; {REC RFastTextRec}
  19.  
  20. PROCEDURE AssignFastText(VAR F:Text; Name:String; BufferSize:Integer);
  21.  
  22. function FixSize (var f; NewSize : longint) : word;
  23. function OpenOutFile (var f; path : string; Create, ResetSize : boolean) : word;
  24.  
  25. implementation
  26.  
  27. function FixSize (var f; NewSize : longint) : word;
  28.   var
  29.     Handle : word absolute f;
  30.     fil : file absolute f;
  31.     rc : integer;
  32.   begin
  33.   {$IFOPT I+} {$i-} {$DEFINE IPlus} {$ENDIF}
  34.     rewrite(fil, 1);
  35.     rc := ioresult;
  36.     if rc <> 0
  37.     then begin
  38.       FixSize := rc;
  39.       exit;
  40.     end;
  41.     reset(fil, 1);
  42.     rc := ioresult;
  43.     FixSize := rc;
  44.   {$IFDEF IPlus} {$i+} {$UNDEF IPlus} {$ENDIF}
  45.   end; {FixSize}
  46.  
  47.  
  48.  
  49. function OpenOutFile (var f; path : string; Create, ResetSize : boolean) : word;
  50.   var
  51.     fm      : Byte;
  52.     fil     : file absolute f;
  53.     iores   : integer;
  54.   begin
  55.     fm:=FileMode;
  56.     filemode := $42;
  57.     assign (fil, path);
  58.   {$IFOPT I+} {$i-} {$DEFINE IPlus} {$ENDIF}
  59.     reset (fil, 1);
  60.   {$IFDEF IPlus} {$i+} {$UNDEF IPlus} {$ENDIF}
  61.     iores := ioresult;
  62.     if iores <> 0
  63.     then begin
  64.       if not Create
  65.       then begin
  66.         OpenOutFile := iores;
  67.         FileMode:=fm;
  68.         exit;
  69.       end;
  70.     {$IFOPT I+} {$i-} {$DEFINE IPlus} {$ENDIF}
  71.       rewrite (fil, 1);
  72.     {$IFDEF IPlus} {$i+} {$UNDEF IPlus} {$ENDIF}
  73.       iores := ioresult;
  74.       if iores <> 0
  75.       then begin
  76.         OpenOutFile := iores;
  77.         FileMode:=fm;
  78.         exit;
  79.       end;
  80.     {$IFOPT I+} {$i-} {$DEFINE IPlus} {$ENDIF}
  81.       reset (fil, 1);          { Filemode: 2 -> $42 }
  82.     {$IFDEF IPlus} {$i+} {$UNDEF IPlus} {$ENDIF}
  83.       iores := ioresult;
  84.       if iores <> 0
  85.       then begin
  86.         OpenOutFile := iores;
  87.         FileMode:=fm;
  88.         exit;
  89.       end;
  90.     end;
  91.     if ResetSize
  92.     then begin
  93.       OpenOutFile := FixSize (fil, 0);
  94.     end
  95.     else OpenOutFile := 0;
  96.     FileMode:=fm;
  97.   end; {OpenOutFile}
  98.  
  99.  
  100.  
  101. FUNCTION FastTextRead(VAR F:TTextRec):Integer; FAR;
  102. { - Called by Read, ReadLn, Eof, Eoln, SeekEof, SeekEoln}
  103. BEGIN
  104.   BlockRead(RFastTextRec(F.UserData).DataFile^, F.BufPtr^, F.BufSize, F.BufEnd);
  105.   F.BufPos:=0;
  106.   FastTextRead:=IOResult; {Return BlockRead IO result}
  107. END; {FUNC FastTextRead}
  108.  
  109. FUNCTION FastTextWrite(VAR F:TTextRec):Integer; FAR;
  110. { - Called by Write, Writeln, Close, and also by fmOutput mode flush}
  111. BEGIN
  112.   BlockWrite(RFastTextRec(F.UserData).DataFile^, F.BufPtr^, F.BufPos);
  113.   F.BufPos:=0;
  114.   FastTextWrite:=IOResult; {Return BlockWrite IO result}
  115. END; {FUNC FastTextWrite}
  116.  
  117. FUNCTION FastTextWriteFlush(VAR F:TTextRec):Integer; FAR;
  118. { - Flush text buffer to disk}
  119. BEGIN
  120.   IF (F.BufSize-F.BufPos) < FastTextFlushTreshold
  121.   THEN FastTextWriteFlush:=FastTextWrite(F) {Write flush}
  122.   ELSE FastTextWriteFlush:=0;
  123. END; {FUNC FastTextWriteFlush}
  124.  
  125. FUNCTION FastTextOpen(VAR F:TTextRec):Integer; FAR;
  126. { - Called by Reset, Append, or Rewrite}
  127. VAR
  128.   FastText : pFastTextRec;
  129.   fm : Byte;
  130.   IORes : Word;
  131. BEGIN
  132.   IORes:=0;
  133.   FastText:=@F.UserData;
  134.   IF not Assigned(F.BufPtr)
  135.   THEN GetMem(F.BufPtr, F.BufSize);   {Allocate buffer}
  136.   IF not Assigned(FastText^.DataFile)
  137.   THEN New(FastText^.DataFile);     {Make space for the device driver file}
  138.   Assign(FastText^.DataFile^, F.Name);  {and assign the file name to it}
  139.   CASE F.Mode OF        {The ,1 indicates a 1 byte block size}
  140.     fmInput  : BEGIN
  141.       fm:=FileMode;
  142.       FileMode:=0;
  143.       Reset(FastText^.DataFile^, 1);
  144.       IORes:=IOResult;
  145.       F.InOutFunc:=@FastTextRead;
  146.       F.FlushFunc:=nil;
  147.       FileMode:=fm;
  148.     END;
  149.     fmInOut  : BEGIN {fmInOut is passed by Append}
  150.       IORes:=OpenOutFile(FastText^.DataFile^, F.Name, False, False);
  151.       IF IORes=0
  152.       THEN BEGIN
  153.         Seek(FastText^.DataFile^, FileSize(FastText^.DataFile^));  {Position for write}
  154.         IORes:=IOResult;
  155.       END;
  156.       F.Mode:=fmOutput; {Switch to write mode}
  157.       F.InOutFunc:=@FastTextWrite;
  158.       F.FlushFunc:=@FastTextWriteFlush;
  159.     END;
  160.     fmOutput : BEGIN
  161.       IORes:=OpenOutFile(FastText^.DataFile^, F.Name, True, True);
  162.       F.InOutFunc:=@FastTextWrite;
  163.       F.FlushFunc:=@FastTextWriteFlush;
  164.     END;
  165.   END;
  166.   F.BufPos:=0; {Reset buffer indexes}
  167.   F.BufEnd:=0;
  168.   FastTextOpen:=IORes; {Return IO result from Reset, Rewrite or Append}
  169. END;
  170.  
  171. FUNCTION FastTextClose(VAR F:TTextRec):Integer; FAR;
  172. { - Close encrypted "text" file}
  173. VAR
  174.   FastText : pFastTextRec;
  175. BEGIN
  176.   FastText:=@F.UserData;
  177.   Close(FastText^.DataFile^);
  178.   Dispose(FastText^.DataFile);
  179.   FastText^.DataFile:=nil;
  180.   FreeMem(F.BufPtr, F.BufSize);
  181.   F.BufPtr:=nil;
  182.   FastTextClose:=IOResult;
  183. END; {FUNC FastTextClose}
  184.  
  185. PROCEDURE AssignFastText(VAR F:Text; Name:String; BufferSize:Integer);
  186. { - Assign a text file to the encryption device driver}
  187. VAR
  188.   TRec : TTextRec Absolute F;
  189. BEGIN
  190.   FillChar(TRec, SizeOf(TRec), 0); {Clean out the TTextRec}
  191.   TRec.OpenFunc:=@FastTextOpen;      {Assign the device driver functions}
  192.   TRec.InOutFunc:=nil;
  193.   TRec.FlushFunc:=nil;
  194.   TRec.CloseFunc:=@FastTextClose;
  195.   TRec.Mode:=fmClosed;             {Initial file mode must be closed}
  196.   TRec.BufSize:=BufferSize;        {Init the buffer size}
  197.   StrPCopy(TRec.Name, Name);       {and the filename}
  198.   IOResult;
  199.   SetLineBreakStyle(F, tlbsCRLF);
  200. END; {PROC AssignFastText}
  201.  
  202.  
  203. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement