-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExplicitStringLists.inc
More file actions
3029 lines (2662 loc) · 101 KB
/
Copy pathExplicitStringLists.inc
File metadata and controls
3029 lines (2662 loc) · 101 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
{-------------------------------------------------------------------------------
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/.
-------------------------------------------------------------------------------}
{===============================================================================
Explicit string lists - main template
Implementation template for all explicit string lists and delimited text
parsers.
Version 1.1.3 (2024-05-05)
Last change 2026-02-26
©2017-2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/ExplicitStringLists
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* BinaryStreamingLite - github.com/TheLazyTomcat/Lib.BinaryStreamingLite
ListSorters - github.com/TheLazyTomcat/Lib.ListSorters
MemoryBuffer - github.com/TheLazyTomcat/Lib.MemoryBuffer
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
Library AuxExceptions is required only when rebasing local exception classes
(see symbol ExplicitStringLists_UseAuxExceptions for details).
BinaryStreamingLite can be replaced by full BinaryStreaming.
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
{!tun_end!} // ignore this line
{$IFDEF ESL_ClassTypes}
{===============================================================================
--------------------------------------------------------------------------------
TESLClassType
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TESLClassType - class types
===============================================================================}
// do not put "Type" here!
TESLListItem = record
Str: TESLStringType;
Obj: TObject;
UserData: PtrInt;
Changed: Boolean;
end;
{$ENDIF ESL_ClassTypes}
{-------------------------------------------------------------------------------
================================================================================
********************************************************************************
********************************************************************************
================================================================================
-------------------------------------------------------------------------------}
{$IFDEF ESL_ClassDeclaration}
{===============================================================================
--------------------------------------------------------------------------------
TESLClassType
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TESLClassType - class declaration
===============================================================================}
protected
fItems: array of TESLListItem;
fNameValueSeparator: TESLCharType;
fLineBreak: TESLStringType;
fDelimiter: TESLCharType;
fQuoteChar: TESLCharType;
// custom sorting
fCompareFuncIndex: TESLSortCompareIndexType;
fCompareFuncString: TESLSortCompareStringType;
// IO events
fOnItemBinarySaveCallback: TESLItemBinaryIOCallbackType;
fOnItemBinarySaveEvent: TESLItemBinaryIOEventType;
fOnItemBinaryLoadCallback: TESLItemBinaryIOCallbackType;
fOnItemBinaryLoadEvent: TESLItemBinaryIOEventType;
// getters, setters
Function GetItem(Index: Integer): TESLListItem; virtual;
Function GetString(Index: Integer): TESLStringType; virtual;
procedure SetString(Index: Integer; const Value: TESLStringType); virtual;
Function GetObject(Index: Integer): TObject; override;
procedure SetObject(Index: Integer; Value: TObject); override;
Function GetItemUserData(Index: Integer): PtrInt; override;
procedure SetItemUserData(Index: Integer; Value: PtrInt); override;
Function GetChanged(Index: Integer): Boolean; override;
procedure SetChanged(Index: Integer; Value: Boolean); override;
Function GetDefString(Index: Integer): String; override;
procedure SetDefString(Index: Integer; const Value: String); override;
Function GetName(Index: Integer): TESLStringType; virtual;
procedure SetName(Index: Integer; const Value: TESLStringType); virtual;
Function GetValue(const Name: TESLStringType): TESLStringType; virtual;
procedure SetValue(const Name: TESLStringType; const Value: TESLStringType); virtual;
Function GetValueFromIndex(Index: Integer): TESLStringType; virtual;
procedure SetValueFromIndex(Index: Integer; const Value: TESLStringType); virtual;
Function GetLineBreak: TESLStringType; virtual;
procedure SetLineBreak(const Value: TESLStringType); virtual;
Function GetLineBreakStyle: TESLLineBreakStyle; override;
procedure SetLineBreakStyle(Value: TESLLineBreakStyle); override;
Function GetLongStrText: TESLLongStringType; virtual;
procedure SetLongStrText(const Value: TESLLongStringType); virtual;
Function GetLongDelimitedText: TESLLongStringType; virtual;
procedure SetLongDelimitedText(const Value: TESLLongStringType); virtual;
Function GetLongCommaText: TESLLongStringType; virtual;
procedure SetLongCommaText(const Value: TESLLongStringType); virtual;
Function GetStrText: TESLStringType; virtual;
procedure SetStrText(const Value: TESLStringType); virtual;
Function GetDelimitedText: TESLStringType; virtual;
procedure SetDelimitedText(const Value: TESLStringType); virtual;
Function GetCommaText: TESLStringType; virtual;
procedure SetCommaText(const Value: TESLStringType); virtual;
// inherited list methods
Function GetCapacity: Integer; override;
// list manipulation methods
procedure SetArrayLength(NewLen: Integer); override;
procedure ClearArrayItem(Index: Integer; CanFreeObj: Boolean = True); override;
// initialization, finalization methods
procedure Initialize; override;
// auxiliary methods
class Function IsBreakChar(C: TESLCharType; IncludeZero: Boolean = True): Boolean; virtual;
class Function PosOfChar(const Str: TESLStringType; C: TESLCharType): TStrSize; virtual;
class Function StrBufferSize(Buffer: Pointer): TMemSize; override;
class Function CharSize: TMemSize; override;
Function ExtractNameStr(const Str: TESLStringType): TESLStringType; virtual;
Function ExtractValueStr(const Str: TESLStringType): TESLStringType; virtual;
Function ExtractNameAndValueStr(const Str: TESLStringType; out Name,Value: TESLStringType): Boolean; virtual;
Function IndexForSortedAddition(const Str: TESLStringType): Integer; virtual;
Function CompareStrings(const Str1, Str2: TESLStringType): Integer; overload; virtual;
Function SortCompare(Idx1,Idx2: Integer): Integer; override;
Function GetWriteLength: TStrSize; override;
procedure WriteItemToStream(Stream: TStream; Index: Integer; Endianness: TESLStringEndianness); override;
procedure WriteLineBreakToStream(Stream: TStream; Endianness: TESLStringEndianness); override;
procedure WriteBOMToStream(Stream: TStream; Endianness: TESLStringEndianness); override;
// internal methods
Function InternalExtract(Index: Integer): TObject; override;
public
// utility methods
class Function ConvertFromString(const Str: String): TESLStringType; virtual;
class Function ConvertToString(const Str: TESLStringType): String; virtual;
class Function CompareStrings(const Str1, Str2: TESLStringType; CaseSensitive: Boolean): Integer; overload; virtual;
// list index methods
Function LowIndex: Integer; override;
Function First: TESLStringType; virtual;
Function Last: TESLStringType; virtual;
// list items methods
Function IndexOf(const Str: TESLStringType): Integer; virtual;
Function IndexOfDefString(const Str: String): Integer; override;
Function IndexOfObject(Obj: TObject): Integer; override;
Function IndexOfUserData(UserData: PtrInt): Integer; override;
Function IndexOfName(const Name: TESLStringType): Integer; virtual;
Function IndexOfValue(const Value: TESLStringType): Integer; virtual;
Function Find(const Str: TESLStringType; out Index: Integer): Boolean; virtual;
Function FindDefString(const Str: String; out Index: Integer): Boolean; override;
Function FindObject(Obj: TObject; out Index: Integer): Boolean; override;
Function FindUserData(UserData: PtrInt; out Index: Integer): Boolean; override;
Function FindName(const Name: TESLStringType; out Index: Integer): Boolean; virtual;
Function FindValue(const Value: TESLStringType; out Index: Integer): Boolean; virtual;
Function Add(const Str: TESLStringType): Integer; virtual;
Function AddObject(const Str: TESLStringType; Obj: TObject): Integer; virtual;
Function AddDefString(const Str: String): Integer; override;
Function AddDefStringObject(const Str: String; Obj: TObject): Integer; override;
Function AddNameValuePair(const Name,Value: TESLStringType): Integer; virtual;
Function AddNameValuePairObject(const Name,Value: TESLStringType; Obj: TObject): Integer; virtual;
procedure AddStrings(Strings: TESLClassType); overload; virtual; // note, cannot use actual class
procedure AddStrings(Strings: array of TESLStringType); overload; virtual;
procedure Append(const Str: TESLStringType); virtual;
procedure AppendObject(const Str: TESLStringType; Obj: TObject); virtual;
procedure AppendDefString(const Str: String); override;
procedure AppendDefStringObject(const Str: String; Obj: TObject); override;
procedure AppendNameValuePair(const Name,Value: TESLStringType); virtual;
procedure AppendNameValuePairObject(const Name,Value: TESLStringType; Obj: TObject); virtual;
procedure AppendStrings(Strings: TESLClassType); overload; virtual;
procedure AppendStrings(Strings: array of TESLStringType); overload; virtual;
procedure Insert(Index: Integer; const Str: TESLStringType); virtual;
procedure InsertObject(Index: Integer; const Str: TESLStringType; Obj: TObject); virtual;
procedure InsertDefString(Index: Integer; const Str: String); override;
procedure InsertDefStringObject(Index: Integer; const Str: String; Obj: TObject); override;
procedure InsertNameValuePair(Index: Integer; const Name,Value: TESLStringType); virtual;
procedure InsertNameValuePairObject(Index: Integer; const Name,Value: TESLStringType; Obj: TObject); virtual;
procedure Move(SrcIdx,DstIdx: Integer); override;
procedure Exchange(Idx1,Idx2: Integer); override;
Function Extract(const Str: TESLStringType): TObject; virtual;
Function ExtractDefString(const Str: String): TObject; override;
Function ExtractObject(Obj: TObject): TObject; override;
Function ExtractUserData(UserData: PtrInt): TObject; override;
Function ExtractName(const Name: TESLStringType): TObject; virtual;
Function ExtractValue(const Value: TESLStringType): TObject; virtual;
Function Remove(const Str: TESLStringType): Integer; virtual;
Function RemoveDefString(const Str: String): Integer; override;
Function RemoveObject(Obj: TObject): Integer; override;
Function RemoveUserData(UserData: PtrInt): Integer; override;
Function RemoveName(const Name: TESLStringType): Integer; virtual;
Function RemoveValue(const Value: TESLStringType): Integer; virtual;
procedure Delete(Index: Integer); override;
// list manipulation methods
procedure Sort(Reversed: Boolean = False); override;
procedure CustomSort(CompareFunc: TESLSortCompareIndexType; Reversed: Boolean = False); overload; virtual;
procedure CustomSort(CompareFunc: TESLSortCompareStringType; Reversed: Boolean = False); overload; virtual;
Function Equals(Strings: TESLClassType): Boolean; reintroduce; virtual;
// list assignment methods
procedure SetStrings(Strings: TESLClassType); overload; virtual;
procedure SetStrings(Strings: array of TESLStringType); overload; virtual;
procedure SetDefStrings(Strings: TStrings); override;
procedure SetDefStrings(Strings: array of String); override;
procedure Assign(Source: TESLClassType); overload; virtual;
procedure Assign(Source: TStrings); override;
procedure AssignTo(Destination: TESLClassType); overload; virtual;
procedure AssignTo(Destination: TStrings); override;
// streaming methods
procedure LoadFromStream(Stream: TStream; out Endianness: TESLStringEndianness); override;
procedure BinarySaveToStream(Stream: TStream); override;
procedure BinaryLoadFromStream(Stream: TStream); override;
// other methods
Function GetText(out Size: TMemSize): TESLPCharType; overload; virtual;
Function GetText: TESLPCharType; overload; virtual;
procedure SetText(Text: TESLPCharType; Size: TMemSize); overload; virtual;
procedure SetText(Text: TESLPCharType); overload; virtual;
// list data properties
property Items[Index: Integer]: TESLListItem read GetItem;
property Strings[Index: Integer]: TESLStringType read GetString write SetString; default;
property Names[Index: Integer]: TESLStringType read GetName write SetName;
property Values[const Name: TESLStringType]: TESLStringType read GetValue write SetValue;
property ValueFromIndex[Index: Integer]: TESLStringType read GetValueFromIndex write SetValueFromIndex;
// settings properties
property NameValueSeparator: TESLCharType read fNameValueSeparator write fNameValueSeparator;
property LineBreak: TESLStringType read GetLineBreak write SetLineBreak;
property Delimiter: TESLCharType read fDelimiter write fDelimiter;
property QuoteChar: TESLCharType read fQuoteChar write fQuoteChar;
{
Long*Text and *Text are returning the same strings in most cases, only for
short-string lists they are returning different type and possibly also
data, as they may be truncated.
It is here because one might want text of short-string list without it
being limited to 255 characters (max. length of short string).
WARNING - returned short strings are only truncated from full length, they
are not build separately, so there might be missing closing quotes
and other problems. You have been warned.
}
property LongText: TESLLongStringType read GetLongStrText write SetLongStrText;
property LongDelimitedText: TESLLongStringType read GetLongDelimitedText write SetLongDelimitedText;
property LongCommaText: TESLLongStringType read GetLongCommaText write SetLongCommaText;
property Text: TESLStringType read GetStrText write SetStrText;
property DelimitedText: TESLStringType read GetDelimitedText write SetDelimitedText;
property CommaText: TESLStringType read GetCommaText write SetCommaText;
// IO events
property OnItemBinarySaveCallback: TESLItemBinaryIOCallbackType read fOnItemBinarySaveCallback write fOnItemBinarySaveCallback;
property OnItemBinarySaveEvent: TESLItemBinaryIOEventType read fOnItemBinarySaveEvent write fOnItemBinarySaveEvent;
property OnItemBinarySave: TESLItemBinaryIOEventType read fOnItemBinarySaveEvent write fOnItemBinarySaveEvent;
property OnItemBinaryLoadCallback: TESLItemBinaryIOCallbackType read fOnItemBinaryLoadCallback write fOnItemBinaryLoadCallback;
property OnItemBinaryLoadEvent: TESLItemBinaryIOEventType read fOnItemBinaryLoadEvent write fOnItemBinaryLoadEvent;
property OnItemBinaryLoad: TESLItemBinaryIOEventType read fOnItemBinaryLoadEvent write fOnItemBinaryLoadEvent;
{$ENDIF ESL_ClassDeclaration}
{-------------------------------------------------------------------------------
================================================================================
********************************************************************************
********************************************************************************
================================================================================
-------------------------------------------------------------------------------}
{$IFDEF ESL_ClassAuxiliary}
{===============================================================================
Helper defines
===============================================================================}
{$IF Defined(ESL_Short) or (Defined(ESL_Default) and Defined(ESL_DEFAULT_Short)))}
{$DEFINE ESL_LOCAL_Short}
{$IFEND}
{$IF Defined(ESL_Wide) or Defined(ESL_Unicode) or (Defined(ESL_Default) and Defined(ESL_DEFAULT_Unicode))}
{$DEFINE ESL_LOCAL_Wide}
{$IFEND}
{===============================================================================
Auxiliary functions
===============================================================================}
Function ESL_StrLow: TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := 0;
{$ELSE}
Result := 1;
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function ESL_LStrLow: TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := 0;
{$ELSE}
Result := 1;
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function ESL_StrHigh(const Str: TESLStringType): TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := Pred(High(Str)); // zero-base-indexed array with explicit terminating zero
{$ELSE}
Result := Length(Str);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function ESL_LStrHigh(const Str: TESLLongStringType): TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := Pred(High(Str));
{$ELSE}
Result := Length(Str);
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function ESL_StrLength(const Str: TESLStringType): TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := Pred(Length(Str)); // minus explicit terminating zero
{$ELSE}
Result := Length(Str);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function ESL_LStrLength(const Str: TESLLongStringType): TStrSize; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Result := Pred(Length(Str));
{$ELSE}
Result := Length(Str);
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure ESL_StrSetLength(var Str: TESLStringType; NewLength: TStrSize); {$IF Defined(CanInline) and Defined(FPC)}inline;{$IFEND}
begin
{$IFDEF ESL_UCS4}
// explicit terminating zero
SetLength(Str,NewLength + 1);
Str[High(Str)] := 0;
{$ELSE}
SetLength(Str,NewLength);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure ESL_LStrSetLength(var Str: TESLLongStringType; NewLength: TStrSize); {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
SetLength(Str,NewLength + 1);
Str[High(Str)] := 0;
{$ELSE}
SetLength(Str,NewLength);
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure ESL_StrInit(out Str: TESLStringType; NewLength: TStrSize); {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
// explicit terminating zero
Str := nil;
SetLength(Str,NewLength + 1);
Str[High(Str)] := 0;
{$ELSE}
Str := '';
SetLength(Str,NewLength);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure ESL_LStrInit(out Str: TESLLongStringType; NewLength: TStrSize); {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
Str := nil;
SetLength(Str,NewLength + 1);
Str[High(Str)] := 0;
{$ELSE}
Str := '';
SetLength(Str,NewLength);
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function ESL_StrAddr(const Str: TESLStringType; Offset: TStrSize = 0): Pointer;
begin
// note offset is zero-based everywhere
If ESL_StrLength(Str) > 0 then
begin
If (ESL_StrLow + Offset) <= ESL_StrHigh(Str) then
Result := Addr(Str[ESL_StrLow + Offset])
else
raise EESLInvalidValue.CreateFmt('ESL_StrAddr(%s): Offset (%d) out of bounds.',[ESL_IMPLSTR_NAME,Offset]);
end
else raise EESLInvalidValue.CreateFmt('ESL_StrAddr(%s): Empty string.',[ESL_IMPLSTR_NAME]);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function ESL_LStrAddr(const Str: TESLLongStringType; Offset: TStrSize = 0): Pointer;
begin
If ESL_LStrLength(Str) > 0 then
begin
If (ESL_LStrLow + Offset) <= ESL_LStrHigh(Str) then
Result := Addr(Str[ESL_LStrLow + Offset])
else
raise EESLInvalidValue.CreateFmt('ESL_LStrAddr(%s): Offset (%d) out of bounds.',[ESL_IMPLSTR_NAME,Offset]);
end
else raise EESLInvalidValue.CreateFmt('ESL_LStrAddr(%s): Empty string.',[ESL_IMPLSTR_NAME]);
end;
//------------------------------------------------------------------------------
Function ESL_StrEmpty: TESLStringType; {$IFDEF CanInline}inline;{$ENDIF}
begin
ESL_StrInit(Result,0);
end;
//------------------------------------------------------------------------------
Function ESL_LStrEmpty: TESLLongStringType; {$IFDEF CanInline}inline;{$ENDIF}
begin
ESL_LStrInit(Result,0);
end;
//------------------------------------------------------------------------------
Function ESL_StrCopy(const Str: TESLStringType; Start, Count: TStrSize): TESLStringType; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
If (ESL_StrLength(Str) > 0) and (Count > 0) then
begin
If Start <= ESL_StrHigh(Str) then
begin
If Start < ESL_StrLow then
begin
// Start is negative
Count := Count + Start;
If Count < 0 then
Count := 0;
Start := ESL_StrLow;
end;
If (Start + Count) > ESL_StrLength(Str) then
Count := ESL_StrLength(Str) - Start;
ESL_StrInit(Result,Count);
Move(ESL_StrAddr(Str,Start)^,ESL_StrAddr(Result)^,Count * SizeOf(TESLCharType));
end
else ESL_StrInit(Result,0);
end
else ESL_StrInit(Result,0);
{$ELSE}
Result := Copy(Str,Start,Count);
{$ENDIF}
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function ESL_LStrCopy(const Str: TESLLongStringType; Start, Count: TStrSize): TESLLongStringType; {$IFDEF CanInline}inline;{$ENDIF}
begin
{$IFDEF ESL_UCS4}
If (ESL_LStrLength(Str) > 0) and (Count > 0) then
begin
If Start <= ESL_LStrHigh(Str) then
begin
If Start < ESL_LStrLow then
begin
Count := Count + Start;
If Count < 0 then
Count := 0;
Start := ESL_LStrLow;
end;
If (Start + Count) > ESL_LStrLength(Str) then
Count := ESL_LStrLength(Str) - Start;
ESL_LStrInit(Result,Count);
Move(ESL_LStrAddr(Str,Start)^,ESL_LStrAddr(Result)^,Count * SizeOf(TESLCharType));
end
else ESL_LStrInit(Result,0);
end
else ESL_LStrInit(Result,0);
{$ELSE}
Result := Copy(Str,Start,Count);
{$ENDIF}
end;
//------------------------------------------------------------------------------
{$IFDEF ESL_UCS4}
Function ESL_StrAssign(const Str: TESLStringType): TESLStringType;
begin
Result := Copy(Str);
end;
//------------------------------------------------------------------------------
Function ESL_StrFromChar(const C: TESLCharType): TESLStringType;
begin
ESL_StrInit(Result,1);
Result[ESL_StrLow] := C;
end;
//------------------------------------------------------------------------------
Function ESL_StrConcat(const Strs: array of TESLStringType): TESLStringType;
var
i: Integer;
Len: TStrSize;
Off: TStrSize;
begin
If Length(Strs) > 0 then
begin
Len := 0;
For i := Low(Strs) to High(Strs) do
Len := Len + ESL_StrLength(Strs[i]);
ESL_StrInit(Result,Len);
Off := 0;
For i := Low(Strs) to High(Strs) do
begin
Move(ESL_StrAddr(Strs[i])^,ESL_StrAddr(Result,Off)^,ESL_StrLength(Strs[i]) * SizeOf(TESLCharType));
Inc(Off,ESL_StrLength(Strs[i]));
end;
end
else ESL_StrInit(Result,0);
end;
{$ENDIF}
{===============================================================================
TStrings/TStringList feature checks
===============================================================================}
{
Following two constructs are here to check what properties are implemented by
standard TStrings and TStringList classes and therefore can be used by the
lists when assigning (some of the properties were added in newer versions of
Delphi, or are not present in FPC).
}
type
TStringsFeaturesChecker = class(Classes.TStrings)
protected
procedure FeatureChecks; virtual;
end;
//------------------------------------------------------------------------------
procedure TStringsFeaturesChecker.FeatureChecks;
begin
{$IF Declared(LineBreak)} {$DEFINE ESL_STRINGS_LineBreak} {$IFEND}
{$IF Declared(TrailingLineBreak)} {$DEFINE ESL_STRINGS_TrailingLineBreak} {$IFEND}
{$IF Declared(NameValueSeparator)}{$DEFINE ESL_STRINGS_NameValueSeparator}{$IFEND}
{$IF Declared(Delimiter)} {$DEFINE ESL_STRINGS_Delimiter} {$IFEND}
{$IF Declared(StrictDelimiter)} {$DEFINE ESL_STRINGS_StrictDelimiter} {$IFEND}
{$IF Declared(QuoteChar)} {$DEFINE ESL_STRINGS_QuoteChar} {$IFEND}
raise Exception.Create('TStringsFeaturesChecker.FeatureChecks: This method is not supposed to be called.');
end;
//==============================================================================
type
TStringListFeaturesChecker = class(Classes.TStringList)
protected
procedure FeatureChecks; virtual;
end;
//------------------------------------------------------------------------------
procedure TStringListFeaturesChecker.FeatureChecks;
begin
{$IF Declared(OwnsObjects)} {$DEFINE ESL_STRINGLIST_OwnsObjects} {$IFEND}
{$IF Declared(CaseSensitive)}{$DEFINE ESL_STRINGLIST_CaseSensitive}{$IFEND}
{$IF Declared(Duplicates)} {$DEFINE ESL_STRINGLIST_Duplicates} {$IFEND}
{$IF Declared(Sorted)} {$DEFINE ESL_STRINGLIST_Sorted} {$IFEND}
raise Exception.Create('TStringListFeaturesChecker.FeatureChecks: This method is not supposed to be called.');
end;
{$ENDIF ESL_ClassAuxiliary}
{-------------------------------------------------------------------------------
================================================================================
********************************************************************************
********************************************************************************
================================================================================
-------------------------------------------------------------------------------}
{$IFDEF ESL_ClassDelimitedTextParser}
{===============================================================================
--------------------------------------------------------------------------------
TESLDelimitedTextParser
--------------------------------------------------------------------------------
===============================================================================}
type
TESLDelimitedTextParserEvent = procedure(const Str: TESLStringType) of object;
{===============================================================================
TESLDelimitedTextParser - class declaration
===============================================================================}
type
TESLDelimitedTextParser = class(TESLDelimitedTextParserBase)
protected
fDelimiter: TESLCharType;
fQuoteChar: TESLCharType;
fStrictDelimiter: Boolean;
fOnNewString: TESLDelimitedTextParserEvent;
// parsing vars
fParsedString: TESLLongStringType;
Function RemoveDoubleQuotes(const Str: TESLLongStringType): TESLLongStringType; virtual;
Function LookAhead(IsChar: TESLCharType): Boolean; virtual;
Function CurrCharType: TESLDelimitedTextParserCharType; virtual;
procedure DoNewString(const Str: TESLLongStringType); virtual;
procedure Parse; override;
procedure Parse_Initial; override;
procedure Parse_WhiteSpace; override;
procedure Parse_Delimiter; override;
procedure Parse_NormalText; override;
procedure Parse_QuotedText; override;
public
constructor Create(Delimiter,QuoteChar: TESLCharType; StrictDelimiter: Boolean);
procedure Parse(const Text: TESLLongStringType); overload; virtual;
property OnNewString: TESLDelimitedTextParserEvent read fOnNewString write fOnNewString;
end;
{===============================================================================
TESLDelimitedTextParser - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TESLDelimitedTextParser - protected methods
-------------------------------------------------------------------------------}
Function TESLDelimitedTextParser.RemoveDoubleQuotes(const Str: TESLLongStringType): TESLLongStringType;
var
i,ResPos: TStrSize;
LastChar: TESLCharType;
begin
If ESL_LStrLength(Str) > 0 then
begin
ESL_LStrInit(Result,ESL_LStrLength(Str));
ResPos := ESL_LStrLow;
LastChar := TESLCharType(not Ord(fQuoteChar));
For i := ESL_LStrLow to ESL_LStrHigh(Str) do
If not((Str[i] = fQuoteChar) and (LastChar = fQuoteChar)) then
begin
Result[ResPos] := Str[i];
Inc(ResPos);
LastChar := Str[i];
end
else LastChar := TESLCharType(not Ord(fQuoteChar));
ESL_LStrSetLength(Result,ResPos - ESL_LStrLow);
end
else ESL_LStrInit(Result,0);
end;
//------------------------------------------------------------------------------
Function TESLDelimitedTextParser.LookAhead(IsChar: TESLCharType): Boolean;
begin
If (fPosition >= ESL_LStrLow) and (fPosition < ESL_LStrHigh(fParsedString)) then
Result := fParsedString[Succ(fPosition)] = IsChar
else
Result := False;
end;
//------------------------------------------------------------------------------
Function TESLDelimitedTextParser.CurrCharType: TESLDelimitedTextParserCharType;
begin
If Ord(fParsedString[fPosition]) in [0..32] then
Result := dtpcWhiteSpace
else If fParsedString[fPosition] = fDelimiter then
Result := dtpcDelimiter
else If fParsedString[fPosition] = fQuoteChar then
Result := dtpcQuoteChar
else
Result := dtpcGeneral;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.DoNewString(const Str: TESLLongStringType);
begin
If Assigned(fOnNewString) then
fOnNewString({$IFDEF ESL_LOCAL_Short}TESLStringType{$ENDIF}(Str));
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse;
begin
fParserState := dtpsInitial;
fPosition := Pred(ESL_LStrLow);
while fPosition <= ESL_LStrHigh(fParsedString) do
inherited Parse;
case fParserState of
dtpsDelimiter: DoNewString(ESL_LStrEmpty);
dtpsNormalText: DoNewString(ESL_LStrCopy(fParsedString,fItemStart,fItemLength));
dtpsQuotedText: DoNewString(RemoveDoubleQuotes(ESL_LStrCopy(fParsedString,fItemStart,fItemLength)));
end;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse_Initial;
begin
fParserState := dtpsDelimiter;
fPosition := Pred(ESL_LStrLow);
fItemStart := Pred(ESL_LStrLow);
fItemLength := 0;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse_WhiteSpace;
begin
case CurrCharType of
dtpcWhiteSpace:;// ignore
dtpcDelimiter: fParserState := dtpsDelimiter;
dtpcQuoteChar: If not fStrictDelimiter then
begin
fItemStart := Succ(fPosition);
fItemLength := 0;
fParserState := dtpsQuotedText;
end;
dtpcGeneral: If not fStrictDelimiter then
begin
fItemStart := fPosition;
fItemLength := 1;
fParserState := dtpsNormalText;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse_Delimiter;
begin
case CurrCharType of
dtpcWhiteSpace: If fStrictDelimiter then
begin
fItemStart := fPosition;
fItemLength := 1;
fParserState := dtpsNormalText;
end;
dtpcDelimiter: DoNewString(ESL_LStrEmpty);
dtpcQuoteChar: begin
fItemStart := Succ(fPosition);
fItemLength := 0;
fParserState := dtpsQuotedText;
end;
dtpcGeneral: begin
fItemStart := fPosition;
fItemLength := 1;
fParserState := dtpsNormalText;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse_NormalText;
begin
case CurrCharType of
dtpcWhiteSpace: If not fStrictDelimiter then
begin
DoNewString(ESL_LStrCopy(fParsedString,fItemStart,fItemLength));
fParserState := dtpsWhiteSpace;
end
else Inc(fItemLength);
dtpcDelimiter: begin
DoNewString(ESL_LStrCopy(fParsedString,fItemStart,fItemLength));
fParserState := dtpsDelimiter;
end;
dtpcQuoteChar,
dtpcGeneral: Inc(fItemLength);
end;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse_QuotedText;
begin
If CurrCharType = dtpcQuoteChar then
begin
If LookAhead(fQuoteChar) then
begin
Inc(fPosition);
Inc(fItemLength,2);
end
else
begin
DoNewString(RemoveDoubleQuotes(ESL_LStrCopy(fParsedString,fItemStart,fItemLength)));
fParserState := dtpsWhiteSpace;
end;
end
else Inc(fItemLength);
end;
{-------------------------------------------------------------------------------
TESLDelimitedTextParser - public methods
-------------------------------------------------------------------------------}
constructor TESLDelimitedTextParser.Create(Delimiter,QuoteChar: TESLCharType; StrictDelimiter: Boolean);
begin
inherited Create;
fDelimiter := Delimiter;
fQuoteChar := QuoteChar;
fStrictDelimiter := StrictDelimiter;
end;
//------------------------------------------------------------------------------
procedure TESLDelimitedTextParser.Parse(const Text: TESLLongStringType);
begin
fParsedString := {$IFDEF ELS_UCS4}ESL_StrAssing{$ENDIF}(Text);
Parse;
end;
{$ENDIF ESL_ClassDelimitedTextParser}
{-------------------------------------------------------------------------------
================================================================================
********************************************************************************
********************************************************************************
================================================================================
-------------------------------------------------------------------------------}
{$IFDEF ESL_ClassImplementation}
{===============================================================================
--------------------------------------------------------------------------------
TESLClassType
--------------------------------------------------------------------------------
===============================================================================}
const
ESL_DEFVAL_NameValueSeparator = TESLCharType('=');
ESL_DEFVAL_Delimiter = TESLCharType(',');
ESL_DEFVAL_LineBreak = {$IFNDEF ESL_UCS4}TESLStringType{$ENDIF}(sLineBreak);
ESL_DEFVAL_QuoteChar = TESLCharType('"');
{===============================================================================
TESLClassType - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TESLClassType - protected methods
-------------------------------------------------------------------------------}
Function TESLClassType.GetItem(Index: Integer): TESLListItem;
begin
If CheckIndex(Index) then
Result := fItems[Index]
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetItem: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
Function TESLClassType.GetString(Index: Integer): TESLStringType;
begin
If CheckIndex(Index) then
Result := {$IFDEF ELS_UCS4}ESL_StrAssing{$ENDIF}(fItems[Index].Str)
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetString: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
procedure TESLClassType.SetString(Index: Integer; const Value: TESLStringType);
begin
If CheckIndex(Index) then
begin
DoItemChanging(Index);
fItems[Index].Str := {$IFDEF ELS_UCS4}ESL_StrAssing{$ENDIF}(Value);
DoItemChange(Index);
end
else raise EESLIndexOutOfBounds.CreateFmt('%s.SetString: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
Function TESLClassType.GetObject(Index: Integer): TObject;
begin
If CheckIndex(Index) then
Result := fItems[Index].Obj
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetObject: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
procedure TESLClassType.SetObject(Index: Integer; Value: TObject);
begin
If CheckIndex(Index) then
begin
DoItemChanging(Index);
If Assigned(fItems[Index].Obj) and fOwnsObjects then
FreeAndNil(fItems[Index].Obj);
fItems[Index].Obj := Value;
DoItemChange(Index);
end
else raise EESLIndexOutOfBounds.CreateFmt('%s.SetObject: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
Function TESLClassType.GetItemUserData(Index: Integer): PtrInt;
begin
If CheckIndex(Index) then
Result := fItems[Index].UserData
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetItemUserData: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
procedure TESLClassType.SetItemUserData(Index: Integer; Value: PtrInt);
begin
If CheckIndex(Index) then
begin
DoItemChanging(Index);
fItems[Index].UserData := Value;
DoItemChange(Index);
end
else raise EESLIndexOutOfBounds.CreateFmt('%s.SetItemUserData: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
Function TESLClassType.GetChanged(Index: Integer): Boolean;
begin
If CheckIndex(Index) then
Result := fItems[Index].Changed
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetChanged: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
procedure TESLClassType.SetChanged(Index: Integer; Value: Boolean);
begin
If CheckIndex(Index) then
fItems[Index].Changed := Value
else
raise EESLIndexOutOfBounds.CreateFmt('%s.SetChanged: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
Function TESLClassType.GetDefString(Index: Integer): String;
begin
If CheckIndex(Index) then
Result := {$IFNDEF ESL_Default}ConvertToString{$ENDIF}(fItems[Index].Str)
else
raise EESLIndexOutOfBounds.CreateFmt('%s.GetDefString: Index (%d) out of bounds.',[Self.ClassName,Index]);
end;
//------------------------------------------------------------------------------
procedure TESLClassType.SetDefString(Index: Integer; const Value: String);
begin
If CheckIndex(Index) then