-
Notifications
You must be signed in to change notification settings - Fork 34
/
DelphiStream.pas
210 lines (174 loc) · 5.46 KB
/
DelphiStream.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
///////////////////////////////////////////////////////////////////////////////////
//
// DelphiStreams.pas - Delphi stream interface
// -------------------------------------------
// Version: 1999-09-27
// Maintain: Michael Vinther | mv@logicnet·dk
//
//
// Contains:
// TDelphiStream Delphi stream to read from/write to MeeSoft stream
// TSeekableDelphiStream
//
// TDelphiFilterStream MeeSoft seekable stream to read from/write to Delphi stream
unit DelphiStream;
interface
uses Classes, Streams, Monitor;
resourcestring
rsUnknownSeekOrigin = 'Unknown seek origin';
rsSeekingNotSupported = 'Seeking not supported';
type
TDelphiStream = class(TStream)
protected
fNext : TBaseStream;
public
constructor Create(InOutStream: TBaseStream);
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
property Next: TBaseStream read fNext;
// Free this class, Next and all streams below
procedure FreeAll;
end;
TSeekableDelphiStream = class(TDelphiStream)
protected
procedure SetSize(NewSize: Longint); override;
public
constructor Create(InOutStream: TSeekableStream);
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
TDelphiFilterStream = class(TSeekableStream)
protected
Next : TStream;
function GetPos: Integer; override;
function GetSize: Integer; override;
public
// Construct instance of stream
constructor Create(InOutStream: TStream);
// Write a block of data to the stream
function Write(var Buf; Count: integer): integer; override;
// Read a block of data from the stream
function Read(var Buf; Count: integer): integer; override;
// How many bytes are available for reading
function Available: integer; override;
// Seek to a location in the stream
procedure Seek(loc: integer); override;
// Truncate stream at current position
procedure Truncate; override;
// Free this class and Next. Note that FreeAll in a TFilterstream does not
// call this method
procedure FreeAll;
end;
implementation
//==============================================================================================================================
// TDelphiStream
//==============================================================================================================================
constructor TDelphiStream.Create(InOutStream: TBaseStream);
begin
inherited Create;
fNext:=InOutStream;
Next.NoDataExcept:=False;
end;
function TDelphiStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
raise EStreamError.Create(rsSeekingNotSupported);
end;
function TDelphiStream.Read(var Buffer; Count: Longint): Longint;
begin
Result:=Next.Read(Buffer,Count);
end;
function TDelphiStream.Write(const Buffer; Count: Longint): Longint;
begin
Result:=Next.Write(Pointer(@Buffer)^,Count);
end;
procedure TDelphiStream.FreeAll;
var N: TBaseStream;
begin
if Self=nil then Exit;
N:=Next;
Destroy;
if N is TFilterStream then with N as TFilterStream do FreeAll
else N.Free;
end;
//===================================================================================
// TSeekableDelphiStream
//===================================================================================
constructor TSeekableDelphiStream.Create(InOutStream: TSeekableStream);
begin
inherited Create(InOutStream);
end;
function TSeekableDelphiStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
case Origin of
soFromBeginning : Result:=Offset;
soFromEnd : Result:=TSeekableStream(Next).Size+Offset;
soFromCurrent : Result:=TSeekableStream(Next).Position+Offset;
else raise EStreamError.Create(rsUnknownSeekOrigin);
end;
TSeekableStream(Next).Position:=Result;
end;
procedure TSeekableDelphiStream.SetSize(NewSize: Longint);
var
OrgPos : Integer;
begin
if NewSize<TSeekableStream(Next).Size then
begin
OrgPos:=TSeekableStream(Next).Position;
TSeekableStream(Next).Position:=NewSize;
TSeekableStream(Next).Truncate;
if OrgPos<NewSize then TSeekableStream(Next).Position:=OrgPos;
end
else
begin
OrgPos:=TSeekableStream(Next).Position;
TSeekableStream(Next).Position:=NewSize;
TSeekableStream(Next).Position:=OrgPos;
end;
end;
//===================================================================================
// TDelphiFilterStream
//===================================================================================
constructor TDelphiFilterStream.Create(InOutStream: TStream);
begin
inherited Create;
Next:=InOutStream;
fCanRead:=True;
fCanWrite:=True;
end;
function TDelphiFilterStream.Read(var Buf; Count: Integer): Integer;
begin
Result:=Next.Read(Buf,Count);
end;
function TDelphiFilterStream.Write(var Buf; Count: Integer): Integer;
begin
Result:=Next.Write(Buf,Count);
end;
function TDelphiFilterStream.Available: Integer;
begin
Result:=Next.Size-Next.Position;
end;
procedure TDelphiFilterStream.Seek(loc: integer);
begin
Next.Seek(Loc,soFromBeginning);
end;
procedure TDelphiFilterStream.Truncate;
begin
Next.Size:=Position;
end;
function TDelphiFilterStream.GetPos: Integer;
begin
Result:=Next.Position;
end;
function TDelphiFilterStream.GetSize: Integer;
begin
Result:=Next.Size;
end;
procedure TDelphiFilterStream.FreeAll;
begin
if Self<>nil then
begin
Next.Free;
Destroy;
end;
end;
end.