source: trunk/Packages/TemplateGenerics/Specialized/SpecializedStream.pas

Last change on this file was 324, checked in by chronos, 6 months ago
File size: 5.7 KB
Line 
1unit SpecializedStream;
2
3interface
4
5uses
6 Classes, SysUtils, SpecializedList, DateUtils;
7
8type
9 TSeekOrigin = (soBegin, soCurrent, soEnd);
10
11{$MACRO ON}
12
13// TStreamInteger<Integer, Integer>
14{$DEFINE TGStreamIndex := Integer}
15{$DEFINE TGStreamItem := Integer}
16{$DEFINE TGStreamList := TListStreamInteger}
17{$DEFINE TGStream := TStreamInteger}
18{$DEFINE INTERFACE}
19{$I 'GenericStream.inc'}
20
21// TStreamByte<Integer, Byte>
22{$DEFINE TGStreamIndex := Integer}
23{$DEFINE TGStreamItem := Byte}
24{$DEFINE TGStreamList := TListStreamByte}
25{$DEFINE TGStream := TBaseStreamByte}
26{$DEFINE INTERFACE}
27{$I 'GenericStream.inc'}
28
29// TStreamPointer<Integer, Pointer>
30{$DEFINE TGStreamIndex := Integer}
31{$DEFINE TGStreamItem := Pointer}
32{$DEFINE TGStreamList := TListStreamPointer}
33{$DEFINE TGStream := TStreamPointer}
34{$DEFINE INTERFACE}
35{$I 'GenericStream.inc'}
36
37 TStreamByte = class(TBaseStreamByte)
38 function ReadBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
39 function WriteBuffer(var Buffer; Count: Integer): Integer; virtual; abstract;
40 end;
41
42 { TMemoryStreamByte }
43
44 TMemoryStreamByte = class(TStreamByte)
45 private
46 FList: TListByte;
47 FOwnsList: Boolean;
48 FPosition: Integer;
49 public
50 procedure Assign(Source: TBaseStreamByte); override;
51 procedure Write(Item: Byte); override;
52 procedure WriteArray(Values: array of Byte); override;
53 procedure WriteList(List: TListByte);
54 function WriteBuffer(var Buffer; Count: Integer): Integer; override;
55 procedure WriteStream(Stream: TBaseStreamByte; Count: Integer); override;
56 function Read: Byte; override;
57 function ReadArray(Count: Integer): TItemArray; override;
58 function ReadList(List: TListByte; Count: Integer): Integer;
59 function ReadBuffer(var Buffer; Count: Integer): Integer; override;
60 function ReadStream(Stream: TBaseStreamByte; Count: Integer): Integer; override;
61 function Insert(Count: Integer): Integer; override;
62 function Remove(Count: Integer): Integer; override;
63 function Seek(Offset: Integer; Origin: TSeekOrigin = soCurrent): Integer; override;
64 constructor Create; override; overload;
65 constructor Create(AList: TListByte); overload;
66 destructor Destroy; override;
67 property OwnsList: Boolean read FOwnsList write FOwnsList;
68 property List: TListByte read FList;
69 end;
70
71
72implementation
73
74{ TMemoryStreamByte }
75
76procedure TMemoryStreamByte.Assign(Source: TBaseStreamByte);
77begin
78 inherited;
79 if Source is TMemoryStreamByte then begin
80 FList.Assign(TMemoryStreamByte(Source).FList);
81 FPosition := TMemoryStreamByte(Source).FPosition;
82 end;
83end;
84
85procedure TMemoryStreamByte.Write(Item: Byte);
86begin
87 if FList.Count < (FPosition + 1) then
88 FList.Count := FPosition + 1;
89 FList[FPosition] := Item;
90 Inc(FPosition);
91end;
92
93procedure TMemoryStreamByte.WriteArray(Values: array of Byte);
94begin
95 if FList.Count < (FPosition + Length(Values)) then
96 FList.Count := FPosition + Length(Values);
97 FList.ReplaceArray(FPosition, Values);
98 Inc(FPosition, Length(Values));
99end;
100
101procedure TMemoryStreamByte.WriteList(List: TListByte);
102begin
103 FList.ReplaceList(FPosition, List);
104end;
105
106procedure TMemoryStreamByte.WriteStream(Stream: TBaseStreamByte; Count: Integer);
107begin
108
109end;
110
111function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
112begin
113 Result := 0;
114end;
115
116function TMemoryStreamByte.Read: Byte;
117begin
118 Result := FList[FPosition];
119 Inc(FPosition);
120end;
121
122function TMemoryStreamByte.ReadArray(Count: Integer): TItemArray;
123begin
124 Result := FList.GetArray(FPosition, Count);
125end;
126
127function TMemoryStreamByte.ReadList(List: TListByte; Count: Integer): Integer;
128begin
129 if (FPosition + Count) > FList.Count then
130 Count := FList.Count - FPosition;
131 FList.GetList(List, FPosition, Count);
132 Result := Count;
133end;
134
135function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
136begin
137 Result := 0;
138end;
139
140function TMemoryStreamByte.ReadStream(Stream: TBaseStreamByte; Count: Integer
141 ): Integer;
142begin
143 Result := 0;
144end;
145
146function TMemoryStreamByte.Insert(Count: Integer): Integer;
147begin
148 FList.InsertCount(FPosition, Count);
149 Result := Count;
150end;
151
152function TMemoryStreamByte.Remove(Count: Integer): Integer;
153begin
154 Result := FList.Count - FPosition;
155 if Count < Result then Result := Count;
156 FList.DeleteItems(FPosition, Count);
157end;
158
159function TMemoryStreamByte.Seek(Offset: Integer; Origin: TSeekOrigin): Integer;
160begin
161 case Origin of
162 soBegin: FPosition := Offset;
163 soCurrent: FPosition := FPosition + Offset;
164 soEnd: FPosition := FList.Count + Offset;
165 end;
166 if FPosition > FList.Count then FPosition := FList.Count;
167 if FPosition < 0 then FPosition := 0;
168 Result := FPosition;
169end;
170
171constructor TMemoryStreamByte.Create;
172begin
173 inherited;
174 FList := TListByte.Create;
175 OwnsList := True;
176end;
177
178constructor TMemoryStreamByte.Create(AList: TListByte);
179begin
180 inherited Create;
181 FList := AList;
182 OwnsList := False;
183end;
184
185destructor TMemoryStreamByte.Destroy;
186begin
187 if OwnsList then FList.Free;
188 inherited Destroy;
189end;
190
191{$DEFINE IMPLEMENTATION_USES}
192{$I 'GenericStream.inc'}
193
194// TStreamInteger<Integer, Integer>
195{$DEFINE TGStreamIndex := Integer}
196{$DEFINE TGStreamItem := Integer}
197{$DEFINE TGStreamList := TListStreamInteger}
198{$DEFINE TGStream := TStreamInteger}
199{$DEFINE IMPLEMENTATION}
200{$I 'GenericStream.inc'}
201
202// TStreamByte<Integer, Byte>
203{$DEFINE TGStreamIndex := Integer}
204{$DEFINE TGStreamItem := Byte}
205{$DEFINE TGStreamList := TListStreamByte}
206{$DEFINE TGStream := TBaseStreamByte}
207{$DEFINE IMPLEMENTATION}
208{$I 'GenericStream.inc'}
209
210// TStreamPointer<Integer, Pointer>
211{$DEFINE TGStreamIndex := Integer}
212{$DEFINE TGStreamItem := Pointer}
213{$DEFINE TGStreamList := TListStreamPointer}
214{$DEFINE TGStream := TStreamPointer}
215{$DEFINE IMPLEMENTATION}
216{$I 'GenericStream.inc'}
217
218
219
220end.
Note: See TracBrowser for help on using the repository browser.