Ignore:
Timestamp:
Jul 20, 2018, 10:25:06 AM (6 years ago)
Author:
chronos
Message:
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        66compiled
        77heaptrclog.trc
         8LazFuck.dbg
  • trunk/Packages/TemplateGenerics/Additional/UBinarySerializer.pas

    r72 r93  
    11unit UBinarySerializer;
    22
    3 {$mode objfpc}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
     
    1515  TBinarySerializer = class
    1616  private
    17     FStream: TStreamByte;
     17    FGrow: Boolean;
     18    FList: TListByte;
    1819    FEndianness: TEndianness;
    1920    SwapData: Boolean;
     21    procedure SetList(const AValue: TListByte);
    2022    procedure SetEndianness(const AValue: TEndianness);
    2123    procedure ReverseByteOrder(var Buffer; Count: Integer);
    2224  public
     25    Position: Integer;
     26    OwnsList: Boolean;
     27    procedure Write(var Buffer; Count: Integer); //inline;
     28    procedure Read(var Buffer; Count: Integer); //inline;
     29
    2330    procedure Assign(Source: TBinarySerializer);
    2431    procedure WriteByte(Data: Byte);
     
    4855    procedure ReadStream(AStream: TStream; Count: Integer);
    4956    procedure ReadStreamPart(AStream: TStream; Count: Integer);
     57    procedure ReadList(List: TListByte; StartIndex, Count: Integer);
    5058    constructor Create; overload;
    51     constructor Create(AStream: TStreamByte); overload;
    5259    procedure Clear;
    5360    destructor Destroy; override;
    5461    property Endianness: TEndianness read FEndianness write SetEndianness;
    55     property Stream: TStreamByte read FStream write FStream;
     62    property List: TListByte read FList write SetList;
     63    property Grow: Boolean read FGrow write FGrow;
    5664  end;
    5765
     
    6573  StringLength: Longint;
    6674begin
    67   StringLength  := 0;
    68   FStream.ReadBuffer(StringLength, SizeOf(StringLength));
     75  StringLength := 0;
     76  Read(StringLength, SizeOf(StringLength));
    6977  Result := ReadString(StringLength);
    7078end;
     
    7684  OldPosition: Integer;
    7785begin
    78   OldPosition := FStream.Position;
     86  OldPosition := Position;
    7987  Result := '';
    8088  I := 1;
    8189  repeat
    82     if FStream.Position >= FStream.Size then Break;
     90    if Position >= FList.Count then Break;
    8391    Data := Chr(ReadByte);
    8492    if Data <> Terminator[I] then begin
     
    8997  if not (I > Length(Terminator)) then begin
    9098    Result := '';
    91     FStream.Position := OldPosition;
     99    Position := OldPosition;
    92100  end;
    93101end;
     
    96104begin
    97105  Result := 0;
    98   FStream.ReadBuffer(Result, SizeOf(Byte));
     106  Read(Result, SizeOf(Byte));
    99107end;
    100108
     
    102110begin
    103111  Result := 0;
    104   FStream.ReadBuffer(Result, SizeOf(Cardinal));
     112  Read(Result, SizeOf(Cardinal));
    105113  if SwapData then Result := SwapEndian(Result);
    106114end;
     
    109117begin
    110118  Result := 0;
    111   FStream.ReadBuffer(Result, SizeOf(Int64));
     119  Read(Result, SizeOf(Int64));
    112120  if SwapData then Result := SwapEndian(Result);
    113121end;
     
    117125  if Length > 0 then begin
    118126    SetLength(Result, Length);
    119     FStream.ReadBuffer(Result[1], Length);
     127    Read(Result[1], Length);
    120128  end else Result := '';
    121129end;
     
    126134begin
    127135  Count := 0;
    128   FStream.ReadBuffer(Count, 1);
     136  Read(Count, 1);
    129137  Result := ReadString(Count);
    130138end;
     
    136144  if Count > 0 then begin
    137145    SetLength(Buffer, Count);
    138     FStream.ReadBuffer(Buffer[0], Count);
     146    Read(Buffer[0], Count);
    139147    AStream.Size := Count;
    140148    AStream.Position := 0;
     
    149157  if Count > 0 then begin
    150158    SetLength(Buffer, Count);
    151     FStream.ReadBuffer(Buffer[0], Count);
     159    Read(Buffer[0], Count);
    152160    if AStream.Size < (AStream.Position + Count) then
    153161      AStream.Size := AStream.Position + Count;
    154     AStream.Write(Buffer[0], Count);
     162    Write(Buffer[0], Count);
     163  end;
     164end;
     165
     166procedure TBinarySerializer.ReadList(List: TListByte; StartIndex, Count: Integer
     167  );
     168var
     169  Buffer: array of Byte;
     170begin
     171  if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
     172  if Count > 0 then begin
     173    SetLength(Buffer, Count);
     174    Read(Pointer(Buffer)^, Count);
     175    List.ReplaceBuffer(StartIndex, Pointer(Buffer)^, Count);
    155176  end;
    156177end;
     
    164185    SetLength(Buffer, Count);
    165186    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    166     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     187    Write(Pointer(Buffer)^, Count);
    167188  end;
    168189end;
     
    172193  Buffer: array of Byte;
    173194begin
    174   if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
    175   if Count > 0 then begin
    176     SetLength(Buffer, Count);
    177     List.ReadBuffer(Pointer(Buffer)^, Count);
    178     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     195//  if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
     196  if Count > 0 then begin
     197    SetLength(Buffer, Count);
     198    List.GetBuffer(StartIndex, PByte(Buffer)^, Count);
     199    Write(Pointer(Buffer)^, Count);
    179200  end;
    180201end;
     
    184205  inherited;
    185206  Endianness := enLittle;
    186   FStream := nil;
    187 end;
    188 
    189 constructor TBinarySerializer.Create(AStream: TStreamByte);
    190 begin
    191   inherited Create;
    192   Endianness := enLittle;
    193   FStream := AStream;
     207  FList := nil;
     208  FGrow := True;
    194209end;
    195210
    196211procedure TBinarySerializer.Clear;
    197212begin
    198   Stream.Size := 0;
     213  FList.Count := 0;
     214  Position := 0;
    199215end;
    200216
    201217destructor TBinarySerializer.Destroy;
    202218begin
     219  if OwnsList then FList.Free;
    203220  inherited Destroy;
    204221end;
     
    212229begin
    213230  Result := 0;
    214   FStream.ReadBuffer(Result, SizeOf(Double));
     231  Read(Result, SizeOf(Double));
    215232end;
    216233
     
    218235begin
    219236  Result := 0;
    220   FStream.ReadBuffer(Result, SizeOf(Single));
     237  Read(Result, SizeOf(Single));
    221238end;
    222239
     
    224241begin
    225242  Result := 0;
    226   FStream.ReadBuffer(Result, SizeOf(Word));
     243  Read(Result, SizeOf(Word));
    227244  if SwapData then Result := SwapEndian(Result);
     245end;
     246
     247procedure TBinarySerializer.SetList(const AValue: TListByte);
     248begin
     249  if OwnsList then FList.Free;
     250  FList := AValue;
    228251end;
    229252
     
    254277end;
    255278
     279procedure TBinarySerializer.Write(var Buffer; Count: Integer);
     280var
     281  NewCount: Integer;
     282begin
     283  if FGrow then begin
     284    NewCount := Position + Count;
     285    if FList.Count < NewCount then
     286      FList.Count := NewCount;
     287  end;
     288  FList.ReplaceBuffer(Position, Buffer, Count);
     289  Inc(Position, Count);
     290end;
     291
     292procedure TBinarySerializer.Read(var Buffer; Count: Integer);
     293begin
     294  FList.GetBuffer(Position, Buffer, Count);
     295  Inc(Position, Count);
     296end;
     297
    256298procedure TBinarySerializer.Assign(Source: TBinarySerializer);
    257299begin
    258   FStream := Source.FStream;
     300  FList := Source.FList;
    259301end;
    260302
     
    270312procedure TBinarySerializer.WriteByte(Data: Byte);
    271313begin
    272   Data := 0;
    273   FStream.WriteBuffer(Data, SizeOf(Byte));
     314  Write(Data, SizeOf(Byte));
    274315end;
    275316
     
    307348    SetLength(Buffer, Count);
    308349    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    309     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     350    Write(Pointer(Buffer)^, Count);
    310351  end;
    311352end;
Note: See TracChangeset for help on using the changeset viewer.