This repository was archived by the owner on Oct 11, 2022. It is now read-only.
forked from nickelsworth/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0050.pas
More file actions
310 lines (270 loc) · 7.61 KB
/
Copy path0050.pas
File metadata and controls
310 lines (270 loc) · 7.61 KB
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
{*************************************************************************
* LOW LEVEL FILE READING OBJECT *
* *
* Copyright 1992 Tom Clancy *
* *
* Description: *
* *
* This library allows you to create a file of any type record *
* by passing in the record size. You must also pass in a record of *
* the same type that the object has been initialized with so that *
* you don't get errors when reading and writing. *
* *
* There is no internal buffering, but the routines are fairly *
* fast and because each file is actually an object, you can create *
* higher level objects of this object type that allow more *
* flexibility, such as indexing and sorting. *
* *
*************************************************************************}
{$I-} {Turn off I/O checking}
{$S-} {Turn off Stack checking}
{$R-} {Turn off Range checking}
{$V-} {No strict VAR string checking allowed here!}
Unit FileRead;
Interface
Const
Open = 1;
Create = 2;
OpenCreate = 3;
Type
TFreadPtr = ^TFreadObj;
TFreadObj = Object
Constructor Init(fn : string; mode:integer; recsize : longint);
Destructor Done;
{ random access methods. }
Procedure ReadRec(var frec; fpos:longint); virtual;
Procedure WriteRec(var frec; fpos:longint); virtual;
{ sequential access methods. }
Procedure AppendRec(var frec);
Procedure ReadNext(var frec);
Procedure ReadPrevious(var frec);
Procedure ReadCurrent(var frec);
{ various file modification methods. }
Procedure EraseFile;
Function RenameFile(fn:string):boolean;
{ miscellaneous functions and error flag functions. }
Procedure Rewind;
Function NumRecs : Longint;
Function GetFilename : String;
Function GetCurrent : Longint;
Function OpenError : boolean;
Function ReadError : boolean;
Function WriteError : boolean;
private
Ifile : File; {file variable}
Rsize : Longint; {the internal record size}
FileName : String; {physical file name}
Oerror, {open error flag}
Rerror, {read error flag}
Werror : Boolean; {write error flag}
Current : Longint; {current file pointer location}
{ methods used internally. No access allowed! }
Procedure OpenFile;
Procedure CreateFile;
Procedure CloseFile;
end;
Function Exist(fn:string):Boolean;
Implementation
uses
Dos;
{ Pass in a string which contains a file name to see if that file exists.}
Function Exist(fn:string):Boolean;
Var
DirInfo : SearchRec;
Begin
FindFirst(fn,Archive,DirInfo);
Exist:=DosError=0;
End;
{
Initialize the object.
Fn = File name
Mode = Open, Create, or OpenCreate
- Open will try to open the file. An error is set if the file does not
exist.
- Create will create the file regardless if it's there or not.
- OpenCreate will attemp to open the file first, then create it if it's
not there.
RecSize = The size of the records that the file will contain.
- Use Sizeof(Rec) for safety.
}
Constructor TFreadObj.Init(fn:string; mode:integer; recsize:longint);
Begin
Oerror:=true;
Rerror:=false;
Werror:=false;
Rsize:=recsize;
FileName := fn;
Assign(Ifile,FileName);
case mode of
Open : openfile;
Create : createfile;
OpenCreate :
begin
OpenFile;
if Oerror then
CreateFile;
end;
end;
End;
{ Close the file when disposing object. }
Destructor TFreadObj.done;
begin
CloseFile;
end;
{ Open the file. Set an error if it could not open. }
Procedure TFreadObj.OpenFile;
Begin
if Exist(FileName) then
begin
Oerror:=false;
Reset(Ifile,Rsize);
Current:=0;
end
else
Oerror:=true;
End;
{ Create a new file, zeroing out an existing file.}
Procedure TFreadObj.CreateFile;
Begin
Rewrite(Ifile,Rsize);
Current:=0;
Oerror:=Ioresult<>0;
end;
{ Close the file only if it has been successfully opened.}
Procedure TFreadObj.CloseFile;
Begin
if not Oerror then
begin
Close(Ifile);
Oerror:=true;
end;
End;
{ Will erase the file.}
Procedure TFreadObj.EraseFile;
Begin
if not Oerror then
begin
CloseFile;
Erase(Ifile);
end;
End;
{ Renames the file.}
Function TFreadObj.RenameFile(fn:string):Boolean;
Var
Temp : Longint; {Save the current file pointer}
Begin
CloseFile;
FileName:=fn;
Rename(Ifile,FileName);
if ioresult=0 then
begin
Temp:=Current;
Assign(Ifile,FileName);
OpenFile;
Current:=Temp;
end;
RenameFile := not Oerror;
end;
{ Rewinds the file pointer back to the beginning.}
Procedure TFreadObj.Rewind;
Begin
if not Oerror then
begin
Seek(Ifile,0);
Current:=0;
end;
end;
Function TFreadObj.OpenError:Boolean;
Begin
OpenError:=Oerror;
End;
Function TFreadObj.ReadError:Boolean;
Begin
ReadError:=Rerror;
End;
Function TFreadObj.WriteError:Boolean;
Begin
WriteError:=Werror;
End;
{ Reads a record from the file at location FPOS. Returns the record in
Frec.}
Procedure TFreadObj.ReadRec(var frec; fpos:longint);
Var
numread : word;
Begin
Rerror:=false;
if not Oerror then
begin
Seek(Ifile,fpos);
if ioresult<>0 then
Rerror:=true
else
begin
Blockread(Ifile,frec,1,numread);
if (numread<>1) or (ioresult<>0) then
Rerror:=true
else
Current:=fpos;
end;
end;
End;
{ Writes a record to the file at location Fpos.}
Procedure TFreadObj.WriteRec(var frec; fpos:longint);
Var
numwritten : word;
i:integer;
Begin
Werror:=false;
if not Oerror then
begin
Seek(Ifile,fpos);
if Ioresult<>0 then
Werror:=true
else
begin
Blockwrite(Ifile,frec,1,numwritten);
if (numwritten<>1) or (ioresult<>0) then
Werror:=true
else
Current:=fpos;
end;
end;
End;
{ Appends a record to the end of the file.}
Procedure TFreadObj.AppendRec(var frec);
Begin
WriteRec(frec,NumRecs);
End;
{ Reads the next record from the file, allowing sequential access.}
Procedure TFreadObj.ReadNext(var frec);
Begin
ReadRec(frec,Current+1);
End;
{ Reads the previous record from the file. }
Procedure TFreadObj.ReadPrevious(var frec);
Begin
ReadRec(frec,Current-1);
End;
{ Reads the record pointed to by current. }
Procedure TFreadObj.ReadCurrent(var frec);
Begin
ReadRec(frec,Current);
End;
{ Returns the number of records in the file.}
Function TFreadObj.NumRecs:Longint;
Begin
if not Oerror then
NumRecs:=Filesize(Ifile);
End;
{ Returns the file name of the file.}
Function TFreadObj.GetFilename : String;
Begin
GetFilename:=FileName;
End;
{ Returns the number of the current record. }
Function TFreadObj.GetCurrent : Longint;
Begin
GetCurrent:=Current;
End;
{ No initialization required.}
end.