home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / acl-lib.zip / ACLFileIOUtility.pas < prev    next >
Pascal/Delphi Source File  |  2000-07-19  |  3KB  |  145 lines

  1. Unit ACLFileIOUtility;
  2. // Functions for working with OS/2 HFILE
  3.  
  4. Interface
  5.  
  6. uses
  7.   BseDos;
  8.  
  9. // skips over Length bytes
  10. Procedure MySkip( F: HFile; Length: longword );
  11.  
  12. Procedure MySeek( F: HFile; NewPos: longword );
  13.  
  14. function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean;
  15.  
  16. function MyReadLn( F: HFile; Var S: String ): boolean;
  17.  
  18. // Note: Buffer will be resized as needed.
  19. function MyReadParagraph( F: HFile; Buffer: PChar ): boolean;
  20.  
  21. Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord );
  22.  
  23. Procedure MyWriteLn( F: HFile; S: String );
  24.  
  25. Procedure WriteStringToFile( TheString: PChar; FileName: string );
  26.  
  27. Procedure ReadStringFromFile( TheString: PChar; FileName: string );
  28.  
  29. Implementation
  30.  
  31. uses
  32.   OS2Def, SysUtils,
  33.   ACLPCharUtility;
  34.  
  35. // skips over Length bytes
  36. Procedure MySkip( F: HFile; Length: longword );
  37. var
  38.   Actual: ULong;
  39. begin
  40.   DosSetFilePtr( F, Length, FILE_CURRENT, Actual );
  41. end;
  42.  
  43. Procedure MySeek( F: HFile; NewPos: longword );
  44. var
  45.   Actual: ULong;
  46. begin
  47.   DosSetFilePtr( F, NewPos, FILE_BEGIN, Actual );
  48. end;
  49.  
  50. function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean;
  51. var
  52.   Actual: ULong;
  53. begin
  54.   Result:= DosRead( F, Buffer^, Length, Actual ) = 0;
  55.   if Actual = 0 then
  56.     Result:= false;
  57. end;
  58.  
  59. function MyReadLn( F: HFile; Var S: String ): boolean;
  60. var
  61.   C: Char;
  62.   NewFilePtr: ULONG;
  63. begin
  64.   Result:= MyRead( F, Addr( C ), 1 );
  65.   while ( C <> #13 )
  66.         and Result do
  67.   begin
  68.     S:= S + C;
  69.     Result:= MyRead( F, Addr( C ), 1 );
  70.   end;
  71.   Result:= MyRead( F, Addr( C ), 1 );
  72.   if C <> #10 then
  73.   begin
  74.     DosSetFilePtr( F, -1, FILE_CURRENT, NewFilePtr );
  75.   end;
  76. end;
  77.  
  78. function MyReadParagraph( F: HFile; Buffer: PChar ): boolean;
  79. var
  80.   CharBuffer: array[ 0..1 ] of Char;
  81.   NewFilePtr: ULONG;
  82. begin
  83.   StrCopy( Buffer, '' );
  84.   CharBuffer[ 1 ]:= #0;
  85.   Result:= MyRead( F, Addr( CharBuffer ), 1 );
  86.   while ( CharBuffer[ 0 ] <> #13 )
  87.         and Result do
  88.   begin
  89.     AddAndResize( Buffer, CharBuffer );
  90.     Result:= MyRead( F, Addr( CharBuffer ), 1 );
  91.   end;
  92.  
  93.   if not Result then
  94.     exit;
  95.  
  96.   // skip #10 if found
  97.   Result:= MyRead( F, Addr( CharBuffer ), 1 );
  98.   if Result then
  99.     if CharBuffer[ 0 ] <> #10 then
  100.       DosSetFilePtr( F, -1, FILE_CURRENT, NewFilePtr );
  101. end;
  102.  
  103. Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord );
  104. var
  105.   Actual: ULong;
  106. begin
  107.   DosWrite( F, Buffer^, Length, Actual );
  108. end;
  109.  
  110. Procedure MyWriteLn( F: HFile; S: String );
  111. var
  112.   Buffer: PChar;
  113. begin
  114.   Buffer:= StrAlloc( Length( S ) + 3 );
  115.   StrPCopy( Buffer, S );
  116.   StrCat( Buffer, #13 );
  117.   StrCat( Buffer, #10 );
  118.   MyWrite( F, Buffer, StrLen( Buffer ) );
  119.   StrDispose( Buffer );
  120. end;
  121.  
  122. Procedure WriteStringToFile( TheString: PChar; FileName: string );
  123. Var
  124.   TheFile: File;
  125. Begin
  126.   Assign( TheFile, FileName );
  127.   Rewrite( TheFile );
  128.   BlockWrite( TheFile, TheString^, strlen( TheString ) );
  129.   Close( TheFile );
  130. End;
  131.  
  132. Procedure ReadStringFromFile( TheString: PChar; FileName: string );
  133. Var
  134.   TheFile: File;
  135. Begin
  136.   Assign( TheFile, FileName );
  137.   Reset( TheFile );
  138.   BlockRead( TheFile, TheString^, FileSize( TheFile ) );
  139.   TheString[ FileSize( TheFile ) ]:=Chr( 0 );
  140.   Close( TheFile );
  141. End;
  142.  
  143. Initialization
  144. End.
  145.