-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.RichIniFile.pas
More file actions
2173 lines (2002 loc) · 61.4 KB
/
Copy pathMaxLogic.RichIniFile.pas
File metadata and controls
2173 lines (2002 loc) · 61.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
unit MaxLogic.RichIniFile;
interface
uses
System.Classes,
System.Generics.Collections,
System.Generics.Defaults,
System.IniFiles,
System.SysUtils;
type
TEncodingMode = (
eoAutoDetect,
eoUTF8,
eoAnsi,
eoCustom
);
TBomPolicy = (
bpAsSource,
bpNone,
bpForce
);
TNewlineMode = (
nlPreserveInput,
nlPlatform,
nlWindowsCRLF,
nlUnixLF
);
TCommentOwnership = (
coNone,
coAttachToNext,
coAttachToPrev
);
TMissingBracketPolicy = (
mbAcceptAsSection,
mbTreatAsKey
);
TCaseSensitivity = (
csCaseSensitive,
csCaseInsensitive
);
TRichIniOptions = record
LoadEncoding: TEncodingMode;
SaveEncoding: TEncodingMode;
CustomEncoding: TEncoding;
BomPolicy: TBomPolicy;
NewlineMode: TNewlineMode;
CaseSensitivity: TCaseSensitivity;
AcceptMissingBracket: TMissingBracketPolicy;
CommentPrefixes: TArray<string>;
CommentOwnership: TCommentOwnership;
KeyValueDelimiter: Char;
BooleanTrueValues: TArray<string>;
BooleanTrueValue: string;
BooleanFalseValue: string;
end;
function DefaultRichIniOptions: TRichIniOptions;
type
TRichIniLineKind = (
lkSectionHeader,
lkKeyValue,
lkComment,
lkBlank,
lkUnparsed
);
TRichIniLine = class;
TRichIniCommentLine = class;
TRichIniKeyLine = class;
TRichIniSectionBlock = class;
TRichIniLine = class
private
fKind: TRichIniLineKind;
fRawText: string;
fModified: Boolean;
fSectionBlock: TRichIniSectionBlock;
fOwnedComments: TObjectList<TRichIniCommentLine>;
function GetOwnedComments: TObjectList<TRichIniCommentLine>;
public
constructor Create(const aKind: TRichIniLineKind; const aRawText: string);
destructor Destroy; override;
procedure AttachComment(aComment: TRichIniCommentLine);
procedure DetachComment(aComment: TRichIniCommentLine);
procedure ClearOwnedComments;
function OwnedCommentCount: Integer;
property Kind: TRichIniLineKind read fKind;
property RawText: string read fRawText write fRawText;
property Modified: Boolean read fModified write fModified;
property SectionBlock: TRichIniSectionBlock read fSectionBlock write fSectionBlock;
property OwnedComments: TObjectList<TRichIniCommentLine> read GetOwnedComments;
end;
TRichIniCommentLine = class(TRichIniLine)
private
fLeadingWhitespace: string;
fPrefix: string;
fText: string;
fOwnerLine: TRichIniLine;
public
constructor Create(const aRawText, aLeadingWhitespace, aPrefix, aCommentText: string);
property LeadingWhitespace: string read fLeadingWhitespace write fLeadingWhitespace;
property Prefix: string read fPrefix write fPrefix;
property CommentText: string read fText write fText;
property OwnerLine: TRichIniLine read fOwnerLine write fOwnerLine;
end;
TRichIniSectionLine = class(TRichIniLine)
private
fName: string;
public
constructor Create(const aName, aRawText: string);
property Name: string read fName write fName;
end;
TRichIniKeyLine = class(TRichIniLine)
private
fKey: string;
fValue: string;
fDelimiter: Char;
fLookupKey: string;
public
constructor Create(const aKey, aValue, aLookupKey: string; const aDelimiter: Char; const aRawText: string);
property Key: string read fKey write fKey;
property Value: string read fValue write fValue;
property Delimiter: Char read fDelimiter write fDelimiter;
property LookupKey: string read fLookupKey write fLookupKey;
end;
TRichIniSectionBlock = class
private
fName: string;
fLookupName: string;
fNoHeader: Boolean;
fHeaderLine: TRichIniSectionLine;
fKeyMap: TObjectDictionary<string, TObjectList<TRichIniKeyLine>>;
fKeyOrder: TList<TRichIniKeyLine>;
constructor CreateWithComparer(const aName, aLookupName: string; const aNoHeader: Boolean;
const aComparer: IEqualityComparer<string>);
public
constructor Create(const aName, aLookupName: string; const aNoHeader: Boolean);
destructor Destroy; override;
procedure AddKeyLine(aLine: TRichIniKeyLine);
procedure RemoveKeyLine(aLine: TRichIniKeyLine);
function KeyLines(const aToken: string): TObjectList<TRichIniKeyLine>;
property KeyOrder: TList<TRichIniKeyLine> read fKeyOrder;
property Name: string read fName write fName;
property LookupName: string read fLookupName write fLookupName;
property NoHeader: Boolean read fNoHeader write fNoHeader;
property HeaderLine: TRichIniSectionLine read fHeaderLine write fHeaderLine;
end;
type
TRichIniFile = class(TCustomIniFile)
private type
TRichIniParseState = record
CurrentBlock: TRichIniSectionBlock;
PendingComments: TList<TRichIniCommentLine>;
LastContentLine: TRichIniLine;
end;
private
fFileName: string;
fOptions: TRichIniOptions;
fLines: TObjectList<TRichIniLine>;
fSections: TObjectList<TRichIniSectionBlock>;
fSectionMap: TObjectDictionary<string, TObjectList<TRichIniSectionBlock>>;
fComparer: IEqualityComparer<string>;
fDirty: Boolean;
fDetectedNewline: string;
fSourceEncoding: TEncoding;
fSourceBom: Boolean;
fHasSource: Boolean;
function CurrentComparer: IEqualityComparer<string>; inline;
function TokensEqual(const aLeft, aRight: string): Boolean; inline;
procedure EnsureOptionsDefaults;
procedure ClearDocument;
procedure CreateGlobalSection;
function CreateSectionBlock(const aSection: string; const aNoHeader: Boolean): TRichIniSectionBlock;
function GetOrCreateSectionList(const aToken: string): TObjectList<TRichIniSectionBlock>;
function SectionToken(const aSection: string): string; inline;
function KeyToken(const aKey: string): string; inline;
procedure AddLineToDocument(aLine: TRichIniLine);
procedure AttachPendingComments(const aOwner: TRichIniLine; const aPending: TList<TRichIniCommentLine>);
procedure ProcessLine(const aLine: string; var aState: TRichIniParseState);
procedure ParseLines(const aLines: TArray<string>);
procedure ParseText(const aText: string);
function DetectNewline(const aText: string): string;
function ResolveLoadEncoding(const aBuffer: TBytes; out aEncoding: TEncoding; out aHasBom: Boolean): string;
function PlatformNewline: string;
function ResolveNewline: string;
function FindSectionList(const aSection: string): TObjectList<TRichIniSectionBlock>;
function FindLastKeyLine(const aSection, aKey: string): TRichIniKeyLine;
function BuildKeyRawText(const aKey, aValue: string): string;
function EnsureSectionBlockForWrite(const aSection: string): TRichIniSectionBlock;
function GetSectionInsertIndex(const aBlock: TRichIniSectionBlock): Integer;
function FindKeyLineByIndex(const aSection, aKey: string; aKeyIndex: Integer): TRichIniKeyLine;
procedure RemoveOwnedComments(aLine: TRichIniLine);
procedure DeleteKeyLineInstance(aLine: TRichIniKeyLine);
function DefaultCommentPrefix: string;
function IsTrueBooleanValue(const aValue: string): Boolean;
function SplitToLines(const aText: string): TArray<string>;
function CollectCommentText(const aLine: TRichIniLine): string;
function EncodeMultilineValue(const aValue: string): string;
function DecodeMultilineValue(const aValue: string): string;
procedure ResolveSaveEncoding(out aEncoding: TEncoding; out aWriteBom: Boolean);
function CreateTempFileName(const aTarget: string): string;
procedure ReplaceFileAtomic(const aSource, aDestination: string);
protected
function GetLineCount: Integer;
function GetSectionBlockCount: Integer;
public
constructor Create(const aFileName: string; const aOptions: TRichIniOptions); reintroduce; overload;
constructor CreateFromStrings(const aLines: TArray<string>; const aOptions: TRichIniOptions); overload;
destructor Destroy; override;
procedure LoadFromText(const aText: string; const aSourceEncoding: TEncoding = nil; aHasBom: Boolean = False);
procedure LoadFromFile(const aFileName: string);
procedure SaveToFile(const aFileName: string = '');
procedure UpdateFile; override;
function ReadString(const aSection, aKey, aDefault: string): string; override;
procedure WriteString(const aSection, aKey, aValue: string); overload; override;
procedure WriteString(const aSection, aKey, aValue: string; aKeyIndex: Integer); reintroduce; overload;
function ReadInteger(const aSection, aKey: string; aDefault: Integer): Integer; override;
procedure WriteInteger(const aSection, aKey: string; aValue: Integer); override;
function ReadBool(const aSection, aKey: string; aDefault: Boolean): Boolean; override;
procedure WriteBool(const aSection, aKey: string; aValue: Boolean); override;
procedure DeleteKey(const aSection, aKey: string); overload; override;
procedure DeleteKey(const aSection, aKey: string; aKeyIndex: Integer); reintroduce; overload;
procedure EraseSection(const aSection: string); override;
procedure ReadSection(const aSection: string; aStrings: TStrings); override;
procedure ReadSections(aStrings: TStrings); override;
procedure ReadSectionValues(const aSection: string; aStrings: TStrings); override;
function AppendKey(const aSection, aKey, aValue: string): Integer;
function LastKeyIndex(const aSection, aKey: string): Integer;
function KeyCount(const aSection, aKey: string): Integer;
procedure ReadAllKeyValues(const aSection, aKey: string; out aValues: TArray<string>);
function ReadComment(const aSection, aKey: string): string;
procedure WriteComment(const aSection, aKey, aComment: string);
procedure PurgeComments;
procedure ConsolidateSection(const aSection: string);
procedure ConsolidateAll;
procedure WriteMultilineString(const aSection, aKey, aValueWithLineBreaks: string);
function ReadMultilineString(const aSection, aKey, aDefault: string): string;
property FileName: string read fFileName;
property Options: TRichIniOptions read fOptions;
property Dirty: Boolean read fDirty;
property LineCount: Integer read GetLineCount;
property SectionBlockCount: Integer read GetSectionBlockCount;
end;
implementation
uses
System.IOUtils,
MaxLogic.StrUtils
{$IFDEF MSWINDOWS}, Winapi.Windows{$ENDIF};
const
cDefaultCommentPrefixes: array[0..2] of string = (';', '#', '//');
cDefaultBooleanTrueValues: array[0..5] of string = ('1', 'y', 'yes', 'on', 'enabled', 'true');
cDefaultBooleanTrueValue = '1';
cDefaultBooleanFalseValue = '0';
function DefaultRichIniOptions: TRichIniOptions;
var
lIndex: Integer;
begin
Result.LoadEncoding := eoAutoDetect;
Result.SaveEncoding := eoUTF8;
Result.CustomEncoding := nil;
Result.BomPolicy := bpAsSource;
Result.NewlineMode := nlPlatform;
Result.CaseSensitivity := csCaseSensitive;
Result.AcceptMissingBracket := mbTreatAsKey;
Result.CommentOwnership := coAttachToNext;
Result.KeyValueDelimiter := '=';
SetLength(Result.CommentPrefixes, Length(cDefaultCommentPrefixes));
for lIndex := Low(cDefaultCommentPrefixes) to High(cDefaultCommentPrefixes) do
Result.CommentPrefixes[lIndex] := cDefaultCommentPrefixes[lIndex];
SetLength(Result.BooleanTrueValues, Length(cDefaultBooleanTrueValues));
for lIndex := Low(cDefaultBooleanTrueValues) to High(cDefaultBooleanTrueValues) do
Result.BooleanTrueValues[lIndex] := cDefaultBooleanTrueValues[lIndex];
Result.BooleanTrueValue := cDefaultBooleanTrueValue;
Result.BooleanFalseValue := cDefaultBooleanFalseValue;
end;
{ TRichIniLine }
constructor TRichIniLine.Create(const aKind: TRichIniLineKind; const aRawText: string);
begin
inherited Create;
fKind := aKind;
fRawText := aRawText;
end;
destructor TRichIniLine.Destroy;
begin
ClearOwnedComments;
fOwnedComments.Free;
inherited;
end;
procedure TRichIniLine.AttachComment(aComment: TRichIniCommentLine);
begin
if aComment = nil then
Exit;
if OwnedComments.IndexOf(aComment) < 0 then
begin
OwnedComments.Add(aComment);
aComment.OwnerLine := Self;
end;
end;
procedure TRichIniLine.ClearOwnedComments;
var
lComment: TRichIniCommentLine;
begin
if fOwnedComments = nil then
Exit;
for lComment in fOwnedComments do
lComment.OwnerLine := nil;
fOwnedComments.Clear;
end;
procedure TRichIniLine.DetachComment(aComment: TRichIniCommentLine);
begin
if (fOwnedComments = nil) or (aComment = nil) then
Exit;
if fOwnedComments.Remove(aComment) >= 0 then
aComment.OwnerLine := nil;
end;
function TRichIniLine.GetOwnedComments: TObjectList<TRichIniCommentLine>;
begin
if fOwnedComments = nil then
fOwnedComments := TObjectList<TRichIniCommentLine>.Create(False);
Result := fOwnedComments;
end;
function TRichIniLine.OwnedCommentCount: Integer;
begin
if fOwnedComments = nil then
Result := 0
else
Result := fOwnedComments.Count;
end;
{ TRichIniCommentLine }
constructor TRichIniCommentLine.Create(const aRawText, aLeadingWhitespace, aPrefix, aCommentText: string);
begin
inherited Create(lkComment, aRawText);
fLeadingWhitespace := aLeadingWhitespace;
fPrefix := aPrefix;
fText := aCommentText;
end;
{ TRichIniSectionLine }
constructor TRichIniSectionLine.Create(const aName, aRawText: string);
begin
inherited Create(lkSectionHeader, aRawText);
fName := aName;
end;
{ TRichIniKeyLine }
constructor TRichIniKeyLine.Create(const aKey, aValue, aLookupKey: string; const aDelimiter: Char;
const aRawText: string);
begin
inherited Create(lkKeyValue, aRawText);
fKey := aKey;
fValue := aValue;
fLookupKey := aLookupKey;
fDelimiter := aDelimiter;
end;
{ TRichIniSectionBlock }
constructor TRichIniSectionBlock.Create(const aName, aLookupName: string; const aNoHeader: Boolean);
begin
CreateWithComparer(aName, aLookupName, aNoHeader, TFastCaseAwareComparer.Ordinal);
end;
constructor TRichIniSectionBlock.CreateWithComparer(const aName, aLookupName: string; const aNoHeader: Boolean;
const aComparer: IEqualityComparer<string>);
begin
inherited Create;
fName := aName;
fLookupName := aLookupName;
fNoHeader := aNoHeader;
fKeyMap := TObjectDictionary<string, TObjectList<TRichIniKeyLine>>.Create([doOwnsValues], aComparer);
fKeyOrder := TList<TRichIniKeyLine>.Create;
end;
destructor TRichIniSectionBlock.Destroy;
begin
fKeyOrder.Free;
fKeyMap.Free;
inherited;
end;
procedure TRichIniSectionBlock.AddKeyLine(aLine: TRichIniKeyLine);
var
lList: TObjectList<TRichIniKeyLine>;
begin
if not fKeyMap.TryGetValue(aLine.LookupKey, lList) then
begin
lList := TObjectList<TRichIniKeyLine>.Create(False);
fKeyMap.Add(aLine.LookupKey, lList);
end;
lList.Add(aLine);
fKeyOrder.Add(aLine);
end;
function TRichIniSectionBlock.KeyLines(const aToken: string): TObjectList<TRichIniKeyLine>;
begin
if not fKeyMap.TryGetValue(aToken, Result) then
Result := nil;
end;
procedure TRichIniSectionBlock.RemoveKeyLine(aLine: TRichIniKeyLine);
var
lList: TObjectList<TRichIniKeyLine>;
begin
if not fKeyMap.TryGetValue(aLine.LookupKey, lList) then
Exit;
lList.Remove(aLine);
if lList.Count = 0 then
fKeyMap.Remove(aLine.LookupKey);
fKeyOrder.Remove(aLine);
end;
{ TRichIniFile }
constructor TRichIniFile.Create(const aFileName: string; const aOptions: TRichIniOptions);
begin
inherited Create(aFileName);
fFileName := aFileName;
fOptions := aOptions;
EnsureOptionsDefaults;
fComparer := nil;
fLines := TObjectList<TRichIniLine>.Create(True);
fSections := TObjectList<TRichIniSectionBlock>.Create(True);
fSectionMap := TObjectDictionary<string, TObjectList<TRichIniSectionBlock>>.Create([doOwnsValues], CurrentComparer);
ClearDocument;
end;
constructor TRichIniFile.CreateFromStrings(const aLines: TArray<string>; const aOptions: TRichIniOptions);
begin
inherited Create('');
fFileName := '';
fOptions := aOptions;
EnsureOptionsDefaults;
fComparer := nil;
fLines := TObjectList<TRichIniLine>.Create(True);
fSections := TObjectList<TRichIniSectionBlock>.Create(True);
fSectionMap := TObjectDictionary<string, TObjectList<TRichIniSectionBlock>>.Create([doOwnsValues], CurrentComparer);
ClearDocument;
fHasSource := False;
fSourceEncoding := TEncoding.UTF8;
fSourceBom := False;
fDetectedNewline := PlatformNewline;
ParseLines(aLines);
fSourceEncoding := TEncoding.UTF8;
fSourceBom := False;
fHasSource := True;
fDirty := False;
end;
destructor TRichIniFile.Destroy;
begin
fSectionMap.Free;
fSections.Free;
fLines.Free;
inherited;
end;
procedure TRichIniFile.AddLineToDocument(aLine: TRichIniLine);
begin
if aLine = nil then
Exit;
fLines.Add(aLine);
end;
procedure TRichIniFile.AttachPendingComments(const aOwner: TRichIniLine; const aPending: TList<TRichIniCommentLine>);
var
lComment: TRichIniCommentLine;
begin
if (aOwner = nil) or (aPending = nil) or (aPending.Count = 0) then
Exit;
for lComment in aPending do
aOwner.AttachComment(lComment);
aPending.Clear;
end;
procedure TRichIniFile.ProcessLine(const aLine: string; var aState: TRichIniParseState);
var
lLineLen: Integer;
lP: PChar;
lTrimStart: Integer;
lTrimEnd: Integer;
lTrimLength: Integer;
lPrefix: string;
lPrefixLen: Integer;
lCommentTextStart: Integer;
lLeadingWs: string;
lCommentText: string;
lComment: TRichIniCommentLine;
lSectionName: string;
lSectionStart: Integer;
lSectionEnd: Integer;
lClosingPos: Integer;
lSectionLine: TRichIniSectionLine;
lDelimiterPos: Integer;
lKeyStart: Integer;
lKeyEnd: Integer;
lValueStart: Integer;
lValueEnd: Integer;
lKeyText: string;
lValueText: string;
lKeyLine: TRichIniKeyLine;
lLine: TRichIniLine;
i: Integer;
begin
lLineLen := Length(aLine);
lP := PChar(aLine);
lTrimStart := 1;
while (lTrimStart <= lLineLen) and ((lP[lTrimStart - 1] = ' ') or (lP[lTrimStart - 1] = #9)) do
Inc(lTrimStart);
if lTrimStart > lLineLen then
begin
lLine := TRichIniLine.Create(lkBlank, aLine);
lLine.SectionBlock := aState.CurrentBlock;
AddLineToDocument(lLine);
Exit;
end;
lTrimEnd := lLineLen;
while (lTrimEnd >= lTrimStart) and ((lP[lTrimEnd - 1] = ' ') or (lP[lTrimEnd - 1] = #9)) do
Dec(lTrimEnd);
lTrimLength := lTrimEnd - lTrimStart + 1;
for i := 0 to High(fOptions.CommentPrefixes) do
begin
lPrefix := fOptions.CommentPrefixes[i];
lPrefixLen := Length(lPrefix);
if (lPrefixLen > 0) and (lTrimLength >= lPrefixLen) and
(lP[lTrimStart - 1] = lPrefix[1]) and
(StrLComp(lP + (lTrimStart - 1), PChar(lPrefix), lPrefixLen) = 0) then
begin
lCommentTextStart := lTrimStart + lPrefixLen;
while (lCommentTextStart <= lLineLen) and ((lP[lCommentTextStart - 1] = ' ') or (lP[lCommentTextStart - 1] = #9)) do
Inc(lCommentTextStart);
if lCommentTextStart <= lLineLen then
SetString(lCommentText, lP + (lCommentTextStart - 1), lLineLen - lCommentTextStart + 1)
else
lCommentText := '';
if lTrimStart > 1 then
begin
SetString(lLeadingWs, lP, lTrimStart - 1);
lComment := TRichIniCommentLine.Create(aLine, lLeadingWs, lPrefix, lCommentText);
end
else
lComment := TRichIniCommentLine.Create(aLine, '', lPrefix, lCommentText);
lComment.SectionBlock := aState.CurrentBlock;
AddLineToDocument(lComment);
case fOptions.CommentOwnership of
coAttachToNext:
aState.PendingComments.Add(lComment);
coAttachToPrev:
if aState.LastContentLine <> nil then
aState.LastContentLine.AttachComment(lComment);
end;
Exit;
end;
end;
if lP[lTrimStart - 1] = '[' then
begin
lClosingPos := 0;
for i := lTrimStart + 1 to lLineLen do
if lP[i - 1] = ']' then
begin
lClosingPos := i;
Break;
end;
if (lClosingPos > lTrimStart) or
((lClosingPos = 0) and (fOptions.AcceptMissingBracket = mbAcceptAsSection)) then
begin
lSectionStart := lTrimStart + 1;
if lClosingPos > lTrimStart then
lSectionEnd := lClosingPos - 1
else
lSectionEnd := lLineLen;
while (lSectionStart <= lSectionEnd) and ((lP[lSectionStart - 1] = ' ') or (lP[lSectionStart - 1] = #9)) do
Inc(lSectionStart);
while (lSectionEnd >= lSectionStart) and ((lP[lSectionEnd - 1] = ' ') or (lP[lSectionEnd - 1] = #9)) do
Dec(lSectionEnd);
if lSectionStart <= lSectionEnd then
SetString(lSectionName, lP + (lSectionStart - 1), lSectionEnd - lSectionStart + 1)
else
lSectionName := '';
aState.CurrentBlock := CreateSectionBlock(lSectionName, False);
lSectionLine := TRichIniSectionLine.Create(lSectionName, aLine);
lSectionLine.SectionBlock := aState.CurrentBlock;
aState.CurrentBlock.HeaderLine := lSectionLine;
AddLineToDocument(lSectionLine);
AttachPendingComments(lSectionLine, aState.PendingComments);
aState.LastContentLine := lSectionLine;
Exit;
end;
end;
lDelimiterPos := 0;
for i := lTrimStart to lLineLen do
if lP[i - 1] = fOptions.KeyValueDelimiter then
begin
lDelimiterPos := i;
Break;
end;
if lDelimiterPos > 0 then
lKeyEnd := lDelimiterPos - 1
else
lKeyEnd := lTrimEnd;
lKeyStart := lTrimStart;
while (lKeyStart <= lKeyEnd) and ((lP[lKeyStart - 1] = ' ') or (lP[lKeyStart - 1] = #9)) do
Inc(lKeyStart);
while (lKeyEnd >= lKeyStart) and ((lP[lKeyEnd - 1] = ' ') or (lP[lKeyEnd - 1] = #9)) do
Dec(lKeyEnd);
if lKeyStart <= lKeyEnd then
SetString(lKeyText, lP + (lKeyStart - 1), lKeyEnd - lKeyStart + 1)
else
lKeyText := '';
if lDelimiterPos > 0 then
begin
lValueStart := lDelimiterPos + 1;
while (lValueStart <= lLineLen) and ((lP[lValueStart - 1] = ' ') or (lP[lValueStart - 1] = #9)) do
Inc(lValueStart);
lValueEnd := lLineLen;
while (lValueEnd >= lValueStart) and ((lP[lValueEnd - 1] = ' ') or (lP[lValueEnd - 1] = #9)) do
Dec(lValueEnd);
if lValueStart <= lValueEnd then
SetString(lValueText, lP + (lValueStart - 1), lValueEnd - lValueStart + 1)
else
lValueText := '';
end else
lValueText := '';
lKeyLine := TRichIniKeyLine.Create(lKeyText, lValueText, KeyToken(lKeyText), fOptions.KeyValueDelimiter, aLine);
lKeyLine.SectionBlock := aState.CurrentBlock;
aState.CurrentBlock.AddKeyLine(lKeyLine);
AddLineToDocument(lKeyLine);
AttachPendingComments(lKeyLine, aState.PendingComments);
aState.LastContentLine := lKeyLine;
end;
procedure TRichIniFile.ParseLines(const aLines: TArray<string>);
var
lState: TRichIniParseState;
i: Integer;
begin
if fSections.Count = 0 then
CreateGlobalSection;
if (fLines <> nil) and (fLines.Capacity < fLines.Count + Length(aLines)) then
fLines.Capacity := fLines.Count + Length(aLines);
lState.CurrentBlock := fSections[0];
lState.PendingComments := TList<TRichIniCommentLine>.Create;
try
lState.LastContentLine := nil;
for i := 0 to High(aLines) do
ProcessLine(aLines[i], lState);
finally
lState.PendingComments.Free;
end;
end;
procedure TRichIniFile.ClearDocument;
begin
if fLines <> nil then
fLines.Clear;
if fSections <> nil then
fSections.Clear;
if fSectionMap <> nil then
fSectionMap.Clear;
fDirty := False;
fDetectedNewline := '';
fSourceEncoding := nil;
fSourceBom := False;
fHasSource := False;
if fSections <> nil then
CreateGlobalSection;
end;
procedure TRichIniFile.CreateGlobalSection;
begin
CreateSectionBlock('', True);
end;
function TRichIniFile.CreateSectionBlock(const aSection: string; const aNoHeader: Boolean): TRichIniSectionBlock;
var
lToken: string;
begin
lToken := SectionToken(aSection);
Result := TRichIniSectionBlock.CreateWithComparer(aSection, lToken, aNoHeader, CurrentComparer);
fSections.Add(Result);
GetOrCreateSectionList(lToken).Add(Result);
end;
function TRichIniFile.DetectNewline(const aText: string): string;
var
i: Integer;
begin
for i := 1 to Length(aText) do
begin
if aText[i] = #13 then
begin
if (i < Length(aText)) and (aText[i + 1] = #10) then
Exit(#13#10)
else
Exit(#13);
end;
if aText[i] = #10 then
Exit(#10);
end;
Result := '';
end;
procedure TRichIniFile.EnsureOptionsDefaults;
var
lIndex: Integer;
begin
if Length(fOptions.CommentPrefixes) = 0 then
begin
SetLength(fOptions.CommentPrefixes, Length(cDefaultCommentPrefixes));
for lIndex := Low(cDefaultCommentPrefixes) to High(cDefaultCommentPrefixes) do
fOptions.CommentPrefixes[lIndex] := cDefaultCommentPrefixes[lIndex];
end;
if fOptions.KeyValueDelimiter = #0 then
fOptions.KeyValueDelimiter := '=';
if Length(fOptions.BooleanTrueValues) = 0 then
begin
SetLength(fOptions.BooleanTrueValues, Length(cDefaultBooleanTrueValues));
for lIndex := Low(cDefaultBooleanTrueValues) to High(cDefaultBooleanTrueValues) do
fOptions.BooleanTrueValues[lIndex] := cDefaultBooleanTrueValues[lIndex];
end;
if fOptions.BooleanTrueValue = '' then
fOptions.BooleanTrueValue := cDefaultBooleanTrueValue;
if fOptions.BooleanFalseValue = '' then
fOptions.BooleanFalseValue := cDefaultBooleanFalseValue;
end;
function TRichIniFile.SectionToken(const aSection: string): string;
begin
Result := aSection;
end;
function TRichIniFile.KeyToken(const aKey: string): string;
begin
Result := aKey;
end;
function TRichIniFile.GetLineCount: Integer;
begin
if fLines <> nil then
Result := fLines.Count
else
Result := 0;
end;
function TRichIniFile.GetOrCreateSectionList(const aToken: string): TObjectList<TRichIniSectionBlock>;
begin
if not fSectionMap.TryGetValue(aToken, Result) then
begin
Result := TObjectList<TRichIniSectionBlock>.Create(False);
fSectionMap.Add(aToken, Result);
end;
end;
function TRichIniFile.GetSectionBlockCount: Integer;
begin
if fSections <> nil then
Result := fSections.Count
else
Result := 0;
end;
function TRichIniFile.CurrentComparer: IEqualityComparer<string>;
begin
if fComparer = nil then
begin
if fOptions.CaseSensitivity = csCaseInsensitive then
fComparer := TFastCaseAwareComparer.OrdinalIgnoreCase
else
fComparer := TFastCaseAwareComparer.Ordinal;
end;
Result := fComparer;
end;
function TRichIniFile.TokensEqual(const aLeft, aRight: string): Boolean;
begin
Result := CurrentComparer.Equals(aLeft, aRight);
end;
function TRichIniFile.FindSectionList(const aSection: string): TObjectList<TRichIniSectionBlock>;
var
lToken: string;
begin
lToken := SectionToken(aSection);
if not fSectionMap.TryGetValue(lToken, Result) then
Result := nil;
end;
function TRichIniFile.FindLastKeyLine(const aSection, aKey: string): TRichIniKeyLine;
var
lKeyList: TObjectList<TRichIniKeyLine>;
lSectionList: TObjectList<TRichIniSectionBlock>;
lKeyToken: string;
i: Integer;
lBlock: TRichIniSectionBlock;
begin
Result := nil;
if fLines = nil then
Exit;
lSectionList := FindSectionList(aSection);
if lSectionList = nil then
Exit;
lKeyToken := KeyToken(aKey);
for i := lSectionList.Count - 1 downto 0 do
begin
lBlock := lSectionList[i];
lKeyList := lBlock.KeyLines(lKeyToken);
if (lKeyList <> nil) and (lKeyList.Count > 0) then
begin
Result := lKeyList[lKeyList.Count - 1];
Exit;
end;
end;
end;
function TRichIniFile.BuildKeyRawText(const aKey, aValue: string): string;
begin
Result := aKey + string(fOptions.KeyValueDelimiter) + aValue;
end;
function TRichIniFile.EnsureSectionBlockForWrite(const aSection: string): TRichIniSectionBlock;
var
lList: TObjectList<TRichIniSectionBlock>;
lHeaderLine: TRichIniSectionLine;
begin
lList := FindSectionList(aSection);
if (lList <> nil) and (lList.Count > 0) then
Exit(lList[lList.Count - 1]);
if aSection = '' then
begin
Result := CreateSectionBlock('', True);
Exit;
end;
Result := CreateSectionBlock(aSection, False);
lHeaderLine := TRichIniSectionLine.Create(aSection, '[' + aSection + ']');
lHeaderLine.SectionBlock := Result;
lHeaderLine.Modified := True;
Result.HeaderLine := lHeaderLine;
fLines.Add(lHeaderLine);
fDirty := True;
end;
function TRichIniFile.GetSectionInsertIndex(const aBlock: TRichIniSectionBlock): Integer;
var
i: Integer;
lHeaderIndex: Integer;
begin
Result := fLines.Count;
if aBlock = nil then
Exit;
for i := fLines.Count - 1 downto 0 do
if fLines[i].SectionBlock = aBlock then
Exit(i + 1);
if (aBlock.HeaderLine <> nil) then
begin
lHeaderIndex := fLines.IndexOf(aBlock.HeaderLine);
if lHeaderIndex >= 0 then
Exit(lHeaderIndex + 1);
end;
if aBlock.NoHeader then
Exit(0);
end;
function TRichIniFile.FindKeyLineByIndex(const aSection, aKey: string; aKeyIndex: Integer): TRichIniKeyLine;
var
lCount: Integer;
lLine: TRichIniLine;
lSectionToken: string;
lKeyToken: string;
begin
Result := nil;
if aKeyIndex < 0 then
Exit;
lCount := -1;
lSectionToken := SectionToken(aSection);
lKeyToken := KeyToken(aKey);
for lLine in fLines do
begin
if (lLine.Kind <> lkKeyValue) or (lLine.SectionBlock = nil) then
Continue;
if TokensEqual(lLine.SectionBlock.LookupName, lSectionToken) and
TokensEqual(TRichIniKeyLine(lLine).LookupKey, lKeyToken) then
begin
Inc(lCount);
if lCount = aKeyIndex then
begin
Result := TRichIniKeyLine(lLine);
Exit;
end;
end;
end;
end;
procedure TRichIniFile.RemoveOwnedComments(aLine: TRichIniLine);
var
lComment: TRichIniCommentLine;
begin
if (aLine = nil) or (aLine.OwnedCommentCount = 0) then
Exit;
while aLine.OwnedCommentCount > 0 do
begin
lComment := aLine.OwnedComments[aLine.OwnedCommentCount - 1];
aLine.DetachComment(lComment);
if fLines.IndexOf(lComment) >= 0 then
begin
fLines.Remove(lComment);
fDirty := True;
end;
end;
end;
procedure TRichIniFile.DeleteKeyLineInstance(aLine: TRichIniKeyLine);
var
lBlock: TRichIniSectionBlock;
begin
if aLine = nil then
Exit;
RemoveOwnedComments(aLine);
lBlock := aLine.SectionBlock;
if lBlock <> nil then
lBlock.RemoveKeyLine(aLine);
if fLines.IndexOf(aLine) >= 0 then
fLines.Remove(aLine);
fDirty := True;
end;
function TRichIniFile.DefaultCommentPrefix: string;
begin
if Length(fOptions.CommentPrefixes) > 0 then
Result := fOptions.CommentPrefixes[0]
else
Result := ';';
end;
function TRichIniFile.IsTrueBooleanValue(const aValue: string): Boolean;
var
lValue: string;
lTrueValue: string;
begin
lValue := Trim(aValue);
for lTrueValue in fOptions.BooleanTrueValues do
if SameText(lValue, lTrueValue) then
Exit(True);
Result := False;
end;
function TRichIniFile.SplitToLines(const aText: string): TArray<string>;
var
lNormalized: string;
lList: TList<string>;