-
Notifications
You must be signed in to change notification settings - Fork 6
/
DataStorage.pas
849 lines (757 loc) · 22.6 KB
/
DataStorage.pas
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
unit DataStorage;
{ Author: Sergey Bodrov ([email protected]) 2010-2020 }
{$ifdef FPC}
{$mode objfpc}{$H+}
{$endif}
interface
uses
Classes, SysUtils, Types;
type
TDataStorageType = (stUnknown, stString, stInteger, stNumber, stList, stDictionary);
{$ifndef UNICODE}
RawByteString = AnsiString;
{$endif}
{ IDataStorage }
IDataStorage = interface
{ Items count for (List, Dictionary) types }
function GetCount(): integer;
//procedure SetStorageType(const AValue: TDataStorageType);
function GetStorageType(): TDataStorageType;
{ Set value, if storage type is Dictionary, then AName used }
procedure SetValue(AValue: IDataStorage; const AName: string = ''); overload;
procedure SetValue(AValue: RawByteString; const AName: string = ''); overload;
procedure SetValue(AValue: TByteDynArray; const AName: string = ''); overload;
procedure SetValue(AValue: Integer; const AName: string = ''); overload;
procedure SetValue(AValue: Int64; const AName: string = ''); overload;
procedure SetValue(AValue: Real; const AName: string = ''); overload;
procedure SetValue(AValue: Boolean; const AName: string = ''); overload;
{ get storage value }
function GetValue(): RawByteString;
{ Get storage item by name }
function GetObject(const AName: string): IDataStorage; overload;
{ Get storage item by index }
function GetObject(Index: integer): IDataStorage; overload;
{ Get name by index }
function GetObjectName(Index: integer): string;
{ Get string by name (from dictionary). If name empty, get value }
function GetString(const AName: string = ''): string;
function GetBytes(const AName: string = ''): TByteDynArray;
function GetInteger(const AName: string = ''): Integer;
function GetInt64(const AName: string = ''): Int64;
function GetCardinal(const AName: string = ''): Cardinal;
function GetReal(const AName: string = ''): Real;
function GetBool(const AName: string = ''): Boolean;
function HaveName(const AName: string): Boolean;
procedure Clear();
{ get length of all contained items and subitems, including dictionary names }
function GetSize(): Int64;
end;
{ TDataStorage }
TDataStorage = class(TInterfacedObject, IDataStorage)
private
{ stUnknown, stString, stInteger, stNumber, stList, stDictionary }
FStorageType: TDataStorageType;
{ Value for (String, Integer, Number) types }
FValue: RawByteString;
FIntfList: TInterfaceList;
{ [name:object] items storage }
FItems: TStringList;
procedure AddValue(AStorageType: TDataStorageType; const AName: string; const AValue: RawByteString);
public
constructor Create(AStorageType: TDataStorageType);
destructor Destroy(); override;
{ Items count for (List, Dictionary) types }
function GetCount(): integer;
//procedure SetStorageType(const AValue: TDataStorageType);
function GetStorageType(): TDataStorageType;
{ Set value, if storage type is Dictionary, then AName used
for (List) is add value}
procedure SetValue(AValue: IDataStorage; const AName: string = ''); overload;
procedure SetValue(AValue: RawByteString; const AName: string = ''); overload;
procedure SetValue(AValue: TByteDynArray; const AName: string = ''); overload;
procedure SetValue(AValue: Integer; const AName: string = ''); overload;
procedure SetValue(AValue: Int64; const AName: string = ''); overload;
procedure SetValue(AValue: Real; const AName: string = ''); overload;
procedure SetValue(AValue: Boolean; const AName: string = ''); overload;
{ get storage value }
function GetValue(): RawByteString;
{ set storage value }
procedure SetValueStr(const AValue: RawByteString); overload;
{ Get storage item by name }
function GetObject(const AName: string): IDataStorage; overload;
{ Get storage item by index }
function GetObject(Index: integer): IDataStorage; overload;
{ Get name by index }
function GetObjectName(Index: integer): string;
{ Get string by name (from dictionary). If name empty, get value }
function GetString(const AName: string = ''): string;
function GetBytes(const AName: string = ''): TByteDynArray;
function GetInteger(const AName: string = ''): Integer;
function GetInt64(const AName: string = ''): Int64;
function GetCardinal(const AName: string = ''): Cardinal;
function GetReal(const AName: string = ''): Real;
function GetBool(const AName: string = ''): Boolean;
function HaveName(const AName: string): Boolean;
procedure Clear();
{ get size of all contained items and subitems, including dictionary names:
numerics - SizeOf()
strings - Length() }
function GetSize(): Int64;
property Count: Integer read GetCount;
{ stUnknown, stString, stInteger, stNumber, stList, stDictionary }
property StorageType: TDataStorageType read GetStorageType;
{ Value for (String, Integer, Number) types }
property Value: RawByteString read GetValue write SetValueStr;
end;
{ TDataSerializer }
TDataSerializer = class(TObject)
public
function GetName(): string; virtual;
// Serialize storage to string
function StorageToString(AStorage: IDataStorage): RawByteString; virtual; abstract;
// De-serialize string into AStorage (not nil)
function StorageFromString(const AString: RawByteString): IDataStorage; virtual; abstract;
// Save storage to file. Filename must be without extension
function StorageToFile(AStorage: IDataStorage; AFileName: string): Boolean; virtual; abstract;
// Fill AStorage (not nil) from file. Filename must be without extension
function StorageFromFile(AFileName: string): IDataStorage; virtual; abstract;
end;
{ TDataSerializerBencode }
{
Bencode serializer
integers: i<value>e
i0e i42e i-42e
strings: <value_len>:<value>
3:ben 4:code
lists: l<items>e (without any spaces)
l i42e 3:ben 4:code e
dictionaries: d<items>e where items is <string_name><value>
d 4:name 3:ben 4:code i42e e
}
TDataSerializerBencode = class(TDataSerializer)
private
function StorageToBencode(AStorage: IDataStorage): RawByteString;
function StorageToStream(AStorage: IDataStorage; AStream: TStream): Boolean;
function ReadBencodeValue(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): IDataStorage;
function ReadBencodeIntegerStr(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): RawByteString;
function ReadBencodeString(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): RawByteString;
function ReadBencodeList(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): IDataStorage;
function ReadBencodeDictionary(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): IDataStorage;
public
function GetName(): string; override;
function StorageToString(AStorage: IDataStorage): RawByteString; override;
function StorageFromString(const AString: RawByteString): IDataStorage; override;
function StorageToFile(AStorage: IDataStorage; AFileName: string): Boolean; override;
function StorageFromFile(AFileName: string): IDataStorage; override;
end;
// shared functions
function StrToFile(const FileName, Str: RawByteString): Boolean;
function FileToStr(const FileName: string): RawByteString;
var
DataFormatSettings: TFormatSettings;
implementation
function StreamToStr(AStream: TStream): RawByteString;
var
ss: TStringStream;
begin
Result := '';
ss := TStringStream.Create('');
try
AStream.Seek(0, soFromBeginning);
ss.Size := AStream.Size;
ss.Position := 0;
ss.CopyFrom(AStream, AStream.Size);
Result := ss.DataString;
finally
ss.Free();
end;
end;
function StrToStream(const s: RawByteString; AStream: TStream): Boolean;
var
ss: TStringStream;
begin
ss := TStringStream.Create(s);
try
ss.Seek(0, soFromBeginning);
AStream.Seek(0, soFromBeginning);
AStream.CopyFrom(ss, ss.Size);
Result := True;
finally
ss.Free();
end;
end;
function StrToFile(const FileName, Str: RawByteString): Boolean;
var
fs: TFileStream;
begin
Result := False;
try
fs := TFileStream.Create(FileName, fmCreate);
except
fs := nil;
end;
if not Assigned(fs) then
Exit;
try
StrToStream(Str, fs);
Result := True;
finally
FreeAndNil(fs);
end;
end;
function FileToStr(const FileName: string): RawByteString;
var
fs: TFileStream;
begin
Result := '';
if not FileExists(FileName) then
Exit;
try
fs := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
except
fs := nil;
end;
if not Assigned(fs) then
Exit;
try
Result := StreamToStr(fs);
finally
fs.Free();
end;
end;
function RawToBytes(const AValue: UTF8String): TByteDynArray;
var
n: integer;
begin
n := Length(AValue);
SetLength(Result, n);
if n > 0 then
Move(AValue[1], Result[0], n);
end;
function BytesToRaw(const AValue: TByteDynArray): UTF8String;
var
n: integer;
begin
n := Length(AValue);
SetLength(Result, n);
if n > 0 then
Move(AValue[0], Result[1], n);
end;
{ TDataStorage }
constructor TDataStorage.Create(AStorageType: TDataStorageType);
begin
inherited Create();
FStorageType := AStorageType;
FValue := '';
FItems := TStringList.Create();
FIntfList := TInterfaceList.Create();
//FItems.OwnsObjects := True;
end;
destructor TDataStorage.Destroy();
begin
Clear();
FreeAndNil(FIntfList);
FreeAndNil(FItems);
inherited Destroy();
end;
function TDataStorage.GetCount(): integer;
begin
Result := FItems.Count;
end;
function TDataStorage.GetObject(const AName: string): IDataStorage;
var
n: integer;
begin
n := FItems.IndexOf(AName);
if n >= 0 then
begin
Result := IDataStorage(FIntfList[n]);
if Result.GetStorageType() in [stList, stDictionary] then
Exit;
end;
Result := nil;
end;
function TDataStorage.GetObject(Index: integer): IDataStorage;
begin
Result := nil;
if (Index >= 0) and (Index < Count) then
begin
Result := IDataStorage(FIntfList[Index]);
end;
end;
function TDataStorage.GetObjectName(Index: integer): string;
begin
Result := '';
if (Index >= 0) and (Index < FItems.Count) then
Result := FItems[Index];
end;
function TDataStorage.GetString(const AName: string): string;
var
n: integer;
TmpItem: IDataStorage;
begin
Result := '';
if AName = '' then
Result := FValue
else
begin
n := FItems.IndexOf(AName);
if n <> -1 then
begin
TmpItem := IDataStorage(FIntfList[n]);
Result := TmpItem.GetValue();
//if TmpItem.StorageType=stString then Result:=TmpItem.Value;
end;
end;
end;
function TDataStorage.GetBytes(const AName: string = ''): TByteDynArray;
var
n: integer;
TmpItem: IDataStorage;
begin
if AName = '' then
begin
Result := RawToBytes(FValue);
end
else
begin
n := FItems.IndexOf(AName);
if n <> -1 then
begin
TmpItem := IDataStorage(FIntfList[n]);
Result := RawToBytes(TmpItem.GetValue());
//if TmpItem.StorageType=stString then Result:=TmpItem.Value;
end;
end;
end;
function TDataStorage.GetInteger(const AName: string): Integer;
begin
Result := StrToIntDef(GetString(AName), 0);
end;
function TDataStorage.GetInt64(const AName: string = ''): Int64;
begin
Result := StrToInt64Def(GetString(AName), 0);
end;
function TDataStorage.GetCardinal(const AName: string): Cardinal;
begin
Result := StrToInt64Def(GetString(AName), 0);
end;
function TDataStorage.GetReal(const AName: string): Real;
begin
Result := StrToFloatDef(GetString(AName), 0, DataFormatSettings);
end;
function TDataStorage.GetBool(const AName: string): Boolean;
begin
Result := (GetString(AName) = '1');
end;
function TDataStorage.HaveName(const AName: string): Boolean;
begin
Result := (FItems.IndexOf(AName) <> -1);
end;
procedure TDataStorage.Clear();
{var
i: Integer;
TmpObj: TObject; }
begin
{for i := Count-1 downto 0 do
begin
TmpObj := FItems.Objects[i];
FItems.Objects[i] := nil;
TmpObj.Free();
end; }
FItems.Clear();
FIntfList.Clear();
FValue := '';
end;
function TDataStorage.GetStorageType(): TDataStorageType;
begin
Result := FStorageType;
end;
function TDataStorage.GetValue(): RawByteString;
begin
Result := FValue;
end;
procedure TDataStorage.AddValue(AStorageType: TDataStorageType;
const AName: string; const AValue: RawByteString);
var
TmpItem: IDataStorage;
begin
TmpItem := TDataStorage.Create(AStorageType);
TmpItem.SetValue(AValue);
FItems.Add(AName);
FIntfList.Add(TmpItem);
end;
procedure TDataStorage.SetValue(AValue: RawByteString; const AName: string);
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
AddValue(stString, AName, AValue);
end
else
FValue := AValue;
end;
procedure TDataStorage.SetValue(AValue: TByteDynArray; const AName: string = '');
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
AddValue(stString, AName, BytesToRaw(AValue));
end
else
FValue := BytesToRaw(AValue);
end;
procedure TDataStorage.SetValue(AValue: IDataStorage; const AName: string);
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
if Assigned(AValue) then
begin
FIntfList.Add(AValue);
FItems.Add(AName);
end;
end
else
begin
// not valid for current storage type
end;
end;
procedure TDataStorage.SetValue(AValue: Integer; const AName: string);
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
AddValue(stInteger, AName, IntToStr(AValue));
end
else
FValue := IntToStr(AValue);
end;
procedure TDataStorage.SetValue(AValue: Int64; const AName: string);
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
AddValue(stInteger, AName, IntToStr(AValue));
end
else
FValue := IntToStr(AValue);
end;
procedure TDataStorage.SetValue(AValue: Boolean; const AName: string);
begin
if AValue then
Self.SetValue('1', AName)
else
Self.SetValue('0', AName);
end;
procedure TDataStorage.SetValue(AValue: Real; const AName: string);
begin
if (FStorageType = stDictionary) or (FStorageType = stList) then
begin
AddValue(stNumber, AName, FloatToStr(AValue, DataFormatSettings));
end
else
FValue := FloatToStr(AValue, DataFormatSettings);
end;
procedure TDataStorage.SetValueStr(const AValue: RawByteString);
begin
SetValue(AValue);
end;
function TDataStorage.GetSize(): Int64;
var
i: Integer;
begin
case FStorageType of
stUnknown: Result := 0;
stString, stInteger, stNumber: Result := Length(FValue);
stList, stDictionary:
begin
Result := 0;
for i := 0 to Count-1 do
begin
Result := Length(GetObjectName(i)) + GetObject(i).GetSize();
end;
end;
else
Result := 0;
end;
end;
{ TDataSerializer }
function TDataSerializer.GetName: string;
begin
Result := 'NONE';
end;
{ TDataSerializerBencode }
function TDataSerializerBencode.GetName: string;
begin
Result := 'BENCODE';
end;
function TDataSerializerBencode.StorageToBencode(AStorage: IDataStorage): RawByteString;
var
sName: RawByteString;
SubItem: IDataStorage;
i: integer;
s: RawByteString;
begin
Result := '';
case AStorage.GetStorageType() of
stString:
begin
s := AStorage.GetValue();
Result := Result + IntToStr(Length(s)) + ':' + s;
end;
stNumber:
begin
s := AStorage.GetValue();
Result := Result + IntToStr(Length(s)) + ':' + s;
end;
stInteger:
begin
Result := Result + 'i' + AStorage.GetValue() + 'e';
end;
stDictionary:
begin
Result := Result + 'd';
for i := 0 to AStorage.GetCount() - 1 do
begin
sName := AStorage.GetObjectName(i);
SubItem := AStorage.GetObject(i);
// name
Result := Result + IntToStr(Length(sName)) + ':' + sName;
// value
Result := Result + StorageToBencode(SubItem);
end;
Result := Result + 'e';
end;
stList:
begin
Result := Result + 'l';
for i := 0 to AStorage.GetCount() - 1 do
begin
SubItem := AStorage.GetObject(i);
// value
Result := Result + StorageToBencode(SubItem);
end;
Result := Result + 'e';
end;
end;
end;
function TDataSerializerBencode.StorageToStream(AStorage: IDataStorage;
AStream: TStream): Boolean;
procedure WriteStr(const AStr: RawByteString);
begin
if Length(AStr) > 0 then
AStream.Write(AStr[1], Length(AStr));
end;
var
sName: RawByteString;
SubItem: IDataStorage;
i: integer;
s: RawByteString;
begin
case AStorage.GetStorageType() of
stString:
begin
s := AStorage.GetValue();
WriteStr(IntToStr(Length(s)) + ':' + s);
end;
stNumber:
begin
s := AStorage.GetValue();
WriteStr(IntToStr(Length(s)) + ':' + s);
end;
stInteger:
begin
WriteStr('i' + AStorage.GetValue() + 'e');
end;
stDictionary:
begin
WriteStr('d');
for i := 0 to AStorage.GetCount() - 1 do
begin
sName := AStorage.GetObjectName(i);
SubItem := AStorage.GetObject(i);
// name
WriteStr(IntToStr(Length(sName)) + ':' + sName);
// value
StorageToStream(SubItem, AStream);
end;
WriteStr('e');
end;
stList:
begin
WriteStr('l');
for i := 0 to AStorage.GetCount() - 1 do
begin
SubItem := AStorage.GetObject(i);
// value
StorageToStream(SubItem, AStream);
end;
WriteStr('e');
end;
end;
Result := True;
end;
function TDataSerializerBencode.ReadBencodeIntegerStr(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): RawByteString;
begin
Result := '';
if AString[APos] = 'i' then
Inc(APos)
else
Exit;
while APos <= ALen do
begin
if AString[APos] = 'e' then
begin
Inc(APos);
Break
end;
Result := Result + AString[APos];
Inc(APos);
end;
end;
function TDataSerializerBencode.ReadBencodeString(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): RawByteString;
var
sValue: RawByteString;
ValueLen: Cardinal;
begin
Result := '';
sValue := '';
while APos <= ALen do
begin
if AString[APos] = ':' then
begin
ValueLen := StrToIntDef(sValue, 0);
Result := Copy(AString, APos + 1, ValueLen);
APos := APos + ValueLen + 1;
Exit;
end;
sValue := sValue + AString[APos];
Inc(APos);
end;
end;
function TDataSerializerBencode.ReadBencodeDictionary(const AString: RawByteString; var APos: Cardinal; ALen: Cardinal): IDataStorage;
var
sName: RawByteString;
SubStorage: IDataStorage;
begin
Result := nil;
if AString[APos] = 'd' then
Inc(APos)
else
Exit;
Result := TDataStorage.Create(stDictionary);
while APos <= ALen do
begin
if AString[APos] = 'e' then
begin
Inc(APos);
Exit;
end;
sName := ReadBencodeString(AString, APos, ALen);
SubStorage := ReadBencodeValue(AString, APos, ALen);
if Assigned(SubStorage) then
Result.SetValue(SubStorage, sName);
end;
Assert(False, 'End of dict not found');
Result := nil;
end;
function TDataSerializerBencode.ReadBencodeList(const AString: RawByteString;
var APos: Cardinal; ALen: Cardinal): IDataStorage;
var
SubStorage: IDataStorage;
begin
Result := nil;
if AString[APos] = 'l' then
Inc(APos)
else
Exit;
Result := TDataStorage.Create(stList);
while APos <= ALen do
begin
if AString[APos] = 'e' then
begin
Inc(APos);
Exit;
end;
SubStorage := ReadBencodeValue(AString, APos, ALen);
if Assigned(SubStorage) then
Result.SetValue(SubStorage);
end;
Assert(False, 'End of list not found');
Result := nil;
end;
function TDataSerializerBencode.ReadBencodeValue(const AString: RawByteString;
var APos: Cardinal; ALen: Cardinal): IDataStorage;
begin
Result := nil;
if APos <= ALen then
begin
if AString[APos] = 'i' then
begin
// read integer value
Result := TDataStorage.Create(stInteger);
Result.SetValue(ReadBencodeIntegerStr(AString, APos, ALen));
end
else if Pos(AString[APos], '0123456789') > 0 then
begin
// read string value
Result := TDataStorage.Create(stString);
Result.SetValue(ReadBencodeString(AString, APos, ALen));
end
else if AString[APos] = 'd' then
begin
// read dictionary value
Result := ReadBencodeDictionary(AString, APos, ALen);
end
else if AString[APos] = 'l' then
begin
// read list value
Result := ReadBencodeList(AString, APos, ALen);
end
else
begin
// error
Assert(1=0, 'Bencode parsing error, index=' + IntToStr(APos) + ' char=' + AString[APos]);
Exit;
end;
end;
end;
function TDataSerializerBencode.StorageToString(AStorage: IDataStorage): RawByteString;
begin
Result := StorageToBencode(AStorage);
end;
function TDataSerializerBencode.StorageFromString(const AString: RawByteString): IDataStorage;
var
n: Cardinal;
begin
n := 1;
Result := ReadBencodeValue(AString, n, Length(AString));
Assert(Result <> nil, 'Read value failed, len='+IntToStr(Length(AString)));
end;
function TDataSerializerBencode.StorageToFile(AStorage: IDataStorage; AFileName: string): Boolean;
var
ms: TMemoryStream;
begin
Result := False;
if Trim(AFileName) = '' then
Exit;
if Pos('.be', AFileName) < (Length(AFileName) - 2) then
AFileName := AFileName + '.be';
//Result := StrToFile(AFileName, Self.StorageToString(AStorage));
ms := TMemoryStream.Create();
try
ms.Size := Trunc(AStorage.GetSize() * 1.1);
StorageToStream(AStorage, ms);
ms.SaveToFile(AFileName);
finally
ms.Free();
end;
end;
function TDataSerializerBencode.StorageFromFile(AFileName: string): IDataStorage;
begin
Result := nil;
if Trim(AFileName) = '' then
Exit;
if Pos('.be', AFileName) < (Length(AFileName) - 2) then
AFileName := AFileName + '.be';
Result := Self.StorageFromString(FileToStr(AFileName));
end;
initialization
DataFormatSettings.DecimalSeparator := '.';
end.