source: branches/2.20_branch/Library/ACLFileIOUtility.pas@ 461

Last change on this file since 461 was 205, checked in by RBRi, 18 years ago

no longer use ACLStringUtilitiy

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1Unit ACLFileIOUtility;
2// Functions for working with OS/2 HFILE
3
4Interface
5
6uses
7 BseDos;
8
9// skips over Length bytes
10Procedure MySkip( F: HFile; Length: longword );
11
12Procedure MySeek( F: HFile; NewPos: longword );
13
14function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean;
15
16function MyReadLn( F: HFile; Var S: String ): boolean;
17
18// Note: Buffer will be resized as needed.
19function MyReadParagraph( F: HFile; Buffer: PChar ): boolean;
20
21Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord );
22
23Procedure MyWriteLn( F: HFile; S: String );
24
25Procedure WriteStringToFile( TheString: PChar; FileName: string );
26
27Procedure 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.
31function ReadFileBlock( F: HFile;
32 Var Dest: pointer;
33 const StartPosition: longint;
34 const Length: longint ): boolean;
35
36Implementation
37
38uses
39 OS2Def,
40 SysUtils,
41 ACLUtility,
42 StringUtilsUnit,
43 CharUtilsUnit;
44
45// skips over Length bytes
46Procedure MySkip( F: HFile; Length: longword );
47var
48 Actual: ULong;
49 rc: APIRET;
50begin
51 rc := DosSetFilePtr( F, Length, FILE_CURRENT, Actual );
52 if rc <> 0 then
53 raise EInOutError.Create( 'Error seeking: ' + SysErrorMessage( rc ) );
54end;
55
56Procedure MySeek( F: HFile; NewPos: longword );
57var
58 Actual: ULong;
59 rc: APIRET;
60begin
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 ) );
66end;
67
68function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean;
69var
70 Actual: ULong;
71 rc: APIRET;
72begin
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;
80end;
81
82function MyReadLn( F: HFile; Var S: String ): boolean;
83var
84 C: Char;
85begin
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 );
98end;
99
100function MyReadParagraph( F: HFile; Buffer: PChar ): boolean;
101var
102 CharBuffer: array[ 0..1 ] of Char;
103begin
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 );
122end;
123
124Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord );
125var
126 Actual: ULong;
127 rc: APIRET;
128begin
129 rc := DosWrite( F, Buffer^, Length, Actual );
130 if rc <> 0 then
131 raise EInOutError.Create( 'Error reading file: ' + SysErrorMessage( rc ) );
132end;
133
134Procedure MyWriteLn( F: HFile; S: String );
135var
136 Buffer: PChar;
137begin
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 );
144end;
145
146Procedure WriteStringToFile( TheString: PChar; FileName: string );
147Var
148 TheFile: File;
149Begin
150 Assign( TheFile, FileName );
151 Rewrite( TheFile );
152 BlockWrite( TheFile, TheString^, strlen( TheString ) );
153 Close( TheFile );
154End;
155
156Procedure ReadStringFromFile( TheString: PChar; FileName: string );
157Var
158 TheFile: File;
159Begin
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 );
166End;
167
168function ReadFileBlock( F: HFile;
169 Var Dest: pointer;
170 const StartPosition: longint;
171 const Length: longint ): boolean;
172begin
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 );
187end;
188
189Initialization
190End.
Note: See TracBrowser for help on using the repository browser.