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