-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.ArrayIniFile.pas
578 lines (445 loc) · 13.9 KB
/
JPL.ArrayIniFile.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
unit JPL.ArrayIniFile;
{
Jacek Pazera
https://www.pazera-software.com
https://github.com/jackdp
-----------------------------------------------
TArrayIniFile
A lightweight class for managing INI files, based on arrays.
Designed for small console applications.
No Classes, only SysUtils!
-----------------------------------------------
Tips & Infos:
1. Property Ini.FileName is Read and Write.
If you change FileName, TArrayIniFile will load the specified file and add all sections from this file to the existing ones. So, you
can load data from many files.
To remove all sections, call Ini.Clear.
2. To parse custom text (not file content), call Ini.AsString := String_With_The_Valid_Ini_Content;
If you do not want to load data from a file, pass the name of a non-existent file as a parameter in the constructor:
Ini := TArrayIniFile.Create('');
then
Ini.AsString := String_With_The_Valid_Ini_Content;
Similar to FileName, you can call Ini.AsString many times with the different values.
-----------------------------------------------
How to use:
1. First, create instance:
Ini := TArrayIniFile.Create('SomeFile.ini');
OR
Ini := TArrayIniFile.Create('');
Invalid file name. You can assign a valid INI string: Ini.AsString = String_With_The_Valid_Ini_Content;
OR
Ini := TArrayIniFile.Create('SomeFile.ini, TEncoding.UTF8);
Ini := TArrayIniFile.Create('', TEncoding.Unicode);
Ecoding will be used while saving INI file (Ini.UpdateFile). When loading a file, the encoding is detected automatically.
2. Adding sections:
var
Section: TIniSection;
...
Section := Ini.AddSection('SectionName');
...
Section := Ini.AddSection('Second_Section');
3. Getting section:
var Section: TIniSection;
A)
Section := Ini.GetSection('SectionName');
if Assigned(Section) then .. do something ...
B)
Section := Ini.GetSection('SectionName', True); // Second parameter = "True": The section will be created if it does not already exist.
.. do something ...
4. Reading / writing values:
var
Section: TIniSection;
s: string;
x: integer;
Section := Ini.GetSection('SectionName', True);
s := Section.ReadString('Identifier', 'Default value');
Writeln(s);
x := Section.ReadInteger('Identifier2', -1);
Writeln(x);
Section.WriteString('Identifier', 'Value);
Section.WriteInteger('Identifier2', 1024);
5. Save INI file:
var b: Boolean;
b := Ini.UpdateFile;
if b then Writeln('The file "', Ini.FileName, '" has been saved.')
else Writeln('An error occurred while saving the file: "', Ini.FileName, '"!');
6. Ini.Free
}
{$I .\..\jp.inc}
{$IFDEF FPC}
{$MODE DELPHI}
{$MODESWITCH ADVANCEDRECORDS}
{$ENDIF}
interface
uses
SysUtils, Types,
JPL.Strings,
JPL.TStr;
type
TArrayIniFileException = class(Exception);
TIniSectionItem = record
Ident: string;
Value: string;
procedure Clear;
function AsString: string;
end;
TIniSectionItems = array of TIniSectionItem;
{$region ' TIniSection '}
TIniSection = class
private
FItems: TIniSectionItems;
FItemsCount: integer;
FSectionName: string;
FDeltaSize: integer;
function GetCapacity: integer;
function GetItems(Index: integer): TIniSectionItem;
procedure SetItems(Index: integer; Item: TIniSectionItem);
protected
procedure Clear;
function AddIdent(const Ident, Value: string): integer; // Returns the index of the added item. If Ident does not exists, it will be created
procedure SetIdentValue(const Ident, Value: string); // Calls AddIdent
function GetIdentValue(const Ident: string; Default: string): string;
public
constructor Create(const ASectionName: string);
destructor Destroy; override;
function AsString: string;
function GetIdentIndex(const Ident: string): integer;
function IdentExists(const Ident: string): Boolean;
// ------------ Read & Write routines -----------------
procedure WriteString(const Ident, Value: string);
function ReadString(const Ident, Default: string): string;
procedure WriteInteger(const Ident: string; const Value: integer);
function ReadInteger(const Ident: string; Default: integer): integer;
procedure WriteBool(const Ident: string; const Value: Boolean);
function ReadBool(const Ident: string; Default: Boolean): Boolean;
property SectionName: string read FSectionName write FSectionName;
property ItemsCount: integer read FItemsCount;
property Items[Index: integer]: TIniSectionItem read GetItems write SetItems; default;
property Capacity: integer read GetCapacity;
end;
{$endregion TIniSection}
TIniSections = array of TIniSection;
{$region ' TArrayIniFile '}
TArrayIniFile = class
private
FFileName: string;
FSections: TIniSections;
FEncoding: TEncoding;
function GetSectionCount: integer;
function GetAsString: string;
function GetSections(Index: integer): TIniSection;
procedure SetAsString(const IniFileContent: string);
procedure SetFileName(const AFileName: string);
protected
function PerformAddSection(const SectionName: string): integer; // Returns the index of the added section
procedure LoadFile;
procedure ParseText(const Text: string);
public
constructor Create(const AFileName: string); overload;
constructor Create(const AFileName: string; AEncoding: TEncoding); overload;
destructor Destroy; override;
procedure Clear;
function GetSectionIndex(const SectionName: string): integer;
function SectionExists(const SectionName: string): Boolean;
function AddSection(const SectionName: string): TIniSection;
function GetSection(const SectionName: string; CreateIfNotExists: Boolean = True): TIniSection;
function UpdateFile: Boolean;
property FileName: string read FFileName write SetFileName;
property SectionCount: integer read GetSectionCount;
property AsString: string read GetAsString write SetAsString;
property Sections[Index: integer]: TIniSection read GetSections;
end;
{$endregion TArrayIniFile}
implementation
{$region ' TArrayIniFile '}
constructor TArrayIniFile.Create(const AFileName: string);
begin
Create(AFileName, TEncoding.Default);
end;
constructor TArrayIniFile.Create(const AFileName: string; AEncoding: TEncoding);
begin
inherited Create;
FFileName := AFileName;
SetLength(FSections, 0);
FEncoding := AEncoding;
if FileExists(FFileName) then LoadFile;
end;
destructor TArrayIniFile.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TArrayIniFile.Clear;
var
i: integer;
begin
for i := 0 to SectionCount - 1 do
begin
if not Assigned(FSections[i]) then Continue;
FSections[i].Clear;
FSections[i].Free;
end;
SetLength(FSections, 0);
end;
function TArrayIniFile.GetSectionCount: integer;
begin
Result := Length(FSections);
end;
function TArrayIniFile.GetSectionIndex(const SectionName: string): integer;
var
uSectionName: string;
i: integer;
begin
Result := -1;
if SectionCount <= 0 then Exit;
uSectionName := AnsiUpperCase(SectionName);
for i := 0 to SectionCount - 1 do
if AnsiUpperCase(FSections[i].SectionName) = uSectionName then
begin
Result := i;
Break;
end;
end;
function TArrayIniFile.SectionExists(const SectionName: string): Boolean;
begin
Result := GetSectionIndex(SectionName) >= 0;
end;
function TArrayIniFile.AddSection(const SectionName: string): TIniSection;
begin
Result := GetSection(SectionName, True);
end;
function TArrayIniFile.GetSection(const SectionName: string; CreateIfNotExists: Boolean): TIniSection;
var
xInd: integer;
begin
Result := nil;
xInd := GetSectionIndex(SectionName);
if xInd >= 0 then
begin
Result := FSections[xInd];
Exit;
end;
if not CreateIfNotExists then Exit;
xInd := PerformAddSection(SectionName);
Result := FSections[xInd];
end;
function TArrayIniFile.PerformAddSection(const SectionName: string): integer;
var
Section: TIniSection;
begin
Section := TIniSection.Create(SectionName);
SetLength(FSections, Length(FSections) + 1);
FSections[Length(FSections) - 1] := Section;
Result := Length(FSections) - 1;
end;
procedure TArrayIniFile.SetFileName(const AFileName: string);
begin
if FFileName = AFileName then Exit;
FFileName := AFileName;
LoadFile;
end;
procedure TArrayIniFile.LoadFile;
var
s: string;
begin
s := '';
if not FileExists(FFileName) then Exit;
if not LoadStringFromFile(FFileName, s, TEncoding.UTF8) then Exit;
ParseText(s);
end;
function TArrayIniFile.GetAsString: string;
var
i: integer;
begin
Result := '';
for i := 0 to SectionCount - 1 do
Result := Result + FSections[i].AsString;
end;
function TArrayIniFile.GetSections(Index: integer): TIniSection;
begin
if Index > Length(FSections) - 1 then
raise TArrayIniFileException.Create('TArrayIniFile.GetSections: Index out of bounds (' + IntToStr(Index) + ').');
Result := FSections[Index];
end;
procedure TArrayIniFile.SetAsString(const IniFileContent: string);
begin
ParseText(IniFileContent);
end;
procedure TArrayIniFile.ParseText(const Text: string);
var
Section: TIniSection;
i, xp: integer;
Line: string;
SectionName, sIdent, sValue: string;
Arr: TStringDynArray {$IFDEF FPC}= nil{$ENDIF};
begin
Section := nil;
SplitStrToArrayEx(Text, Arr, sLineBreak);
for i := 0 to Length(Arr) - 1 do
begin
Line := Trim(Arr[i]);
if Line = '' then Continue;
if Line[1] = ';' then Continue;
if TStr.IsBoundedWith(Line, '[', ']') then
begin
SectionName := TStr.TrimBounds(Line, '[', ']');
Section := Self.AddSection(SectionName);
Continue;
end;
if not Assigned(Section) then Continue;
xp := Pos('=', Line);
if xp = 0 then Continue;
sIdent := Copy(Line, 1, xp - 1);
sValue := Copy(Line, xp + 1, Length(Line));
Section.AddIdent(sIdent, sValue);
end;
end;
function TArrayIniFile.UpdateFile: Boolean;
begin
Result := SaveStringToFile(FFileName, AsString, FEncoding);
end;
{$endregion TArrayIniFile}
{$region ' TIniSection '}
constructor TIniSection.Create(const ASectionName: string);
begin
inherited Create;
FSectionName := ASectionName;
FItemsCount := 0;
FDeltaSize := 100;
SetLength(FItems, FDeltaSize);
end;
destructor TIniSection.Destroy;
begin
Clear;
SetLength(FItems, 0);
inherited Destroy;
end;
function TIniSection.AsString: string;
var
i: integer;
begin
Result := '[' + FSectionName + ']' + sLineBreak;
for i := 0 to FItemsCount - 1 do
Result := Result + FItems[i].AsString + sLineBreak;
end;
procedure TIniSection.Clear;
var
i: integer;
begin
for i := 0 to FItemsCount - 1 do FItems[i].Clear;
FItemsCount := 0;
SetLength(FItems, FDeltaSize);
end;
function TIniSection.AddIdent(const Ident, Value: string): integer;
var
xInd: integer;
begin
xInd := GetIdentIndex(Ident);
if xInd >= 0 then
begin
FItems[xInd].Ident := Ident;
FItems[xInd].Value := Value;
Result := xInd;
Exit;
end;
if Length(FItems) = FItemsCount then SetLength(FItems, Length(FItems) + FDeltaSize);
Inc(FItemsCount);
xInd := FItemsCount - 1;
FItems[xInd].Ident := Ident;
FItems[xInd].Value := Value;
Result := xInd;
end;
procedure TIniSection.SetIdentValue(const Ident, Value: string);
begin
AddIdent(Ident, Value);
end;
function TIniSection.GetIdentValue(const Ident: string; Default: string): string;
var
xInd: integer;
begin
xInd := GetIdentIndex(Ident);
if xInd >= 0 then Result := FItems[xInd].Value
else Result := Default;
end;
function TIniSection.GetIdentIndex(const Ident: string): integer;
var
i: integer;
uIdent: string;
begin
Result := -1;
if FItemsCount = 0 then Exit;
uIdent := AnsiUpperCase(Ident);
for i := 0 to FItemsCount - 1 do
if AnsiUpperCase(FItems[i].Ident) = uIdent then
begin
Result := i;
Break;
end;
end;
function TIniSection.IdentExists(const Ident: string): Boolean;
begin
Result := GetIdentIndex(Ident) >= 0;
end;
function TIniSection.GetItems(Index: integer): TIniSectionItem;
begin
if Index > FItemsCount - 1 then
raise TArrayIniFileException.Create('TIniSection.GetItems: Index out of bounds (' + IntToStr(Index) + ').');
Result := FItems[Index];
end;
function TIniSection.GetCapacity: integer;
begin
Result := Length(FItems);
end;
procedure TIniSection.SetItems(Index: integer; Item: TIniSectionItem);
begin
if Index > FItemsCount - 1 then
raise TArrayIniFileException.Create('TIniSection.SetItems: Index out of bounds (' + IntToStr(Index) + ').');
FItems[Index].Ident := Item.Ident;
FItems[Index].Value := Item.Value;
end;
procedure TIniSection.WriteString(const Ident, Value: string);
begin
AddIdent(Ident, Value);
end;
function TIniSection.ReadString(const Ident, Default: string): string;
begin
Result := GetIdentValue(Ident, Default);
end;
procedure TIniSection.WriteInteger(const Ident: string; const Value: integer);
begin
AddIdent(Ident, IntToStr(Value));
end;
function TIniSection.ReadInteger(const Ident: string; Default: integer): integer;
begin
Result := StrToIntDef(GetIdentValue(Ident, ''), Default);
end;
procedure TIniSection.WriteBool(const Ident: string; const Value: Boolean);
var
s: string;
begin
if Value then s := '1' else s := '0';
WriteString(Ident, s);
end;
function TIniSection.ReadBool(const Ident: string; Default: Boolean): Boolean;
var
s: string;
begin
if Default then s := '1' else s := '0';
s := ReadString(Ident, s);
if s = '1' then Result := True
else if s = '0' then Result := False
else Result := Default;
end;
{$endregion TIniSection}
{$region ' TIniSectionItem '}
procedure TIniSectionItem.Clear;
begin
Ident := '';
Value := '';
end;
function TIniSectionItem.AsString: string;
begin
Result := Ident + '=' + Value;
end;
{$endregion TIniSectionItem}
end.