| 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 | // Read the specified block of F, allocating enough memory in Dest if needed. | 
|---|
| 30 | // Uses AllocateMemory, so use DeallocateMemory to free. | 
|---|
| 31 | function ReadFileBlock( F: HFile; | 
|---|
| 32 | Var Dest: pointer; | 
|---|
| 33 | const StartPosition: longint; | 
|---|
| 34 | const Length: longint ): boolean; | 
|---|
| 35 |  | 
|---|
| 36 | Implementation | 
|---|
| 37 |  | 
|---|
| 38 | uses | 
|---|
| 39 | OS2Def, | 
|---|
| 40 | SysUtils, | 
|---|
| 41 | ACLUtility, | 
|---|
| 42 | StringUtilsUnit, | 
|---|
| 43 | CharUtilsUnit; | 
|---|
| 44 |  | 
|---|
| 45 | // skips over Length bytes | 
|---|
| 46 | Procedure MySkip( F: HFile; Length: longword ); | 
|---|
| 47 | var | 
|---|
| 48 | Actual: ULong; | 
|---|
| 49 | rc: APIRET; | 
|---|
| 50 | begin | 
|---|
| 51 | rc := DosSetFilePtr( F, Length, FILE_CURRENT, Actual ); | 
|---|
| 52 | if rc <> 0 then | 
|---|
| 53 | raise EInOutError.Create( 'Error seeking: ' + SysErrorMessage( rc ) ); | 
|---|
| 54 | end; | 
|---|
| 55 |  | 
|---|
| 56 | Procedure MySeek( F: HFile; NewPos: longword ); | 
|---|
| 57 | var | 
|---|
| 58 | Actual: ULong; | 
|---|
| 59 | rc: APIRET; | 
|---|
| 60 | begin | 
|---|
| 61 | rc := DosSetFilePtr( F, NewPos, FILE_BEGIN, Actual ); | 
|---|
| 62 | if rc <> 0 then | 
|---|
| 63 | raise EInOutError.Create( 'Error seeking: ' + SysErrorMessage( rc ) ); | 
|---|
| 64 | if Actual <> NewPos then | 
|---|
| 65 | raise EInOutError.Create( 'Cannot seek to position ' + IntToStr( NewPos ) ); | 
|---|
| 66 | end; | 
|---|
| 67 |  | 
|---|
| 68 | function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean; | 
|---|
| 69 | var | 
|---|
| 70 | Actual: ULong; | 
|---|
| 71 | rc: APIRET; | 
|---|
| 72 | begin | 
|---|
| 73 | rc := DosRead( F, Buffer^, Length, Actual ); | 
|---|
| 74 | if rc <> 0 then | 
|---|
| 75 | raise EInOutError.Create( 'Error reading file: ' + SysErrorMessage( rc ) ); | 
|---|
| 76 |  | 
|---|
| 77 | Result:= rc = 0; | 
|---|
| 78 | if Actual = 0 then | 
|---|
| 79 | Result:= false; | 
|---|
| 80 | end; | 
|---|
| 81 |  | 
|---|
| 82 | function MyReadLn( F: HFile; Var S: String ): boolean; | 
|---|
| 83 | var | 
|---|
| 84 | C: Char; | 
|---|
| 85 | begin | 
|---|
| 86 | Result:= MyRead( F, Addr( C ), 1 ); | 
|---|
| 87 | while ( C <> #13 ) | 
|---|
| 88 | and Result do | 
|---|
| 89 | begin | 
|---|
| 90 | S:= S + C; | 
|---|
| 91 | Result:= MyRead( F, Addr( C ), 1 ); | 
|---|
| 92 | end; | 
|---|
| 93 | // see if there is a #10 to skip after the #13 | 
|---|
| 94 | Result:= MyRead( F, Addr( C ), 1 ); | 
|---|
| 95 | if Result then | 
|---|
| 96 | if C <> #10 then | 
|---|
| 97 | MySkip( F, -1 ); | 
|---|
| 98 | end; | 
|---|
| 99 |  | 
|---|
| 100 | function MyReadParagraph( F: HFile; Buffer: PChar ): boolean; | 
|---|
| 101 | var | 
|---|
| 102 | CharBuffer: array[ 0..1 ] of Char; | 
|---|
| 103 | begin | 
|---|
| 104 | StrCopy( Buffer, '' ); | 
|---|
| 105 | CharBuffer[ 1 ]:= #0; | 
|---|
| 106 | Result:= MyRead( F, Addr( CharBuffer ), 1 ); | 
|---|
| 107 | while ( CharBuffer[ 0 ] <> #13 ) | 
|---|
| 108 | and Result do | 
|---|
| 109 | begin | 
|---|
| 110 | AddAndResize( Buffer, CharBuffer ); | 
|---|
| 111 | Result:= MyRead( F, Addr( CharBuffer ), 1 ); | 
|---|
| 112 | end; | 
|---|
| 113 |  | 
|---|
| 114 | if not Result then | 
|---|
| 115 | exit; | 
|---|
| 116 |  | 
|---|
| 117 | // skip #10 if found | 
|---|
| 118 | Result:= MyRead( F, Addr( CharBuffer ), 1 ); | 
|---|
| 119 | if Result then | 
|---|
| 120 | if CharBuffer[ 0 ] <> #10 then | 
|---|
| 121 | MySkip( F, -1 ); | 
|---|
| 122 | end; | 
|---|
| 123 |  | 
|---|
| 124 | Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord ); | 
|---|
| 125 | var | 
|---|
| 126 | Actual: ULong; | 
|---|
| 127 | rc: APIRET; | 
|---|
| 128 | begin | 
|---|
| 129 | rc := DosWrite( F, Buffer^, Length, Actual ); | 
|---|
| 130 | if rc <> 0 then | 
|---|
| 131 | raise EInOutError.Create( 'Error reading file: ' + SysErrorMessage( rc ) ); | 
|---|
| 132 | end; | 
|---|
| 133 |  | 
|---|
| 134 | Procedure MyWriteLn( F: HFile; S: String ); | 
|---|
| 135 | var | 
|---|
| 136 | Buffer: PChar; | 
|---|
| 137 | begin | 
|---|
| 138 | Buffer:= StrAlloc( Length( S ) + 3 ); | 
|---|
| 139 | StrPCopy( Buffer, S ); | 
|---|
| 140 | StrCat( Buffer, #13 ); | 
|---|
| 141 | StrCat( Buffer, #10 ); | 
|---|
| 142 | MyWrite( F, Buffer, StrLen( Buffer ) ); | 
|---|
| 143 | StrDispose( Buffer ); | 
|---|
| 144 | end; | 
|---|
| 145 |  | 
|---|
| 146 | Procedure WriteStringToFile( TheString: PChar; FileName: string ); | 
|---|
| 147 | Var | 
|---|
| 148 | TheFile: File; | 
|---|
| 149 | Begin | 
|---|
| 150 | Assign( TheFile, FileName ); | 
|---|
| 151 | Rewrite( TheFile ); | 
|---|
| 152 | BlockWrite( TheFile, TheString^, strlen( TheString ) ); | 
|---|
| 153 | Close( TheFile ); | 
|---|
| 154 | End; | 
|---|
| 155 |  | 
|---|
| 156 | Procedure ReadStringFromFile( TheString: PChar; FileName: string ); | 
|---|
| 157 | Var | 
|---|
| 158 | TheFile: File; | 
|---|
| 159 | Begin | 
|---|
| 160 | Assign( TheFile, FileName ); | 
|---|
| 161 | FileMode := fmInput; | 
|---|
| 162 | Reset( TheFile ); | 
|---|
| 163 | BlockRead( TheFile, TheString^, FileSize( TheFile ) ); | 
|---|
| 164 | TheString[ FileSize( TheFile ) ]:=Chr( 0 ); | 
|---|
| 165 | Close( TheFile ); | 
|---|
| 166 | End; | 
|---|
| 167 |  | 
|---|
| 168 | function ReadFileBlock( F: HFile; | 
|---|
| 169 | Var Dest: pointer; | 
|---|
| 170 | const StartPosition: longint; | 
|---|
| 171 | const Length: longint ): boolean; | 
|---|
| 172 | begin | 
|---|
| 173 | result := true; | 
|---|
| 174 |  | 
|---|
| 175 | if Length = 0 then | 
|---|
| 176 | // thar's nowt to be done. | 
|---|
| 177 | exit; | 
|---|
| 178 |  | 
|---|
| 179 | MySeek( F, StartPosition ); | 
|---|
| 180 |  | 
|---|
| 181 | if Dest = nil then | 
|---|
| 182 | Dest := AllocateMemory( Length ); | 
|---|
| 183 |  | 
|---|
| 184 | result := MyRead( F, | 
|---|
| 185 | Dest, | 
|---|
| 186 | Length ); | 
|---|
| 187 | end; | 
|---|
| 188 |  | 
|---|
| 189 | Initialization | 
|---|
| 190 | End. | 
|---|