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
|
unit memini;
interface
function OpenStorage(fname:PWideChar):pointer; overload;
function OpenStorage(fname:PAnsiChar):pointer; overload;
function OpenStorageBuf(buf:PAnsiChar):pointer;
procedure CloseStorage(storage:pointer);
//function GetNamespaceList(storage:pointer):PAnsiChar;
function GetSectionList(storage:pointer;namespace:PAnsiChar=nil):PAnsiChar;
procedure FreeSectionList(ptr:PAnsiChar);
function GetParamStr(storage:pointer;section,param:PAnsiChar;default:PAnsiChar=nil;
namespace:PAnsiChar=nil):PAnsiChar;
function GetParamInt(storage:pointer;section,param:PAnsiChar;default:integer=0;
namespace:PAnsiChar=nil):integer;
function SearchSection(storage:pointer;section:PAnsiChar;namespace:PAnsiChar=nil):pointer;
function GetSectionName(section:pointer):PAnsiChar;
function GetParamSectionStr(section:pointer;param:PAnsiChar;default:PAnsiChar=nil):PAnsiChar;
function GetParamSectionInt(section:pointer;param:PAnsiChar;default:integer=0):integer;
implementation
uses windows,io,common;
const
arstep = 8;
const
ns_separator = ':';
line_separator = '\';
type
pParam = ^tParam;
tParam = record
hash :integer; // param name hash
name :PAnsiChar; // points to source (for write only)
value :PAnsiChar; // points to source? or modified
assign:boolean; // newly assigned value or in INI buffer
end;
pSection = ^tSection;
tSection = record
ns :integer; // namespace hash
code :integer; // section name hash
full :integer; // namespace+section name hash
fullname:PAnsiChar; // pointer to namespace:name
name :PAnsiChar; // pointer to name only
numparam:integer;
arParams:array of tParam;
end;
pStorage = ^tStorage;
tStorage = record
name :PAnsiChar; // filename
buffer :PAnsiChar; // source (INI) text
numsect :integer;
arSection: array of tSection;
end;
function HashOf(txt:PAnsiChar):integer;
begin
result:=Hash(txt,StrLen(txt));
{
result:=0;
while txt^<>#0 do
begin
result:=((result shl 2) or (result shr (SizeOf(result)*8-2))) xor Ord(UpCase(txt^));
inc(txt);
end;
}
end;
// sections adds 1 by 1, without duplicate check
procedure AddSection(data:pStorage;anamespace,aname:PAnsiChar);
var
i:integer;
fnhash:integer;
c:AnsiChar;
begin
// search section with same name
fnhash:=HashOf(anamespace);
for i:=0 to data.numsect-1 do
begin
if data.arSection[i].full=fnhash then
exit; // Doing nothing!
end;
// add section
if data.numsect>High(data.arSection) then
SetLength(data.arSection,Length(data.arSection)+arstep);
FillChar(data.arSection[data.numsect],SizeOf(tSection),0);
with data.arSection[data.numsect] do
begin
fullname:=anamespace;
name :=aname;
full :=fnhash;
if anamespace<>aname then
begin
c:=(aname-1)^;
(aname-1)^:=#0;
code:=HashOf(aname);
ns :=HashOf(anamespace);
(aname-1)^:=c;
end;
end;
inc(data.numsect);
end;
procedure AddParam(data:pStorage;aname,avalue:PAnsiChar;assignvalue:boolean);
begin
// search param with same name?
with data.arSection[data.numsect-1] do
begin
// add param
if numparam>High(arParams) then
SetLength(arParams,Length(arParams)+arstep);
FillChar(arParams[numparam],SizeOf(tParam),0);
with arParams[numparam] do
begin
hash :=HashOf(aname);
name :=aname;
value :=avalue;
assign:=assignvalue;
end;
inc(numparam);
end;
end;
// quotes, multiline etc
// result = pointer to non-parameter line
// pointers: start of value, start of current line, end of value in line, end of current line
function ProcessParamValue(var start:PAnsiChar):PAnsiChar;
var
lineend,eol,dst,bov:PAnsiChar;
multiline,crlf:boolean;
begin
dst:=start;
bov:=start;
result:=nil;
repeat
multiline:=false;
crlf :=false;
// skip starting spaces
while start^ in [#9,' '] do inc(start);
if start^ in [#0,#10,#13] then // empty value or end
begin
while start^ in [#10,#13] do inc(start);
exit;
end;
lineend:=start;
while not (lineend^ in [#0,#10,#13]) do inc(lineend);
eol:=lineend;
dec(lineend);
while lineend^ in [#9,' '] do dec(lineend);
if lineend^=line_separator then // multiline or part of value
begin
if (lineend-1)^ in [#9,' '] then // multiline
begin
dec(lineend);
multiline:=true;
while lineend^ in [#9,' '] do dec(lineend);
end
// double separator = multiline + crlf saving
else if ((lineend-1)^=line_separator) and ((lineend-2)^ in [#9,' ']) then
begin
dec(lineend,2);
multiline:=true;
crlf :=true;
while lineend^ in [#9,' '] do dec(lineend);
end;
end;
// lineend points to last char
// start points to first char
// eol points to end of line
//!! now just starting/ending quotes
if (start^ in ['''','"']) and (lineend^ in ['''','"']) then
begin
inc(start);
dec(lineend);
end;
while start<=lineend do
begin
dst^:=start^;
inc(dst);
inc(start);
end;
if crlf then
begin
dst^:=#13;
inc(dst);
dst^:=#10;
inc(dst);
end;
start:=eol;
while start^ in [#10,#13] do inc(start);
until not multiline;
dst^:=#0;
result:=bov;
end;
procedure TranslateData(data:pStorage);
var
pc2,pc1,pc:PAnsiChar;
len:integer;
begin
pc:=data^.buffer;
data.numsect:=0;
while pc^<>#0 do
begin
while pc^ in [#9,#10,#13,' '] do inc(pc);
// comment
if pc^=';' then
begin
// skip to next line (or end)
while not (pc^ in [#0,#10,#13]) do inc(pc);
// skip empty
while pc^ in [#9,#10,#13,' '] do inc(pc);
end
// section
else if pc^='[' then
begin
inc(pc);
//!! without #0 check
pc1:=pc;
pc2:=pc;
//?? while pc^ in sLatWord do inc(pc);
{}
repeat
len:=CharUTF8Len(pc);
if (len>1) or (pc^ in sLatWord) then
inc(pc,len)
else
break;
until false;
{}
// namespace
if pc^=ns_separator then
begin
inc(pc);
pc2:=pc;
end;
// while pc^ <> ']' do inc(pc);
{}
repeat
len:=CharUTF8Len(pc);
if (len=1) and (pc^ = ']') then
break;
inc(pc,len);
until false;
{}
pc^:=#0; //!!
AddSection(data,pc1,pc2);
inc(pc);
end
// parameter
else if pc^ in sIdFirst then
begin
pc1:=pc;
// skip param name
while pc^ in sLatWord do inc(pc);
pc^:=#0; //!!
// skip spaces
while pc^ in [#9,' '] do inc(pc);
inc(pc); // must be "="
// skip spaces
while pc^ in [#9,' '] do inc(pc);
// pc2:=pc;
// parameter can be quoted
// here need to cut spaces, comments but join next lines
pc2:=ProcessParamValue(pc);
AddParam(data,pc1,pc2,false);
end
else // wrong thing, skip line
while not (pc^ in [#10,#13]) do inc(pc);
end;
end;
function OpenStorageBuf(buf:PAnsiChar):pointer;
begin
result:=nil;
if (buf<>nil) and (buf^<>#0) then
begin
GetMem(result,SizeOf(tStorage));
FillChar(result^,SizeOf(tStorage),0);
StrDup(pStorage(result)^.buffer,buf);
TranslateData(pStorage(result));
end;
end;
function OpenFileStorage(h:THANDLE):pointer;
var
size:integer;
begin
result:=nil;
if h<>THANDLE(INVALID_HANDLE_VALUE) then
begin
size:=FileSize(h);
if size>0 then
begin
GetMem(result,SizeOf(tStorage));
FillChar(result^,SizeOf(tStorage),0);
// save name too?
GetMem(pStorage(result)^.buffer,size+1);
BlockRead(h,pStorage(result)^.buffer^,size);
pStorage(result)^.buffer[size]:=#0;
end;
CloseHandle(h);
TranslateData(pStorage(result));
end;
end;
function OpenStorage(fname:PWideChar):pointer;
begin
if FileExists(fname) then
result:=OpenFileStorage(Reset(fname))
else
result:=nil;
end;
function OpenStorage(fname:PAnsiChar):pointer;
begin
if FileExists(fname) then
result:=OpenFileStorage(Reset(fname))
else
result:=nil;
end;
procedure CloseStorage(storage:pointer);
var
i:integer;
begin
if storage=nil then exit;
with pStorage(storage)^ do
begin
if name<>nil then FreeMem(name);
// cycle by sections
for i:=0 to HIGH(arSection) do
SetLength(arSection[i].arParams,0);
SetLength(arSection,0);
FreeMem(buffer);
end;
FreeMem(storage);
end;
{
function GetNamespaceList(storage:pointer):PAnsiChar;
begin
if storage=nil then
begin
result:=nil;
exit;
end;
end;
}
function GetSectionList(storage:pointer;namespace:PAnsiChar=nil):PAnsiChar;
var
i,lsize,lns:integer;
pc:PAnsiChar;
begin
if storage=nil then
begin
result:=nil;
exit;
end;
// calculate size
lsize:=0;
if (namespace<>nil) and (namespace^<>#0) then
lns:=HashOf(namespace)
else
lns:=0;
with pStorage(storage)^ do
begin
for i:=0 to HIGH(arSection) do
begin
with arSection[i] do
begin
if (namespace=nil) or
((namespace^=#0) and (name=fullname)) then
inc(lsize,StrLen(fullname)+1)
else
begin
if lns<>ns then
continue;
inc(lsize,StrLen(name)+1);
end;
end;
{
if (namespace<>nil) and (namespace^<>#0) then //?? ns=0
begin
if ns<>arSection[i].ns then
continue;
inc(size,StrLen(arSection[i].name)+1);
end
else
inc(size,StrLen(arSection[i].fullname)+1);
}
end;
inc(lsize);
// get memory
GetMem(pc,lsize);
result:=pc;
// fill
for i:=0 to HIGH(arSection) do
begin
with arSection[i] do
begin
if (namespace=nil) or
((namespace^=#0) and (name=fullname)) then
pc:=StrCopyE(pc,fullname)
else
begin
if lns<>ns then
continue;
pc:=StrCopyE(pc,name);
end;
end;
{
if (namespace<>nil) and (namespace^<>#0) then //?? ns=0
begin
if ns<>arSection[i].ns then
continue;
pc:=StrCopyE(pc,arSection[i].name);
end
else
pc:=StrCopyE(pc,arSection[i].fullname);
}
inc(pc);
end;
pc^:=#0;
end;
end;
procedure FreeSectionList(ptr:PAnsiChar);
begin
FreeMem(ptr);
end;
function SearchSection(storage:pointer;section:PAnsiChar;namespace:PAnsiChar=nil):pointer;
var
i:integer;
nsn,nss:integer;
begin
result:=nil;
if storage=nil then exit;
nss:=HashOf(section);
if namespace=nil then
begin
with pStorage(storage)^ do
for i:=0 to numsect-1 do
begin
if arSection[i].full=nss then
begin
result:=@arSection[i];
break;
end;
end;
end
else
begin
nsn:=HashOf(namespace);
with pStorage(storage)^ do
begin
for i:=0 to numsect-1 do
begin
if (arSection[i].ns=nsn) and (arSection[i].code=nss) then
begin
result:=@arSection[i];
break;
end;
end;
end;
end;
end;
function GetSectionName(section:pointer):PAnsiChar;
begin
if section=nil then
result:=nil
else
result:=pSection(section).name;
end;
function SearchParameter(section:pointer;param:PAnsiChar):pointer;
var
i:integer;
nsp:integer;
begin
result:=nil;
if section<>nil then
begin
nsp:=HashOf(param);
with pSection(section)^ do
begin
for i:=0 to numparam-1 do
begin
if arParams[i].hash=nsp then
begin
result:=@arParams[i];
break;
end;
end;
end;
end;
end;
function GetParamSectionStr(section:pointer;param:PAnsiChar;default:PAnsiChar=nil):PAnsiChar;
var
pn:pParam;
begin
result:=default;
if section<>nil then
begin
pn:=SearchParameter(section,param);
if pn<>nil then
result:=pn.value //StrCopy(buffer,value,len-1);
end;
end;
function GetParamSectionInt(section:pointer;param:PAnsiChar;default:integer=0):integer;
var
pn:pParam;
begin
result:=default;
if section<>nil then
begin
pn:=SearchParameter(section,param);
if pn<>nil then
begin
if pn.value[0]='$' then
result:=HexToInt(PAnsiChar(@pn.value[1]))
else
result:=StrToInt(pn.value);
end;
end;
end;
function GetParamStr(storage:pointer;section,param:PAnsiChar;default:PAnsiChar=nil;
namespace:PAnsiChar=nil):PAnsiChar;
var
sn:pSection;
begin
if storage=nil then
begin
result:=default;
exit;
end;
sn:=SearchSection(storage,section,namespace);
result:=GetParamSectionStr(sn,param,default);
end;
function GetParamInt(storage:pointer;section,param:PAnsiChar;default:integer=0;
namespace:PAnsiChar=nil):integer;
var
sn:pSection;
begin
if storage=nil then
begin
result:=default;
exit;
end;
sn:=SearchSection(storage,section,namespace);
result:=GetParamSectionInt(sn,param,default);
end;
end.
|