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
|
{
This file is part of the Free Pascal Integrated Development Environment
Copyright (c) 1998 by B‚rczi G bor
Reading and writing .INI files
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit WINI;
{$ifdef cpullvm}
{$modeswitch nestedprocvars}
{$endif}
interface
uses Objects;
type
PINIEntry = ^TINIEntry;
TINIEntry = object(TObject)
constructor Init(const ALine: string);
constructor Init(const ATag,AValue,AComment: string);
function GetText: string;
function GetTag: string;
function GetComment: string;
function GetValue: string;
procedure SetValue(const S: string);
procedure SetComment(const S: string);
destructor Done; virtual;
private
TagHash : Cardinal;
Tag : PString;
Value : PString;
Comment : PString;
Text : PString;
Modified : boolean;
procedure Split;
end;
PINISection = ^TINISection;
TINISection = object(TObject)
constructor Init(const AName: string);
function GetName: string;
function AddEntry(const S: string): PINIEntry;
function AddEntry(const Tag,Value,Comment: string): PINIEntry;
function SearchEntry(Tag: string): PINIEntry; virtual;
procedure DeleteEntry(Tag: string);
procedure ForEachEntry(EnumProc: TCallbackProcParam); virtual;
destructor Done; virtual;
private
NameHash : Cardinal;
Name : PString;
Entries : PCollection;
end;
PINIFile = ^TINIFile;
TINIFile = object(TObject)
MakeNullEntries: boolean;
constructor Init(const AFileName: string);
function GetFileName: string;
function Read: boolean; virtual;
function Update: boolean; virtual;
function IsModified: boolean; virtual;
function SearchSection(Section: string): PINISection; virtual;
function SearchEntry(const Section, Tag: string): PINIEntry; virtual;
procedure ForEachSection(EnumProc: TCallbackProcParam); virtual;
procedure ForEachEntry(const Section: string; EnumProc: TCallbackProcParam); virtual;
function GetEntry(const Section, Tag, Default: string): string; virtual;
procedure SetEntry(const Section, Tag, Value: string); virtual;
procedure SetEntry(const Section, Tag, Value,Comment: string); virtual;
function GetIntEntry(const Section, Tag: string; Default: longint): longint; virtual;
procedure SetIntEntry(const Section, Tag: string; Value: longint); virtual;
procedure DeleteSection(const Section: string); virtual;
procedure DeleteEntry(const Section, Tag: string);
destructor Done; virtual;
private
{ ReadOnly: boolean;}
Sections: PCollection;
FileName: PString;
end;
const MainSectionName : string[40] = 'MainSection';
CommentChar : char = ';';
ValidStrDelimiters: set of char = ['''','"'];
function EscapeIniText(S : string) : String;
implementation
uses
WUtils;
function EscapeIniText(S : string) : String;
var
delimiter : char;
i: integer;
begin
delimiter:=#0;
while delimiter < #255 do
begin
if (delimiter in ValidStrDelimiters) and
(pos(delimiter,S)=0) then
break;
delimiter:=succ(delimiter);
end;
if delimiter=#255 then
begin
{ we use " delimiter, but the text also contains double quotes,
which need to be escaped, by doubling it }
delimiter:='"';
for i:=length(s) downto 1 do
if (s[i]=delimiter) then
s:=copy(s,1,i-1)+delimiter+copy(s,i+1,length(s));
end;
EscapeIniText:=delimiter+s+delimiter;
end;
{$IFOPT Q+}
{$Q-}
{$DEFINE REENABLE_Q}
{$ENDIF}
{$IFOPT R+}
{$R-}
{$DEFINE REENABLE_R}
{$ENDIF}
function CalcHash(const s: String): Cardinal;
var
i: integer;
begin
CalcHash := 0;
for i := 1 to Length(s) do
CalcHash := CalcHash shl 9 - CalcHash shl 4 + Ord(S[I]);
end;
{$IFDEF REENABLE_Q}
{$Q+}
{$ENDIF}
{$IFDEF REENABLE_R}
{$R+}
{$ENDIF}
constructor TINIEntry.Init(const ALine: string);
begin
inherited Init;
Text:=NewStr(ALine);
Split;
end;
constructor TINIEntry.Init(const ATag,AValue,AComment: string);
begin
inherited Init;
Tag:=NewStr(ATag);
Value:=NewStr(AValue);
Comment:=NewStr(AComment);
Text:=NewStr(GetText);
end;
function TINIEntry.GetText: string;
var S,CoS: string;
begin
if Text=nil then
begin
CoS:=GetComment;
S:=GetTag+'='+GetValue;
if Trim(S)='=' then S:=CoS else
if CoS<>'' then
begin
{ if Value contains CommentChar, we need to add delimiters }
if pos(CommentChar,S)>0 then
S:=EscapeIniText(S);
S:=S+' '+CommentChar+' '+CoS;
end
end
else S:=Text^;
GetText:=S;
end;
function TINIEntry.GetTag: string;
begin
GetTag:=GetStr(Tag);
end;
function TINIEntry.GetComment: string;
begin
GetComment:=GetStr(Comment);
end;
function TINIEntry.GetValue: string;
begin
GetValue:=GetStr(Value);
end;
procedure TINIEntry.SetValue(const S: string);
begin
if GetValue<>S then
begin
if Text<>nil then DisposeStr(Text); Text:=nil;
if Value<>nil then DisposeStr(Value);
Value:=NewStr(S);
Modified:=true;
end;
end;
procedure TINIEntry.SetComment(const S: string);
begin
if (GetComment<>S) then
begin
if Text<>nil then DisposeStr(Text); Text:=nil;
if Comment<>nil then DisposeStr(Comment);
Comment:=NewStr(S);
Modified:=true;
end;
end;
procedure TINIEntry.Split;
var S,ValueS: string;
P,P2,StartP: longint;
{ using byte for P2 lead to infinite loops PM }
C: char;
InString: boolean;
Delimiter: char;
begin
S:=GetText;
Delimiter:=#0;
P:=Pos('=',S); P2:=Pos(CommentChar,S);
if (P2<>0) and (P2<P) then
P:=0;
if P<>0 then
begin
Tag:=NewStr(copy(S,1,P-1));
TagHash:=CalcHash(UpcaseStr(Tag^));
P2:=P+1; InString:=false; ValueS:='';
StartP:=P2;
while (P2<=length(S)) do
begin
C:=S[P2];
if (P2=StartP) and (C in ValidStrDelimiters) then
begin
Delimiter:=C;
InString:=true;
end
else if (C=Delimiter) then
begin
{ Delimiter inside escaped Value are simply doubled }
if (P2+1<length(S)) and (S[P2+1]=Delimiter) then
ValueS:=ValueS+Delimiter
else
InString:=not InString;
end
else if (C=CommentChar) and (InString=false) then
Break
else
ValueS:=ValueS+C;
Inc(P2);
end;
Value:=NewStr(Trim(ValueS));
Comment:=NewStr(copy(S,P2+1,High(S)));
{ dispose raw text as special treatment is needed for
write }
if assigned(Comment) and assigned(Text) and
(delimiter<>#0) then
begin
DisposeStr(Text);
Text:=nil;
end;
end
else
begin
Tag:=nil;
TagHash:=0;
Value:=nil;
Comment:=NewStr(S);
end;
end;
destructor TINIEntry.Done;
begin
inherited Done;
if Text<>nil then DisposeStr(Text);
if Tag<>nil then DisposeStr(Tag);
if Value<>nil then DisposeStr(Value);
if Comment<>nil then DisposeStr(Comment);
end;
constructor TINISection.Init(const AName: string);
begin
inherited Init;
Name:=NewStr(AName);
NameHash:=CalcHash(UpcaseStr(AName));
New(Entries, Init(50,500));
end;
function TINISection.GetName: string;
begin
GetName:=GetStr(Name);
end;
function TINISection.AddEntry(const S: string): PINIEntry;
var
E: PINIEntry;
Tag : String;
begin
if pos('=',S)>0 then
begin
Tag:=copy(S,1,pos('=',S)-1);
E:=SearchEntry(Tag);
end
else
E:=nil;
if not assigned(E) then
begin
New(E, Init(S));
Entries^.Insert(E);
end
else
begin
if assigned(E^.Text) then
DisposeStr(E^.Text);
E^.Text:=NewStr(S);
E^.Split;
end;
AddEntry:=E;
end;
function TINISection.AddEntry(const Tag,Value,Comment: string): PINIEntry;
var E: PINIEntry;
begin
E:=SearchEntry(Tag);
if not assigned(E) then
begin
New(E, Init(Tag,Value,Comment));
Entries^.Insert(E);
end
else
begin
E^.SetValue(Value);
if Comment<>'' then
E^.SetComment(Comment);
end;
AddEntry:=E;
end;
procedure TINIFile.ForEachSection(EnumProc: TCallbackProcParam);
var I: Sw_integer;
S: PINISection;
begin
for I:=0 to Sections^.Count-1 do
begin
S:=Sections^.At(I);
CallPointerLocal(EnumProc,get_caller_frame(get_frame,get_pc_addr),S);
end;
end;
procedure TINISection.ForEachEntry(EnumProc: TCallbackProcParam);
var I: integer;
E: PINIEntry;
begin
for I:=0 to Entries^.Count-1 do
begin
E:=Entries^.At(I);
CallPointerLocal(EnumProc,get_caller_frame(get_frame,get_pc_addr),E);
end;
end;
function TINISection.SearchEntry(Tag: string): PINIEntry;
var
P : PINIEntry;
I : Sw_integer;
Hash : Cardinal;
begin
SearchEntry:=nil;
Tag:=UpcaseStr(Tag);
Hash:=CalcHash(Tag);
for I:=0 to Entries^.Count-1 do
begin
P:=Entries^.At(I);
if (P^.TagHash=Hash) and (UpcaseStr(P^.GetTag)=Tag) then
begin
SearchEntry:=P;
break;
end;
end;
end;
procedure TINISection.DeleteEntry(Tag: string);
var
P : PIniEntry;
begin
P:=SearchEntry(Tag);
if assigned(P) then
Entries^.Free(P);
end;
destructor TINISection.Done;
begin
inherited Done;
if Name<>nil then DisposeStr(Name);
Dispose(Entries, Done);
end;
constructor TINIFile.Init(const AFileName: string);
begin
inherited Init;
FileName:=NewStr(AFileName);
New(Sections, Init(50,50));
Read;
end;
function TINIFile.GetFileName: string;
begin
GetFileName:=GetStr(FileName);
end;
function TINIFile.Read: boolean;
var f: text;
OK: boolean;
S,TS: string;
P: PINISection;
I: integer;
begin
New(P, Init(MainSectionName));
Sections^.Insert(P);
Assign(f,FileName^);
{$I-}
Reset(f);
OK:=EatIO=0;
while OK and (Eof(f)=false) do
begin
readln(f,S);
TS:=Trim(S);
OK:=EatIO=0;
if OK then
if TS<>'' then
if copy(TS,1,1)='[' then
begin
I:=Pos(']',TS); if I=0 then I:=length(TS)+1;
New(P, Init(copy(TS,2,I-2)));
Sections^.Insert(P);
end else
begin
P^.AddEntry(S);
end;
end;
Close(f);
EatIO;
{$I+}
Read:=true;
end;
function TINIFile.IsModified: boolean;
function SectionModified(P: PINISection): boolean;
function EntryModified(E: PINIEntry): boolean;
begin
EntryModified:=E^.Modified;
end;
begin
SectionModified:=(P^.Entries^.FirstThat(TCallbackFunBoolParam(@EntryModified))<>nil);
end;
begin
IsModified:=(Sections^.FirstThat(TCallbackFunBoolParam(@SectionModified))<>nil);
end;
function TINIFile.Update: boolean;
var f: text;
OK: boolean;
P: PINISection;
E: PINIEntry;
I,J: integer;
begin
Assign(f,FileName^);
{$I-}
Rewrite(f);
OK:=EatIO=0;
if OK then
for I:=0 to Sections^.Count-1 do
begin
P:=Sections^.At(I);
if I<>0 then writeln(f,'['+P^.GetName+']');
for J:=0 to P^.Entries^.Count-1 do
begin
E:=P^.Entries^.At(J);
writeln(f,E^.GetText);
OK:=EatIO=0;
if OK=false then Break;
end;
if OK and ((I>0) or (P^.Entries^.Count>0)) and (I<Sections^.Count-1) then
writeln(f,'');
OK:=OK and (EatIO=0);
if OK=false then Break;
end;
Close(f);
EatIO;
{$I+}
if OK then
for I:=0 to Sections^.Count-1 do
begin
P:=Sections^.At(I);
for J:=0 to P^.Entries^.Count-1 do
begin
E:=P^.Entries^.At(J);
E^.Modified:=false;
end;
end;
Update:=OK;
end;
function TINIFile.SearchSection(Section: string): PINISection;
var
P : PINISection;
I : Sw_integer;
Hash : Cardinal;
begin
SearchSection:=nil;
Section:=UpcaseStr(Section);
Hash:=CalcHash(Section);
for I:=0 to Sections^.Count-1 do
begin
P:=Sections^.At(I);
if (P^.NameHash=Hash) and (UpcaseStr(P^.GetName)=Section) then
begin
SearchSection:=P;
break;
end;
end;
end;
function TINIFile.SearchEntry(const Section, Tag: string): PINIEntry;
var P: PINISection;
E: PINIEntry;
begin
P:=SearchSection(Section);
if P=nil then E:=nil else
E:=P^.SearchEntry(Tag);
SearchEntry:=E;
end;
procedure TINIFile.ForEachEntry(const Section: string; EnumProc: TCallbackProcParam);
var P: PINISection;
E: PINIEntry;
I: integer;
begin
P:=SearchSection(Section);
if P<>nil then
for I:=0 to P^.Entries^.Count-1 do
begin
E:=P^.Entries^.At(I);
CallPointerMethodLocal(EnumProc,get_frame,@Self,E);
end;
end;
function TINIFile.GetEntry(const Section, Tag, Default: string): string;
var E: PINIEntry;
S: string;
begin
E:=SearchEntry(Section,Tag);
if E=nil then S:=Default else
S:=E^.GetValue;
GetEntry:=S;
end;
procedure TINIFile.SetEntry(const Section, Tag, Value,Comment: string);
var E: PINIEntry;
P: PINISection;
begin
E:=SearchEntry(Section,Tag);
if E=nil then
if (MakeNullEntries=true) or (Value<>'') then
begin
P:=SearchSection(Section);
if P=nil then
begin
New(P, Init(Section));
Sections^.Insert(P);
end;
E:=P^.AddEntry(Tag,Value,Comment);
E^.Modified:=true;
end;
if E<>nil then
E^.SetValue(Value);
end;
procedure TINIFile.SetEntry(const Section, Tag, Value: string);
begin
SetEntry(Section,Tag,Value,'');
end;
function TINIFile.GetIntEntry(const Section, Tag: string; Default: longint): longint;
var L: longint;
begin
L:=StrToInt(GetEntry(Section,Tag,IntToStr(Default)));
if LastStrToIntResult<>0 then L:=Default;
GetIntEntry:=L;
end;
procedure TINIFile.SetIntEntry(const Section, Tag: string; Value: longint);
begin
SetEntry(Section,Tag,IntToStr(Value));
end;
procedure TINIFile.DeleteSection(const Section: string);
var P: PINISection;
begin
P:=SearchSection(Section);
if P<>nil then
Sections^.Free(P);
end;
procedure TINIFile.DeleteEntry(const Section, Tag: string);
var P: PINISection;
begin
P:=SearchSection(Section);
if P<>nil then
P^.DeleteEntry(Tag);
end;
destructor TINIFile.Done;
begin
if IsModified then
Update;
inherited Done;
if FileName<>nil then
DisposeStr(FileName);
Dispose(Sections, Done);
end;
END.
|