-
Notifications
You must be signed in to change notification settings - Fork 792
/
DataChest.pas
1299 lines (1158 loc) · 32.6 KB
/
DataChest.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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
======================================
Language:
Object Pascal
Name:
DataChest
数据存储柜
Description:
使用动态index存储数据
Writer:
黄锐( [email protected] )
Date:
2010-4-8 12:54:43
36816
======================================
}
unit DataChest;
interface
uses
Classes, SysUtils, xmldom, XMLDoc, XMLIntf, EncdDecd;
const
ChestTypeEmpty = 0;
ChestTypeInteger = 1;
ChestTypeReal = 2;
ChestTypeBoolean = 3;
ChestTypeString = 4;
ChestTypePointer = 5;
type
{
======================================
TChestElement
不建副本到内存
======================================
}
TChestElement = class(TObject)
ChestType: Word;
Value: Pointer;
Name: String;
end;
TChestString = class(TObject)
Value: string;
end;
{
======================================
TChest
不建副本到内存
======================================
}
TChest = class(TObject)
const
Version = '1.0.0.0';
private
class var List: TList;
class var Checked: Boolean;
class var Sorted: Boolean;
class var _Publisher: string;
class var FDuplicates: TDuplicates;
class function SelfCheck: Boolean; static;
class procedure SortByName; static;
class function GetValue(index: string): Pointer; static;
class function GetValueAndType(index: string;
var ChestType: Word): Pointer; static;
class procedure SetValue(index: string; const Value: Pointer); static;
class procedure SetValueAndType(index: string; const Value: Pointer;
ChestType: Word = ChestTypeEmpty); static;
class function GetCount: integer; static;
class procedure _Delete(index: integer); overload; static;
class function GetItem(index: integer): TChestElement; static;
class procedure SetItem(index: integer; const Value: TChestElement); static;
public
class property Item[index: integer]
: TChestElement read GetItem write SetItem; default;
class property AsPointer[index: string]
: Pointer read GetValue write SetValue default nil;
class property Count: integer read GetCount default 0;
class property Publisher: string read _Publisher write _Publisher;
class property Duplicates: TDuplicates read FDuplicates write FDuplicates;
class function Find(const S: string; var Index: integer): Boolean; static;
class procedure Clear; static;
class procedure _Delete(index: string); overload; static;
class procedure DoInitialization; static;
class procedure DoFinalization; static;
end;
{
======================================
TMemChest
不建副本到内存
======================================
}
TMemChest = class(TChest)
private
class var TempList: TList;
class procedure Delete(index: integer); overload; static;
class function GetXML: WideString; static;
class procedure SetXML(const Value: WideString); static;
public
class procedure FreeMemory; static;
class procedure Delete(index: string); overload; static;
class procedure SaveToXMLFile(FileName: string); static;
class procedure LoadFromXMLFile(FileName: string); static;
class procedure SaveToEncryptFile(FileName: string); static;
class procedure LoadFromEncryptFile(FileName: string); static;
class procedure SaveToFile(FileName: string); static;
class procedure LoadFromFile(FileName: string); static;
class property XML: WideString read GetXML write SetXML;
end;
{
======================================
TIntChest
建副本到内存
======================================
}
TIntChest = class(TMemChest)
private
class function GetIntValue(index: string): integer; static;
class procedure SetIntValue(index: string; const Value: integer); static;
class function GetStringValue(index: string): String; static;
class procedure SetStringValue(index: string; const Value: String); static;
public
class property AsInt[index: string]
: integer read GetIntValue write SetIntValue; default;
class property AsString[index: string]
: String read GetStringValue write SetStringValue;
end;
{
======================================
TRealChest
建副本到内存
======================================
}
TRealChest = class(TMemChest)
private
class function GetRealValue(index: string): Real; static;
class procedure SetRealValue(index: string; const Value: Real); static;
class function GetIntValue(index: string): integer; static;
class function GetStringValue(index: string): String; static;
class procedure SetIntValue(index: string; const Value: integer); static;
class procedure SetStringValue(index: string; const Value: String); static;
public
class property AsReal[index: string]
: Real read GetRealValue write SetRealValue; default;
class property AsInt[index: string]
: integer read GetIntValue write SetIntValue;
class property AsString[index: string]
: String read GetStringValue write SetStringValue;
end;
{
======================================
TBooleanChest
建副本到内存
======================================
}
TBooleanChest = class(TMemChest)
private
class function GetRealValue(index: string): Real; static;
class procedure SetRealValue(index: string; const Value: Real); static;
class function GetIntValue(index: string): integer; static;
class function GetStringValue(index: string): String; static;
class procedure SetIntValue(index: string; const Value: integer); static;
class procedure SetStringValue(index: string; const Value: String); static;
class function GetBooleanValue(index: string): Boolean; static;
class procedure SetBooleanValue(index: string; const Value: Boolean);
static;
public
class property AsBoolean[index: string]
: Boolean read GetBooleanValue write SetBooleanValue; default;
class property AsReal[index: string]
: Real read GetRealValue write SetRealValue;
class property AsInt[index: string]
: integer read GetIntValue write SetIntValue;
class property AsString[index: string]
: String read GetStringValue write SetStringValue;
end;
{
======================================
TStringChest
建副本到内存
======================================
}
TStringChest = class(TMemChest)
private
class function GetStringValue(index: string): string; static;
class procedure SetStringValue(index: string; const Value: string); static;
public
class property AsString[index: string]
: string read GetStringValue write SetStringValue; default;
end;
{
======================================
TObjectChest
不建副本到内存
======================================
}
TObjectChest = class(TChest)
private
class function GetObjectValue(index: string): TObject; static;
class procedure SetObjectValue(index: string; const Value: TObject); static;
public
class property AsObject[index: string]
: TObject read GetObjectValue write SetObjectValue; default;
end;
{
======================================
TComponentChest
不建副本到内存
======================================
}
TComponentChest = class(TObjectChest)
private
class function GetComponentValue(index: string): TComponent; static;
class procedure SetComponentValue(index: string; const Value: TComponent);
static;
public
class property AsComponent[index: string]
: TComponent read GetComponentValue write
SetComponentValue; default;
end;
{
======================================
XML interface(AutoCreated)
======================================
}
{ Forward Decls }
IXMLDataChestType = interface;
IXMLPublisherType = interface;
IXMLDataType = interface;
IXMLDataTypeList = interface;
{ IXMLDataChestType }
IXMLDataChestType = interface(IXMLNode)
['{02B0FB9E-6CEA-44DE-BB53-63C688856EB7}']
{ Property Accessors }
function Get_Publisher: IXMLPublisherType;
function Get_Data: IXMLDataTypeList;
{ Methods & Properties }
property Publisher: IXMLPublisherType read Get_Publisher;
property Data: IXMLDataTypeList read Get_Data;
end;
{ IXMLPublisherType }
IXMLPublisherType = interface(IXMLNode)
['{B2149CEA-4B15-4BC1-B12A-D396BBADE025}']
{ Property Accessors }
function Get_Info: UnicodeString;
function Get_Version: UnicodeString;
procedure Set_Info(Value: UnicodeString);
procedure Set_Version(Value: UnicodeString);
{ Methods & Properties }
property Info: UnicodeString read Get_Info write Set_Info;
property Version: UnicodeString read Get_Version write Set_Version;
end;
{ IXMLDataType }
IXMLDataType = interface(IXMLNode)
['{956479AF-F38D-46E3-94AA-03EE6464F9E8}']
{ Property Accessors }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
function Get_Value: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
procedure Set_Value(Value: UnicodeString);
{ Methods & Properties }
property Name: UnicodeString read Get_Name write Set_Name;
property Type_: UnicodeString read Get_Type_ write Set_Type_;
property Value: UnicodeString read Get_Value write Set_Value;
end;
{ IXMLDataTypeList }
IXMLDataTypeList = interface(IXMLNodeCollection)
['{9A322421-9DD5-48C2-ACB0-4E0A9A0AF866}']
{ Methods & Properties }
function Add: IXMLDataType;
function Insert(const Index: integer): IXMLDataType;
function Get_Item(Index: integer): IXMLDataType;
property Items[Index: integer]: IXMLDataType read Get_Item; default;
end;
{ Forward Decls }
TXMLDataChestType = class;
TXMLPublisherType = class;
TXMLDataType = class;
TXMLDataTypeList = class;
{ TXMLDataChestType }
TXMLDataChestType = class(TXMLNode, IXMLDataChestType)
private
FData: IXMLDataTypeList;
protected
{ IXMLDataChestType }
function Get_Publisher: IXMLPublisherType;
function Get_Data: IXMLDataTypeList;
public
procedure AfterConstruction; override;
end;
{ TXMLPublisherType }
TXMLPublisherType = class(TXMLNode, IXMLPublisherType)
protected
{ IXMLPublisherType }
function Get_Info: UnicodeString;
function Get_Version: UnicodeString;
procedure Set_Info(Value: UnicodeString);
procedure Set_Version(Value: UnicodeString);
end;
{ TXMLDataType }
TXMLDataType = class(TXMLNode, IXMLDataType)
protected
{ IXMLDataType }
function Get_Name: UnicodeString;
function Get_Type_: UnicodeString;
function Get_Value: UnicodeString;
procedure Set_Name(Value: UnicodeString);
procedure Set_Type_(Value: UnicodeString);
procedure Set_Value(Value: UnicodeString);
end;
{ TXMLDataTypeList }
TXMLDataTypeList = class(TXMLNodeCollection, IXMLDataTypeList)
protected
{ IXMLDataTypeList }
function Add: IXMLDataType;
function Insert(const Index: integer): IXMLDataType;
function Get_Item(Index: integer): IXMLDataType;
end;
{
======================================
Global Functions
======================================
}
function ChestTypeToString(ChestType: Word): string;
function StringToChestType(S: string): Word;
function GetDataChest(Doc: IXMLDocument): IXMLDataChestType;
function LoadDataChest(const FileName: string): IXMLDataChestType;
function NewDataChest: IXMLDataChestType;
const
TargetNamespace = '';
{
======================================
ChestValue
======================================
}
var
Chest: TChest;
IntChest: TIntChest;
RealChest: TRealChest;
StringChest: TStringChest;
{
======================================
======================================
======================================
======================================
Implementation
======================================
======================================
======================================
}
implementation
{ TChest }
class function TChest.SelfCheck: Boolean;
begin
if (not TChest.Checked) then
begin
if not Assigned(TChest.List) then
begin
TChest.List := TList.Create;
Result := False;
end
else
Result := True;
TChest.Checked := True;
end
else
Result := True;
end;
class procedure TChest.Clear;
var
i: integer;
Item: Pointer;
begin
for i := 0 to TChest.List.Count - 1 do
begin
Item := TChest.List.Items[i];
TChestElement(Item^).Free;
FreeMem(Item, SizeOf(TChestElement));
end;
TChest.List.Clear;
end;
class procedure TChest._Delete(index: integer);
var
Item: Pointer;
begin
Item := TChest.List.Items[index];
TChestElement(Item^).Free;
FreeMem(Item, SizeOf(TChestElement));
TChest.List.Delete(index);
TChest.Sorted := False;
end;
class procedure TChest._Delete(index: string);
var
i: integer;
begin
if not TChest.Sorted then
TChest.SortByName;
if TChest.Find(index, i) then
TChest._Delete(i);
end;
class procedure TChest.DoFinalization;
begin
if Assigned(TChest.List) then
begin
TChest.Clear;
TChest.List.Free;
TChest.List := nil;
end;
TChest.Checked := False;
TChest.Sorted := False;
end;
class procedure TChest.DoInitialization;
begin
TChest.Checked := False;
TChest.Sorted := False;
TChest.SelfCheck;
TChest.Clear;
end;
class function TChest.Find(const S: string; var Index: integer): Boolean;
var
L, H, i, C: integer;
begin
Result := False;
L := 0;
H := TChest.List.Count - 1;
while L <= H do
begin
i := (L + H) shr 1;
C := CompareText(TChestElement(TChest.List.Items[i]^).Name, S);
if C < 0 then
L := i + 1
else
begin
H := i - 1;
if C = 0 then
begin
Result := True;
if Duplicates <> dupAccept then
L := i;
end;
end;
end;
Index := L;
end;
class function TChest.GetCount: integer;
begin
if Assigned(TChest.List) then
Result := TChest.List.Count
else
Result := 0;
end;
class function TChest.GetItem(index: integer): TChestElement;
begin
Result := TChestElement(TChest.List.Items[index]^);
end;
class function TChest.GetValue(index: string): Pointer;
var
i: integer;
begin
if not TChest.SelfCheck then
begin
Result := nil;
Exit;
end
else
begin
if not TChest.Sorted then
TChest.SortByName;
if TChest.Find(index, i) then
begin
Result := TChestElement(TChest.List.Items[i]^).Value;
Exit;
end
else
begin
Result := nil;
Exit;
end;
end;
end;
class function TChest.GetValueAndType(index: string;
var ChestType: Word): Pointer;
var
i: integer;
Item: Pointer;
begin
if not TChest.SelfCheck then
begin
ChestType := ChestTypeEmpty;
Result := nil;
Exit;
end
else
begin
if not TChest.Sorted then
TChest.SortByName;
if TChest.Find(index, i) then
begin
Item := TChest.List.Items[i];
ChestType := TChestElement(Item^).ChestType;
Result := TChestElement(Item^).Value;
Exit;
end
else
begin
Result := nil;
Exit;
end;
end;
end;
class procedure TChest.SetItem(index: integer; const Value: TChestElement);
begin
TChestElement((TChest.List.Items[index])^) := Value;
end;
class procedure TChest.SetValue(index: string; const Value: Pointer);
var
i: integer;
P: Pointer;
begin
TChest.SelfCheck;
if TChest.Find(index, i) then
begin
TChestElement((TChest.List.Items[i])^).Value := Value;
Exit;
end
else
begin
P := GetMemory(SizeOf(TChestElement));
TChestElement(P^) := TChestElement.Create;
TChestElement(P^).Name := index;
TChestElement(P^).Value := Value;
TChest.List.Add(P);
TChest.Sorted := False;
end;
end;
class procedure TChest.SetValueAndType(index: string; const Value: Pointer;
ChestType: Word = ChestTypeEmpty);
var
i: integer;
P, Item: Pointer;
begin
TChest.SelfCheck;
if TChest.Find(index, i) then
begin
Item := TChest.List.Items[i];
TChestElement(Item^).Value := Value;
TChestElement(Item^).ChestType := ChestType;
Exit;
end
else
begin
P := GetMemory(SizeOf(TChestElement));
TChestElement(P^) := TChestElement.Create;
TChestElement(P^).Name := index;
TChestElement(P^).Value := Value;
TChestElement(P^).ChestType := ChestType;
TChest.List.Add(P);
TChest.Sorted := False;
end;
end;
class procedure TChest.SortByName;
procedure QuickSort(iLo, iHi: integer);
var
Lo, Hi: integer;
Mid: string;
begin
Lo := iLo;
Hi := iHi;
Mid := TChestElement(TChest.List.Items[(Lo + Hi) div 2]^).Name;
repeat
while CompareText(TChestElement(TChest.List.Items[Lo]^).Name, Mid) < 0 do
Inc(Lo);
while CompareText(TChestElement(TChest.List.Items[Hi]^).Name, Mid) > 0 do
Dec(Hi);
if Lo <= Hi then
begin
TChest.List.Exchange(Lo, Hi);
Inc(Lo);
Dec(Hi);
end;
until Lo > Hi;
if Hi > iLo then
QuickSort(iLo, Hi);
if Lo < iHi then
QuickSort(Lo, iHi);
end;
begin
if not TChest.Sorted then
begin
if TChest.List.Count > 0 then
QuickSort(0, TChest.List.Count - 1);
TChest.Sorted := True;
end;
end;
{ TMemChest }
class procedure TMemChest.Delete(index: integer);
procedure SelfDelete(index, Size: integer);
begin
FreeMem(TChestElement((TMemChest.List.Items[index])^).Value, Size);
TChest._Delete(index);
end;
begin
case TChestElement((TChest.List.Items[index])^).ChestType of
ChestTypeInteger:
SelfDelete(index, SizeOf(integer));
ChestTypeReal:
SelfDelete(index, SizeOf(Real));
ChestTypeBoolean:
SelfDelete(index, SizeOf(Boolean));
ChestTypeString:
begin
TChestString(TChestElement((TChest.List.Items[index])^).Value^).Free;
SelfDelete(index, SizeOf(TChestString));
end;
ChestTypePointer:
SelfDelete(index, SizeOf(Pointer));
end;
TMemChest.TempList.Delete(index);
end;
class procedure TMemChest.Delete(index: string);
var
i: integer;
begin
if not TMemChest.Sorted then
TMemChest.SortByName;
if TMemChest.Find(index, i) then
TMemChest.Delete(i);
end;
class procedure TMemChest.FreeMemory;
var
i: integer;
begin
if Assigned(TMemChest.TempList) then
begin
for i := TMemChest.List.Count - 1 downto 0 do
begin
TMemChest.Delete(i);
end;
TMemChest.TempList.Clear;
TMemChest.TempList.Free;
TMemChest.TempList := nil;
end;
end;
class function TMemChest.GetXML: WideString;
var
XMLDataChest: IXMLDataChestType;
XMLData: IXMLDataType;
XML: WideString;
i: integer;
Item: TChestElement;
begin
XMLDataChest := NewDataChest;
for i := 0 to TMemChest.Count - 1 do
begin
Item := TChestElement(TMemChest.List.Items[i]^);
if (Item.Value <> nil) then
begin
XMLData := XMLDataChest.Data.Add;
XMLData.Name := Item.Name;
case Item.ChestType of
ChestTypeInteger:
begin
XMLData.Type_ := 'Integer';
XMLData.Value := IntToStr(integer(Item.Value^));
end;
ChestTypeBoolean:
begin
XMLData.Type_ := 'Boolean';
if (Boolean(Item.Value^)) then
XMLData.Value := 'True'
else
XMLData.Value := 'False';
end;
ChestTypeReal:
begin
XMLData.Type_ := 'Real';
XMLData.Value := FloatToStr(Real(Item.Value^));
end;
ChestTypeString:
begin
XMLData.Type_ := 'String';
XMLData.Value := TChestString(Item.Value^).Value;
end;
else
begin
XMLData.Type_ := 'Empty';
XMLData.Value := 'Invalid';
end;
end;
end;
end;
if TMemChest._Publisher = '' then
XMLDataChest.Publisher.Info :=
'DataChest,©黄锐,[email protected],http://hi.baidu.com/蝶晓梦'
else
XMLDataChest.Publisher.Info := TMemChest._Publisher;
XMLDataChest.Publisher.Version := '1.0';
XMLDataChest.OwnerDocument.Encoding := 'UTF-8';
XMLDataChest.OwnerDocument.SaveToXML(XML);
Result := XML;
end;
class procedure TMemChest.LoadFromEncryptFile(FileName: string);
var
StringStream: TStringStream;
Temp: WideString;
begin
Temp := '';
if (FileExists(FileName)) then
begin
StringStream := TStringStream.Create;
StringStream.LoadFromFile(FileName);
Temp := StringStream.ReadString(StringStream.Size);
StringStream.Free;
end;
TMemChest.XML := DecodeString(Temp);
end;
class procedure TMemChest.LoadFromFile(FileName: string);
begin
TMemChest.LoadFromEncryptFile(FileName);
end;
class procedure TMemChest.LoadFromXMLFile(FileName: string);
var
StringList: TStringList;
begin
StringList := TStringList.Create;
StringList.LoadFromFile(FileName, TEncoding.UTF8);
TMemChest.XML := StringList.Text;
StringList.Free;
end;
class procedure TMemChest.SaveToEncryptFile(FileName: string);
var
StringStream: TStringStream;
Temp: string;
begin
StringStream := TStringStream.Create;
Temp := EncodeString(TMemChest.XML);
StringStream.WriteString(Temp);
StringStream.SaveToFile(FileName);
StringStream.Free;
end;
class procedure TMemChest.SaveToFile(FileName: string);
begin
TMemChest.SaveToEncryptFile(FileName);
end;
class procedure TMemChest.SaveToXMLFile(FileName: string);
{
耗时操作
}
var
StringList: TStringList;
begin
StringList := TStringList.Create;
StringList.Text := FormatXMLData(TMemChest.XML);
StringList.SaveToFile(FileName, TEncoding.UTF8);
StringList.Free;
end;
class procedure TMemChest.SetXML(const Value: WideString);
var
XMLDataChest: IXMLDataChestType;
XMLData: IXMLDataType;
i: integer;
ChestType: Word;
Doc: IXMLDocument;
begin
Doc := NewXMLDocument;
Doc.LoadFromXML(Value);
XMLDataChest := GetDataChest(Doc);
for i := 0 to XMLDataChest.Data.Count - 1 do
begin
XMLData := XMLDataChest.Data.Items[i];
ChestType := StringToChestType(XMLData.Type_);
case ChestType of
ChestTypeInteger:
begin
TIntChest.AsString[XMLData.Name] := XMLData.Value;
end;
ChestTypeReal:
begin
TRealChest.AsString[XMLData.Name] := XMLData.Value;
end;
ChestTypeBoolean:
begin
TBooleanChest.AsString[XMLData.Name] := XMLData.Value;
end;
ChestTypeString:
begin
TStringChest.AsString[XMLData.Name] := XMLData.Value;
end;
end;
end;
end;
{ TIntChest }
class function TIntChest.GetIntValue(index: string): integer;
var
P: Pointer;
ChestType: Word;
begin
P := TIntChest.GetValueAndType(index, ChestType);
if (P <> nil) then
begin
if (ChestType = ChestTypeInteger) then
Result := integer(P^)
else
Result := 0;
end
else
begin
Result := 0;
end;
end;
class function TIntChest.GetStringValue(index: string): String;
begin
Result := IntToStr(TIntChest.GetIntValue(index));
end;
class procedure TIntChest.SetIntValue(index: string; const Value: integer);
var
P: ^integer;
begin
if not Assigned(TIntChest.TempList) then
TIntChest.TempList := TList.Create;
GetMem(P, SizeOf(integer));
TIntChest.TempList.Add(P);
P^ := Value;
TIntChest.SetValueAndType(index, P, ChestTypeInteger);
end;
class procedure TIntChest.SetStringValue(index: string; const Value: String);
begin
TIntChest.SetIntValue(index, StrToInt(Value));
end;
{ TRealChest }
class function TRealChest.GetIntValue(index: string): integer;
begin
Result := Round(TRealChest.GetRealValue(index));
end;
class function TRealChest.GetRealValue(index: string): Real;
var
P: Pointer;
ChestType: Word;
begin
P := TRealChest.GetValueAndType(index, ChestType);
if (P <> nil) then
begin
if (ChestType = ChestTypeReal) then
Result := Real(P^)
else
Result := 0.0;
end
else
begin
Result := 0.0;
end;
end;
class function TRealChest.GetStringValue(index: string): String;
begin
Result := FloatToStr(TRealChest.GetRealValue(index));
end;
class procedure TRealChest.SetIntValue(index: string; const Value: integer);
begin
TRealChest.SetRealValue(index, Value);
end;
class procedure TRealChest.SetRealValue(index: string; const Value: Real);
var
P: ^Real;
begin
if not Assigned(TRealChest.TempList) then
TRealChest.TempList := TList.Create;
GetMem(P, SizeOf(Real));
TRealChest.TempList.Add(P);
P^ := Value;
TRealChest.SetValueAndType(index, P, ChestTypeReal);
end;
class procedure TRealChest.SetStringValue(index: string; const Value: String);
begin
TRealChest.SetRealValue(index, StrToFloat(Value));
end;
{ TBooleanChest }
class function TBooleanChest.GetBooleanValue(index: string): Boolean;
var
P: Pointer;
ChestType: Word;
begin
P := TBooleanChest.GetValueAndType(index, ChestType);
if (P <> nil) then
begin
if (ChestType = ChestTypeBoolean) then
Result := Boolean(P^)
else
Result := False;
end
else
begin
Result := False;
end;
end;
class function TBooleanChest.GetIntValue(index: string): integer;
begin
if (TBooleanChest.GetBooleanValue(index)) then
Result := 1
else
Result := 0;
end;
class function TBooleanChest.GetRealValue(index: string): Real;
begin
if (TBooleanChest.GetBooleanValue(index)) then
Result := 1
else
Result := 0;