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