Changeset 16


Ignore:
Timestamp:
Mar 30, 2010, 11:23:20 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Přidána podpora pro endianicitu číselných typů.
  • Přidáno: Metody pro zápis a čtení typů Double a Single.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MemoryStreamEx/UMemoryStreamEx.pas

    r2 r16  
    11unit UMemoryStreamEx;
     2
     3{$mode delphi}{$H+}
    24
    35interface
     
    79
    810type
     11  TEndianness = (enBig, enLittle);
     12 
     13  { TMemoryStreamEx }
     14
    915  TMemoryStreamEx = class(TMemoryStream)
     16  private
     17    FEndianness: TEndianness;
     18    SwapData: Boolean;
     19    procedure SetEndianness(const AValue: TEndianness);
     20  public
    1021    procedure WriteByte(Data: Byte);
    1122    procedure WriteWord(Data: Word);
     
    1324    procedure WriteInt64(Data: Int64);
    1425    procedure WriteShortString(Data: ShortString);
     26    procedure WriteAnsiString(Data: string);
    1527    procedure WriteUnixTime(Data: TDateTime);
    16     procedure WriteStream(Stream: TStream);
     28    procedure WriteStream(Stream: TStream; Count: Integer);
     29    procedure WriteDouble(Value: Double);
     30    procedure WriteSingle(Value: Single);
    1731    function ReadByte: Byte;
    1832    function ReadWord: Word;
     
    2034    function ReadInt64: Int64;
    2135    function ReadShortString: string;
     36    function ReadAnsiString: string;
    2237    function ReadUnixTime: TDateTime;
     38    function ReadDouble: Double;
     39    function ReadSingle: Single;
    2340    procedure ReadStream(var Stream: TStream; Count: Integer);
     41    constructor Create;
     42    property Endianness: TEndianness read FEndianness write SetEndianness;
    2443  end;
    2544
     
    2847{ TMemoryStreamEx }
    2948
     49procedure TMemoryStreamEx.WriteAnsiString(Data: string);
     50var
     51  StringLength: Longint;
     52begin
     53  StringLength := Length(Data);
     54  Write(StringLength, SizeOf(StringLength));
     55  Write(Data[1], StringLength);
     56end;
     57
     58function TMemoryStreamEx.ReadAnsiString: string;
     59var
     60  StringLength: Longint;
     61begin
     62  Read(StringLength, SizeOf(StringLength));
     63  SetLength(Result, StringLength);
     64  if StringLength > 0 then begin
     65    Read(Result[1], StringLength);
     66  end;
     67end;
     68
    3069function TMemoryStreamEx.ReadByte: Byte;
    3170begin
    32   Read(Result, 1);
     71  Read(Result, SizeOf(Byte));
    3372end;
    3473
    3574function TMemoryStreamEx.ReadCardinal: Cardinal;
    3675begin
    37   Read(Result, 4);
     76  Read(Result, SizeOf(Cardinal));
     77  if SwapData then Result := Swap(Result);
    3878end;
    3979
    4080function TMemoryStreamEx.ReadInt64: Int64;
    4181begin
    42   Read(Result, 8);
     82  Read(Result, SizeOf(Int64));
     83  if SwapData then Result := Swap(Result);
    4384end;
    4485
     
    65106end;
    66107
     108constructor TMemoryStreamEx.Create;
     109begin
     110  Endianness := enLittle;
     111end;
     112
    67113function TMemoryStreamEx.ReadUnixTime: TDateTime;
    68114begin
     
    70116end;
    71117
     118function TMemoryStreamEx.ReadDouble: Double;
     119begin
     120  Read(Result, SizeOf(Double));
     121end;
     122
     123function TMemoryStreamEx.ReadSingle: Single;
     124begin
     125  Read(Result, SizeOf(Single));
     126end;
     127
    72128function TMemoryStreamEx.ReadWord: Word;
    73129begin
    74   Read(Result, 2);
     130  Read(Result, SizeOf(Word));
     131  if SwapData then Result := Swap(Result);
     132end;
     133
     134procedure TMemoryStreamEx.SetEndianness(const AValue: TEndianness);
     135begin
     136  FEndianness := AValue;
     137  {$ifdef FPC_LITTLE_ENDIAN}
     138  SwapData := FEndianness = enBig;
     139  {$elseifdef FPC_BIG_ENDIAN}
     140  SwapData := FEndianness = enLittle;
     141  {$endif}
    75142end;
    76143
    77144procedure TMemoryStreamEx.WriteByte(Data: Byte);
    78145begin
    79   Write(Data, 1);
     146  Write(Data, SizeOf(Byte));
    80147end;
    81148
    82149procedure TMemoryStreamEx.WriteCardinal(Data: Cardinal);
    83150begin
    84   Write(Data, 4);
     151  if SwapData then Data := Swap(Data);
     152  Write(Data, SizeOf(Cardinal));
    85153end;
    86154
    87155procedure TMemoryStreamEx.WriteInt64(Data: Int64);
    88156begin
    89   Write(Data, 8);
     157  if SwapData then Data := Swap(Data);
     158  Write(Data, SizeOf(Int64));
    90159end;
    91160
     
    93162begin
    94163  WriteByte(Length(Data));
    95   Write(Data[1], Length(Data))
    96 end;
    97 
    98 procedure TMemoryStreamEx.WriteStream(Stream: TStream);
     164  Write(Data[1], Length(Data));
     165end;
     166
     167procedure TMemoryStreamEx.WriteStream(Stream: TStream; Count: Integer);
    99168var
    100169  Buffer: array of Byte;
    101170begin
     171  if Count > Stream.Size then Count := Stream.Size; // Limit max. stream size
    102172  Stream.Position := 0;
    103   SetLength(Buffer, Stream.Size);
    104   Stream.Read(Buffer[0], Stream.Size);
    105   Write(Buffer[0], Stream.Size);
     173  if Count > 0 then begin
     174    SetLength(Buffer, Count);
     175    Stream.Read(Buffer[0], Count);
     176    Write(Buffer[0], Count);
     177  end;
     178end;
     179
     180procedure TMemoryStreamEx.WriteDouble(Value: Double);
     181begin
     182  Write(Value, SizeOf(Double));
     183end;
     184
     185procedure TMemoryStreamEx.WriteSingle(Value: Single);
     186begin
     187  Write(Value, SizeOf(Single));
    106188end;
    107189
     
    111193begin
    112194  DataUnix := DateTimeToUnix(Data);
    113   Write(DataUnix, 4);
     195  WriteCardinal(DataUnix);
    114196end;
    115197
    116198procedure TMemoryStreamEx.WriteWord(Data: Word);
    117199begin
    118   Write(Data, 2);
     200  if SwapData then Data := Swap(Data);
     201  Write(Data, SizeOf(Word));
    119202end;
    120203
Note: See TracChangeset for help on using the changeset viewer.