Changeset 16
- Timestamp:
- Mar 30, 2010, 11:23:20 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MemoryStreamEx/UMemoryStreamEx.pas
r2 r16 1 1 unit UMemoryStreamEx; 2 3 {$mode delphi}{$H+} 2 4 3 5 interface … … 7 9 8 10 type 11 TEndianness = (enBig, enLittle); 12 13 { TMemoryStreamEx } 14 9 15 TMemoryStreamEx = class(TMemoryStream) 16 private 17 FEndianness: TEndianness; 18 SwapData: Boolean; 19 procedure SetEndianness(const AValue: TEndianness); 20 public 10 21 procedure WriteByte(Data: Byte); 11 22 procedure WriteWord(Data: Word); … … 13 24 procedure WriteInt64(Data: Int64); 14 25 procedure WriteShortString(Data: ShortString); 26 procedure WriteAnsiString(Data: string); 15 27 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); 17 31 function ReadByte: Byte; 18 32 function ReadWord: Word; … … 20 34 function ReadInt64: Int64; 21 35 function ReadShortString: string; 36 function ReadAnsiString: string; 22 37 function ReadUnixTime: TDateTime; 38 function ReadDouble: Double; 39 function ReadSingle: Single; 23 40 procedure ReadStream(var Stream: TStream; Count: Integer); 41 constructor Create; 42 property Endianness: TEndianness read FEndianness write SetEndianness; 24 43 end; 25 44 … … 28 47 { TMemoryStreamEx } 29 48 49 procedure TMemoryStreamEx.WriteAnsiString(Data: string); 50 var 51 StringLength: Longint; 52 begin 53 StringLength := Length(Data); 54 Write(StringLength, SizeOf(StringLength)); 55 Write(Data[1], StringLength); 56 end; 57 58 function TMemoryStreamEx.ReadAnsiString: string; 59 var 60 StringLength: Longint; 61 begin 62 Read(StringLength, SizeOf(StringLength)); 63 SetLength(Result, StringLength); 64 if StringLength > 0 then begin 65 Read(Result[1], StringLength); 66 end; 67 end; 68 30 69 function TMemoryStreamEx.ReadByte: Byte; 31 70 begin 32 Read(Result, 1);71 Read(Result, SizeOf(Byte)); 33 72 end; 34 73 35 74 function TMemoryStreamEx.ReadCardinal: Cardinal; 36 75 begin 37 Read(Result, 4); 76 Read(Result, SizeOf(Cardinal)); 77 if SwapData then Result := Swap(Result); 38 78 end; 39 79 40 80 function TMemoryStreamEx.ReadInt64: Int64; 41 81 begin 42 Read(Result, 8); 82 Read(Result, SizeOf(Int64)); 83 if SwapData then Result := Swap(Result); 43 84 end; 44 85 … … 65 106 end; 66 107 108 constructor TMemoryStreamEx.Create; 109 begin 110 Endianness := enLittle; 111 end; 112 67 113 function TMemoryStreamEx.ReadUnixTime: TDateTime; 68 114 begin … … 70 116 end; 71 117 118 function TMemoryStreamEx.ReadDouble: Double; 119 begin 120 Read(Result, SizeOf(Double)); 121 end; 122 123 function TMemoryStreamEx.ReadSingle: Single; 124 begin 125 Read(Result, SizeOf(Single)); 126 end; 127 72 128 function TMemoryStreamEx.ReadWord: Word; 73 129 begin 74 Read(Result, 2); 130 Read(Result, SizeOf(Word)); 131 if SwapData then Result := Swap(Result); 132 end; 133 134 procedure TMemoryStreamEx.SetEndianness(const AValue: TEndianness); 135 begin 136 FEndianness := AValue; 137 {$ifdef FPC_LITTLE_ENDIAN} 138 SwapData := FEndianness = enBig; 139 {$elseifdef FPC_BIG_ENDIAN} 140 SwapData := FEndianness = enLittle; 141 {$endif} 75 142 end; 76 143 77 144 procedure TMemoryStreamEx.WriteByte(Data: Byte); 78 145 begin 79 Write(Data, 1);146 Write(Data, SizeOf(Byte)); 80 147 end; 81 148 82 149 procedure TMemoryStreamEx.WriteCardinal(Data: Cardinal); 83 150 begin 84 Write(Data, 4); 151 if SwapData then Data := Swap(Data); 152 Write(Data, SizeOf(Cardinal)); 85 153 end; 86 154 87 155 procedure TMemoryStreamEx.WriteInt64(Data: Int64); 88 156 begin 89 Write(Data, 8); 157 if SwapData then Data := Swap(Data); 158 Write(Data, SizeOf(Int64)); 90 159 end; 91 160 … … 93 162 begin 94 163 WriteByte(Length(Data)); 95 Write(Data[1], Length(Data)) 96 end; 97 98 procedure TMemoryStreamEx.WriteStream(Stream: TStream );164 Write(Data[1], Length(Data)); 165 end; 166 167 procedure TMemoryStreamEx.WriteStream(Stream: TStream; Count: Integer); 99 168 var 100 169 Buffer: array of Byte; 101 170 begin 171 if Count > Stream.Size then Count := Stream.Size; // Limit max. stream size 102 172 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; 178 end; 179 180 procedure TMemoryStreamEx.WriteDouble(Value: Double); 181 begin 182 Write(Value, SizeOf(Double)); 183 end; 184 185 procedure TMemoryStreamEx.WriteSingle(Value: Single); 186 begin 187 Write(Value, SizeOf(Single)); 106 188 end; 107 189 … … 111 193 begin 112 194 DataUnix := DateTimeToUnix(Data); 113 Write (DataUnix, 4);195 WriteCardinal(DataUnix); 114 196 end; 115 197 116 198 procedure TMemoryStreamEx.WriteWord(Data: Word); 117 199 begin 118 Write(Data, 2); 200 if SwapData then Data := Swap(Data); 201 Write(Data, SizeOf(Word)); 119 202 end; 120 203
Note:
See TracChangeset
for help on using the changeset viewer.