-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQFILES.PAS
executable file
·1185 lines (1070 loc) · 33.1 KB
/
QFILES.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
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ Copyright 2015 Jerome Shidel }
(*
This project and related files are subject to either the terms
specified in the included LICENSE.TXT file or the GNU GPLv2.0.
*)
unit QFiles; { QuickCrt Files }
{$I QCRT.DEF}
{$D-,Y-,L-}
interface
uses QClass;
const
class_TAbstractFile : TObjectClass = 'TAbstractFile';
class_TFile : TObjectClass = 'TFile';
const
{ File status flags }
flInvalid = $0000; { Is Not Valid }
flAssigned = $0001; { IsAssigned }
flOpened = $0002; { IsOpen }
flFlush = $0004; { Needs Flushed }
flErase = $0008; { Erase when disposed (not advised) }
flError = $0010; { Is Error Code }
flRead = $0020; { File can be read }
flWrite = $0040; { File can be written }
flRandom = $0060; { File can be read or written }
{ SetIOResult return commands }
fcOK = $00;
fcIgnore = $01;
fcRetry = $02;
fcAbort = $03;
type
PAbstractFile = ^TAbstractFile;
TAbstractFile = object (TObject)
public { protected }
function ObjectClass ( AName : String ) : String; virtual;
public
Ptr : LongInt;
Mode : word;
Flags : word;
Name : String;
Result : integer;
constructor Create;
destructor Destroy; virtual;
procedure Assign( AFileName : String ); virtual;
procedure BlockRead(const ABuf; ACount: Word; var AResult: Word); virtual;
procedure BlockWrite(const ABuf; ACount: Word; var AResult: Word); virtual;
procedure Close; virtual;
procedure Erase; virtual;
function FileSize: Longint; virtual;
procedure Flush; virtual;
procedure Rename( ANewName : String ); virtual;
procedure Reset; virtual;
procedure Rewrite; virtual;
procedure Seek(AFilePos: Longint); virtual;
procedure Truncate; virtual;
function SetIOResult ( AIOResult : integer ) : byte; virtual;
{ the following procedures/functions should not need to be overridden }
procedure Append;
procedure Copy(var AFile : PAbstractFile; ACount: LongInt);
function Sof: Boolean;
function Eof: Boolean;
function FilePos: Longint;
function IOResult: Integer;
function SeekSof: Boolean;
function SeekEof: Boolean;
procedure SetFileMode ( AMode : word );
{ Should never, ever need overridden (they all call BlockRead) }
procedure Insert(ACount : longint);
procedure Delete(ACount : longint);
procedure ReadRecord (const AAdr; ASize : word);
procedure WriteRecord (const AAdr; ASize : word);
procedure ReadChar (const AChr );
procedure WriteChar (const AChr );
procedure ReadBoolean (const ABoolean);
procedure WriteBoolean (const ABoolean);
procedure ReadByteBool (const AByteBool);
procedure WriteByteBool (const AByteBool);
procedure ReadWordBool (const AWordBool);
procedure WriteWordBool (const AWordBool);
procedure ReadLongBool (const ALongBool);
procedure WriteLongBool (const ALongBool);
procedure ReadByte (const AByte);
procedure WriteByte (const AByte);
procedure ReadWord (const AWord);
procedure WriteWord (const AWord);
procedure ReadShortInt (const AShortInt);
procedure WriteShortInt (const AShortInt);
procedure ReadInteger (const AInteger);
procedure WriteInteger (const AInteger);
procedure ReadLongInt (const ALongInt);
procedure WriteLongInt (const ALongInt);
procedure ReadReal (const AReal);
procedure WriteReal (const AReal);
procedure ReadSingle (const ASingle);
procedure WriteSingle (const ASingle);
procedure ReadDouble (const ADouble);
procedure WriteDouble (const ADouble);
procedure ReadComp (const AComp);
procedure WriteComp (const AComp);
procedure ReadExtended (const AExtended);
procedure WriteExtended (const AExtended);
procedure ReadString (const AString; AStrSize : word);
procedure WriteString (const AString; AStrSize : word);
function GetString : String;
procedure PutString (const AString);
{ Text files }
function Soln: boolean;
function Eoln: Boolean;
function SeekSoln: boolean;
function SeekEoln: Boolean;
function SeekLn ( ANumber : LongInt ) : boolean;
procedure Read ( const AStr; AMax : word );
procedure Write ( const AStr );
procedure ReadLn ( const AStr; AMax : word );
procedure WriteLn ( const AStr );
function LengthLn : word;
procedure InsertLn ( ALen : word );
procedure DeleteLn;
procedure ResizeLn ( ALen : word );
end;
PFile = ^TFile;
TFile = object( TAbstractFile )
public { protected }
function ObjectClass ( AName : String ) : String; virtual;
public
FRec : File;
constructor Create;
destructor Destroy; virtual;
procedure Assign( AFileName : String ); virtual;
procedure BlockRead(const ABuf; ACount: Word; var AResult: Word); virtual;
procedure BlockWrite(const ABuf; ACount: Word; var AResult: Word); virtual;
procedure Close; virtual;
procedure Erase; virtual;
function FileSize: Longint; virtual;
procedure Flush; virtual;
procedure Rename( ANewName : String ); virtual;
procedure Reset; virtual;
procedure Rewrite; virtual;
procedure Seek(AFilePos: Longint); virtual;
procedure Truncate; virtual;
function GetName : String;
end;
implementation
uses QErrors, QDos;
const
CRLF : String[2] = #13#10;
var
Buf : array[0..511] of byte;
function TAbstractFile.ObjectClass(AName : String) : String;
begin
if (AName = '') or (AName = class_TAbstractFile) then
ObjectClass := class_TAbstractFile
else
ObjectClass := inherited ObjectClass(AName);
end;
constructor TAbstractFile.Create;
begin
Ptr := 0;
Mode := 0;
Name := '';
Flags := flInvalid;
SetIOResult(erNone);
end;
destructor TAbstractFile.Destroy;
begin
if Flags and flOpened = flOpened then Close;
if Flags and flErase = flErase then Erase;
end;
procedure TAbstractFile.Assign( AFileName : String );
begin
if (Flags and flOpened = flOpened) then
SetIOResult(erFile_access_denied)
else
begin
SetIOResult ( erNone );
Name := AFileName;
end;
end;
procedure TAbstractFile.BlockRead(const ABuf; ACount: Word; var AResult: Word);
begin
AResult := 0;
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened ) then
SetIOResult(erFile_not_open)
else
if (Flags and flRead <> flRead) then
SetIOResult(erFile_not_open_for_input)
else
SetIOResult ( erNone );
end;
procedure TAbstractFile.BlockWrite(const ABuf; ACount: Word; var AResult: Word);
begin
AResult := 0;
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
if (Flags and flWrite <> flWrite) then
SetIOResult(erFile_not_open_for_output)
else
SetIOResult ( erNone );
end;
procedure TAbstractFile.Close;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
begin
SetIOResult ( erNone );
if Flags and flFlush = flFlush then Flush;
end;
end;
procedure TAbstractFile.Erase;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened = flOpened) then
SetIOResult(erFile_access_denied)
else
SetIOResult ( erNone );
end;
function TAbstractFile.FilePos: Longint;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
begin
SetIOResult( erNone );
FilePos := Ptr;
end;
end;
function TAbstractFile.FileSize: Longint;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
begin
SetIOResult ( erNone );
FileSize := 0;
end;
end;
procedure TAbstractFile.Flush;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
if (Flags and flWrite <> flWrite) then
SetIOResult(erFile_not_open_for_output)
else
SetIOResult ( erNone );
end;
function TAbstractFile.IOResult: Integer;
begin
IOResult := Result;
SetIOResult(erNone);
end;
function TAbstractFile.SetIOResult ( AIOResult : integer ) : byte;
begin
if AIOResult <> erNone then
Flags := Flags or flError
else
Flags := Flags and (not flError);
Result := AIOResult;
case AIOResult of
erNone : SetIOResult := fcOK;
else
SetIOResult := fcAbort;
{$IFOPT I+}
RunError(AIOResult);
{$ENDIF}
end;
end;
procedure TAbstractFile.Rename;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
SetIOResult ( erNone );
end;
procedure TAbstractFile.Reset;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
begin
SetIOResult ( erNone );
if (Flags and flOpened = flOpened) then
Close;
if (Flags and flError <> flError) then
SetFileMode ( FileMode );
end;
end;
procedure TAbstractFile.Rewrite;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
begin
SetIOResult ( erNone );
if (Flags and flOpened = flOpened) then
Close;
if (Flags and flError <> flError) then
SetFileMode ( (FileMode and (not 3)) or 2 );
end;
end;
procedure TAbstractFile.Seek(AFilePos: Longint);
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
SetIOResult ( erNone );
end;
procedure TAbstractFile.Truncate;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
if (Flags and flOpened <> flOpened) then
SetIOResult(erFile_not_open)
else
SetIOResult ( erNone );
end;
procedure TAbstractFile.Append;
begin
if (Flags and flAssigned <> flAssigned) then
SetIOResult(erFile_not_assigned)
else
begin
SetIOResult ( erNone );
if (Flags and flOpened = flOpened) then
Close;
if (Flags and flError <> flError) then
SetFileMode ( (FileMode and (not 3)) or 1 );
end;
end;
procedure TAbstractFile.Copy(var AFile : PAbstractFile; ACount: LongInt);
var
RCount, WCount : word;
begin
repeat
BlockRead(Buf, Sizeof(Buf), RCount);
if (Flags and flError <> flError) then
AFile^.BlockWrite(Buf, RCount, WCount);
until Eof or (Flags and flError = flError) or
(AFile^.Flags and flError = flError) or (RCount <> Sizeof(Buf)) or
(RCount <> WCount);
end;
function TAbstractFile.Sof: Boolean;
begin
Sof := (Ptr = 0 );
end;
function TAbstractFile.Eof: Boolean;
begin
Eof := (FileSize <= Ptr );
end;
function TAbstractFile.SeekSof: Boolean;
begin
Seek( 0 );
SeekSof := Flags and flError <> flError;
end;
function TAbstractFile.SeekEof: Boolean;
var
Size : LongInt;
begin
Size := FileSize;
if (Flags and flError <> flError) then
Seek( Size );
SeekEof := Flags and flError <> flError;
end;
procedure TAbstractFile.SetFileMode ( AMode : word );
begin
Mode := AMode;
Flags := Flags and (Not flRandom);
case Mode and 2 of
0 : Flags := Flags or flRead;
1 : Flags := Flags or flWrite;
2 : Flags := Flags or flRandom;
end;
end;
procedure TAbstractFile.Insert(ACount : longint);
var
Size,
Count : word;
OldPtr : LongInt;
CurPtr : LongInt;
begin
OldPtr := FilePos;
if (Flags and flError = flError) or (ACount < 1) then Exit;
CurPtr := FileSize;
if (Flags and flError <> flError) then
repeat
Size := Sizeof(Buf);
if CurPtr - Size < OldPtr then
Size := CurPtr - OldPtr;
Dec(CurPtr, Size);
Seek(CurPtr);
if (Flags and flError <> flError) then BlockRead(Buf, Size, Count);
if (Flags and flError <> flError) then Seek(CurPtr + ACount);
if (Flags and flError <> flError) then BlockWrite(Buf, Size, Count);
until (CurPtr = OldPtr) or (Flags and flError = flError);
if (Flags and flError <> flError) then Seek(OldPtr);
end;
procedure TAbstractFile.Delete(ACount : longint);
var
Size,
Count : word;
OldPtr : LongInt;
CurPtr : LongInt;
FSize : LongInt;
begin
OldPtr := FilePos;
if (Flags and flError = flError) or (ACount < 1) then Exit;
FSize := FileSize;
CurPtr := OldPtr + ACount;
if (Flags and flError <> flError) then
repeat
Size := Sizeof(Buf);
if CurPtr + Size > FSize then
Size := FSize - CurPtr;
Seek(CurPtr);
if (Flags and flError <> flError) then BlockRead(Buf, Size, Count);
if (Flags and flError <> flError) then Seek(CurPtr - ACount);
if (Flags and flError <> flError) then BlockWrite(Buf, Size, Count);
Inc(CurPtr, Size);
until (CurPtr >= FSize) or (Flags and flError = flError);
if (Flags and flError <> flError) then Truncate;
if (Flags and flError <> flError) then Seek(OldPtr);
end;
procedure TAbstractFile.ReadRecord (const AAdr; ASize : word);
var
Count : word;
begin
BlockRead ( AAdr, ASize, Count );
if (Flags and flError <> flError) and (Count <> ASize) then
SetIOResult(erDevice_Read_Fault);
end;
procedure TAbstractFile.WriteRecord (const AAdr; ASize : word);
var
Count : word;
begin
BlockWrite ( AAdr, ASize, Count );
if (Flags and flError <> flError) and (Count <> ASize) then
SetIOResult(erDevice_Write_Fault);
end;
procedure TAbstractFile.ReadChar ( const AChr );
begin
ReadRecord(AChr, Sizeof(Char));
end;
procedure TAbstractFile.WriteChar ( const AChr );
begin
WriteRecord(AChr, Sizeof(Char));
end;
procedure TAbstractFile.ReadBoolean (const ABoolean);
begin
ReadRecord(ABoolean, Sizeof(Boolean));
end;
procedure TAbstractFile.WriteBoolean (const ABoolean);
begin
WriteRecord(ABoolean, Sizeof(Boolean));
end;
procedure TAbstractFile.ReadByteBool (const AByteBool);
begin
ReadRecord(AByteBool, Sizeof(ByteBool));
end;
procedure TAbstractFile.WriteByteBool (const AByteBool);
begin
WriteRecord(AByteBool, Sizeof(ByteBool));
end;
procedure TAbstractFile.ReadWordBool (const AWordBool);
begin
ReadRecord(AWordBool, Sizeof(WordBool));
end;
procedure TAbstractFile.WriteWordBool (const AWordBool);
begin
WriteRecord(AWordBool, Sizeof(WordBool));
end;
procedure TAbstractFile.ReadLongBool (const ALongBool);
begin
ReadRecord(ALongBool, Sizeof(LongBool));
end;
procedure TAbstractFile.WriteLongBool (const ALongBool);
begin
WriteRecord(ALongBool, Sizeof(LongBool));
end;
procedure TAbstractFile.ReadByte (const AByte);
begin
ReadRecord(AByte, Sizeof(Byte));
end;
procedure TAbstractFile.WriteByte (const AByte);
begin
WriteRecord(AByte, Sizeof(Byte));
end;
procedure TAbstractFile.ReadWord (const AWord);
begin
ReadRecord(AWord, Sizeof(Word));
end;
procedure TAbstractFile.WriteWord (const AWord);
begin
WriteRecord(AWord, Sizeof(Word));
end;
procedure TAbstractFile.ReadShortInt (const AShortInt);
begin
ReadRecord(AShortInt, Sizeof(ShortInt));
end;
procedure TAbstractFile.WriteShortInt (const AShortInt);
begin
WriteRecord(AShortInt, Sizeof(ShortInt));
end;
procedure TAbstractFile.ReadInteger (const AInteger);
begin
ReadRecord(AInteger, Sizeof(Integer));
end;
procedure TAbstractFile.WriteInteger (const AInteger);
begin
WriteRecord(AInteger, Sizeof(Integer));
end;
procedure TAbstractFile.ReadLongInt (const ALongInt);
begin
ReadRecord(ALongInt, Sizeof(LongInt));
end;
procedure TAbstractFile.WriteLongInt (const ALongInt);
begin
WriteRecord(ALongInt, Sizeof(LongInt));
end;
procedure TAbstractFile.ReadReal (const AReal);
begin
ReadRecord(AReal, Sizeof(Real));
end;
procedure TAbstractFile.WriteReal (const AReal);
begin
WriteRecord(AReal, Sizeof(Real));
end;
procedure TAbstractFile.ReadSingle (const ASingle);
begin
ReadRecord(ASingle, Sizeof(Single));
end;
procedure TAbstractFile.WriteSingle (const ASingle);
begin
WriteRecord(ASingle, Sizeof(Single));
end;
procedure TAbstractFile.ReadDouble (const ADouble);
begin
ReadRecord(ADouble, Sizeof(Double));
end;
procedure TAbstractFile.WriteDouble (const ADouble);
begin
WriteRecord(ADouble, Sizeof(Double));
end;
procedure TAbstractFile.ReadComp (const AComp);
begin
ReadRecord(AComp, Sizeof(Comp));
end;
procedure TAbstractFile.WriteComp (const AComp);
begin
WriteRecord(AComp, Sizeof(Comp));
end;
procedure TAbstractFile.ReadExtended (const AExtended);
begin
ReadRecord(AExtended, Sizeof(Extended));
end;
procedure TAbstractFile.WriteExtended (const AExtended);
begin
WriteRecord(AExtended, Sizeof(Extended));
end;
procedure TAbstractFile.ReadString (const AString; AStrSize : word);
begin
ReadRecord(AString, Sizeof(String));
end;
procedure TAbstractFile.WriteString (const AString; AStrSize : word);
begin
WriteRecord(AString, Sizeof(String));
end;
function TAbstractFile.GetString : String;
var
Str : String;
begin
ReadByte(Str[0]);
if (Flags and flError <> flError) then
ReadRecord(Str[1], Byte(Str[0]));
if (Flags and flError <> flError) then
GetString := Str
else
GetString := '';
end;
procedure TAbstractFile.PutString (const AString);
begin
WriteRecord(AString, Length(String(AString)) + 1);
end;
{ Text files }
function TAbstractFile.Soln: Boolean;
var
Test : word;
Count : word;
P : LongInt;
begin
Soln := False;
P := FilePos;
if (Flags and flError = flError) then Exit;
case P of
1 : Exit;
0 : Soln := True;
else
Seek(P - 2);
if (Flags and flError <> flError) then
BlockRead ( Test, Sizeof(Test), Count );
Soln := (Count = 2) and (Test = $0A0D) and
(Flags and flError <> flError);
end;
end;
function TAbstractFile.EoLn: Boolean;
var
Test : word;
Count : word;
P : LongInt;
begin
EoLn := False;
P := FilePos;
if (Flags and flError = flError) then Exit;
BlockRead ( Test, Sizeof(Test), Count );
Eoln := (Count = 2) and (Test = $0A0D) and
(Flags and flError <> flError);
Seek(P);
end;
function TAbstractFile.SeekSoln: Boolean;
var
SLn : boolean;
begin
SetIOResult ( erNone );
repeat
SLn := Soln;
if (Flags and flError <> flError) and (Not SLn) then
Seek(Ptr - 1);
until SLn or (Flags and flError = flError);
SeekSoln := Soln;
end;
function TAbstractFile.SeekEoln: Boolean;
var
EndLn, EndF : boolean;
begin
SetIOResult ( erNone );
repeat
EndLn := Eoln;
if (Flags and flError <> flError) then EndF := Eof;
if (Flags and flError <> flError) and (Not EndLn) and (Not EndF) then
Seek(Ptr + 1);
until EndLn or EndF or (Flags and flError = flError);
SeekEoln := Eoln;
end;
{ Old version
procedure TAbstractFile.Read ( const AStr; AMax : word );
var
P : ^String;
I : word;
C : Char;
begin
P := @AStr;
I := 0;
P^[0] := #0;
while (Not EOF) and (Not Eoln) and (Flags and flError <> flError) and (I < AMax) do
begin
ReadRecord(C, Sizeof(C));
if (Flags and flError <> flError) then
begin
Inc(I);
if I <= 255 then
begin
P^[0] := Char(I);
P^[I] := C;
end;
end;
end;
end; }
procedure TAbstractFile.Read ( const AStr; AMax : word );
var
P, T : ^String;
Max, Count : word;
I : integer;
L : LongInt;
begin
P := @AStr;
P^[0] := #0;
T := @Buf;
L := FilePos;
if Max > 255 then Max := 255;
if (AMax > 0) and (Flags and flError <> flError) then begin
BlockRead(Buf[1], Max, Count); { Buffer is 512, Max String is 255 }
if (Flags and flError <> flError) then
begin
Buf[0] := Count;
I := Pos(#$0A#$0D, T^);
if I > 0 then begin
Buf[0] := I - 1;
Seek(L + I + 1);
{ end else begin
Seek(L + Count + 1);}
end;
P^ := T^;
end;
end;
end;
procedure TAbstractFile.Write ( const AStr );
begin
WriteRecord(String(AStr)[1], Length(String(AStr)));
end;
{ Old Version
procedure TAbstractFile.ReadLn ( const AStr; AMax : word );
var
P : ^String;
I : word;
C : Char;
begin
P := @AStr;
I := 0;
P^[0] := #0;
while (Not EOF) and (Not Eoln) and (Flags and flError <> flError) do
begin
ReadRecord(C, Sizeof(C));
if (Flags and flError <> flError) and (I < AMax) then
begin
Inc(I);
if I <= 255 then
begin
P^[0] := Char(I);
P^[I] := C;
end;
end;
end;
if (Flags and flError <> flError) and (Not EOF) then ReadRecord ( I, Sizeof(I) );
end; }
procedure TAbstractFile.ReadLn ( const AStr; AMax : word );
var
P, T : ^String;
Max, Count : word;
I : integer;
L : LongInt;
begin
P := @AStr;
P^[0] := #0;
T := @Buf;
L := FilePos;
I := 0;
if Max > 255 then Max := 255;
if (AMax > 0) and (Flags and flError <> flError) then begin
BlockRead(Buf[1], Max, Count); { Buffer is 512, Max String is 255 }
if (Flags and flError <> flError) then
begin
Buf[0] := Count;
I := Pos(#$0D#$0A, T^);
if I > 0 then
Buf[0] := I - 1;
P^ := T^;
end;
end;
while (FilePos > 0) and ( I = 0 ) and (Not EOF) and (Flags and flError <> flError) do begin
Seek(FilePos - 1);
BlockRead(Buf[1], 255, Count);
Buf[0] := Count;
I := Pos(#$0A#$0D, T^);
end;
if I <> 0 then
Seek(L + I + 1);
end;
procedure TAbstractFile.WriteLn ( const AStr );
const
CRLF : word = $0A0D;
begin
WriteRecord(String(AStr)[1], Length(String(AStr)));
if Flags and flError <> flError then WriteWord ( CRLF );
end;
function TAbstractFile.LengthLn : word;
var
OPtr : longInt;
SPtr : longInt;
EPtr : longInt;
begin
LengthLn := 0;
OPtr := FilePos;
if (Flags and flError <> flError) then SeekSoln;
if (Flags and flError <> flError) then SPtr := FilePos;
if (Flags and flError <> flError) then SeekEoln;
if (Flags and flError <> flError) then EPtr := FilePos;
if (Flags and flError <> flError) then Seek(OPtr);
if (Flags and flError <> flError) then LengthLn := EPtr - SPtr;
end;
procedure TAbstractFile.InsertLn ( ALen : word );
var
OPtr : longInt;
begin
SeekSoln;
if (Flags and flError <> flError) then OPtr := FilePos;
if (Flags and flError <> flError) then Insert(ALen + 2);
if (Flags and flError <> flError) then Seek(OPtr + ALen);
if (Flags and flError <> flError) then Write(CRLF);
if (Flags and flError <> flError) then Seek(OPtr);
end;
procedure TAbstractFile.DeleteLn;
var
OLen : word;
OPtr : LongInt;
begin
SeekSoln;
if (Flags and flError <> flError) then OPtr := FilePos;
if (Flags and flError <> flError) then OLen := LengthLn;
if (Flags and flError <> flError) then Delete(OLen + 2);
if (Flags and flError <> flError) then Seek(OPtr);
end;
procedure TAbstractFile.ResizeLn ( ALen : word );
var
OLen : word;
OPtr : LongInt;
begin
OLen := LengthLn;
if (Flags and flError <> flError) then
begin
if OLen < ALen then
begin
SeekSoln;
if (Flags and flError <> flError) then OPtr := FilePos;
if (Flags and flError <> flError) then Seek(OPtr + OLen);
if (Flags and flError <> flError) then Insert(ALen - OLen);
if (Flags and flError <> flError) then SeekSoln;
end
else
if OLen > ALen then
begin
SeekSoln;
if (Flags and flError <> flError) then OPtr := FilePos;
if (Flags and flError <> flError) then Seek(OPtr + ALen);
if (Flags and flError <> flError) then Delete(OLen - ALen);
if (Flags and flError <> flError) then Seek(OPtr);
end
else
SeekSoln;
end;
end;
function TAbstractFile.SeekLn ( ANumber : LongInt ) : boolean;
var
I : LongInt;
S : String[1];
begin
SeekSof;
for I := 1 to ANumber - 1 do
if (Flags and flError <> flError) then
ReadLn ( S, 0 );
SeekLn := (Flags and flError <> flError);
end;
{ ------------ TFile ------------ }
function TFile.ObjectClass(AName : String) : String;
begin
if (AName = '') or (AName = class_TFile) then
ObjectClass := class_TFile
else
ObjectClass := inherited ObjectClass(AName);
end;
constructor TFile.Create;
begin
inherited Create;
end;
destructor TFile.Destroy;
begin
inherited Destroy;
end;
procedure TFile.Assign( AFileName : String );
var
Action : byte;
begin
inherited Assign(AFileName);
if (Flags and flError <> flError) then
begin
repeat
System.Assign(FRec, AFileName);
Action := SetIOResult(System.IOResult);
until (Action <> fcRetry);
if (Action <> fcAbort) then
Flags := Flags or flAssigned;
end;
end;
procedure TFile.BlockRead(const ABuf; ACount: Word; var AResult: Word);
var
P : Pointer;