-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuconfigparser.pas
556 lines (489 loc) · 12.7 KB
/
uconfigparser.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
unit uConfigParser;
{ Nginx Config Parser
Copyright (C) 2018 rasberryrabbit github.com
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
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. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, contnrs;
type
{ TNginxItem }
TNginxItem = class
private
FLevel:Integer;
FName:string;
FValue:string;
public
property NameItem:string read FName write FName;
property Value:string read FValue write FValue;
property Level:Integer read FLevel write FLevel;
end;
{ TNginxItemGroup }
TNginxItemGroup = class(TNginxItem)
private
FParents:TNginxItemGroup;
FItemList: TObjectList;
public
constructor Create;
destructor Destroy; override;
function AddNameValue(lvl: Integer; const sName, sValue: string): Integer;
function InsertNameValue(Idx, lvl: Integer; const sName, sValue: string): TNginxItem;
function AddNameGroup(lvl: Integer; const sName, sValue: string): Integer;
procedure MarkClose;
function InsertNameGroup(Idx, lvl: Integer; const sName, sValue: string): TNginxItemGroup;
function FindItemName(const str: string): TNginxItem;
function FindItemNameNext(BaseItem:TNginxItem; const str: string): TNginxItem;
function FindItemGroup(const str: string): TNginxItemGroup;
function FindItemGroupNext(BaseItem:TNginxItemGroup; const str: string): TNginxItemGroup;
procedure DeleteItem(item:TObject);
function IndexOfItem(item:TNginxItem):Integer;
property Parents:TNginxItemGroup read FParents write FParents;
property ItemList:TObjectList read FItemList;
end;
{ TNginxConfigParser }
TNginxConfigOnToken = procedure(const sName,sValue:string) of object;
TNginxConfigParser = class
private
FStream:TStream;
FBuffer:array[0..4095] of char;
FBufLen:Integer;
FBufIdx:Integer;
FCurLevel:Integer;
FItem:TNginxItemGroup;
FOnToken:TNginxConfigOnToken;
function ReadBufferChk:Boolean;
function ReadToken:string;
function Eof:Boolean;
procedure ReadSkipSpace;
protected
procedure IterGroupSave(Obj:TNginxItemGroup);
public
constructor Create;
destructor Destroy; override;
procedure LoadFromStream(Stream:TStream);
procedure Load(FileName:string);
procedure SaveToStream(Stream:TStream);
procedure Save(FileName:string);
property OnToken:TNginxConfigOnToken read FOnToken write FOnToken;
property ItemList:TNginxItemGroup read FItem;
end;
function NginxRemoveTrailValue(const s:string; delim:char=';'):string;
implementation
uses
strutils;
function NginxRemoveTrailValue(const s: string; delim: char): string;
var
i, l, fspace:Integer;
ch : char;
begin
Result:='';
i:=Length(s);
l:=i;
fspace:=0;
while i>0 do begin
ch := s[i];
if fspace>0 then begin
if ch=delim then begin
Dec(i);
break;
end;
end else begin
if ch>#32 then begin
fspace:=i;
Inc(i);
end;
end;
Dec(i);
end;
// remove space
while i>0 do begin
ch:=s[i];
if ch>#32 then
break;
Dec(i);
end;
if i<=0 then
i:=l;
Result:=Copy(s,1,i);
end;
{ TNginxConfigParser }
function TNginxConfigParser.ReadBufferChk: Boolean;
begin
if Assigned(FStream) then begin
if FBufIdx>=FBufLen then begin
FBufLen:=FStream.Read(FBuffer[0],sizeof(FBuffer));
Result:=FBufLen>0;
FBufIdx:=0;
end else
Result:=True;
end else begin
Result:=False;
FBufLen:=0;
end;
end;
function TNginxConfigParser.ReadToken: string;
var
ch:char;
bReadEol:Boolean;
begin
Result:='';
bReadEol:=False;
ReadSkipSpace;
while FBufIdx<FBufLen do begin
ch:=FBuffer[FBufIdx];
if not bReadEol then begin
{ break on delimiter or space }
if (ch<=#32) or
((ch = ';') and (Result<>'')) or
((ch = '{') and (Result<>''))
then
break;
{ check comment }
if (ch='#') and (Result='') then
bReadEol:=True;
end else
{ read end of line }
if ch in [#10,#13] then
break;
Result:=Result+ch;
Inc(FBufIdx);
if not ReadBufferChk then
break;
end;
end;
function TNginxConfigParser.Eof: Boolean;
begin
ReadBufferChk;
Result:=FBufLen<=0;
end;
procedure TNginxConfigParser.ReadSkipSpace;
var
ch:char;
begin
while ReadBufferChk and (FBufIdx<FBufLen) do begin
ch:=FBuffer[FBufIdx];
if ch>#32 then
break;
Inc(FBufIdx);
end;
end;
procedure TNginxConfigParser.IterGroupSave(Obj: TNginxItemGroup);
var
stemp:string;
temp:TObject;
dummy:Pointer;
begin
for dummy in Obj.ItemList do begin
temp:=TObject(dummy);
stemp:=DupeString(#9,(temp as TNginxItem).Level)+
(temp as TNginxItem).NameItem+
' '+
(temp as TNginxItem).Value+
LineEnding;
if Assigned(FStream) then
FStream.Write(stemp[1],Length(stemp));
if temp is TNginxItemGroup then
IterGroupSave(TNginxItemGroup(temp));
end;
end;
constructor TNginxConfigParser.Create;
begin
inherited Create;
FBufIdx:=0;
FBufLen:=0;
FCurLevel:=0;
FillChar(FBuffer,sizeof(FBuffer),0);
{ root item }
FItem:=TNginxItemGroup.Create;
FItem.NameItem:='';
end;
destructor TNginxConfigParser.Destroy;
begin
FItem.Free;
inherited Destroy;
end;
procedure TNginxConfigParser.LoadFromStream(Stream: TStream);
var
sName, sValue, sTemp: string;
temp: TNginxItemGroup;
iLVL, iCurr: Integer;
begin
FStream:=Stream;
FItem.ItemList.Clear;
FItem.NameItem:='';
temp:=FItem;
iLVL:=0;
while not Eof do begin
sName:=ReadToken;
sValue:='';
if (sName<>'}') and ((sName<>'') and (sName[1]<>'#')) then begin
{ read all values }
repeat
sTemp:=ReadToken;
if (sValue<>'') and (sTemp<>';') then
sValue:=sValue+' ';
sValue:=sValue+sTemp;
until (sTemp=';') or (sTemp='{') or (FBufLen=0);
{ add group or item }
if stemp='{' then begin
iCurr:=temp.AddNameGroup(iLVL,sName,sValue);
temp:=TNginxItemGroup(temp.ItemList[iCurr]);
Inc(iLVL);
end else
iCurr:=temp.AddNameValue(iLVL,sName,sValue);
end else begin
iCurr:=temp.AddNameValue(iLVL,sName,sValue);
{ group end bracket }
if sName='}' then begin
if temp.Parents<>nil then
temp:=temp.Parents;
Dec(iLVL);
end
end;
if Assigned(FOnToken) then
FOnToken(sName,sValue);
end;
end;
// utf8
procedure TNginxConfigParser.Load(FileName: string);
var
fsFile:TFileStream;
begin
fsFile:=TFileStream.Create(FileName,fmOpenRead);
try
LoadFromStream(fsFile);
finally
fsFile.Free;
FStream:=nil;
end;
end;
procedure TNginxConfigParser.SaveToStream(Stream: TStream);
var
temp:TNginxItemGroup;
begin
FStream:=Stream;
temp:=FItem;
IterGroupSave(temp);
end;
// utf8
procedure TNginxConfigParser.Save(FileName: string);
var
fsFile:TFileStream;
begin
fsFile:=TFileStream.Create(FileName,fmCreate or fmOpenWrite);
try
SaveToStream(fsFile);
finally
fsFile.Free;
fsFile:=nil;
end;
end;
{ TNginxItemGroup }
constructor TNginxItemGroup.Create;
begin
inherited Create;
FItemList:=TObjectList.Create(True);
FParents:=nil;
end;
destructor TNginxItemGroup.Destroy;
begin
FItemList.Free;
inherited Destroy;
end;
function TNginxItemGroup.AddNameValue(lvl:Integer; const sName, sValue: string): Integer;
var
temp, tail:TNginxItem;
begin
temp:=TNginxItem.Create;
try
temp.Level:=lvl;
temp.NameItem:=sName;
temp.Value:=sValue;
if (FItemList.Count=0) or
(TNginxItem(FItemList[FItemList.Count-1]).NameItem<>'}')
then
Result:=FItemList.Add(temp)
else begin
FItemList.Insert(FItemList.Count-1,temp);
Result:=FItemList.Count-2;
end;
except
temp.Free;
end;
end;
function TNginxItemGroup.InsertNameValue(Idx, lvl: Integer; const sName,
sValue: string): TNginxItem;
var
temp:TNginxItem;
begin
Result:=nil;
temp:=TNginxItem.Create;
try
temp.Level:=lvl;
temp.NameItem:=sName;
temp.Value:=sValue;
FItemList.Insert(Idx,temp);
Result:=temp;
except
temp.Free;
end;
end;
function TNginxItemGroup.AddNameGroup(lvl: Integer; const sName, sValue: string
): Integer;
var
temp:TNginxItemGroup;
begin
temp:=TNginxItemGroup.Create;
try
temp.Level:=lvl;
temp.NameItem:=sName;
temp.Value:=sValue;
temp.Parents:=self;
if (FItemList.Count=0) or
((FItemList[FItemList.Count-1] is TNginxItem) and
(TNginxItem(FItemList[FItemList.Count-1]).NameItem<>'}'))
then
Result:=FItemList.Add(temp)
else begin
FItemList.Insert(FItemList.Count-1,temp);
Result:=FItemList.Count-2;
end;
except
temp.Free;
end;
end;
procedure TNginxItemGroup.MarkClose;
var
temp:TNginxItem;
begin
temp:=TNginxItem.Create;
try
temp.Level:=Level;
temp.NameItem:='}';
temp.Value:='';
FItemList.Insert(FItemList.Count,temp);
except
temp.Free;
end;
end;
function TNginxItemGroup.InsertNameGroup(Idx, lvl: Integer; const sName,
sValue: string): TNginxItemGroup;
var
temp:TNginxItemGroup;
begin
Result:=nil;
temp:=TNginxItemGroup.Create;
try
temp.Level:=lvl;
temp.NameItem:=sName;
temp.Value:=sValue;
FItemList.Insert(Idx,temp);
Result:=temp;
except
temp.Free;
end;
end;
function TNginxItemGroup.FindItemName(const str: string): TNginxItem;
var
iptr:Pointer;
temp:TObject;
stemp:string;
begin
Result:=nil;
for iptr in FItemList do begin
temp:=TObject(iptr);
if temp is TNginxItem then begin
stemp:=(temp as TNginxItem).NameItem;
if CompareText(str,stemp)=0 then
Result:=(temp as TNginxItem);
end;
end;
end;
function TNginxItemGroup.FindItemNameNext(BaseItem: TNginxItem;
const str: string): TNginxItem;
var
i:Integer;
iptr:Pointer;
temp:TObject;
stemp:string;
begin
Result:=nil;
i:=FItemList.IndexOf(BaseItem);
for iptr in FItemList do begin
temp:=TObject(iptr);
if temp is TNginxItem then begin
stemp:=(temp as TNginxItem).NameItem;
if (FItemList.IndexOf(temp)<i) and (CompareText(str,stemp)=0) then
Result:=(temp as TNginxItem);
end;
end;
end;
function TNginxItemGroup.FindItemGroup(const str: string): TNginxItemGroup;
var
iptr:Pointer;
temp:TObject;
stemp:string;
begin
Result:=nil;
for iptr in FItemList do begin
temp:=TObject(iptr);
if temp is TNginxItemGroup then begin
stemp:=(temp as TNginxItemGroup).NameItem;
if CompareText(str,stemp)=0 then
Result:=(temp as TNginxItemGroup);
end;
end;
end;
// Item search reverse order, return prev item
function TNginxItemGroup.FindItemGroupNext(BaseItem: TNginxItemGroup;
const str: string): TNginxItemGroup;
var
i:Integer;
iptr:Pointer;
temp:TObject;
stemp:string;
begin
Result:=nil;
i:=FItemList.IndexOf(BaseItem);
for iptr in FItemList do begin
temp:=TObject(iptr);
if temp is TNginxItemGroup then begin
stemp:=(temp as TNginxItemGroup).NameItem;
if (FItemList.IndexOf(temp)<i) and (CompareText(str,stemp)=0) then
Result:=(temp as TNginxItemGroup);
end;
end;
end;
procedure TNginxItemGroup.DeleteItem(item: TObject);
var
i: Integer;
begin
i:=FItemList.IndexOf(item);
FItemList.Delete(i);
end;
function TNginxItemGroup.IndexOfItem(item: TNginxItem): Integer;
begin
Result:=FItemList.IndexOf(item);
end;
end.