source: branches/2.19_branch/Library/ACLFileIOUtility.pas@ 338

Last change on this file since 338 was 17, checked in by RBRi, 19 years ago

+ Library

  • 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, SysUtils,
40 ACLStringUtility, ACLUtility;
41
42// skips over Length bytes
43Procedure MySkip( F: HFile; Length: longword );
44var
45 Actual: ULong;
46 rc: APIRET;
47begin
48 rc := DosSetFilePtr( F, Length, FILE_CURRENT, Actual );
49 if rc <> 0 then
50 raise EInOutError.Create( 'Error seeking: ' + SysErrorMessage( rc ) );
51end;
52
53Procedure MySeek( F: HFile; NewPos: longword );
54var
55 Actual: ULong;
56 rc: APIRET;
57begin
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 ) );
63end;
64
65function MyRead( F: HFile; Buffer: pointer; Length: LongWord ): boolean;
66var
67 Actual: ULong;
68 rc: APIRET;
69begin
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;
77end;
78
79function MyReadLn( F: HFile; Var S: String ): boolean;
80var
81 C: Char;
82begin
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 );
95end;
96
97function MyReadParagraph( F: HFile; Buffer: PChar ): boolean;
98var
99 CharBuffer: array[ 0..1 ] of Char;
100begin
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 );
119end;
120
121Procedure MyWrite( F: HFile; Buffer: pointer; Length: LongWord );
122var
123 Actual: ULong;
124 rc: APIRET;
125begin
126 rc := DosWrite( F, Buffer^, Length, Actual );
127 if rc <> 0 then
128 raise EInOutError.Create( 'Error reading file: ' + SysErrorMessage( rc ) );
129end;
130
131Procedure MyWriteLn( F: HFile; S: String );
132var
133 Buffer: PChar;
134begin
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 );
141end;
142
143Procedure WriteStringToFile( TheString: PChar; FileName: string );
144Var
145 TheFile: File;
146Begin
147 Assign( TheFile, FileName );
148 Rewrite( TheFile );
149 BlockWrite( TheFile, TheString^, strlen( TheString ) );
150 Close( TheFile );
151End;
152
153Procedure ReadStringFromFile( TheString: PChar; FileName: string );
154Var
155 TheFile: File;
156Begin
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 );
163End;
164
165function ReadFileBlock( F: HFile;
166 Var Dest: pointer;
167 const StartPosition: longint;
168 const Length: longint ): boolean;
169begin
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 );
184end;
185
186Initialization
187End.
Note: See TracBrowser for help on using the repository browser.