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
|
{
Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
This module provides stream classes
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit cstreams;
{$i fpcdefs.inc}
interface
uses
cutils;
{****************************************************************************
TCStream
****************************************************************************}
{
TCStream is copied directly from classesh.inc from the FCL so
it's compatible with the normal Classes.TStream.
TCFileStream is a merge of THandleStream and TFileStream and updated
to have a 'file' type instead of Handle.
TCCustomMemoryStream and TCMemoryStream are direct copies.
}
const
{ TCStream seek origins }
soFromBeginning = 0;
soFromCurrent = 1;
soFromEnd = 2;
{ TCFileStream create mode }
fmCreate = $FFFF;
fmOpenRead = 0;
fmOpenWrite = 1;
fmOpenReadWrite = 2;
var
{ Used for Error reporting instead of exceptions }
CStreamError : longint;
type
{ Fake TComponent class, it isn't used any futher }
TCComponent = class(TObject)
end;
{ TCStream abstract class }
TCStream = class(TObject)
private
function GetPosition: Longint;
procedure SetPosition(Pos: Longint);
function GetSize: Longint;
protected
procedure SetSize(NewSize: Longint); virtual;
public
function Read(var Buffer; Count: Longint): Longint; virtual; abstract;
function Write(const Buffer; Count: Longint): Longint; virtual; abstract;
function Seek(Offset: Longint; Origin: Word): Longint; virtual; abstract;
procedure ReadBuffer(var Buffer; Count: Longint);
procedure WriteBuffer(const Buffer; Count: Longint);
function CopyFrom(Source: TCStream; Count: Longint): Longint;
function ReadComponent(Instance: TCComponent): TCComponent;
function ReadComponentRes(Instance: TCComponent): TCComponent;
procedure WriteComponent(Instance: TCComponent);
procedure WriteComponentRes(const ResName: string; Instance: TCComponent);
procedure WriteDescendent(Instance, Ancestor: TCComponent);
procedure WriteDescendentRes(const ResName: string; Instance, Ancestor: TCComponent);
procedure WriteResourceHeader(const ResName: string; {!!!:out} var FixupInfo: Integer);
procedure FixupResourceHeader(FixupInfo: Integer);
procedure ReadResHeader;
function ReadByte : Byte;
function ReadWord : Word;
function ReadDWord : Cardinal;
function ReadAnsiString : AnsiString;
procedure WriteByte(b : Byte);
procedure WriteWord(w : Word);
procedure WriteDWord(d : Cardinal);
Procedure WriteAnsiString (S : AnsiString);
property Position: Longint read GetPosition write SetPosition;
property Size: Longint read GetSize write SetSize;
end;
{ TCCustomFileStream class }
TCCustomFileStream = class(TCStream)
protected
FFileName : String;
public
constructor Create(const AFileName: string;{shortstring!} Mode: Word); virtual; abstract;
function EOF: boolean; virtual; abstract;
property FileName : String Read FFilename;
end;
{ TFileStream class }
TCFileStream = class(TCCustomFileStream)
Private
FHandle: File;
protected
procedure SetSize(NewSize: Longint); override;
public
constructor Create(const AFileName: string; Mode: Word); override;
destructor Destroy; override;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
function EOF: boolean; override;
end;
TCFileStreamClass = class of TCCustomFileStream;
var
CFileStreamClass: TCFileStreamClass = TCFileStream;
type
{ TCustomMemoryStream abstract class }
TCCustomMemoryStream = class(TCStream)
private
FMemory: Pointer;
FSize, FPosition: Longint;
protected
procedure SetPointer(Ptr: Pointer; ASize: Longint);
public
function Read(var Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
procedure SaveToStream(Stream: TCStream);
procedure SaveToFile(const FileName: string);
property Memory: Pointer read FMemory;
end;
{ TCMemoryStream }
TCMemoryStream = class(TCCustomMemoryStream)
private
FCapacity: Longint;
procedure SetCapacity(NewCapacity: Longint);
protected
function Realloc(var NewCapacity: Longint): Pointer; virtual;
property Capacity: Longint read FCapacity write SetCapacity;
public
destructor Destroy; override;
procedure Clear;
procedure LoadFromStream(Stream: TCStream);
procedure LoadFromFile(const FileName: string);
procedure SetSize(NewSize: Longint); override;
function Write(const Buffer; Count: Longint): Longint; override;
end;
implementation
Type
PByte = ^Byte;
{*****************************************************************************
TCStream
*****************************************************************************}
function TCStream.GetPosition: Longint;
begin
Result:=Seek(0,soFromCurrent);
end;
procedure TCStream.SetPosition(Pos: Longint);
begin
Seek(pos,soFromBeginning);
end;
function TCStream.GetSize: Longint;
var
p : longint;
begin
p:=GetPosition;
GetSize:=Seek(0,soFromEnd);
Seek(p,soFromBeginning);
end;
procedure TCStream.SetSize(NewSize: Longint);
begin
// We do nothing. Pipe streams don't support this
// As well as possible read-ony streams !!
end;
procedure TCStream.ReadBuffer(var Buffer; Count: Longint);
begin
CStreamError:=0;
if Read(Buffer,Count)<Count then
CStreamError:=102;
end;
procedure TCStream.WriteBuffer(const Buffer; Count: Longint);
begin
CStreamError:=0;
if Write(Buffer,Count)<Count then
CStreamError:=103;
end;
function TCStream.CopyFrom(Source: TCStream; Count: Longint): Longint;
var
i : longint;
buffer : array[0..1023] of byte;
begin
CStreamError:=0;
Result:=0;
while Count>0 do
begin
if (Count>sizeof(buffer)) then
i:=sizeof(Buffer)
else
i:=Count;
i:=Source.Read(buffer,i);
i:=Write(buffer,i);
dec(count,i);
inc(Result,i);
if i=0 then
exit;
end;
end;
function TCStream.ReadComponent(Instance: TCComponent): TCComponent;
begin
Result:=nil;
end;
function TCStream.ReadComponentRes(Instance: TCComponent): TCComponent;
begin
Result:=nil;
end;
procedure TCStream.WriteComponent(Instance: TCComponent);
begin
end;
procedure TCStream.WriteComponentRes(const ResName: string; Instance: TCComponent);
begin
end;
procedure TCStream.WriteDescendent(Instance, Ancestor: TCComponent);
begin
end;
procedure TCStream.WriteDescendentRes(const ResName: string; Instance, Ancestor: TCComponent);
begin
end;
procedure TCStream.WriteResourceHeader(const ResName: string; {!!!: out} var FixupInfo: Integer);
begin
end;
procedure TCStream.FixupResourceHeader(FixupInfo: Integer);
begin
end;
procedure TCStream.ReadResHeader;
begin
end;
function TCStream.ReadByte : Byte;
var
b : Byte;
begin
ReadBuffer(b,1);
ReadByte:=b;
end;
function TCStream.ReadWord : Word;
var
w : Word;
begin
ReadBuffer(w,2);
ReadWord:=w;
end;
function TCStream.ReadDWord : Cardinal;
var
d : Cardinal;
begin
ReadBuffer(d,4);
ReadDWord:=d;
end;
Function TCStream.ReadAnsiString : AnsiString;
Var
TheSize : Longint;
P : PByte ;
begin
ReadBuffer (TheSize,SizeOf(TheSize));
SetLength(Result,TheSize);
// Illegal typecast if no AnsiStrings defined.
if TheSize>0 then
begin
ReadBuffer (Pointer(Result)^,TheSize);
P:=PByte(PtrInt(Result)+TheSize);
p^:=0;
end;
end;
Procedure TCStream.WriteAnsiString (S : AnsiString);
Var L : Longint;
begin
L:=Length(S);
WriteBuffer (L,SizeOf(L));
WriteBuffer (Pointer(S)^,L);
end;
procedure TCStream.WriteByte(b : Byte);
begin
WriteBuffer(b,1);
end;
procedure TCStream.WriteWord(w : Word);
begin
WriteBuffer(w,2);
end;
procedure TCStream.WriteDWord(d : Cardinal);
begin
WriteBuffer(d,4);
end;
{****************************************************************************}
{* TCFileStream *}
{****************************************************************************}
constructor TCFileStream.Create(const AFileName: string; Mode: Word);
var
oldfilemode : byte;
begin
FFileName:=AFileName;
If Mode=fmcreate then
begin
system.assign(FHandle,AFileName);
{$push} {$I-}
system.rewrite(FHandle,1);
{$pop}
CStreamError:=IOResult;
end
else
begin
oldfilemode:=filemode;
filemode:=$40 or Mode;
system.assign(FHandle,AFileName);
{$push} {$I-}
system.reset(FHandle,1);
{$pop}
CStreamError:=IOResult;
filemode:=oldfilemode;
end;
end;
destructor TCFileStream.Destroy;
begin
{$push} {$I-}
System.Close(FHandle);
{$pop}
CStreamError:=IOResult;
end;
function TCFileStream.Read(var Buffer; Count: Longint): Longint;
begin
CStreamError:=0;
BlockRead(FHandle,Buffer,Count,Result);
If Result=-1 then Result:=0;
end;
function TCFileStream.Write(const Buffer; Count: Longint): Longint;
begin
CStreamError:=0;
BlockWrite (FHandle,(@Buffer)^,Count,Result);
If Result=-1 then Result:=0;
end;
Procedure TCFileStream.SetSize(NewSize: Longint);
begin
{$push} {$I-}
System.Seek(FHandle,NewSize);
System.Truncate(FHandle);
{$pop}
CStreamError:=IOResult;
end;
function TCFileStream.Seek(Offset: Longint; Origin: Word): Longint;
var
l : longint;
begin
{$push} {$I-}
case Origin of
soFromBeginning :
begin
System.Seek(FHandle,Offset);
l:=Offset;
end;
soFromCurrent :
begin
l:=System.FilePos(FHandle);
inc(l,Offset);
System.Seek(FHandle,l);
end;
soFromEnd :
begin
l:=System.FileSize(FHandle);
dec(l,Offset);
if l<0 then
l:=0;
System.Seek(FHandle,l);
end;
else
begin
CStreamError:=103;
l:=Offset;
end;
end;
{$pop}
CStreamError:=IOResult;
Result:=l;
end;
function TCFileStream.EOF: boolean;
begin
EOF:=system.eof(FHandle);
end;
{****************************************************************************}
{* TCustomMemoryStream *}
{****************************************************************************}
procedure TCCustomMemoryStream.SetPointer(Ptr: Pointer; ASize: Longint);
begin
FMemory:=Ptr;
FSize:=ASize;
end;
function TCCustomMemoryStream.Read(var Buffer; Count: Longint): Longint;
begin
Result:=0;
If (FSize>0) and (FPosition<Fsize) then
begin
Result:=FSize-FPosition;
If Result>Count then Result:=Count;
Move (Pointer(PtrUInt(FMemory)+PtrUInt(FPosition))^,Buffer,Result);
FPosition:=Fposition+Result;
end;
end;
function TCCustomMemoryStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
Case Origin of
soFromBeginning : FPosition:=Offset;
soFromEnd : FPosition:=FSize+Offset;
soFromCurrent : FpoSition:=FPosition+Offset;
end;
Result:=FPosition;
end;
procedure TCCustomMemoryStream.SaveToStream(Stream: TCStream);
begin
if FSize>0 then Stream.WriteBuffer (FMemory^,FSize);
end;
procedure TCCustomMemoryStream.SaveToFile(const FileName: string);
Var S : TCCustomFileStream;
begin
Try
S:=CFileStreamClass.Create (FileName,fmCreate);
SaveToStream(S);
finally
S.free;
end;
end;
{****************************************************************************}
{* TCMemoryStream *}
{****************************************************************************}
Const TMSGrow = 4096; { Use 4k blocks. }
procedure TCMemoryStream.SetCapacity(NewCapacity: Longint);
begin
SetPointer (Realloc(NewCapacity),Fsize);
FCapacity:=NewCapacity;
end;
function TCMemoryStream.Realloc(var NewCapacity: Longint): Pointer;
Var MoveSize : Longint;
begin
CStreamError:=0;
If NewCapacity>0 Then // round off to block size.
NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1);
// Only now check !
If NewCapacity=FCapacity then
Result:=FMemory
else
If NewCapacity=0 then
begin
FreeMem (FMemory,Fcapacity);
Result:=nil;
end
else
begin
GetMem (Result,NewCapacity);
If Result=Nil then
CStreamError:=204;
If FCapacity>0 then
begin
MoveSize:=FSize;
If MoveSize>NewCapacity then MoveSize:=NewCapacity;
Move (Fmemory^,Result^,MoveSize);
FreeMem (FMemory,FCapacity);
end;
end;
end;
destructor TCMemoryStream.Destroy;
begin
Clear;
Inherited Destroy;
end;
procedure TCMemoryStream.Clear;
begin
FSize:=0;
FPosition:=0;
SetCapacity (0);
end;
procedure TCMemoryStream.LoadFromStream(Stream: TCStream);
begin
Stream.Position:=0;
SetSize(Stream.Size);
If FSize>0 then Stream.ReadBuffer(FMemory^,FSize);
end;
procedure TCMemoryStream.LoadFromFile(const FileName: string);
Var S : TCCustomFileStream;
begin
Try
S:=CFileStreamClass.Create (FileName,fmOpenRead);
LoadFromStream(S);
finally
S.free;
end;
end;
procedure TCMemoryStream.SetSize(NewSize: Longint);
begin
SetCapacity (NewSize);
FSize:=NewSize;
IF FPosition>FSize then
FPosition:=FSize;
end;
function TCMemoryStream.Write(const Buffer; Count: Longint): Longint;
Var NewPos : Longint;
begin
If Count=0 then
begin
Result:=0;
exit;
end;
NewPos:=FPosition+Count;
If NewPos>Fsize then
begin
IF NewPos>FCapacity then
SetCapacity (NewPos);
FSize:=Newpos;
end;
System.Move (Buffer,Pointer(Ptruint(FMemory)+PtrUInt(FPosition))^,Count);
FPosition:=NewPos;
Result:=Count;
end;
end.
|