-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileIO.pas
394 lines (358 loc) · 7.54 KB
/
FileIO.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
unit FileIO;
(*
Copyright 1999,2009 Csabo.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
interface
Uses
Dialogs, SysUtils, Stringz, Globals;
Const
DataMax = $C000;
Type
TData = Array [ 0 .. DataMax - 1 ] Of Byte;
PData = ^TData;
Var
fOpenCount : Integer;
//
sTempFile : String;
//
f : File;
fo : File;
//
LastFileReadOnly : Boolean;
// Common file operations
Function fOpen ( sFN : String ) : Boolean;
Function fOpen_ ( sFN : String; bQuiet : Boolean ) : Boolean;
procedure fClose;
Function fSeek ( Var f : File; iPos : Integer ) : Boolean;
Function GetString ( Var f : File; iLength : Integer ) : String;
Function GetString8 ( Var f : File ) : String;
Function GetZString ( Var f : File ) : String;
Function GetLongUnreal ( Var f : File ) : Integer;
Function GetLong2 ( Var f : File ) : Integer;
Function GetLong ( Var f : File ) : Integer;
Function GetWord2 ( Var f : File ) : SmallInt;
Function GetWord ( Var f : File ) : SmallInt;
Function GetByte ( Var f : File ) : ShortInt;
// Export Procedures
Function fOpenTemp : Boolean; // for exporting
procedure SendByte ( Var f : File; b : Byte );
Procedure SendWord ( Var f : File; w : SmallInt );
Procedure SendLong ( Var f : File; i : Integer );
Procedure SendLong2 ( Var f : File; i : Integer );
Procedure SendString8 ( Var f : File; s : String );
// CopyData
procedure CopyData ( Var FromFile, ToFile : File; DataLength : Integer );
implementation
// ********************************************************************
//
// Common file operations
//
// ********************************************************************
Function fOpen ( sFN : String ) : Boolean;
Begin
fOpen := fOpen_ ( sFN, False );
End;
Function fOpen_ ( sFN : String; bQuiet : Boolean ) : Boolean;
Var
i : Integer;
Begin
Inc ( fOpenCount );
//
If fOpenCount > 1 Then
Begin
fOpen_ := True; // already open
End
Else
Begin
LastFileReadOnly := False;
{$I-}
AssignFile ( f, sFN );
FileMode := fmOpenReadWrite;
Reset ( f, 1 );
{$I+}
i := IOResult;
If i = 5 Then
Begin
//
// - retry as read-only
//
{$I-}
AssignFile ( f, sFN );
FileMode := fmOpenRead;
Reset ( f, 1 );
{$I+}
i := IOResult;
LastFileReadOnly := True;
End;
//
If i <> 0 Then
Begin
If Not bQuiet Then
Begin
If i = 2 Then
Begin
MessageDlg ( 'File not found: ' + sFN, mtWarning, [mbOK], 0 );
End
Else
Begin
MessageDlg ( 'File could not be opened. (Error ' + IntToStr ( i ) + ')' + #13 + sFN, mtWarning, [mbOK], 0 );
End;
End;
//
fOpen_ := False;
LastFileReadOnly := False;
Dec ( fOpenCount );
End
Else
Begin
fOpen_ := True;
End;
End;
end;
procedure fClose;
Begin
If fOpenCount > 0 Then
Begin
Dec ( fOpenCount );
If ( fOpenCount = 0 ) Then
Begin
CloseFile ( f );
End;
End;
end;
function fSeek ( Var f : File; iPos : Integer ) : Boolean;
Var
iFS : Integer;
Begin
iFS := FileSize ( f );
If ( iFS >= iPos ) And ( iPos >= 0 ) Then
Begin
Seek ( f, iPos );
fSeek := True;
End
Else
Begin
MessageDlg ( 'Invalid seek.' + #13 +
'(' + Comma ( iPos ) + ' - file size ' + Comma ( iFS ) + ')',
mtError, [mbOK], 0 );
fSeek := False;
End;
End;
function fOpenTemp : Boolean;
Begin
sTempFile := sTempFolder + '(xwe)' +
IntToHex ( Random ( $FF ), 2 ) +
IntToHex ( Random ( $FF ), 2 ) +
IntToHex ( Random ( $FF ), 2 ) +
IntToHex ( Random ( $FF ), 2 ) + '.TMP';
//
AssignFile ( fo, sTempFile );
FileMode := fmOpenReadWrite;
ReWrite ( fo, 1 );
//
fOpenTemp := True;
End;
Function GetString ( Var f : File; iLength : Integer ) : String;
Var
s : String;
i : Integer;
Begin
s := '';
//
If iLength > 0 Then
Begin
For i := 1 To iLength Do
s := s + '_';
//
BlockRead ( F, s [ 1 ], iLength, i );
//
While i < iLength Do
Begin
Inc ( i );
s [ i ] := #32;
End;
//
For i := 1 To iLength Do
Begin
If s [ i ] = #0 Then
s [ i ] := #32;
End;
End;
//
GetString := s;
end;
Function GetString8 ( Var f : File ) : String;
Var
s : String;
i : Integer;
Begin
s := '________';
//
BlockRead ( F, s [ 1 ], 8 );
//
i := 1;
While i <= 8 Do
Begin
If s [ i ] = #0 Then
Begin
s := Copy ( Copy ( s, 1, i - 1 ) + ' ', 1, 8 );
i := 8;
End;
Inc ( i );
End;
//
GetString8 := s;
end;
Function GetZString ( Var f : File ) : String;
Var
s : String;
b : Byte;
Begin
s := '';
b := GetByte ( f );
While b <> 0 Do
Begin
s := s + Chr ( b );
b := GetByte ( f );
End;
//
GetZString := s;
end;
Function GetLong2 ( Var f : File ) : Integer;
Var
b1, b2, b3, b4 : Byte;
Begin
BlockRead ( f, b1, 1 );
BlockRead ( f, b2, 1 );
BlockRead ( f, b3, 1 );
BlockRead ( f, b4, 1 );
//
GetLong2 := ( b1 Shl 24 ) Or ( b2 Shl 16 ) Or ( b3 Shl 8 ) Or b4;
end;
Function GetLong ( Var f : File ) : Integer;
Var
i : Integer;
Begin
BlockRead ( f, i, 4 );
GetLong := i;
end;
Function GetLongUnreal ( Var f : File ) : Integer;
Var
b : Byte;
bCont : Boolean;
bNeg : Boolean;
v, Bits : Integer;
Begin
// get first byte
b := GetByte ( f );
//
bNeg := ( b And $80 ) = $80; // keep negative bit
bCont := ( b And $40 ) = $40; // get "continue" bit
//
v := b And $3F;
Bits := 6;
//
While bCont Do
Begin
//
b := GetByte ( f );
bCont := ( b And $80 ) = $80; // get "continue" bit
v := v Or Integer ( ( ( b And $7F ) Shl Bits ) );
Inc ( Bits, 7 );
//
End;
//
If bNeg Then v := -v;
GetLongUnreal := v;
End;
Function GetWord2 ( Var f : File ) : SmallInt;
Var
b1, b2 : Byte;
Begin
BlockRead ( f, b1, 1 );
BlockRead ( f, b2, 1 );
//
GetWord2 := ( b1 Shl 8 ) Or b2;
End;
Function GetWord ( Var f : File ) : SmallInt;
Var
i : SmallInt;
Begin
BlockRead ( F, i, 2 );
GetWord := i;
end;
Function GetByte ( Var f : File ) : ShortInt;
Var
i : ShortInt;
Begin
BlockRead ( F, i, 1 );
GetByte := i;
end;
// ****************************
// *** File output functions
// ****************************
Procedure SendByte ( Var f : File; b : Byte );
Begin
BlockWrite ( f, b, 1 );
End;
Procedure SendWord ( Var f : File; w : SmallInt );
Begin
BlockWrite ( f, w, 2 );
End;
Procedure SendLong ( Var f : File; i : Integer );
Begin
BlockWrite ( f, i, 4 );
End;
Procedure SendLong2 ( Var f : File; i : Integer );
Begin
SendByte ( f, i Shr 24 And 255 );
SendByte ( f, i Shr 16 And 255 );
SendByte ( f, i Shr 8 And 255 );
SendByte ( f, i And 255 );
End;
Procedure SendString8 ( Var f : File; s : String );
Begin
s := Trim ( s );
If Not gDontAutoCapitalize Then
Begin
s := UpperCase ( s );
End;
While Length ( s ) < 8 Do
Begin
s := s + #0;
End;
BlockWrite ( f, s [ 1 ], 8 );
End;
//********************************************
// Copies some bytes from one file to another
//
procedure CopyData ( Var FromFile, ToFile : File; DataLength : Integer );
Var
c : Integer;
d : PData;
Begin
GetMem ( d, DataMax );
//
While DataLength > 0 Do
Begin
c := DataLength;
If c > DataMax Then
c := DataMax;
BlockRead ( FromFile, d^, c );
BlockWrite ( ToFile, d^, c );
Dec ( DataLength, c );
End;
//
FreeMem ( d, DataMax );
end;
end.