Menu

[4a4584]: / Source / EmmaFileHelper.pas  Maximize  Restore  History

Download this file

350 lines (297 with data), 9.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
(***********************************************************************)
(* Delphi Code Coverage *)
(* *)
(* A quick hack of a Code Coverage Tool for Delphi *)
(* by Christer Fahlgren and Nick Ring *)
(* *)
(* This Source Code Form is subject to the terms of the Mozilla Public *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this *)
(* file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
unit EmmaFileHelper;
interface
uses
System.SysUtils,
System.Types,
System.Classes;
type
TMultiBooleanArray = array of TBooleanDynArray;
TDataIO = class(TInterfacedObject, IInterface)
strict private
FFile: TStream;
protected
function ReverseInt64_Pure(const AValue: Int64): Int64;
function ReverseInt_Pure(const AValue: Integer): Integer; inline;
function ReverseWord(const AValue: Word): Word; inline;
property DataFile: TStream read FFile write FFile;
public
constructor Create(AFile: TStream);
end;
IEmmaDataInput = interface
['{4846A91D-E71E-4DDE-9F68-19CFA7060F84}']
function ReadInt64: Int64;
function ReadInteger: Integer;
function ReadByte: Byte;
function ReadBoolean: Boolean;
function ReadWord: Word;
function ReadUTF: string;
procedure ReadIntArray(var AIntArray: TIntegerDynArray);
function ReadBooleanArray: TBooleanDynArray;
end;
TEmmaDataInput = class(TDataIO, IEmmaDataInput)
public
function ReadInt64: Int64;
function ReadInteger: Integer;
function ReadByte: Byte;
function ReadBoolean: Boolean;
function ReadWord: Word;
function ReadUTF: string;
procedure ReadIntArray(var AIntArray: TIntegerDynArray);
function ReadBooleanArray: TBooleanDynArray;
end;
IEmmaDataOutput = interface
['{ACE23F4B-1BCD-466D-BE4B-2EA2812C807E}']
procedure WriteInt64(const AValue: Int64);
procedure WriteInteger(const AValue: Integer);
procedure WriteByte(const AValue: Byte);
procedure WriteBoolean(const AValue: Boolean);
procedure WriteWord(const AValue: Word);
procedure WriteUTF(const AValue: String);
procedure WriteIntArray(const AValues: TIntegerDynArray);
procedure WriteBooleanArray(const AValues: TBooleanDynArray);
end;
TEmmaDataOutput = class(TDataIO, IEmmaDataOutput)
public
procedure WriteInt64(const AValue: Int64);
procedure WriteInteger(const AValue: Integer);
procedure WriteByte(const AValue: Byte);
procedure WriteBoolean(const AValue: Boolean);
procedure WriteWord(const AValue: Word);
procedure WriteUTF(const AValue: String);
procedure WriteIntArray(const AValues: TIntegerDynArray);
procedure WriteBooleanArray(const AValues: TBooleanDynArray);
end;
EEmmaException = Exception;
function GetUtf8Length(const AValue: string): Integer;
function GetEntryLength(const AIntArray: TIntegerDynArray): Int64; overload;
function GetEntryLength(const ABoolArray: TBooleanDynArray): Int64; overload;
implementation
uses
{$IFDEF MSWINDOWS}
Winapi.WinSock
{$ELSE}
Posix.ArpaInet
{$ENDIF};
{$region 'Helpers'}
function GetUtf8Length(const AValue: string): Integer;
var
Str: RawByteString;
begin
Str := UTF8Encode(AValue);
Result := Length(Str) + SizeOf(Word);
end;
function GetEntryLength(const AIntArray: TIntegerDynArray): Int64;
begin
Result := SizeOf(Integer) + Length(AIntArray) * SizeOf(Integer);
end;
function GetEntryLength(const ABoolArray: TBooleanDynArray): Int64;
begin
Result := SizeOf(Integer) + Length(ABoolArray) * SizeOf(Boolean);
end;
{$endregion 'Helpers'}
{$region 'TDataIO'}
constructor TDataIO.Create(AFile: TStream);
begin
inherited Create;
FFile := AFile;
end;
function TDataIO.ReverseInt64_Pure(const AValue: Int64): Int64;
begin
Int64Rec(Result).Bytes[0] := Int64Rec(AValue).Bytes[7];
Int64Rec(Result).Bytes[1] := Int64Rec(AValue).Bytes[6];
Int64Rec(Result).Bytes[2] := Int64Rec(AValue).Bytes[5];
Int64Rec(Result).Bytes[3] := Int64Rec(AValue).Bytes[4];
Int64Rec(Result).Bytes[4] := Int64Rec(AValue).Bytes[3];
Int64Rec(Result).Bytes[5] := Int64Rec(AValue).Bytes[2];
Int64Rec(Result).Bytes[6] := Int64Rec(AValue).Bytes[1];
Int64Rec(Result).Bytes[7] := Int64Rec(AValue).Bytes[0];
end;
function TDataIO.ReverseInt_Pure(const AValue: Integer): Integer;
begin
Result := ntohl(AValue);
end;
function TDataIO.ReverseWord(const AValue: Word): Word;
begin
Result := ntohs(AValue);
end;
{$endregion 'TDataIO'}
{$region 'TDataInput'}
function TEmmaDataInput.ReadByte: Byte;
begin
DataFile.Read(Result, 1);
end;
function TEmmaDataInput.ReadBoolean: Boolean;
begin
Result := Boolean(ReadByte);
end;
function TEmmaDataInput.ReadUTF: string;
var
DataSize: Word;
RawData: RawByteString;
BytesRead: Integer;
begin
DataSize := ReadWord;
SetLength(RawData, DataSize);
BytesRead := DataFile.Read(RawData[1], DataSize);
if (DataSize <> BytesRead) then
begin
raise EEmmaException.Create('Reading string but EOF encountered');
end;
{$IF CompilerVersion > 19}
Result := UTF8ToString(RawData);
{$ELSE}
Result := UTF8Decode(RawData);
{$IFEND}
end;
procedure TEmmaDataInput.ReadIntArray(var AIntArray: TIntegerDynArray);
var
ArrayLength: Integer;
I: Integer;
begin
ArrayLength := ReadInteger;
SetLength(AIntArray, ArrayLength);
for I := 0 to ArrayLength - 1 do
begin
AIntArray[I] := ReadInteger;
end;
end;
function TEmmaDataInput.ReadBooleanArray: TBooleanDynArray;
var
ArrayLength: Integer;
I: Integer;
begin
ArrayLength := ReadInteger;
SetLength(Result, ArrayLength);
for I := 0 to ArrayLength - 1 do
begin
Result[I] := ReadBoolean;
end;
end;
function TEmmaDataInput.ReadInt64: Int64;
var
Int64Value: Int64;
BytesRead: Integer;
begin
BytesRead := DataFile.Read(Int64Value, SizeOf(Int64Value));
if (BytesRead <> SizeOf(Int64Value)) then
begin
raise EEmmaException.Create('Not enough bytes to read an Int64');
end;
Result := ReverseInt64_Pure(Int64Value);
end;
function TEmmaDataInput.ReadInteger: Integer;
var
IntValue: Integer;
BytesRead: Integer;
begin
BytesRead := DataFile.Read(IntValue, SizeOf(IntValue));
if (BytesRead <> SizeOf(IntValue)) then
begin
raise EEmmaException.Create('Not enough bytes to read an Integer');
end;
Result := ReverseInt_Pure(IntValue);
end;
function TEmmaDataInput.ReadWord: Word;
var
WordValue: Word;
BytesRead: Integer;
begin
BytesRead := DataFile.Read(WordValue, SizeOf(WordValue));
if (BytesRead <> SizeOf(WordValue)) then
begin
raise EEmmaException.Create('Not enough bytes to read a Word');
end;
Result := ReverseWord(WordValue);
end;
{$endregion 'TDataInput'}
{$region 'TDataOutput'}
procedure TEmmaDataOutput.WriteByte(const AValue: Byte);
begin
DataFile.Write(AValue, 1);
end;
procedure TEmmaDataOutput.WriteBoolean(const AValue: Boolean);
begin
WriteByte(Byte(AValue));
end;
procedure TEmmaDataOutput.WriteUTF(const AValue: string);
var
DataSize: Word;
RawData: RawByteString;
BytesWritten: Integer;
begin
RawData := UTF8Encode(AValue);
DataSize := Length(RawData);
WriteWord(DataSize);
BytesWritten := DataFile.Write(RawData[1], DataSize);
if (DataSize <> BytesWritten) then
begin
raise EEmmaException.Create('Writing string but not enough chars were written');
end;
end;
procedure TEmmaDataOutput.WriteIntArray(const AValues: TIntegerDynArray);
var
I: Integer;
begin
WriteInteger(Length(AValues));
for I := 0 to High(AValues) do
begin
WriteInteger(AValues[I]);
end;
end;
procedure TEmmaDataOutput.WriteBooleanArray(const AValues: TBooleanDynArray);
var
I: Integer;
begin
WriteInteger(Length(AValues));
for I := 0 to High(AValues) do
begin
WriteBoolean(AValues[I]);
end;
end;
procedure TEmmaDataOutput.WriteInt64(const AValue: Int64);
var
RawData: Int64;
BytesWritten: Integer;
begin
RawData := ReverseInt64_Pure(AValue);
BytesWritten := DataFile.Write(RawData, SizeOf(RawData));
if (BytesWritten <> SizeOf(RawData)) then
begin
raise EEmmaException.Create('Not enough bytes written');
end;
end;
procedure TEmmaDataOutput.WriteInteger(const AValue: Integer);
var
RawData: Integer;
BytesWritten: Integer;
begin
RawData := ReverseInt_Pure(AValue);
BytesWritten := DataFile.Write(RawData, SizeOf(RawData));
if (BytesWritten <> SizeOf(RawData)) then
begin
raise EEmmaException.Create('Not enough bytes written for an Integer');
end;
end;
procedure TEmmaDataOutput.WriteWord(const AValue: Word);
var
RawData: Word;
BytesWritten: Integer;
begin
RawData := ReverseWord(AValue);
BytesWritten := DataFile.Write(RawData, SizeOf(RawData));
if (BytesWritten <> SizeOf(RawData)) then
begin
raise EEmmaException.Create('Not enough bytes written for a word');
end;
end;
{$endregion 'TDataInput'}
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.