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

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 7.2 KB
Line 
1unit SpecializedStream;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, SpecializedList, DateUtils;
9
10type
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
86implementation
87
88{ TMemoryStreamByte }
89
90procedure TMemoryStreamByte.Assign(Source: TBaseStreamByte);
91begin
92 inherited;
93 if Source is TMemoryStreamByte then begin
94 FList.Assign(TMemoryStreamByte(Source).FList);
95 FPosition := TMemoryStreamByte(Source).FPosition;
96 end;
97end;
98
99procedure TMemoryStreamByte.Write(Item: Byte);
100begin
101 if FList.Count < (FPosition + 1) then
102 FList.Count := FPosition + 1;
103 FList[FPosition] := Item;
104 Inc(FPosition);
105end;
106
107procedure TMemoryStreamByte.WriteArray(Values: array of Byte);
108begin
109 if FList.Count < (FPosition + Length(Values)) then
110 FList.Count := FPosition + Length(Values);
111 FList.ReplaceArray(FPosition, Values);
112 Inc(FPosition, Length(Values));
113end;
114
115procedure TMemoryStreamByte.WriteList(List: TListByte);
116begin
117 FList.ReplaceList(FPosition, List);
118end;
119
120procedure TMemoryStreamByte.WriteStream(Stream: TBaseStreamByte; Count: Integer);
121begin
122
123end;
124
125function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
126begin
127
128end;
129
130function TMemoryStreamByte.Read: Byte;
131begin
132 Result := FList[FPosition];
133 Inc(FPosition);
134end;
135
136function TMemoryStreamByte.ReadArray(Count: Integer): TStreamByteItemArray;
137begin
138 Result := FList.GetArray(FPosition, Count);
139end;
140
141function TMemoryStreamByte.ReadList(List: TListByte; Count: Integer): Integer;
142begin
143 if (FPosition + Count) > FList.Count then
144 Count := FList.Count - FPosition;
145 FList.GetList(List, FPosition, Count);
146 Result := Count;
147end;
148
149function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
150begin
151
152end;
153
154function TMemoryStreamByte.ReadStream(Stream: TBaseStreamByte; Count: Integer
155 ): Integer;
156begin
157
158end;
159
160function TMemoryStreamByte.Insert(Count: Integer): Integer;
161begin
162 FList.InsertCount(FPosition, Count);
163 Result := Count;
164end;
165
166function TMemoryStreamByte.Remove(Count: Integer): Integer;
167begin
168 Result := FList.Count - FPosition;
169 if Count < Result then Result := Count;
170 FList.DeleteItems(FPosition, Count);
171end;
172
173function TMemoryStreamByte.Seek(Offset: Integer; Origin: TSeekOrigin): Integer;
174begin
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;
183end;
184
185constructor TMemoryStreamByte.Create;
186begin
187 inherited;
188 FList := TListByte.Create;
189 OwnsList := True;
190end;
191
192constructor TMemoryStreamByte.Create(AList: TListByte);
193begin
194 inherited Create;
195 FList := AList;
196 OwnsList := False;
197end;
198
199destructor TMemoryStreamByte.Destroy;
200begin
201 if OwnsList then FList.Free;
202 inherited Destroy;
203end;
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
246end.
Note: See TracBrowser for help on using the repository browser.