-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGS.Common.pas
More file actions
1135 lines (983 loc) · 25.1 KB
/
GS.Common.pas
File metadata and controls
1135 lines (983 loc) · 25.1 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
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
/// Title : GS.Common
/// Short Desc : Common code, no dependancy.
/// Source : https://github.com/VincentGsell
///
/// History
/// 20180101 - VGS - Create
/// 20190301 - VGS - Generic condition compilation (FPC Link problem).
unit GS.Common;
interface
{$I GSCore.inc}
Uses Classes, SysUtils,
{$IFDEF USE_GENERIC}
Generics.Collections,
{$ENDIF}
SyncObjs
;
Const
{$I GSCoreConst.inc}
CST_BUSTIMER = 250; //MilliSec.
CST_DEFAULT_WILDCARD = '\';
//Data repo feature extention,
CST_DATAREPONAME_PREFIX = 'Sys.';
CST_DATAREPONAME_SUFFIX = '.DataRepo';
CST_ARRAY_INIT_QTE = 10;
CST_DEFAULT_CYPH_KEY = 'efsdsadjpodjlkkassdsasrjhedfjhdbfsaiaweibw_WDcsew1212'+
'aweeosdnhehfalhgwmduzt1 .v 9^32e1cacjfalcurnclyaucal'+
'2384ehq b 8bz 2q*x"*c*&%t has gf3qfauw xq33rt waza2q'+
'sugc gf8f2qr lbc8 29KZGZRNCQ43ZN2CAN. eniwuz45835n4'+
'erraqaqi32zremmrkqncddqnkjdgfendshgflerkj<sjagfkudkzq'+
'zgfesibfnlir197g9332290 s 329em*%Refcq*Rfysnfcaw.3oiv'+
'lwsau4zreo nfli43emecue239brul.infawdhtnwufsu.nlfoiew'+
'me9854z842924986ttrvsger257vskjgiu*%e*asdwdjlwqkjlkej';
{$IFDEF FPC}
const
INFINITE = Cardinal($FFFFFFFF);
{$ENDIF}
Type
TKeyValueStringItem = Packed Record
Key : UTF8String;
Value : TObject;
End;
TStringItemArray = array of TKeyValueStringItem;
TUTF8StringArray = array of UTF8String;
//Object list implementation for non generic usage.
TList_ObjectArray = class
private
protected
FArray : Array of TObject;
FIndex : UInt32;
FInitialized : Boolean;
FOwned : Boolean;
Procedure ManagedAdd(aObject : TObject); Virtual;
Procedure ManagedSet(Index : Uint32; aObject : TObject); Virtual;
function GetCount: Uint32;
function GetItem(Index: Uint32): TObject; virtual;
procedure SetItem(Index: Uint32; const Value: TObject); virtual;
public
constructor Create(Const Owned : Boolean = false); Reintroduce; Virtual;
Destructor Destroy; Override;
Procedure Clear; Virtual;
Procedure Delete(Index : Uint32);
Procedure Remove(aObject : TObject);
procedure Add(aObject : TObject);
Function IndexOf(aObject : TObject) : Int32;
Property Owned : Boolean read FOwned Write FOwned;
Property Count : Uint32 read GetCount;
Property Items[Index : Uint32] : TObject read GetItem Write SetItem; default;
end;
//Key Values (basic replacement for Generic Dictionnary key paired String/Object, binSearch support.
TKeysValues_UTF8StringToObject = Class
private
protected
FThreadProtector : TCriticalSection;
FArray : TStringItemArray;
FIndex : UInt32;
FInitialized : Boolean;
FSorted: Boolean;
FOwned : Boolean;
function GetItem(Index: UInt32): TKeyValueStringItem;
procedure SetItem(Index: UInt32; const Value: TKeyValueStringItem);
function GetCount: Uint32;
procedure InternalClearObject;
public
Constructor Create(Const Owned : Boolean = false); Reintroduce; Virtual;
Destructor Destroy; Override;
Function TryGetValue(aKey : String; var aResult : TObject) : Boolean; virtual;
Procedure Add(aKey : String; aObject : TObject); virtual;
Procedure Remove(Index : UInt32); Overload;
Procedure Remove(aKey : String); Overload;
Procedure Clear; Virtual;
procedure lock;
procedure unlock;
Property Count : Uint32 read GetCount;
Property Items[Index : UInt32] : TKeyValueStringItem read GetItem Write SetItem; default;
end;
TList_UTF8String = Class
Private
FArray : TUTF8StringArray;
FIndex : UInt32;
FInitialized : Boolean;
FSorted: Boolean;
function GetUTF8String(Index: Uint32): UTF8String;
function GetUTF8StringCount: Uint32;
procedure SetUTF8String(Index: Uint32; const Value: UTF8String);
Public
constructor Create(const Sorted : Boolean = True); Reintroduce;
Procedure Add(aString : UTF8String);
Procedure Clear;
Property Items[Index : Uint32] : UTF8String read GetUTF8String Write SetUTF8String; default;
Property Count : Uint32 read GetUTF8StringCount;
Property Sorted : Boolean read FSorted Write FSorted;
end;
IGSStringList = interface
procedure add(aString : String);
function count : uint32;
procedure clear;
function lines(index : uint32) : string; overload;
procedure lines(index : uint32; value : string); overload;
function text : string;
procedure setText(aTxt : string);
procedure saveToFile(afileName : string);
procedure loadFromFile(aFileName : string);
function ValueFromIndex(index : integer) : String;
function Values(key : string) : String;
function Names(index : integer) : String;
function indexOf(astring : string) : Integer;
procedure insert(index : integer; value : String);
function ToStringArray : TArray<String>;
procedure SetDelimiter(aDelimiter : char);
procedure SetDelimitedText(aText : String);
function DelimitedText : string;
procedure setTrailinglineBreak(value : Boolean);
procedure setEncoding(value : TEncoding); //Convert. Fix it on start. Default : UTF8.
end;
IGSThreadSafeList = interface
procedure lock;
procedure unlock;
end;
TGSStringList = class(TInterfacedObject,IGSStringList)
private
FST : TStringList;
public
//IGSStringList
procedure add(aString : String);
function count : uint32;
procedure clear;
function lines(index : uint32) : string; overload;
procedure lines(index : uint32; value : string); overload;
function text : string;
procedure setText(aTxt : string);
procedure saveToFile(afileName : string);
procedure loadFromFile(aFileName : string);
function ValueFromIndex(index : integer) : String;
function Values(key : string) : String;
function Names(index : integer) : String;
function indexOf(astring : string) : Integer;
procedure insert(index : integer; value : String);
function ToStringArray : TArray<String>;
procedure SetDelimiter(aDelimiter : char);
procedure SetDelimitedText(aText : String);
function DelimitedText : string;
procedure setTrailinglineBreak(value : Boolean); //True by default in EMB StringList.
procedure setEncoding(value : TEncoding);
Constructor Create; virtual;
Destructor Destroy; override;
End;
TGSStringListWithThreadAutoLock = class(TInterfacedObject,IGSStringList, IGSThreadSafeList)
private
FCS : TCriticalSection;
FST : TStringList;
//IGSThreadSafeList
procedure lock;
procedure unlock;
Public
//IGSStringList
procedure add(aString : String);
function count : uint32;
procedure clear;
function lines(index : uint32) : string; overload;
procedure lines(index : uint32; value : string); overload;
function text : string;
procedure setText(aTxt : string);
procedure saveToFile(afileName : string);
procedure loadFromFile(aFileName : string);
function Values(key : string) : String;
function ValueFromIndex(index : integer) : String;
function Names(index : integer) : String;
function indexOf(astring : string) : Integer;
procedure insert(index : integer; value : String);
function ToStringArray : TArray<String>;
procedure SetDelimiter(aDelimiter : char);
procedure SetDelimitedText(aText : String);
function DelimitedText : string;
procedure setTrailinglineBreak(value : Boolean);
procedure setEncoding(value : TEncoding);
Constructor Create; virtual;
Destructor Destroy; override;
end;
TGSFactory = Class
Class function GetStringList_ThreadSafeAuto : IGSStringList;
class function GetStringList : IGSStringList;
End;
{$IFDEF USE_GENERIC}
TObjectDictionnaryWithSequentialMode = class(TObjectDictionary<String,TObject>)
private
public
// Property Items[Index : UInt32] : TObject read GetSeqItem Write SetSeqItem;
end;
TObjectDictionary_StringStream = TObjectDictionary<String,TStream>;
TObjectDictionary_StringObject = TObjectDictionary<String,TObject>;
{$ELSE}
TObjectDictionary_StringStream = Class(TKeysValues_UTF8StringToObject)
private
protected
public
Function TryGetValue(aKey : String; var aResult : TStream) : Boolean; Reintroduce;
Procedure Add(aString : String; aStream : TStream); Reintroduce;
End;
TObjectDictionary_StringObject = Class(TKeysValues_UTF8StringToObject)
private
protected
public
end;
{$ENDIF}
Procedure StreamEncrypt(aStreamToEncrypt : TMemoryStream);
Procedure StreamDecrypt(aStreamToDecrypt : TMemoryStream);
procedure QuickSort(var aArray: TStringItemArray; Start, Stop: Integer);
function BinSearch(aArray: TStringItemArray; aKey: UTF8String; Const Stop: Integer = -1): Integer;
var GLB_OTP_FILE : String; //One-time pad key file (https://en.wikipedia.org/wiki/One-time_pad) -> Should be localized in a secured dir/file.
implementation
function sencrypt_XorCipher(const Key, Source: TBytes): TBytes;
var
I: Integer;
begin
if Length(Key) = 0 then
Exit(Source);
SetLength(Result, Length(Source));
for I := Low(Source) to High(Source) do
Result[I] := Key[I mod Length(Key)] xor Source[I];
end;
procedure BetterThanNothingEncryptDecrypt(aStreamToEncrypt : TMemoryStream);
var b : TMemoryStream;
Key,r : TBytes;
function GetOTPfile : TBytes;
var c : TMemoryStream;
begin
if FileExists(GLB_OTP_FILE) then
begin
c := TMemoryStream.Create;
try
c.LoadFromFile(GLB_OTP_FILE);
SetLEngth(result,c.Size);
c.Read(result,c.Size);
finally
FreeAndNil(c);
end;
end
else
begin
Result := BytesOf(CST_DEFAULT_CYPH_KEY); //-/
end;
end;
begin
b := TMemoryStream.Create;
try
b.LoadFromStream(aStreamToEncrypt);
key := GetOTPFile;
SetLength(r,b.Size);
b.Read(r,b.Size);
r := sencrypt_XorCipher(Key,r);
b.Clear;
aStreamToEncrypt.Clear;
aStreamToEncrypt.Write(r,length(r));
finally
FreeAndNil(b);
end;
end;
{$IFDEF LIB_CIPHERS_ENABLED}
//uses ...; Here a nice ciphers lib.
{$ENDIF}
Procedure StreamEncrypt(aStreamToEncrypt : TMemoryStream);
{$IFDEF LIB_CIPHERS_ENABLED}
{$ENDIF}
begin
{$IFDEF LIB_CIPHERS_ENABLED}
raise Exception.Create('To implement');
{$ELSE}
BetterThanNothingEncryptDecrypt(aStreamToEncrypt);
{$ENDIF}
end;
Procedure StreamDecrypt(aStreamToDecrypt : TMemoryStream);
{$IFDEF LIB_CIPHERS_ENABLED}
{$ENDIF}
begin
{$IFDEF LIB_CIPHERS_ENABLED}
raise Exception.Create('To implement');
{$ELSE}
BetterThanNothingEncryptDecrypt(aStreamToDecrypt);
{$ENDIF}
end;
{--------------------------------------------------------------------}
//Start is the index of the first item of the array - usually 0
//Stop is the index of the last item of the array
procedure QuickSort(var aArray: TStringItemArray; Start, Stop: Integer);
var
Left: Integer;
Right: Integer;
Mid: Integer;
Pivot: UTF8String;
Temp: TKeyValueStringItem;
begin
Left := Start;
Right := Stop;
Mid := (Start + Stop) div 2;
Pivot := aArray[mid].Key;
repeat
while aArray[Left].Key < Pivot do Inc(Left);
while Pivot < aArray[Right].Key do Dec(Right);
if Left <= Right then
begin
Temp := aArray[Left];
aArray[Left] := aArray[Right]; // Swops the two Strings
aArray[Right] := Temp;
Inc(Left);
Dec(Right);
end;
until Left > Right;
if Start < Right then QuickSort(aArray, Start, Right); // Uses
if Left < Stop then QuickSort(aArray, Left, Stop); // Recursion
end;
procedure QuickSortStringArray(var aArray: TUTF8StringArray; Start, Stop: Integer);
var
Left: Integer;
Right: Integer;
Mid: Integer;
Pivot: UTF8String;
Temp: UTF8String;
begin
Left := Start;
Right := Stop;
Mid := (Start + Stop) div 2;
Pivot := aArray[mid];
repeat
while aArray[Left] < Pivot do Inc(Left);
while Pivot < aArray[Right] do Dec(Right);
if Left <= Right then
begin
Temp := aArray[Left];
aArray[Left] := aArray[Right]; // Swops the two Strings
aArray[Right] := Temp;
Inc(Left);
Dec(Right);
end;
until Left > Right;
if Start < Right then QuickSortStringArray(aArray, Start, Right); // Uses
if Left < Stop then QuickSortStringArray(aArray, Left, Stop); // Recursion
end;
{--------------------------------------------------------------------}
function BinSearch(aArray: TStringItemArray; aKey: UTF8String; Const Stop: Integer = -1): Integer;
var
First: Integer;
Last: Integer;
Pivot: Integer;
Found: Boolean;
begin
First := Low(aArray); //Sets the first item of the range
Last := Stop; //Sets the last item of the range
Found := False; //Initializes the Found flag (Not found yet)
Result := -1; //Initializes the Result
//If First > Last then the searched item doesn't exist
//If the item is found the loop will stop
while (First <= Last) and (not Found) do
begin
//Gets the middle of the selected range
Pivot := (First + Last) div 2;
//Compares the String in the middle with the searched one
if aArray[Pivot].Key = aKey then
begin
Found := True;
Result := Pivot;
end
//If the Item in the middle has a bigger value than
//the searched item, then select the first half
else if aArray[Pivot].Key > aKey then
Last := Pivot - 1
//else select the second half
else
First := Pivot + 1;
end;
end;
{ TList_ObjectArray }
procedure TList_ObjectArray.Add(aObject: TObject);
begin
ManagedAdd(aObject);
end;
procedure TList_ObjectArray.Clear;
var i : Integer;
begin
if FOwned then
begin
for I := 0 to Count-1 do
TObject(FArray[i]).Free;
end;
FArray := Nil;
SetLength(Farray,CST_ARRAY_INIT_QTE);
FInitialized := False;
end;
constructor TList_ObjectArray.Create(const Owned: Boolean);
begin
SetLength(Farray,CST_ARRAY_INIT_QTE);
FInitialized := False;
FOwned := Owned;
end;
procedure TList_ObjectArray.Delete(Index: Uint32);
var
ALength: Uint32;
i: Cardinal;
begin
ALength := Length(FArray);
Assert(ALength > 0);
Assert(Index < ALength);
if FOwned then
FArray[Index].Free;
for i := Index + 1 to ALength - 1 do
FArray[i - 1] := FArray[i];
SetLength(FArray, ALength - 1);
Dec(FIndex);
end;
destructor TList_ObjectArray.Destroy;
begin
Clear;
inherited;
end;
function TList_ObjectArray.GetCount: Uint32;
begin
result := FIndex;
end;
function TList_ObjectArray.GetItem(Index: Uint32): TObject;
begin
assert(Index<Count);
result := FArray[Index];
end;
function TList_ObjectArray.IndexOf(aObject: TObject): Int32;
var i : Int32;
begin
result := -1;
for I := 0 to FIndex do
begin
if FArray[i] = aObject then
begin
result := i;
break;
end;
end;
end;
procedure TList_ObjectArray.ManagedAdd(aObject: TObject);
begin
Assert(Assigned(aObject));
if FInitialized then
begin
if Int32(FIndex) = Length(FArray) then
begin
SetLength(FArray,(Length(FArray)+1)*2);
end;
FArray[FIndex] := aObject;
end
else
begin
FInitialized := true;
FIndex := 0;
FArray[FIndex] := aObject;
end;
Inc(FIndex);
end;
procedure TList_ObjectArray.ManagedSet(Index : Uint32; aObject: TObject);
var i : UInt32;
begin
Assert(assigned(aObject));
{$IFDEF DEBUG}
for I := 0 to FIndex do
begin
if FArray[i] = aObject then
raise Exception.Create('TList_ObjectArray : Duplicate item (Pos'+IntToStr(i)+')');
end;
{$ENDIF}
FArray[Index] := aObject;
end;
procedure TList_ObjectArray.Remove(aObject : TObject);
var i : integer;
begin
i := IndexOf(aObject);
if i>-1 then
Delete(i);
end;
procedure TList_ObjectArray.SetItem(Index: Uint32; const Value: TObject);
begin
ManagedSet(Index,Value);
end;
{ TList_UTF8String }
procedure TList_UTF8String.Add(aString: UTF8String);
begin
if FInitialized then
begin
if Int32(FIndex) = Length(FArray) then
begin
SetLength(FArray,Length(FArray)*2);
end;
FArray[FIndex] := aString;
end
else
begin
FInitialized := true;
FIndex := 0;
FArray[FIndex] := aString;
end;
Inc(FIndex);
if FSorted then
QuickSortStringArray(FArray,0,FIndex-1);
end;
procedure TList_UTF8String.Clear;
begin
FArray := nil;
SetLength(FArray,CST_ARRAY_INIT_QTE);
FIndex := 0;
FInitialized := False;
end;
constructor TList_UTF8String.Create(const Sorted : Boolean);
begin
Inherited Create;
FSorted := Sorted;
Clear;
end;
function TList_UTF8String.GetUTF8String(Index: Uint32): UTF8String;
begin
assert(Index<Count);
result := FArray[Index];
end;
function TList_UTF8String.GetUTF8StringCount: Uint32;
begin
result := FIndex;
end;
procedure TList_UTF8String.SetUTF8String(Index: Uint32;
const Value: UTF8String);
begin
assert(Index<Count);
FArray[Index] := Value;
end;
{ TKeysValues_UTF8StringToObject }
procedure TKeysValues_UTF8StringToObject.Add(aKey: String; aObject: TObject);
begin
if FInitialized then
begin
if Int32(FIndex) = Length(FArray) then
begin
SetLength(FArray,Length(FArray)*2);
end;
FArray[FIndex].Key := UTF8String(aKey);
FArray[FIndex].Value := aObject;
end
else
begin
FInitialized := true;
FIndex := 0;
FArray[FIndex].Key := UTF8String(aKey);
FArray[FIndex].Value := aObject;
end;
Inc(FIndex);
QuickSort(FArray,0,FIndex-1);
end;
procedure TKeysValues_UTF8StringToObject.Clear;
begin
InternalClearObject;
FArray := nil;
SetLength(FArray,CST_ARRAY_INIT_QTE);
FIndex := 0;
FInitialized := False;
end;
constructor TKeysValues_UTF8StringToObject.Create(Const Owned : Boolean = false);
begin
Inherited Create;
FOwned := Owned;
FThreadProtector := TCriticalSection.Create;
Clear;
end;
destructor TKeysValues_UTF8StringToObject.Destroy;
begin
InternalClearObject;
FThreadProtector.Free;
inherited;
end;
function TKeysValues_UTF8StringToObject.GetCount: Uint32;
begin
result := FIndex;
end;
function TKeysValues_UTF8StringToObject.GetItem(
Index: UInt32): TKeyValueStringItem;
begin
assert(Index<Count);
result := FArray[Index];
end;
procedure TKeysValues_UTF8StringToObject.InternalClearObject;
var i : integer;
begin
if FOwned then
if FIndex>0 then
for i := FIndex-1 downto 0 do
FArray[i].Value.Free;
end;
procedure TKeysValues_UTF8StringToObject.lock;
begin
FThreadProtector.Enter;
end;
procedure TKeysValues_UTF8StringToObject.Remove(aKey: String);
var l : int32;
begin
l := BinSearch(FArray,UTF8String(aKey),FIndex);
if l>-1 then
Remove(l);
end;
procedure TKeysValues_UTF8StringToObject.Remove(Index: UInt32);
var llength : UInt32;
i : integer;
begin
llength := Length(FArray);
if llength = 0 then
exit;
if Index<llength then
begin
if FOwned then
Items[Index].Value.Free;
for i := Index + 1 to lLength - 1 do
FArray[i - 1] := FArray[i];
SetLength(FArray, lLength - 1);
Dec(FIndex);
end;
end;
procedure TKeysValues_UTF8StringToObject.SetItem(Index: UInt32;
const Value: TKeyValueStringItem);
begin
assert(Index<Count);
FArray[Index] := Value;
end;
function TKeysValues_UTF8StringToObject.TryGetValue(aKey: String;
var aResult: TObject): Boolean;
var l : int32;
begin
aResult := nil;
l := BinSearch(FArray,UTF8String(aKey),FIndex-1);
Result := l>-1;
if result then
aResult := FArray[l].Value;
end;
procedure TKeysValues_UTF8StringToObject.unlock;
begin
FThreadProtector.Leave;
end;
{$IFNDEF USE_GENERIC}
{ TObjectDictionary_StringStream }
procedure TObjectDictionary_StringStream.Add(aString: String; aStream: TStream);
begin
Assert(assigned(aStream));
Inherited Add(aString, aStream);
end;
function TObjectDictionary_StringStream.TryGetValue(aKey: String;
var aResult: TStream): Boolean;
begin
Result := Inherited TryGetValue(aKey,TObject(aResult));
end;
{$ENDIF}
{ TGSStringListWithThreadAutoLock }
procedure TGSStringListWithThreadAutoLock.add(aString: String);
begin
lock;
try
FST.Add(aString);
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.clear;
begin
lock;
try
FST.Clear;
finally
unlock;
end;
end;
function TGSStringListWithThreadAutoLock.count: uint32;
begin
lock;
try
result := FST.Count;
finally
unlock;
end;
end;
constructor TGSStringListWithThreadAutoLock.Create;
begin
Inherited Create;
FCS := TCriticalSection.Create;
FST := TStringList.Create;
setEncoding(TEncoding.UTF8);
end;
function TGSStringListWithThreadAutoLock.DelimitedText: string;
begin
lock;
try
result := FST.DelimitedText;
finally
unlock;
end;
end;
destructor TGSStringListWithThreadAutoLock.Destroy;
begin
FreeAndNil(FCS);
FreeAndNil(FST);
inherited;
end;
function TGSStringListWithThreadAutoLock.indexOf(astring: string): Integer;
begin
lock;
try
result := FST.IndexOf(astring);
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.insert(index: integer; value: String);
begin
lock;
try
FST.Insert(index,value);
finally
unlock;
end;
end;
function TGSStringListWithThreadAutoLock.lines(index: uint32): string;
begin
lock;
try
result := FST[index];
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.lines(index: uint32; value: string);
begin
lock;
try
FST[index] := value;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.loadFromFile(aFileName: string);
begin
lock;
try
FST.LoadFromFile(aFileName);
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.lock;
begin
FCS.Acquire;
end;
function TGSStringListWithThreadAutoLock.Names(index: integer): String;
begin
lock;
try
result := FST.Names[index];
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.saveToFile(afileName: string);
begin
lock;
try
FST.SaveToFile(aFileName);
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.SetDelimitedText(aText: String);
begin
lock;
try
FST.DelimitedText := aText;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.SetDelimiter(aDelimiter: char);
begin
lock;
try
FST.Delimiter := aDelimiter;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.setEncoding(value: TEncoding);
begin
lock;
try
FST.DefaultEncoding := value;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.setText(aTxt: string);
begin
lock;
try
FST.Text := aTxt;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.setTrailinglineBreak(value: Boolean);
begin
lock;
try
FST.TrailingLineBreak := value;
finally
unlock;
end;
end;
function TGSStringListWithThreadAutoLock.text: string;
begin
lock;
try
result := FST.Text;
finally
unlock;
end;
end;
function TGSStringListWithThreadAutoLock.ToStringArray: TArray<String>;
var
i: Integer;
begin
lock;
try
begin
SetLength(Result,FST.Count);
for i := 0 to FST.Count-1 do
result[i] := FST[i];
end;
finally
unlock;
end;
end;
procedure TGSStringListWithThreadAutoLock.unlock;
begin
FCS.Leave;
end;
function TGSStringListWithThreadAutoLock.ValueFromIndex(index: integer): String;
begin
lock;
try
result := FST.ValueFromIndex[index];
finally
unlock;
end;
end;
function TGSStringListWithThreadAutoLock.Values(key: string): String;
begin
lock;
try
result := FST.Values[key];
finally
unlock;
end;