-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrg_ofile.pas
More file actions
566 lines (486 loc) · 13.4 KB
/
rg_ofile.pas
File metadata and controls
566 lines (486 loc) · 13.4 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
{$A-,B-,D-,E-,F+,G+,I-,L-,N-,O+,P+,Q-,R-,S-,T-,V-,X+}
unit Rg_OFile;
{
Code: 2759
Data: 52
}
INTERFACE
uses
dos, Objects,
Rg_Obj,
CommDec,
BCShare;
Type
BufType=array[1..65535] of char;
pBuf = ^BufType;
TYPE
RgFileObj=object(rgobj)
blocksize:word; { Block Size of data, Must pass at least 1 }
FSize:LongInt; { Size of file after FileSize call }
Result:word; { Amount actually read }
IoRes:Word; { IoResult Value }
IsOpen:Boolean; { is file open? }
Attr:Word; { File attributes - retrieved auto }
FileName:Str120; { Name of current file }
Fil:File; { Generic File }
Accesserror:Boolean; { TRUE if CheckIo is what sets Not OK }
Data:pBuf; { Scratch variable }
Constructor Init(L:LongInt);
Destructor Done;
Procedure CheckIo; { Checks IoResult, Sets IoRes }
Procedure ExpandFile(Quantity:LongInt);
{ Extends size of fil by Quantity amount of records }
{ Returns Not OK if not able to allocate memory or if error }
Procedure Contract(Quantity:LongInt);
{ Shrinks file by Quantity amount of records }
{ will not do any operations if Quantity is > filesize }
Procedure Assign(const FilName:Str255);
Procedure BlockRead(Var Buf; Amt:Word); Virtual;
{ Reads Amt from Fil into Buf }
Procedure BlockWrite(Var Buf; Amt:Word); Virtual;
{ Writes Amt from Buf into Fil }
Procedure Read(Var Buf);
{ Reads blocksize into Buf from Fil }
Procedure Write(Var buf);
{ Writes blocksize into Fil from Buf }
Procedure Close;
Function EOF:Boolean;
Procedure Erase;
Function FilePos:LongInt;
Function RealFilePos:LongInt;
Function FileSize:LongInt;
Function RealFileSize:LongInt;
Procedure Reset; virtual;
Procedure Rewrite; virtual;
Procedure ResetMake;
{ Resets file if it exists, else rewrites file }
Procedure Seek(Where:LongInt);
{ Goes to Where in Fil, Units of blocksize}
Procedure RealSeek(Where:LongInt);
{ Goes to Where if Fil, Absolute coords }
procedure Truncate;
{ Truncates file at current pos (at begining of data block) }
Procedure GetFAttr(Atr:word);
Procedure SetFAttr(Atr:word);
{ Sets File attribute, WILL CLOSE FILE! }
Procedure GetFTime(Time:LongInt);
Procedure SetFTime(Time:LongInt);
{ Sets file time, File MUST BE OPEN! }
Function HasfAttr(Which:Word):boolean;
Function FileOpen:Boolean;
{ TRUE if File is open }
Procedure MoveRecord(FromWhere, ToWhere:LongInt);
{ Moves record to pos, overwriting dest and deleting orig }
Procedure MoveInsertRecord(FromWhere, BeforeWhere:LongInt);
{ Moves FromWhere to BeforeWhere and dels orig }
Procedure CopyRecord(FromWhere, ToWhere:LongInt);
{ Copies from orig to dest, leave orig }
Procedure CopyInsertRecord(FromWhere, BeforeWhere:LongInt);
{ inserts blank and copies to pos }
Procedure SwapRecord(R1, R2:LongInt);
{ Swaps two records }
Procedure DeleteRecord(FromWhere:LongInt);
{ Deletes record and packs file }
{ at later date can override and add free list pointers }
Procedure ClearRecord(Where:LongInt);
{ Blanks record }
Procedure InsertRecord(BeforeWhere:LongInt);
{ Insert blank record before specified record }
Procedure InsertRecords(BeforeWhere:LongInt; Quantity:Word);
{ Inserts n amount of blank records before specified record }
end;
pRgFileObj=^RgFileObj;
TYPE
pRgShareFileObj=^RgShareFileObj;
RgShareFileObj=Object(RgFileObj)
ShareMode { Set to FALSE to NOT lock file }
:boolean;
FilMode:Byte;
LoopCount:Word;
DelayAmt:word; { How long to wait before next access attempt }
Constructor Init(L:LongInt);
Destructor Done; virtual;
Procedure Reset; virtual;
Procedure Rewrite; virtual;
Procedure SetFileMode(Mde:Byte);
{ Actually sets file mode }
Procedure DefaultFileMode(Mde:Byte);
Procedure BlockWrite(Var Buf; Count:Word); Virtual;
Procedure SetLoopCount(ToWhat:Word); { Defaults to 5 }
{ Sets the looping retry on }
Procedure SetDelayAmt(ToWhat:Word); { Defaults to 20 }
{ Sets the delay for the delay() loop on file lock/write }
end;
IMPLEMENTATION
Uses Crt, Common;
(*
function exist(const fn:str255):boolean;
var srec:searchrec;
begin
findfirst(fn,anyfile,srec);
exist:=(doserror=0);
end;
*)
{
-----------------------------------------------------
-----------------------------------------------------[RgFileObject]---
-----------------------------------------------------
}
Constructor RgFileObj.Init(L:LongInt);
begin
inherited init;
blocksize := L;
if blocksize = 0 then
blocksize := 1;
FSize := 0;
Result := 0;
IoRes := 0;
IsOpen := FALSE;
Attr := 0;
FileName := '';
Accesserror := FALSE;
GetMem(Data, blocksize);
if Data=NIL then
fail;
end;
Destructor RgFileObj.Done;
begin
FreeMem(Data,blocksize);
If (IsOpen) and (Ok) then Close;
end;
Procedure RgFileObj.ExpandFile(Quantity:LongInt);
{ Extends size of fil by Quantity amount of records }
VAR
Loop:LongInt;
Loop2:word;
begin
If Ok then
begin
Fillchar(Data^, blocksize, #0);
Seek(FileSize);
For Loop := 1 to Quantity do
begin
Write(Data^);
end;
end;
end;
Procedure RgFileObj.Contract(Quantity:LongInt);
{ Shrinks file by Quantity amount of records }
{ will not do any operations if Quantity is > filesize }
begin
if Quantity > FileSize then
exit;
Seek(FileSize-quantity);
truncate;
end;
Procedure RgFileObj.Assign(const FilName:Str255);
begin
system.assign(Fil, FilName);
GetFAttr(Attr);
end;
Procedure RgFileObj.CheckIo; { Checks IoResult, Sets IoRes }
begin
IoRes := IoResult;
if (IoRes <> 0) then
begin
SetOk(FALSE);
Accesserror := FALSE;
end;
end;
Procedure RgFileObj.BlockRead(Var Buf; Amt:Word); { Reads Amt from Fil into Buf }
begin
System.BlockRead(Fil, Buf, Amt, Result);
CheckIo;
end;
Procedure RgFileObj.BlockWrite(Var Buf; Amt:Word); { Writes Amt from Buf into Fil }
begin
System.BlockWrite(Fil, Buf, Amt, Result);
CheckIo;
end;
Procedure RgFileObj.Read(Var Buf); { Reads blocksize into Buf from Fil }
begin
BlockRead(Buf, blocksize);
end;
Procedure RgFileObj.Write(Var buf); { Writes blocksize into Fil from Buf }
begin
BlockWrite(Buf, blocksize);
end;
Procedure RgFileObj.Close;
begin
If IsOpen then
begin
IsOpen := FALSE;
System.Close(Fil);
CheckIo;
end;
end;
Function RgFileObj.EOF:Boolean;
begin
Eof := System.Eof(Fil);
CheckIo;
end;
Procedure RgFileObj.Erase;
VAR
F:File;
begin
Close;
if Exist(FileName) then
begin
assign(FileName);
System.Erase(Fil);
end;
CheckIo;
end;
Function RgFileObj.FilePos:LongInt;
Var T:LongInt;
begin
T := System.FilePos(Fil);
CheckIo;
FilePos := T div blocksize;
end;
Function RgFileObj.RealFilePos:LongInt;
var T:LongInt;
begin
RealFilePos := System.FilePos(Fil);
CheckIo;
end;
Function RgFileObj.FileSize:LongInt;
begin
FSize := System.FileSize(Fil);
CheckIo;
FileSize := FSize div blocksize;
end;
Function RgFileObj.RealFileSize:LongInt;
begin
RealFileSize := System.FileSize(Fil);
end;
Procedure RgFileObj.Reset;
begin
System.Reset(Fil, 1);
CheckIo;
end;
Procedure RgFileObj.Rewrite;
begin
System.Rewrite(Fil, 1);
CheckIo;
end;
Procedure RgFileObj.ResetMake;
begin
if Exist(FileName) then
Reset
else
Rewrite;
end;
Procedure RgFileObj.Seek(Where:LongInt); { Goes to Where in Fil, Units of blocksize}
begin
System.Seek(Fil, (Where*blocksize));
CheckIo;
end;
Procedure RgFileObj.RealSeek(Where:LongInt);
begin
System.Seek(Fil, Where);
CheckIo;
end;
procedure RgFileObj.Truncate; { Truncates file at current pos (at begining of data block) }
begin
System.Truncate(Fil);
CheckIo;
end;
Procedure RgFileObj.GetFAttr(Atr:word);
begin
Dos.GetFAttr(Fil, Atr);
CheckIo;
end;
Procedure RgFileObj.SetFAttr(Atr:word); { Sets File attribute, WILL CLOSE FILE! }
begin
Close;
Dos.SetFAttr(Fil, Atr);
CheckIo;
end;
Procedure RgFileObj.GetFTime(Time:LongInt);
begin
Dos.GetFTime(Fil, Time);
CheckIo;
end;
Procedure RgFileObj.SetFTime(Time:LongInt); { Sets file time, File MUST BE OPEN! }
begin
Dos.SetFTime(Fil, Time);
CheckIo;
end;
Function RgFileObj.HasfAttr(Which:Word):boolean;
begin
HasfAttr := ((Which AND Attr) <> 0);
end;
Function RgFileObj.FileOpen:Boolean;
Begin
if (FileRec(Fil).Mode = fmClosed) then
FileOpen := FALSE
else
FileOpen := TRUE;
End;
Procedure RgFileObj.MoveRecord(FromWhere, ToWhere:LongInt);
{ Moves record to pos, overwriting dest and deleting orig }
begin
CopyRecord(FromWhere, ToWhere);
DeleteRecord(FromWhere);
end;
Procedure RgFileObj.MoveInsertRecord(FromWhere, BeforeWhere:LongInt);
Begin
InsertRecord(BeforeWhere);
MoveRecord(FromWhere, BeforeWhere-1);
End;
Procedure RgFileObj.CopyRecord(FromWhere, ToWhere:LongInt);
{ Copies from orig to dest, leave orig }
begin
Seek(FromWhere);
read(data^);
Seek(ToWhere);
Write(Data^);
end;
Procedure RgFileObj.CopyInsertRecord(FromWhere, BeforeWhere:LongInt);
begin
InsertRecord(BeforeWhere);
CopyRecord(FromWhere, BeforeWhere-1);
end;
Procedure RgFileObj.SwapRecord(R1, R2:LongInt);
{ Swaps two records }
VAR
t:pbuf;
begin
GetMem(T, blocksize);
if (T=NIL) then
begin
SetOk(FALSE);
Exit;
end;
Seek(R1); Read(Data);
Seek(R2); Read(T);
Seek(R2); Write(Data);
Seek(R1); Write(T);
FreeMem(T, blocksize);
end;
Procedure RgFileObj.DeleteRecord(FromWhere:LongInt);
{ Deletes record and packs file }
{ at later date can override and add free list pointers }
VAR
FS:LongInt;
Loop:LongInt;
begin
FS := FileSize;
if (FromWhere < FS) then
begin
For loop := FromWhere to FS-1 do
CopyRecord(Loop+1, Loop);
end;
Contract(1);
end;
Procedure RgFileObj.ClearRecord(Where:LongInt);
{ Blanks record }
begin
FillChar(Data^, blocksize, #0);
Seek(Where);
Write(Data^);
end;
Procedure RgFileObj.InsertRecord(BeforeWhere:LongInt);
{ Insert blank record before specified record }
VAR
Loop, Fs:LongInt;
begin
InsertRecords(BeforeWhere, 1);
end;
Procedure RgFileObj.InsertRecords(BeforeWhere:LongInt; Quantity:Word);
{ Inserts n amount of blank records before specified record }
VAR
Fs, Loop :LongInt;
begin
ExpandFile(Quantity);
Fs := FileSize;
For Loop := Fs downto BeforeWhere+Quantity-1 do
begin
SwapRecord(Loop, Loop-Quantity);
end;
end;
{
-----------------------------------------------------
-----------------------------------------------------[RgFileObject]---
-----------------------------------------------------
}
{
-----------------------------------------------------
-----------------------------------------------------[RgShareFileObj]---
-----------------------------------------------------
}
Constructor RgShareFileObj.Init(L:LongInt);
begin
Inherited Init(L);
ShareMode := Shared;
FileMode := 66;
(* FilMode := WriteDenyNone;*)
SetLoopCount(5);
SetDelayAmt(20);
end;
Destructor RgShareFileObj.Done;
begin
Inherited Done;
end;
procedure RgShareFileObj.Reset;
begin
(* SetFileMode(FilMode);*)
Inherited Reset;
end;
procedure RgShareFileObj.Rewrite;
begin
(* SetFileMode(FilMode);*)
Inherited Rewrite;
end;
procedure RgShareFileObj.DefaultfileMode(Mde:Byte);
begin
filmode := mde;
end;
Procedure RgShareFileObj.SetFileMode(Mde:byte);
Begin
bcShare.SetFileMode(mde);
End;
Procedure RgShareFileObj.BlockWrite(Var Buf; Count:Word);
VAR
Fs, Fp : Longint;
TempCount:Word;
Begin
Fs := RealFileSize;
Fp := RealFilePos;
TempCount := 0;
Repeat
if (ShareMode) and (FS <> FP) then
begin
With FileRec(Fil) Do
LockFile(Handle,Lock,FP,blocksize);
end;
System.BlockWrite(Fil, Buf, Count, Result);
if (ShareMode) and (FS <> FP) then
begin
With FileRec(Fil) do
LockFile(Handle, UnLock, FP, blocksize);
end;
CheckIo;
if IoRes = 5 then
begin
Delay(DelayAmt);
end;
Inc(TempCount);
until (IoRes <> 5) and (TempCount <= LoopCount);
End;
Procedure RgShareFileobj.SetLoopCount(ToWhat:Word);
begin
LoopCount := ToWhat;
end;
Procedure RgShareFileObj.SetDelayAmt(ToWhat:Word);
begin
DelayAmt := ToWhat;
end;
{
-----------------------------------------------------
-----------------------------------------------------[RgShareFileObj]---
-----------------------------------------------------
}
begin
end.