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