-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSTREAMS.PAS
1092 lines (1015 loc) · 25 KB
/
STREAMS.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
unit Streams;
{$I STDEFINE.INC}
{Cat = Aleksej Kozlov, 2:5030/1326.13@fidonet}
{Cat
28/08/2001 - ¯¥à¥¤¥« « ç⥨¥/§ ¯¨áì áâப ¢ ¯®â®ª¨ ¤«ï ᮢ¬¥á⨬®á⨠á
⨯®¬ AnsiString; ¤®¡ ¢¨« ç⥨¥/§ ¯¨áì ¤«¨ëå áâப (LongString) ¢ ¯®â®ª¨
}
interface
uses
VpSysLow, Defines, Objects2, lfn
;
const
{ TStream access modes }
stCreate = $FFFF; { Create new file }
stOpenRead = Open_Access_ReadOnly or open_share_DenyNone;
{ Read access only }
stOpenWrite = Open_Access_WriteOnly or open_share_DenyNone;
{ Write access only }
stOpen = Open_Access_ReadWrite or open_share_DenyNone;
{ Read and write access }
stOpenPacked = Open_Access_ReadWrite+1;
{ Read access only, packed files too }
{ File share mode constants }
fmClean = $FF00;
{ Mask to clean low byte of file mode constatns }
fmOpenMode = $FFF0;
{ Mask to apply fmReadOnly/fmWriteOnly/fmReadWrite }
fmReadOnly = Open_Access_ReadOnly; { Open read-only file }
fmWriteOnly = Open_Access_WriteOnly; { Open file for write only }
fmReadWrite = Open_Access_ReadWrite;
{ Open file as for read, as for write }
fmPacked = Open_Access_ReadWrite+1; { Open a packed file, if can }
fmDeny = $FF0F; { Mask to apply fmDenyXXX }
fmDenyAll = Open_Share_DenyReadWrite; { Exclusive file use }
fmDenyWrite = Open_Share_DenyWrite; { Deny write access }
fmDenyRead = Open_Share_DenyRead; { Deny read access }
fmDenyNone = open_share_DenyNone; { Deny no access }
fmDenyChild = open_share_DenyNone; { Don't give right's to child }
{ TStream error codes }
stOK = 0; { No error }
stError = -1; { Access error }
stInitError = -2; { Cannot initialize stream }
stReadError = -3; { Read beyond end of stream }
stWriteError = -4; { Cannot expand stream }
stGetError = -5; { Get of unregistered object type }
stPutError = -6; { Put of unregistered object type }
stSeekError = -7; { Stream seek error }
stOpenError = -8; { Stream open error }
type
PStreamRec = ^TStreamRec;
TStreamRec = record
ObjType: Word;
VmtLink: Pointer;
Load: Pointer;
Store: Pointer;
Next: PStreamRec;
end;
PStream = ^TStream;
TStream = object(TObject)
{Cat: íâ®â ®¡ê¥ªâ ¢ë¥á¥ ¢ ¯« £¨ãî ¬®¤¥«ì; ¨§¬¥ïâì ªà ©¥ ®áâ®à®¦®!}
Status: Integer;
ErrorInfo: Integer;
StreamSize: TFileSize; { Stream current size }
Position: TFileSize; { Current Position }
procedure CopyFrom(var S: TStream; Count: TFileSize);
procedure Error(Code, Info: Integer); virtual;
procedure Flush; virtual;
function Get: PObject;
function GetPos: TFileSize; virtual;
function GetSize: TFileSize; virtual;
procedure Put(P: PObject);
procedure Read(var Buf; Count: SW_Word); virtual;
function ReadStr: PString;
function ReadLongStr: PLongString;
procedure ReadStrV(var S: String);
procedure ReadLongStrV(var S: LongString);
procedure Reset;
procedure Seek(Pos: TFileSize); virtual;
function StrRead: PChar;
procedure StrWrite(P: PChar);
procedure Truncate; virtual;
procedure Write(const Buf; Count: SW_Word); virtual;
procedure WriteStr(P: PString);
procedure WriteLongStr(P: PLongString);
function Eof: Boolean;
procedure Close; virtual;
function MaxDataBlock: Longint; virtual;
end;
PFileStream = ^TFileStream;
TFileStream = object(TStream)
AbsFile: PlFile;
constructor Init(AAbsFile: PlFile; FileName: FNameStr; Mode: Word);
destructor Done; virtual;
procedure Read(var Buf; Count: SW_Word); virtual;
procedure Seek(Pos: TFileSize); virtual;
procedure SeekEOF; virtual;
procedure Truncate; virtual;
procedure Write(const Buf; Count: SW_Word); virtual;
procedure Close; virtual;
function MaxDataBlock: Longint; virtual;
end;
PDosStream = ^TDosStream;
TDOSStream = object(TFileStream)
constructor Init(FileName: FNameStr; Mode: Word);
end;
PBufStream = ^TBufStream;
TBufStream = object(TDOSStream)
{`2}
{Cat: íâ®â ®¡ê¥ªâ ¢ë¥á¥ ¢ ¯« £¨ãî ¬®¤¥«ì; ¨§¬¥ïâì ªà ©¥ ®áâ®à®¦®!}
Buffer: PByteArray;
BufSize: SW_Word;
BufPtr: SW_Word;
{` ˆ¤¥ªá ¢ Buffer^ ¯¥à¢®£® ¥®¡à ¡®â ®£® ¡ ©â , â® ¥áâì
¥¯à®ç¨â ®£® ¯à¨ ç⥨¨, ¥ § ¯¨á ®£® ¯à¨ § ¯¨á¨`}
BufEnd: SW_Word;
{` ˆ¤¥ªá ¢ Buffer^ ¯¥à¢®£® ¥§ ¯®«¥®£® ¡ ©â `}
ModBufStart, ModBufEnd: SW_Word;
{` ¢ ¡ãä¥à¥ ¬®¤¨ä¨æ¨à®¢ ë ¡ ©âë á ModBufStart ¯® ModBufEnd-1 `}
constructor Init(FileName: FNameStr; Mode: Word; Size: SW_Word);
destructor Done; virtual;
procedure Flush; virtual;
procedure Read(var Buf; Count: SW_Word); virtual;
procedure Seek(Pos: TFileSize); virtual;
procedure Truncate; virtual;
procedure Write(const Buf; Count: SW_Word); virtual;
procedure Close; virtual;
end;
{`}
PMemoryStream = ^TMemoryStream;
TMemoryStream = object(TStream)
{Cat: íâ®â ®¡ê¥ªâ ¢ë¥á¥ ¢ ¯« £¨ãî ¬®¤¥«ì; ¨§¬¥ïâì ªà ©¥ ®áâ®à®¦®!}
BlkCount: LongInt;
{` Number of segments `}
BlkSize: Word;
{` Memory block size `}
MemSize: LongInt;
{` Memory alloc size `}
BlkList: PPointerArray;
{` Memory block list `}
constructor Init(ALimit: LongInt; ABlockSize: Word);
destructor Done; virtual;
procedure Read(var Buf; Count: SW_Word); virtual;
procedure Truncate; virtual;
procedure Write(const Buf; Count: SW_Word); virtual;
private
function ChangeListSize(ALimit: LongInt): Boolean;
end;
procedure RegisterType(var S: TStreamRec);
procedure ReRegisterType(var S: TStreamRec);
function GetAnyMemoryStream: PStream;
implementation
uses
{$IFDEF PLUGIN}Plugin, {$ENDIF}
Strings, Memory, Advance1
{$IFDEF DPMI32}, LfnVP {$ENDIF}, Dos
;
const
StreamTypes: PStreamRec = nil;
procedure RegisterError;
begin
end;
procedure RegisterType(var S: TStreamRec);
const
SRegisterError = 'RegisterError, ObjType=';
var
P: PStreamRec;
begin
if S.ObjType = 0 then
begin
Writeln(SRegisterError, S.ObjType);
RegisterError;
end;
P := StreamTypes;
while (P <> nil) and (P^.ObjType <> S.ObjType) do
P := P^.Next;
if P = nil then
begin
S.Next := StreamTypes;
StreamTypes := @S;
end
else if (P^.VmtLink <> S.VmtLink) or (P^.Load <> S.Load)
or (P^.Store <> S.Store)
then
begin
Writeln(SRegisterError, S.ObjType);
RegisterError;
end;
end { RegisterType };
procedure ReRegisterType(var S: TStreamRec);
var
P, L: PStreamRec;
begin
if S.ObjType = 0 then
RegisterError;
P := StreamTypes;
L := nil;
while (P <> nil) and (P^.ObjType <> S.ObjType) do
begin
L := P;
P := P^.Next;
end;
if P <> nil then
if L <> nil then
L^.Next := P^.Next
else
StreamTypes := P^.Next;
S.Next := StreamTypes;
StreamTypes := @S;
end;
const
TStream_Error = vmtHeaderSize+$04;
TStream_Flush = vmtHeaderSize+$08;
TStream_Read = vmtHeaderSize+$14;
TStream_Write = vmtHeaderSize+$20;
StreamMagic = $590C5CF1;
{ Stream error handler }
{ In eax = Error info }
{ dl = Error code }
{ ecx = Stream object pointer }
{ Uses eax,edx }
procedure DoStreamError;
assembler; {$USES ecx}
{$FRAME-}
asm
movsx edx,dl
push edx { [1]:Integer = Code }
push eax { [2]:Integer = Info }
push ecx { [3]:Pointer = Self }
mov eax,[ecx]
call DWord Ptr [eax].TStream_Error
end;
procedure TStream.CopyFrom(var S: TStream; Count: TFileSize);
var
N: Word;
Buffer: PByteArray;
BufSize: Word;
Allocated: Boolean;
TTempBuf: array[0..255] of Byte;
begin
BufSize := MinBufSize(Count, 32768);
GetMem(Buffer, BufSize);
if Buffer = nil then
begin
Allocated := False;
Buffer := PByteArray(@TTempBuf);
BufSize := 256;
end
else
Allocated := True;
while Count > 0 do
begin
N := MinBufSize(Count, BufSize);
S.Read(Buffer^, N);
Write(Buffer^, N);
Count := Count - N;
end;
if Allocated then
FreeMem(Buffer, BufSize);
end { TStream.CopyFrom };
procedure TStream.Error(Code, Info: Integer);
type
TErrorProc = procedure (var S: TStream);
begin
Status := Code;
ErrorInfo := Info;
end;
procedure TStream.Flush;
begin
end;
function TStream.Get: PObject;
assembler; {$USES None}
{$FRAME+}
asm
{Cat: ¤®¡ ¢¨« ¯à®¢¥àªã StreamMagic}
push eax
mov eax,esp
push eax { [1]:Pointer = Buf }
push 4 { [2]:DWord = Count }
mov eax,Self
push eax { [3]:Pointer = Self }
mov eax,[eax]
call DWord Ptr [eax].TStream_Read
pop eax
cmp eax,StreamMagic
jne @@Error
{/Cat}
push eax
mov eax,esp
push eax { [1]:Pointer = Buf }
push 4 { [2]:DWord = Count }
mov eax,Self
push eax { [3]:Pointer = Self }
mov eax,[eax]
call DWord Ptr [eax].TStream_Read
pop eax
test eax,eax { Return nil }
jz @@4
{Cat: ¥á«¨ ¯ëâ ¥¬áï ¯à®ç¨â âì ®¡ê¥ªâ, ॣ¨áâà¨àã¥¬ë© ¯« £¨®¬, â® ¤®
á ç « § ¯ãáâ¨âì íâ®â ¯« £¨, çâ®¡ë ® ¯à®¢ñ« ॣ¨áâà æ¨î}
{$IFDEF PLUGIN}
push eax
push eax
call PluginRegisterObject
pop eax
{$ENDIF}
{/Cat}
mov edx,StreamTypes
jmp @@2
@@1:
cmp eax,[edx].TStreamRec.ObjType
je @@3
mov edx,[edx].TStreamRec.Next
@@2:
test edx,edx
jnz @@1
@@Error:
mov ecx,Self
mov dl,stGetError
call DoStreamError
xor eax,eax { Return nil }
jmp @@4
@@3:
push Self
{ [1]:Pointer = TStream }
push [edx].TStreamRec.VmtLink
{ [2]:DWord = VMT }
push 0
{ [3]:Pointer = Self = nil: allocate in dynamic memory }
call [edx].TStreamRec.Load
@@4:
{ Return Self or nil }
end;
function TStream.GetPos: TFileSize;
begin
if (Status = stOK) then
GetPos := Position
else
GetPos := -1;
end;
function TStream.GetSize: TFileSize;
begin
if (Status = stOK) then
GetSize := StreamSize
else
GetSize := -1;
end;
procedure TStream.Put(P: PObject);
assembler; {$USES None}
{$FRAME+}
asm
{Cat: ¤®¡ ¢¨« § ¯¨áì StreamMagic}
push StreamMagic
mov eax,esp
push eax { [1]:Pointer = Buf }
push 4 { [2]:DWord = Size }
mov eax,Self { [3]:Pointer = Self }
push eax
mov eax,[eax]
call DWord Ptr [eax].TStream_Write
pop ecx
{/Cat}
mov ecx,P
jecxz @@4
mov eax,[ecx] { VMT pointer }
mov edx,StreamTypes
jmp @@2
@@1:
cmp eax,[edx].TStreamRec.VmtLink
je @@3
mov edx,[edx].TStreamRec.Next
@@2:
test edx,edx
jne @@1
mov ecx,Self
mov dl,stPutError
call DoStreamError
jmp @@5
{/Cat}
@@3:
mov ecx,[edx].TStreamRec.ObjType
@@4:
push edx
push ecx { Write object type }
mov eax,esp
push eax { [1]:Pointer = Buf }
push 4 { [2]:DWord = Size }
mov eax,Self { [3]:Pointer = Self }
push eax
mov eax,[eax]
call DWord Ptr [eax].TStream_Write
pop ecx
pop edx
jecxz @@5
push Self
{ [1]:Pointer = TStream }
push P
{ [2]:Pointer = Self }
call [edx].TStreamRec.Store
@@5:
end;
procedure TStream.Read(var Buf; Count: SW_Word);
begin
end;
{Cat}
function TStream.ReadStr: PString;
var
L: LongInt;
P: PString;
begin
L := 0;
Read(L, 1);
if L > 0 then
begin
GetMem(P, L+1);
SetLength(P^, L);
Read(P^[1], L);
ReadStr := P;
end
else
ReadStr := nil;
end;
function TStream.ReadLongStr: PLongString;
var
L: LongInt;
P: PLongString;
begin
Read(L, SizeOf(L));
if L > 0 then
if L > MaxLongStringLength then
begin
New(P);
SetLength(P^, MaxLongStringLength);
Read(P^[1], MaxLongStringLength);
Seek(Position-MaxLongStringLength+L);
ReadLongStr := P;
end
else
begin
New(P);
SetLength(P^, L);
Read(P^[1], L);
ReadLongStr := P;
end
else
ReadLongStr := nil;
end { TStream.ReadLongStr: };
procedure TStream.ReadStrV(var S: String);
var
L: LongInt;
begin
L := 0;
Read(L, 1);
if L > 0 then
begin
SetLength(S, L);
Read(S[1], L);
end
else
S := '';
end;
procedure TStream.ReadLongStrV(var S: LongString);
var
L: LongInt;
begin
Read(L, SizeOf(L));
if L > 0 then
if L > MaxLongStringLength then
begin
SetLength(S, MaxLongStringLength);
Read(S[1], MaxLongStringLength);
Seek(Position-MaxLongStringLength+L);
end
else
begin
SetLength(S, L);
Read(S[1], L);
end
else
S := '';
end;
{/Cat}
procedure TStream.Reset;
begin
Status := 0;
ErrorInfo := 0;
end;
procedure TStream.Seek(Pos: TFileSize);
begin
if Status = stOK then
if Pos < 0 then
Pos := 0
else if Pos <= StreamSize then
Position := Pos
else
Error(stSeekError, 0{!!Pos});
end;
function TStream.StrRead: PChar;
var
L: Word;
P: PChar;
begin
Read(L, SizeOf(L));
if L = 0 then
StrRead := nil
else
begin
GetMem(P, L+1);
Read(P[0], L);
P[L] := #0;
StrRead := P;
end;
end;
procedure TStream.StrWrite(P: PChar);
var
L: Word;
begin
if P = nil then
L := 0
else
L := StrLen(P);
Write(L, SizeOf(L));
if P <> nil then
Write(P[0], L);
end;
procedure TStream.Truncate;
begin
end;
procedure TStream.Write(const Buf; Count: SW_Word);
begin
end;
{Cat}
procedure TStream.WriteStr(P: PString);
var
L: LongInt;
begin
if P <> nil then
Write(P^[0], Length(P^)+1)
else
begin
L := 0;
Write(L, 1);
end;
end;
procedure TStream.WriteLongStr(P: PLongString);
var
L: LongInt;
begin
if P <> nil then
begin
L := Length(P^);
Write(L, 4);
Write(P^[1], L);
end
else
begin
L := 0;
Write(L, SizeOf(L));
end
end;
{/Cat}
function TStream.Eof: Boolean;
begin
Eof := (GetPos >= GetSize);
end;
procedure TStream.Close;
begin
end;
function TStream.MaxDataBlock: Longint;
begin
Result := 0;
end;
(*
{AK155}
function ASetFileSize(Handle: Word; FileSize: TFileSize): Word;
begin
ASetFileSize := 0;
if (ASetFilePos(Handle, FileSize, 0, FileSize) <> 0) or
(SysFileSetSize(Handle, FileSize) <> 0)
then
ASetFileSize := 1
else
ASetFileSize := 0;
end;
{/AK155}
*)
constructor TDOSStream.Init(FileName: FNameStr; Mode: Word);
var
P: PlFile;
begin
New(P);
FileName := lFileInit(P^, FileName);
inherited Init(P, FileName, Mode);
end { TDOSStream.Init };
constructor TBufStream.Init(FileName: FNameStr; Mode: Word; Size: SW_Word);
begin
inherited Init(FileName, Mode);
BufSize := Size;
if Size = 0 then
Error(stInitError, 0)
else
begin
GetMem(Buffer, Size);
if Buffer = nil then
Error(stInitError, 0);
ModBufStart := $FFFF;
end;
end;
destructor TBufStream.Done;
begin
Flush;
if Buffer <> nil then
begin
FreeMem(Buffer, BufSize);
Buffer := nil;
end;
inherited Done;
end;
procedure TBufStream.Flush;
var
Success: Integer;
W, Len: SW_Word;
Pos: TFileSize;
begin
if ModBufEnd > ModBufStart then
begin
if AbsFile^.Handle = -1 then
Error(stError, 103)
else
begin
Len := ModBufEnd - ModBufStart;
Pos := Position;
inherited Seek(Position - BufPtr + ModBufStart);
AbsFile^.Write(Buffer^[ModBufStart], Len, W);
Success := IOResult;
inherited Seek(Pos);
if (Success <> 0) or (W <> Len) then
Error(stError, Success);
end;
ModBufStart := $FFFF; ModBufEnd := 0;
end;
end { TBufStream.Flush };
procedure TBufStream.Read(var Buf; Count: SW_Word);
var
Success: Integer;
W, Bw: SW_Word;
P: PByteArray;
begin
P := @Buf;
if AbsFile^.Handle = -1 then
Error(stReadError, 103)
else if Position+Count > StreamSize then
Error(stReadError, 0)
else
begin
while (Count > 0) and (Status = stOK) do
begin
if BufPtr = BufEnd then
begin { Buffer is empty }
Bw := MinBufSize(StreamSize-Position, BufSize);
begin
Flush;
AbsFile^.Read(Buffer^, Bw, W);
Success := IOResult;
end;
BufPtr := 0;
BufEnd := W;
if (Success <> 0) or (Bw <> W) then
begin
Error(stReadError, Success);
BufEnd := W;
end;
end;
if Status = stOK then
begin
W := BufEnd-BufPtr;
if Count < W then
W := Count;
Move(Buffer^[BufPtr], P^, W);
Dec(Count, W);
Inc(BufPtr, W);
P := Pointer(LongInt(P)+W);
Position := Position + W;
end;
end;
end;
if (Status <> stOK) and (Count > 0) then
FillChar(P^, Count, #0);
end { TBufStream.Read };
procedure TBufStream.Seek(Pos: TFileSize);
var
BufStart: TFileSize; // ä ©«®¢ë© ¤à¥á ç « ¡ãä¥à
begin
Status := stOK;
if Position = Pos then
Exit;
BufStart := Position-BufPtr;
if (Pos >= BufStart) and (Pos < BufStart+BufEnd) then
begin { AK155 “áâ ®¢ª ¢ ¯à¥¤¥« å ¡ãä¥à ¡¥§ ®¡à é¥¨ï ª ä ©«ã}
BufPtr := i32(Pos - BufStart);
Position := Pos;
end
else
begin
Flush;
if (Status <> stOK) then
Exit;
inherited Seek(Pos);
BufPtr := 0;
BufEnd := 0;
end;
end;
procedure TBufStream.Truncate;
begin
Flush;
inherited Truncate;
StreamSize := AbsFile.FSize;
end;
procedure TBufStream.Write(const Buf; Count: SW_Word);
var
Success: Integer;
W: SW_Word;
P: PByteArray;
begin
if AbsFile^.Handle = -1 then
Error(stWriteError, 103)
else
begin
P := @Buf;
while (Count > 0) and (Status = stOK) do
begin
if BufPtr = BufSize then
begin { Buffer is full }
Flush;
BufPtr := 0;
BufEnd := 0;
end;
if Status = stOK then
begin
W := BufSize-BufPtr;
if Count < W then
W := Count;
Move(P^, Buffer^[BufPtr], W);
if BufPtr < ModBufStart then
ModBufStart := BufPtr;
Dec(Count, W);
Inc(BufPtr, W);
if BufPtr > ModBufEnd then
ModBufEnd := BufPtr;
P := Pointer(LongInt(P)+W);
Position := Position + W;
if Position > StreamSize then
StreamSize := Position;
if BufEnd < BufPtr then
BufEnd := BufPtr;
end;
end;
end;
end { TBufStream.Write };
procedure TBufStream.Close;
begin
Flush;
inherited Close;
end;
constructor TMemoryStream.Init(ALimit: LongInt; ABlockSize: Word);
var
W: LongInt;
begin
inherited Init;
if ABlockSize = 0 then
BlkSize := 8192
else
BlkSize := ABlockSize;
if ALimit <= 0 then
W := 1
else
W := (ALimit+BlkSize-1) div BlkSize;
if not ChangeListSize(W) then
Error(stInitError, 0);
(*
StreamSize:=MemSize;
{AK155 2-03-2002
à¨ à ¡®â¥ á Clipboard StreamSize ¨á¯®«ì§ã¥âáï ¢ ª ç¥á⢥ 㪧 ⥫ï
ª®æ ¯®â®ª , ¯®í⮬ã ã ᢥ¦¥á®§¤ ®£® ¯®â®ª ® ¤®«¦¥ ¡ëâì à ¢¥ ã«î.
€ íâ® ¯à¨á¢®¥¨¥ ᮧ¤ ¥â ¢®§¬®¦®áâì çâ¥¨ï ¬ãá®à § ¯à¥¤¥« ¬¨ ä ªâ¨ç¥áª¨
§ ¯¨á ®£® ¯®â®ª . ¯à¨¬¥à, ¥á«¨ ¢ dn.ini 㪠§ âì MaxClipboardSize=0,
â® DN ¯®ç⨠£ à â¨à®¢ ® ¢¨á¥â ¯à¨ ¯¥à¢®¬ ¦¥ ¢§ï⨨ ¢ ¡ãä¥à ¢ । ªâ®à¥.
® ¢ FileCopy GetSize (â® ¥áâì StreamSize) ¨á¯®«ì§ã¥âáï ¢ ª ç¥á⢥
à §¬¥à ¯®â®ª (çâ® ¥¥áâ¥á⢥®), ¯®í⮬ã â ¬ ⮦¥ ¥áâì ᮮ⢥âáâ¢ãîé ï
ª®à४æ¨ï. „àã£¨å ¬¥áâ, £¤¥ ¨á¯®«ì§ã¥âáï StreamSize ¤«ï TMemoryStream,
ï ¥ 襫.}
*)
end { TMemoryStream.Init };
destructor TMemoryStream.Done;
begin
ChangeListSize(0);
inherited Done;
end;
function TMemoryStream.ChangeListSize(ALimit: LongInt): Boolean;
var
I, W: LongInt;
Li: LongInt;
P: PPointerArray;
begin
if (ALimit <> BlkCount) then
begin
ChangeListSize := False;
if ALimit > MaxPtrs then
Exit;
if ALimit > 0 then
begin
Li := ALimit*SizeOf(Pointer);
if not LowMemory and (MaxAvail > Li) then
begin
GetMem(P, Li);
FillChar(P^, Li, #0);
end
else
Exit;
if (BlkCount <> 0) and (BlkList <> nil) then
if BlkCount <= ALimit then
Move(BlkList^, P^, BlkCount*SizeOf(Pointer))
else
Move(BlkList^, P^, Li);
end
else
begin
P := nil;
ALimit := 0;
end;
if ALimit < BlkCount then
for W := BlkCount-1 downto ALimit do
FreeMem(BlkList^[W], BlkSize);
if (P <> nil) and (ALimit > BlkCount) then
begin
for W := BlkCount to ALimit-1 do
begin
if LowMemory or (MaxAvail < BlkSize) then
begin
for I := BlkCount to W-1 do
FreeMem(P^[I], BlkSize);
FreeMem(P, Li);
Exit;
end
else
GetMem(P^[W], BlkSize);
end;
end;
if (BlkCount <> 0) and (BlkList <> nil) then
FreeMem(BlkList, BlkCount*SizeOf(Pointer));
BlkList := P;
BlkCount := ALimit;
{ * REMARK * - Do not shorten this, result can be > 64K }
MemSize := BlkCount;
MemSize := MemSize*BlkSize;
{ * REMARK END * - Leon de Boer }
end;
ChangeListSize := True;
end { TMemoryStream.ChangeListSize };
procedure TMemoryStream.Read(var Buf; Count: SW_Word);
var
W, CurBlock: Word;
BlockPos: Longint;
Li: TFileSize;
P, Q: PByteArray;
begin
P := @Buf;
if Position+Count > StreamSize then
Error(stReadError, 0)
else
begin
while (Count > 0) and (Status = stOK) do
begin
CurBlock := Trunc(Position / BlkSize);
{ * REMARK * - Do not shorten this, result can be > 64K }
Li := CurBlock;
Li := Li*BlkSize;
{ * REMARK END * - Leon de Boer }
BlockPos := i32(Position-Li);
W := BlkSize-BlockPos;
if W > Count then
W := Count;
Q := Pointer(LongInt(BlkList^[CurBlock])+BlockPos);
Move(Q^, P^, W);
Position := Position + W;
P := Pointer(LongInt(P)+W);
Dec(Count, W);
end;
end;
if Count <> 0 then
FillChar(P^, Count, #0);
end { TMemoryStream.Read };
procedure TMemoryStream.Truncate;
var
W: Word;
begin
if Status = stOK then
begin
if Position = 0 then
W := 1
else
W := Trunc((Position+BlkSize-1) / BlkSize);
if ChangeListSize(W) then
StreamSize := Position
else
Error(stError, 0);
end;
end;
procedure TMemoryStream.Write(const Buf; Count: SW_Word);
var
W, CurBlock, BlockPos: Word;
Li: TFileSize;
P, Q: PByteArray;
begin
if Position+Count > MemSize then
begin
if Position+Count = 0 then
W := 1
else
W := Trunc((Position+Count+BlkSize-1) / BlkSize);
if not ChangeListSize(W) then
begin
Error(stWriteError, 0);
Exit;
end;
end;
P := @Buf;
while (Count > 0) and (Status = stOK) do
begin
CurBlock := Trunc(Position / BlkSize);
{ * REMARK * - Do not shorten this, result can be > 64K }
Li := CurBlock;
Li := Li*BlkSize;
{ * REMARK END * - Leon de Boer }
BlockPos := i32(Position-Li);