| 1 | unit SpecializedStream;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, SpecializedList, DateUtils;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 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 |
|
|---|
| 72 | implementation
|
|---|
| 73 |
|
|---|
| 74 | { TMemoryStreamByte }
|
|---|
| 75 |
|
|---|
| 76 | procedure TMemoryStreamByte.Assign(Source: TBaseStreamByte);
|
|---|
| 77 | begin
|
|---|
| 78 | inherited;
|
|---|
| 79 | if Source is TMemoryStreamByte then begin
|
|---|
| 80 | FList.Assign(TMemoryStreamByte(Source).FList);
|
|---|
| 81 | FPosition := TMemoryStreamByte(Source).FPosition;
|
|---|
| 82 | end;
|
|---|
| 83 | end;
|
|---|
| 84 |
|
|---|
| 85 | procedure TMemoryStreamByte.Write(Item: Byte);
|
|---|
| 86 | begin
|
|---|
| 87 | if FList.Count < (FPosition + 1) then
|
|---|
| 88 | FList.Count := FPosition + 1;
|
|---|
| 89 | FList[FPosition] := Item;
|
|---|
| 90 | Inc(FPosition);
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | procedure TMemoryStreamByte.WriteArray(Values: array of Byte);
|
|---|
| 94 | begin
|
|---|
| 95 | if FList.Count < (FPosition + Length(Values)) then
|
|---|
| 96 | FList.Count := FPosition + Length(Values);
|
|---|
| 97 | FList.ReplaceArray(FPosition, Values);
|
|---|
| 98 | Inc(FPosition, Length(Values));
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | procedure TMemoryStreamByte.WriteList(List: TListByte);
|
|---|
| 102 | begin
|
|---|
| 103 | FList.ReplaceList(FPosition, List);
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | procedure TMemoryStreamByte.WriteStream(Stream: TBaseStreamByte; Count: Integer);
|
|---|
| 107 | begin
|
|---|
| 108 |
|
|---|
| 109 | end;
|
|---|
| 110 |
|
|---|
| 111 | function TMemoryStreamByte.WriteBuffer(var Buffer; Count: Integer): Integer;
|
|---|
| 112 | begin
|
|---|
| 113 | Result := 0;
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | function TMemoryStreamByte.Read: Byte;
|
|---|
| 117 | begin
|
|---|
| 118 | Result := FList[FPosition];
|
|---|
| 119 | Inc(FPosition);
|
|---|
| 120 | end;
|
|---|
| 121 |
|
|---|
| 122 | function TMemoryStreamByte.ReadArray(Count: Integer): TItemArray;
|
|---|
| 123 | begin
|
|---|
| 124 | Result := FList.GetArray(FPosition, Count);
|
|---|
| 125 | end;
|
|---|
| 126 |
|
|---|
| 127 | function TMemoryStreamByte.ReadList(List: TListByte; Count: Integer): Integer;
|
|---|
| 128 | begin
|
|---|
| 129 | if (FPosition + Count) > FList.Count then
|
|---|
| 130 | Count := FList.Count - FPosition;
|
|---|
| 131 | FList.GetList(List, FPosition, Count);
|
|---|
| 132 | Result := Count;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | function TMemoryStreamByte.ReadBuffer(var Buffer; Count: Integer): Integer;
|
|---|
| 136 | begin
|
|---|
| 137 | Result := 0;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 | function TMemoryStreamByte.ReadStream(Stream: TBaseStreamByte; Count: Integer
|
|---|
| 141 | ): Integer;
|
|---|
| 142 | begin
|
|---|
| 143 | Result := 0;
|
|---|
| 144 | end;
|
|---|
| 145 |
|
|---|
| 146 | function TMemoryStreamByte.Insert(Count: Integer): Integer;
|
|---|
| 147 | begin
|
|---|
| 148 | FList.InsertCount(FPosition, Count);
|
|---|
| 149 | Result := Count;
|
|---|
| 150 | end;
|
|---|
| 151 |
|
|---|
| 152 | function TMemoryStreamByte.Remove(Count: Integer): Integer;
|
|---|
| 153 | begin
|
|---|
| 154 | Result := FList.Count - FPosition;
|
|---|
| 155 | if Count < Result then Result := Count;
|
|---|
| 156 | FList.DeleteItems(FPosition, Count);
|
|---|
| 157 | end;
|
|---|
| 158 |
|
|---|
| 159 | function TMemoryStreamByte.Seek(Offset: Integer; Origin: TSeekOrigin): Integer;
|
|---|
| 160 | begin
|
|---|
| 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;
|
|---|
| 169 | end;
|
|---|
| 170 |
|
|---|
| 171 | constructor TMemoryStreamByte.Create;
|
|---|
| 172 | begin
|
|---|
| 173 | inherited;
|
|---|
| 174 | FList := TListByte.Create;
|
|---|
| 175 | OwnsList := True;
|
|---|
| 176 | end;
|
|---|
| 177 |
|
|---|
| 178 | constructor TMemoryStreamByte.Create(AList: TListByte);
|
|---|
| 179 | begin
|
|---|
| 180 | inherited Create;
|
|---|
| 181 | FList := AList;
|
|---|
| 182 | OwnsList := False;
|
|---|
| 183 | end;
|
|---|
| 184 |
|
|---|
| 185 | destructor TMemoryStreamByte.Destroy;
|
|---|
| 186 | begin
|
|---|
| 187 | if OwnsList then FList.Free;
|
|---|
| 188 | inherited Destroy;
|
|---|
| 189 | end;
|
|---|
| 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 |
|
|---|
| 220 | end.
|
|---|