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

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