-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.IniFile.pas
2408 lines (2042 loc) · 69 KB
/
JPL.IniFile.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit JPL.IniFile;
{
Jacek Pazera
https://www.pazera-software.com
First version: 06.2015
TJPIniFile - INI file with comments support
// Co mnie napadło, żeby to zrobić na Kolekcjach?!!!
HINTS
1.
If you want to perform large number of operations, do it on the TJPIniSection (or TJPIniSectionItems) object, not TJPIniFile.
It's much faster this way.
var
Section: TJPIniSection;
...
Section := Ini.GetSection('SectionName');
Section.WriteInteger...
Section.ReadString....
2.
The first Section (Index = 0, Name = '') stores the INI file comment(s). It's not a real INI file section!
3.
WriteIniComment adds one line to the end of the current INI file comment (Section 0).
4.
WriteComment(Section... - adds one line to the Section comment (at the beginning or the end of the given Section).
5.
INI_NO_GRAPHICS
Define this condition if you don't want to use the Graphics unit and its dependencies.
This can significantly reduce the size of the executable, especially for console applications.
TODO
Add support for the Key comment
}
//{$define INI_NO_GRAPHICS}
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
SysUtils, Classes, StrUtils, {$IFNDEF INI_NO_GRAPHICS}Graphics, JPL.Colors,{$ENDIF}
JPL.Strings, JPL.Conversion;
type
TJPIniItemType = (iitNormal, iitComment, iitTextLine);
// iitNormal: TJPIniItem.Value = Key value
// iitComment: TJPIniItem.Value = comment
// iitTextLine: TJPIniItem.Value = text line
TJPIniItemTypeSet = set of TJPIniItemType;
{$region ' ------------- TJPIniItem - collection item -------------- '}
TJPIniItem = class(TCollectionItem)
private
FItemType: TJPIniItemType;
FKey: string;
FValue: string;
procedure SetItemType(const Value: TJPIniItemType);
procedure SetKey(const Value: string);
procedure SetValue(const Value: string);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function AsString: string;
property ItemType: TJPIniItemType read FItemType write SetItemType;
property Key: string read FKey write SetKey;
property Value: string read FValue write SetValue;
end;
{$endregion}
{$region ' --------------- TJPIniSectionItems - collection ---------------- '}
TJPIniSectionItems = class(TCollection)
private
procedure SetItem(Index: Integer; const Value: TJPIniItem);
protected
function GetKeyIndex(Key: string): integer;
function GetNewSectionCommentItem: TJPIniItem;
public
constructor Create({%H-}AItemClass: TCollectionItemClass);
function GetItem(Index: Integer): TJPIniItem; overload;
function GetItem(Key: string; CreateIfNotExists: Boolean = True): TJPIniItem; overload;
function Add: TJPIniItem;
procedure Delete(Index: Integer);
function Insert(Index: Integer): TJPIniItem;
function AsString: string;
function ReadString(const Key, Default: string): string;
function ReadInteger(const Key: string; Default: integer): integer;
function ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
function ReadBool(const Key: string; Default: Boolean): Boolean;
function ReadFloat(const Key: string; Default: Double): Double;
function ReadDate(const Key: string; Default: TDateTime): TDateTime;
function ReadDateTime(const Key: string; Default: TDateTime): TDateTime;
function ReadTime(const Key: string; Default: TDateTime): TDateTime;
function ReadBinaryStream(const Key: string; Value: TStream): integer;
{$IFNDEF INI_NO_GRAPHICS}
function ReadColor(const Key: string; Default: TColor): TColor;
function ReadHtmlColor(const Key: string; const Default: TColor): TColor;
function ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles;
{$ENDIF}
procedure WriteString(const Key, Value: string);
procedure WriteInteger(const Key: string; const Value: integer);
procedure WriteBool(const Key: string; const Value: Boolean);
procedure WriteFloat(const Key: string; const Value: Double);
procedure WriteDate(const Key: string; const Value: TDateTime);
procedure WriteDateTime(const Key: string; const Value: TDateTime);
procedure WriteTime(const Key: string; const Value: TDateTime);
procedure WriteBinaryStream(const Key: string; Value: TStream);
{$IFNDEF INI_NO_GRAPHICS}
procedure WriteColor(const Key: string; const Value: TColor);
procedure WriteHtmlColor(const Key: string; const Value: TColor);
procedure WriteFontStyle(const Key: string; const Value: TFontStyles);
{$ENDIF}
procedure WriteComment(const Value: string; bAddToTop: Boolean); overload;
procedure WriteComment(Strings: TStrings; bAddToTop: Boolean); overload;
function RemoveSectionComment(bFromTop: Boolean): Boolean;
procedure WriteTextLine(const Value: string);
function DeletKey(const Key: string): Boolean;
function RenameKey(const OldKeyName, NewKeyName: string): Boolean;
function KeyExists(const Key: string): Boolean;
procedure RemoveAllComments;
property Items[Index: Integer]: TJPIniItem read GetItem write SetItem; default;
end;
{$endregion}
{$region ' --------------- TJPIniSection - collection item ---------------- '}
TJPIniSection = class(TCollectionItem)
private
FName: string;
FSectionItems: TJPIniSectionItems;
procedure SetName(const Value: string);
procedure SetSectionItems(const Value: TJPIniSectionItems);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
function AsString: string;
function ReadString(const Key, Default: string): string;
function ReadInteger(const Key: string; Default: integer): integer;
function ReadIntegerInRange(const Key: string; const Default, Min, Max: integer): integer;
function ReadBool(const Key: string; Default: Boolean): Boolean;
function ReadFloat(const Key: string; Default: Double): Double;
function ReadDate(const Key: string; Default: TDateTime): TDateTime;
function ReadDateTime(const Key: string; Default: TDateTime): TDateTime;
function ReadTime(const Key: string; Default: TDateTime): TDateTime;
function ReadBinaryStream(const Key: string; Value: TStream): integer;
{$IFNDEF INI_NO_GRAPHICS}
function ReadColor(const Key: string; Default: TColor): TColor;
function ReadHtmlColor(const Key: string; const Default: TColor): TColor;
function ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles;
{$ENDIF}
procedure WriteString(const Key, Value: string);
procedure WriteInteger(const Key: string; const Value: integer);
procedure WriteBool(const Key: string; const Value: Boolean);
procedure WriteFloat(const Key: string; const Value: Double);
procedure WriteDate(const Key: string; const Value: TDateTime);
procedure WriteDateTime(const Key: string; const Value: TDateTime);
procedure WriteTime(const Key: string; const Value: TDateTime);
procedure WriteBinaryStream(const Key: string; Value: TStream);
{$IFNDEF INI_NO_GRAPHICS}
procedure WriteColor(const Key: string; const Value: TColor);
procedure WriteHtmlColor(const Key: string; const Value: TColor);
procedure WriteFontStyle(const Key: string; const Value: TFontStyles);
{$ENDIF}
procedure WriteComment(const Value: string; bAddToTop: Boolean); overload;
procedure WriteComment(Strings: TStrings; bAddToTop: Boolean); overload;
function RemoveSectionComment(bFromTop: Boolean): Boolean;
procedure WriteTextLine(const Value: string);
function DeletKey(const Key: string): Boolean;
function RenameKey(const OldKeyName, NewKeyName: string): Boolean;
function KeyExists(const Key: string): Boolean;
procedure RemoveAllComments;
property SectionItems: TJPIniSectionItems read FSectionItems write SetSectionItems;
property Name: string read FName write SetName;
end;
{$endregion}
{$region ' --------------- TJPIniSections - collection ---------------- '}
TJPIniSections = class(TCollection)
private
FSectionsSeparator: string;
function GetItem(Index: Integer): TJPIniSection;
procedure SetItem(Index: Integer; const Value: TJPIniSection);
procedure SetSectionsSeparator(const Value: string);
public
constructor Create({%H-}AItemClass: TCollectionItemClass);
function Add: TJPIniSection;
procedure Delete(Index: Integer);
function Insert(Index: Integer): TJPIniSection;
function AsString: string;
function GetSectionIndex(SectionName: string): integer;
function GetSection(SectionName: string; CreateIfNotExists: Boolean = True): TJPIniSection;
function SectionExists(const SectionName: string): Boolean;
function ReadString(const SectionName, Key, Default: string): string;
function ReadInteger(const SectionName, Key: string; Default: integer): integer;
function ReadBool(const SectionName, Key: string; Default: Boolean): Boolean;
function ReadFloat(const SectionName, Key: string; Default: Double): Double;
function ReadDate(const SectionName, Key: string; Default: TDateTime): TDateTime;
function ReadDateTime(const SectionName, Key: string; Default: TDateTime): TDateTime;
function ReadTime(const SectionName, Key: string; Default: TDateTime): TDateTime;
function ReadBinaryStream(const SectionName, Key: string; Value: TStream): integer;
{$IFNDEF INI_NO_GRAPHICS}
function ReadColor(const SectionName, Key: string; Default: TColor): TColor;
function ReadFontStyle(const SectionName, Key: string; Default: TFontStyles): TFontStyles;
{$ENDIF}
procedure WriteString(const SectionName, Key, Value: string);
procedure WriteInteger(const SectionName, Key: string; const Value: integer);
procedure WriteBool(const SectionName, Key: string; const Value: Boolean);
procedure WriteFloat(const SectionName, Key: string; const Value: Double);
procedure WriteDate(const SectionName, Key: string; const Value: TDateTime);
procedure WriteDateTime(const SectionName, Key: string; const Value: TDateTime);
procedure WriteTime(const SectionName, Key: string; const Value: TDateTime);
procedure WriteBinaryStream(const SectionName, Key: string; Value: TStream);
{$IFNDEF INI_NO_GRAPHICS}
procedure WriteColor(const SectionName, Key: string; const Value: TColor);
procedure WriteFontStyle(const SectionName, Key: string; const Value: TFontStyles);
{$ENDIF}
procedure WriteComment(const SectionName, Value: string; bAddToTop: Boolean); overload;
procedure WriteComment(const SectionName: string; Strings: TStrings; bAddToTop: Boolean); overload;
procedure WriteTextLine(const SectionName, Value: string);
function DeletKey(const SectionName, Key: string): Boolean;
function RenameKey(const SectionName, OldKeyName, NewKeyName: string): Boolean;
function KeyExists(const SectionName, Key: string): Boolean;
property Items[Index: Integer]: TJPIniSection read GetItem write SetItem; default;
property SectionsSeparator: string read FSectionsSeparator write SetSectionsSeparator;
end;
{$endregion}
{$region ' -------------- TJPIniFile ---------------- '}
TJPIniFile = class(TObject)
private
FFileName: string;
FSections: TJPIniSections;
FSectionsSeparator: string;
FCurrentSection: string;
FEncoding: TEncoding;
FUpdateFileOnExit: Boolean;
FIgnoreExceptionsOnSave: Boolean;
{$IFDEF HAS_TSTRINGS_WRITEBOM}FWriteBOM: Boolean;{$ENDIF}
procedure LoadFile;
procedure ParseText(Source: string);
procedure SetSectionsSeparator(const Value: string);
procedure SetCurrentSection(const Value: string);
procedure SetText(const Value: string);
function GetText: string;
procedure SetEncoding(const Value: TEncoding);
procedure SetUpdateFileOnExit(const Value: Boolean);
procedure SetIgnoreExceptionsOnSave(const Value: Boolean);
{$IFDEF HAS_TSTRINGS_WRITEBOM}procedure SetWriteBOM(AValue: Boolean);{$ENDIF}
public
constructor Create(const FileName: string); overload;
constructor Create(const FileName: string; Encoding: TEncoding); overload;
constructor Create(const FileName: string; Encoding: TEncoding; bIgnoreExceptionsOnSave: Boolean); overload;
destructor Destroy; override;
procedure Clear;
procedure Rename(const FileName: string; Reload: Boolean);
procedure UpdateFile;
procedure SaveToFile(const FileName: string);
function GetSectionItems(const SectionName: string): TJPIniSectionItems; // returns reference to TJPIniSectionItems or nil
function GetSection(const SectionName: string; CreateIfNotExists: Boolean): TJPIniSection; // returns reference to TJPIniSection or nil
procedure ClearSection(const SectionName: string); // Clears section, but does not remove
procedure ClearSectionComments(const SectionName: string); // Removes all comments from the given section
procedure ClearSectionKeys(const SectionName: string);
function ReadString(const SectionName, Key, Default: string): string; overload;
function ReadString(const Key, Default: string): string; overload;
function ReadInteger(const SectionName, Key: string; Default: integer): integer; overload;
function ReadInteger(const Key: string; Default: integer): integer; overload;
function ReadBool(const SectionName, Key: string; Default: Boolean): Boolean; overload;
function ReadBool(const Key: string; Default: Boolean): Boolean; overload;
function ReadFloat(const SectionName, Key: string; Default: Double): Double; overload;
function ReadFloat(const Key: string; Default: Double): Double; overload;
function ReadDate(const SectionName, Key: string; Default: TDateTime): TDateTime; overload;
function ReadDate(const Key: string; Default: TDateTime): TDateTime; overload;
function ReadDateTime(const SectionName, Key: string; Default: TDateTime): TDateTime; overload;
function ReadDateTime(const Key: string; Default: TDateTime): TDateTime; overload;
function ReadTime(const SectionName, Key: string; Default: TDateTime): TDateTime; overload;
function ReadTime(const Key: string; Default: TDateTime): TDateTime; overload;
function ReadBinaryStream(const SectionName, Key: string; Value: TStream): integer; overload;
function ReadBinaryStream(const Key: string; Value: TStream): integer; overload;
{$IFNDEF INI_NO_GRAPHICS}
function ReadColor(const SectionName, Key: string; Default: TColor): TColor; overload;
function ReadColor(const Key: string; Default: TColor): TColor; overload;
function ReadFontStyle(const SectionName, Key: string; Default: TFontStyles): TFontStyles; overload;
function ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles; overload;
{$ENDIF}
procedure WriteString(const SectionName, Key, Value: string); overload;
procedure WriteString(const Key, Value: string); overload;
procedure WriteInteger(const SectionName, Key: string; const Value: integer); overload;
procedure WriteInteger(const Key: string; const Value: integer); overload;
procedure WriteBool(const SectionName, Key: string; const Value: Boolean); overload;
procedure WriteBool(const Key: string; const Value: Boolean); overload;
procedure WriteFloat(const SectionName, Key: string; const Value: Double); overload;
procedure WriteFloat(const Key: string; const Value: Double); overload;
procedure WriteDate(const SectionName, Key: string; const Value: TDateTime); overload;
procedure WriteDate(const Key: string; const Value: TDateTime); overload;
procedure WriteDateTime(const SectionName, Key: string; const Value: TDateTime); overload;
procedure WriteDateTime(const Key: string; const Value: TDateTime); overload;
procedure WriteTime(const SectionName, Key: string; const Value: TDateTime); overload;
procedure WriteTime(const Key: string; const Value: TDateTime); overload;
procedure WriteBinaryStream(const SectionName, Key: string; Value: TStream); overload;
procedure WriteBinaryStream(const Key: string; Value: TStream); overload;
{$IFNDEF INI_NO_GRAPHICS}
procedure WriteColor(const SectionName, Key: string; const Value: TColor); overload;
procedure WriteColor(const Key: string; const Value: TColor); overload;
procedure WriteFontStyle(const SectionName, Key: string; const Value: TFontStyles); overload;
procedure WriteFontStyle(const Key: string; const Value: TFontStyles); overload;
{$ENDIF}
// bAddToTop
// If True, the comment will be added at the TOP of the Section
// If False, the omment will be added at the END of the section
procedure WriteComment(const SectionName, Value: string; bAddToTop: Boolean); overload;
procedure WriteComment(const Value: string; bAddToTop: Boolean); overload; // writes comment to the CurrentSection
procedure WriteComment(const SectionName: string; Strings: TStrings; bAddToTop: Boolean); overload;
procedure WriteComment(Strings: TStrings; bAddToTop: Boolean); overload;
function RemoveSectionComment(const SectionName: string; bFromTop: Boolean): Boolean;
procedure WriteTextLine(const SectionName, Value: string); overload;
procedure WriteTextLine(const Value: string); overload;
// INI comment - comment lines before the first section
procedure ReadIniComment(Strings: TStrings);
procedure WriteIniComment(Strings: TStrings); overload;
procedure WriteIniComment(const Value: string); overload;
procedure ClearIniComment;
procedure ReadSectionKeys(const SectionName: string; Strings: TStrings); // returns list of keys in given section
procedure ReadSection(const SectionName: string; Strings: TStrings); // = ReadSectionKeys; Added for combatibility with TIniFile
procedure ReadSections(Strings: TStrings); // returns list of section names
procedure ReadSectionValues(const SectionName: string; Strings: TStrings); // returns list key=value
procedure ReadSectionComments(const SectionName: string; Strings: TStrings);
procedure EraseSection(const SectionName: string); // Clears and removes section
function DeleteKey(const SectionName, Key: string): Boolean;
function RenameKeyKey(const SectionName, OldKeyName, NewKeyName: string): Boolean;
function KeyExists(const SectionName, Key: string): Boolean;
function ValueExists(const SectionName, Key: string): Boolean; // = KeyExists; Added for compatibility with TIniFile
function SectionExists(const SectionName: string): Boolean;
function AddSection(const SectionName: string): Boolean; // Adds new section. Returns False if section already exists or if SectionName = ''
function RenameSection(const OldName, NewName: string): Boolean;
function ItemsCount(const SectionName: string): integer; overload;
function ItemsCount: integer; overload;
function HasItems(const SectionName: string): Boolean; overload;
function HasItems: Boolean; overload;
function KeysCount(const SectionName: string): integer; overload;
function KeysCount: integer; overload;
function HasKeys(const SectionName: string): Boolean; overload;
function HasKeys: Boolean; overload;
function CommentsCount(const SectionName: string): integer; overload;
function CommentsCount: integer; overload;
function HasComments(const SectionName: string): Boolean; overload;
function HasComments: Boolean; overload;
function TextLinesCount(const SectionName: string): integer; overload;
function TextLinesCount: integer; overload;
function HasTextLines(const SectionName: string): Boolean; overload;
function HasTextLines: Boolean; overload;
property FileName: string read FFileName;
property Sections: TJPIniSections read FSections;
property SectionsSeparator: string read FSectionsSeparator write SetSectionsSeparator; //Sections separator within INI file. Default blank line (#13#10)
property CurrentSection: string read FCurrentSection write SetCurrentSection; // Used in Read/Write routines without SectionName
property Text: string read GetText write SetText; // Reads/sets INI source
property Encoding: TEncoding read FEncoding write SetEncoding;
property UpdateFileOnExit: Boolean read FUpdateFileOnExit write SetUpdateFileOnExit; // if True, FileName will be saved on Destroy. Default True
property IgnoreExceptionsOnSave: Boolean read FIgnoreExceptionsOnSave write SetIgnoreExceptionsOnSave;
{$IFDEF HAS_TSTRINGS_WRITEBOM}
property WriteBOM: Boolean read FWriteBOM write SetWriteBOM;
{$ENDIF}
end;
{$endregion TJPIniFile}
{$IFNDEF INI_NO_GRAPHICS}
function FontStylesToStr(FontStyles: TFontStyles): string;
function StrToFontStyles(s: string): TFontStyles;
{$ENDIF}
implementation
{$region ' ------- helpers ---------- '}
{$IFNDEF INI_NO_GRAPHICS}
function FontStylesToStr(FontStyles: TFontStyles): string;
var
s: string;
begin
s := '';
if fsBold in FontStyles then s := 'Bold';
if fsItalic in FontStyles then s := s + ',Italic';
if fsUnderline in FontStyles then s := s + ',Underline';
if fsStrikeOut in FontStyles then s := s + ',StrikeOut';
s := TrimFromStart(s, ',');
Result := s;
end;
function StrToFontStyles(s: string): TFontStyles;
begin
Result := [];
s := UpperCase(s);
if Pos('BOLD', s) > 0 then Result := Result + [fsBold];
if Pos('ITALIC', s) > 0 then Result := Result + [fsItalic];
if Pos('UNDERLINE', s) > 0 then Result := Result + [fsUnderline];
if Pos('STRIKEOUT', s) > 0 then Result := Result + [fsStrikeOut];
end;
{$ENDIF}
{$endregion helpers}
{$region ' ------------------------------------ TJPIniFile ---------------------------------------- '}
constructor TJPIniFile.Create(const FileName: string; Encoding: TEncoding; bIgnoreExceptionsOnSave: Boolean);
begin
inherited Create;
FEncoding := Encoding;
{$IFDEF HAS_TSTRINGS_WRITEBOM}FWriteBOM := True;{$ENDIF}
FFileName := FileName;
FSectionsSeparator := ENDL; //#13#10
FSections := TJPIniSections.Create(TJPIniSection);
FSections.SectionsSeparator := FSectionsSeparator;
FUpdateFileOnExit := True;
FIgnoreExceptionsOnSave := bIgnoreExceptionsOnSave;
LoadFile;
end;
constructor TJPIniFile.Create(const FileName: string);
begin
Create(FileName, nil, False);
end;
constructor TJPIniFile.Create(const FileName: string; Encoding: TEncoding);
begin
Create(FileName, Encoding, False);
end;
destructor TJPIniFile.Destroy;
begin
//FSections.Clear;
if FUpdateFileOnExit then UpdateFile;
if Assigned(FSections) then FSections.Free;
inherited;
end;
procedure TJPIniFile.Clear;
begin
if Assigned(FSections) then FSections.Clear;
end;
{$region ' ------- TJPIniFile.LoadFile & ParseText -------- '}
procedure TJPIniFile.LoadFile;
var
sl: TStringList;
begin
Clear;
if not FileExists(FFileName) then Exit;
sl := TStringList.Create;
try
{$IFDEF DCC}sl.LoadFromFile(FFileName, FEncoding);{$ENDIF}
{$IFDEF FPC}
{$IFDEF HAS_INIFILE_WITH_ENCODING}sl.LoadFromFile(FFileName, FEncoding);{$ELSE}sl.LoadFromFile(FFileName); {$ENDIF}
{$ENDIF}
ParseText(sl.Text);
finally
sl.Free;
end;
end;
procedure TJPIniFile.ParseText(Source: string);
var
sl: TStringList;
i, x: integer;
Line: string;
Section: TJPIniSection;
IniItem: TJPIniItem;
begin
Clear;
sl := TStringList.Create;
try
sl.Text := Source;
Section := Sections.Add;
Section.Name := '';
for i := 0 to sl.Count - 1 do
begin
Line := Trim(sl[i]);
if Line = '' then Continue;
// ------------ section -----------------
if IsBoundedWith(Line, '[', ']') then
begin
Section := Sections.Add;
Section.Name := Trim(TrimBounds(Line, '[', ']'));
end
// ---------- comment --------------
else if Line[1] = ';' then
begin
IniItem := Section.SectionItems.Add;
IniItem.ItemType := iitComment;
IniItem.Value := Copy(Line, 2, Length(Line) - 1);
end
else
begin
IniItem := Section.SectionItems.Add;
x := Pos('=', Line);
// -------------- key=value ---------------
if x > 0 then
begin
IniItem.ItemType := iitNormal;
IniItem.Key := Copy(Line, 1, x - 1);
IniItem.Value := Copy(Line, x + 1, Length(Line));
end
else
// ------------- text line --------------
begin
IniItem.ItemType := iitTextLine;
IniItem.Value := Line;
end;
end;
end;
finally
sl.Free;
end;
end;
{$endregion}
procedure TJPIniFile.ClearIniComment;
begin
ClearSection('');
end;
procedure TJPIniFile.ClearSection(const SectionName: string);
var
Section: TJPIniSection;
begin
Section := GetSection(SectionName, False);
if Section <> nil then Section.SectionItems.Clear;
end;
procedure TJPIniFile.ClearSectionComments(const SectionName: string);
var
Items: TJPIniSectionItems;
begin
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
Items.RemoveAllComments;
end;
procedure TJPIniFile.ClearSectionKeys(const SectionName: string);
var
Items: TJPIniSectionItems;
i: integer;
begin
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := Items.Count - 1 downto 0 do
if Items[i].ItemType = iitNormal then Items.Delete(i);
end;
function TJPIniFile.CommentsCount: integer;
begin
Result := CommentsCount(FCurrentSection);
end;
function TJPIniFile.CommentsCount(const SectionName: string): integer;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := 0;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitComment then Inc(Result);
end;
function TJPIniFile.DeleteKey(const SectionName, Key: string): Boolean;
begin
Result := Sections.DeletKey(SectionName, Key);
end;
function TJPIniFile.RenameKeyKey(const SectionName, OldKeyName, NewKeyName: string): Boolean;
begin
Result := Sections.RenameKey(SectionName, OldKeyName, NewKeyName);
end;
function TJPIniFile.GetSection(const SectionName: string; CreateIfNotExists: Boolean): TJPIniSection;
begin
Result := Sections.GetSection(SectionName, CreateIfNotExists);
end;
function TJPIniFile.GetSectionItems(const SectionName: string): TJPIniSectionItems;
var
Section: TJPIniSection;
begin
Section := GetSection(SectionName, False);
if Section <> nil then Result := Section.SectionItems
else Result := nil;
end;
function TJPIniFile.GetText: string;
begin
if Assigned(FSections) then Result := FSections.AsString
else Result := '';
end;
function TJPIniFile.HasComments(const SectionName: string): Boolean;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := False;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitComment then
begin
Result := True;
Break;
end;
end;
function TJPIniFile.HasComments: Boolean;
begin
Result := HasComments(FCurrentSection);
end;
function TJPIniFile.HasItems(const SectionName: string): Boolean;
begin
Result := ItemsCount(SectionName) > 0;
end;
function TJPIniFile.HasItems: Boolean;
begin
Result := HasItems(FCurrentSection);
end;
function TJPIniFile.HasKeys: Boolean;
begin
Result := HasKeys(FCurrentSection);
end;
function TJPIniFile.HasTextLines: Boolean;
begin
Result := HasTextLines(FCurrentSection);
end;
function TJPIniFile.HasTextLines(const SectionName: string): Boolean;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := False;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitTextLine then
begin
Result := True;
Break;
end;
end;
function TJPIniFile.HasKeys(const SectionName: string): Boolean;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := False;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitNormal then
begin
Result := True;
Break;
end;
end;
function TJPIniFile.ItemsCount: integer;
begin
Result := ItemsCount(FCurrentSection);
end;
function TJPIniFile.ItemsCount(const SectionName: string): integer;
var
Section: TJPIniSection;
begin
Result := 0;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Section := GetSection(SectionName, False);
if Section <> nil then Result := Section.SectionItems.Count;
end;
procedure TJPIniFile.Rename(const FileName: string; Reload: Boolean);
begin
FFileName := FileName;
if Reload then LoadFile;
end;
function TJPIniFile.RenameSection(const OldName, NewName: string): Boolean;
var
Section: TJPIniSection;
begin
Result := False;
//if (OldName.Trim = '') or (NewName.Trim = '') then Exit;
if (Trim(OldName) = '') or (Trim(NewName) = '') then Exit;
if SectionExists(NewName) then Exit;
Section := GetSection(OldName, False);
if Section = nil then Exit;
//Section.Name := NewName.Trim;
Section.Name := Trim(NewName);
Result := True;
end;
procedure TJPIniFile.EraseSection(const SectionName: string);
var
x: integer;
begin
x := Sections.GetSectionIndex(SectionName);
if x >= 0 then Sections.Delete(x);
end;
function TJPIniFile.KeyExists(const SectionName, Key: string): Boolean;
begin
Result := Sections.KeyExists(SectionName, Key);
end;
function TJPIniFile.KeysCount: integer;
begin
Result := KeysCount(FCurrentSection);
end;
function TJPIniFile.KeysCount(const SectionName: string): integer;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := 0;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitNormal then Inc(Result);
end;
function TJPIniFile.AddSection(const SectionName: string): Boolean;
begin
//if (SectionExists(SectionName)) or (SectionName.Trim = '') then Result := False
if (SectionExists(SectionName)) or (Trim(SectionName) = '') then Result := False
else Result := GetSection(SectionName, True) <> nil;
end;
function TJPIniFile.SectionExists(const SectionName: string): Boolean;
begin
Result := Sections.SectionExists(SectionName);
end;
procedure TJPIniFile.SetCurrentSection(const Value: string);
begin
FCurrentSection := Value;
end;
procedure TJPIniFile.SetEncoding(const Value: TEncoding);
begin
FEncoding := Value;
end;
procedure TJPIniFile.SetIgnoreExceptionsOnSave(const Value: Boolean);
begin
FIgnoreExceptionsOnSave := Value;
end;
{$IFDEF HAS_TSTRINGS_WRITEBOM}
procedure TJPIniFile.SetWriteBOM(AValue: Boolean);
begin
FWriteBOM := AValue;
end;
{$ENDIF}
procedure TJPIniFile.SetSectionsSeparator(const Value: string);
begin
FSectionsSeparator := Value;
end;
procedure TJPIniFile.SetText(const Value: string);
begin
ParseText(Value);
end;
procedure TJPIniFile.SetUpdateFileOnExit(const Value: Boolean);
begin
FUpdateFileOnExit := Value;
end;
function TJPIniFile.TextLinesCount: integer;
begin
Result := TextLinesCount(FCurrentSection);
end;
function TJPIniFile.TextLinesCount(const SectionName: string): integer;
var
Items: TJPIniSectionItems;
i: integer;
begin
Result := 0;
//if SectionName.Trim = '' then Exit;
if Trim(SectionName) = '' then Exit;
Items := GetSectionItems(SectionName);
if Items = nil then Exit;
for i := 0 to Items.Count - 1 do
if Items[i].ItemType = iitTextLine then Inc(Result);
end;
procedure TJPIniFile.SaveToFile(const FileName: string);
var
sl: TStringList;
begin
//if FileName.Trim = '' then Exit; //raise Exception.Create('File name can not be blank!');
if Trim(FileName) = '' then Exit;
sl := TStringList.Create;
try
{$IFDEF HAS_TSTRINGS_WRITEBOM}sl.WriteBOM := FWriteBOM;{$ENDIF}
{$IFDEF DCC}
sl.Text := Text;
if FIgnoreExceptionsOnSave then
try
sl.SaveToFile(FileName, FEncoding);
except
end
else sl.SaveToFile(FileName, FEncoding);
{$ELSE}
sl.Text := Text;
if FIgnoreExceptionsOnSave then
try
{$IFDEF HAS_INIFILE_WITH_ENCODING}sl.SaveToFile(FileName, FEncoding);{$ELSE}sl.SaveToFile(FileName);{$ENDIF}
except
end
else {$IFDEF HAS_INIFILE_WITH_ENCODING}sl.SaveToFile(FileName, FEncoding);{$ELSE}sl.SaveToFile(FileName);{$ENDIF}
{$ENDIF}
finally
sl.Free;
end;
end;
procedure TJPIniFile.UpdateFile;
begin
SaveToFile(FFileName);
end;
function TJPIniFile.ValueExists(const SectionName, Key: string): Boolean;
begin
Result := KeyExists(SectionName, Key);
end;
{$region ' --------- Read XXX --------- '}
function TJPIniFile.ReadBinaryStream(const SectionName, Key: string; Value: TStream): integer;
begin
Result := Sections.ReadBinaryStream(SectionName, Key, Value);
end;
function TJPIniFile.ReadBinaryStream(const Key: string; Value: TStream): integer;
begin
Result := ReadBinaryStream(FCurrentSection, Key, Value);
end;
function TJPIniFile.ReadBool(const Key: string; Default: Boolean): Boolean;
begin
Result := ReadBool(FCurrentSection, Key, Default);
end;
function TJPIniFile.ReadBool(const SectionName, Key: string; Default: Boolean): Boolean;
begin
Result := Sections.ReadBool(SectionName, Key, Default);
end;
{$IFNDEF INI_NO_GRAPHICS}
function TJPIniFile.ReadColor(const Key: string; Default: TColor): TColor;
begin
Result := ReadColor(FCurrentSection, Key, Default);
end;
function TJPIniFile.ReadColor(const SectionName, Key: string; Default: TColor): TColor;
begin
Result := Sections.ReadColor(SectionName, Key, Default);
end;
{$ENDIF}
function TJPIniFile.ReadDate(const SectionName, Key: string; Default: TDateTime): TDateTime;
begin
Result := Sections.ReadDate(SectionName, Key, Default);
end;
function TJPIniFile.ReadDate(const Key: string; Default: TDateTime): TDateTime;
begin
Result := ReadDate(FCurrentSection, Key, Default);
end;
function TJPIniFile.ReadDateTime(const Key: string; Default: TDateTime): TDateTime;
begin
Result := ReadDateTime(FCurrentSection, Key, Default);
end;
function TJPIniFile.ReadDateTime(const SectionName, Key: string; Default: TDateTime): TDateTime;
begin
Result := Sections.ReadDateTime(SectionName, Key, Default);
end;
function TJPIniFile.ReadFloat(const SectionName, Key: string; Default: Double): Double;
begin
Result := Sections.ReadFloat(SectionName, Key, Default);
end;
function TJPIniFile.ReadFloat(const Key: string; Default: Double): Double;
begin
Result := ReadFloat(FCurrentSection, Key, Default);
end;
{$IFNDEF INI_NO_GRAPHICS}
function TJPIniFile.ReadFontStyle(const Key: string; Default: TFontStyles): TFontStyles;
begin
Result := ReadFontStyle(FCurrentSection, Key, Default);
end;
function TJPIniFile.ReadFontStyle(const SectionName, Key: string; Default: TFontStyles): TFontStyles;