-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUINIFILE.PAS
142 lines (121 loc) · 3.12 KB
/
UINIFILE.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
{
uinifile Unit
Reads INI files
2022 LRT
}
unit
uinifile;
interface
uses
consts, utils, uexc, uclasses, types, locale, uobject,
strings, ustream, udict;
type
PINIFile = ^TINIFile;
TINIFile = object (TObject)
public
constructor initWithFile(filename: string);
constructor initWithStream(stream: PStream);
function getValue(path: string): string;
destructor done; virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_root, _current: PDictionary;
procedure parseLine(s: string);
end;
implementation
const
C_COMMENT_MARKER = '#';
C_LABEL_START = '[';
C_LABEL_END = ']';
C_KEY_VALUE_DELIMITER = '=';
{ TINIFile public }
constructor TINIFile.initWithFile(filename: string);
var
s: string;
handle: text;
begin
inherited init;
_root := new(PDictionary, init);
_current := _root;
assign(handle, filename);
{$I-} reset(handle); {$I+}
iassert(IOResult = 0, @self, 0, S_ERR_RESOURCE_NOT_FOUND);
while not seekeof(handle) do
begin
readln(handle, s);
parseLine(s);
end;
close(handle);
end;
constructor TINIFile.initWithStream(stream: PStream);
begin
inherited init;
_root := new(PDictionary, init);
_current := _root;
while not stream^.isEOF do parseLine(stream^.readln);
end;
destructor TINIFile.done;
begin
_root^.release;
inherited done;
end;
function TINIFile.getValue(path: string): string;
begin
getValue := _root^.getStringWithPath(path, '');
end;
function TINIFile.getClassName: string;
begin
getClassName := 'TINIFile';
end;
function TINIFile.getClassId: word;
begin
getClassId := C_CLASS_ID_INIFile;
end;
{ TINIFile private }
procedure TINIFile.parseLine(s: string);
var
len, i: byte;
procedure skipSpaces;
begin
while (s[i] = ' ') and (i <= len) do inc(i);
end;
procedure moveUntil(ch: char);
begin
while (s[i] <> ch) and (i <= len) do inc(i);
end;
procedure updateGroup;
var
pfrom: byte;
groupName: string32;
begin
pFrom := i + 1;
moveUntil(C_LABEL_END);
if (i > len) then exit; { malformed line, skipping }
groupName := subString(s, pFrom, i - pFrom);
_current := New(PDictionary, init);
_root^.addObject(_current, groupName);
_current^.release;
end;
procedure addValue;
var
p: byte;
key: string32;
value: string;
begin
p := Pos(C_KEY_VALUE_DELIMITER, s);
if p = 0 then exit; { discard malformed line }
key := SubString(s, 1, p - 1);
value := SubString(s, p + 1, len - p);
_current^.addString(value, key);
end;
begin
len := length(s);
i := 1;
skipSpaces;
if (i > len) then exit; { ignore empty line }
if (s[i] = C_COMMENT_MARKER) then exit; { ignore comments }
if (s[i] = C_LABEL_START) then updateGroup else addValue;
end;
{ Other }
end.