-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataConfig.ob2
214 lines (201 loc) · 6.29 KB
/
DataConfig.ob2
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
(**
`INI` file format config parser with similar functions to Python
version except for multiline values.
*)
MODULE DataConfig;
IMPORT Object, Char, Str := ArrayOfChar, String;
IMPORT Dictionary := ADTDictionary, Vector := ADTVector, Set := ADTSet;
TYPE
(* Config Parser Class *)
Parser* = POINTER TO ParserDesc;
ParserDesc* = RECORD
sections* : Set.SetStr;
entries* : Dictionary.DictionaryStrStr;
END;
(** Initialize Parser *)
PROCEDURE InitParser*(parser : Parser);
BEGIN
NEW(parser.sections);
parser.sections.Init();
NEW(parser.entries);
parser.entries.Init();
END InitParser;
(** Clear data *)
PROCEDURE (this : Parser) Clear*();
BEGIN
this.sections.Clear();
this.entries.Clear();
END Clear;
(**
Get config value.
Return `TRUE` if success.
*)
PROCEDURE (this : Parser) Get*(VAR value : String.STRING; section- : ARRAY OF CHAR; key- : ARRAY OF CHAR) : BOOLEAN;
VAR name, ikey: String.STRING;
BEGIN
IF (Str.Length(section) = 0) OR (Str.Length(key) = 0) THEN RETURN FALSE END;
String.Assign(name, key);
Str.LowerCase(name^);
String.Format(ikey, "%s=%s", section, name^);
RETURN this.entries.Get(ikey^, value)
END Get;
(**
Set config value.
Return `TRUE` if success.
*)
PROCEDURE (this : Parser) Set*(section- : ARRAY OF CHAR; key- : ARRAY OF CHAR; value- : ARRAY OF CHAR) : BOOLEAN;
VAR name, ikey : String.STRING;
BEGIN
IF (Str.Length(section) = 0) OR (Str.Length(key) = 0) THEN RETURN FALSE END;
IF ~this.sections.In(section) THEN RETURN FALSE END;
String.Assign(name, key);
Str.LowerCase(name^);
String.Format(ikey, "%s=%s", section, name^);
RETURN this.entries.Set(ikey^, value)
END Set;
(**
Delete config value.
Return `TRUE` if success.
*)
PROCEDURE (this : Parser) Delete*(section- : ARRAY OF CHAR; key- : ARRAY OF CHAR) : BOOLEAN;
VAR name, ikey : String.STRING;
BEGIN
IF (Str.Length(section) = 0) OR (Str.Length(key) = 0) THEN RETURN FALSE END;
String.Assign(name, key);
Str.LowerCase(name^);
String.Format(ikey, "%s=%s", section, name^);
RETURN this.entries.Delete(ikey^)
END Delete;
(**
Delete config section and coresponding entries.
Return `TRUE` if success.
*)
PROCEDURE (this : Parser) DeleteSection*(section- : ARRAY OF CHAR) : BOOLEAN;
VAR
it : Dictionary.DictionaryStrStrIterator;
keys : Vector.VectorOfString;
name, key : String.STRING;
res : BOOLEAN;
BEGIN
IF Str.Length(section) = 0 THEN RETURN FALSE END;
IF ~this.sections.In(section) THEN RETURN FALSE END;
NEW(keys); keys.Init();
it := this.entries.Iterator();
WHILE it.Next() DO
key := it.Key();
IF Str.StartsWith(key^, section) THEN keys.Append(key^) END;
END;
WHILE keys.Pop(key) DO
res := this.entries.Delete(key^)
END;
res := this.sections.Remove(section);
RETURN TRUE
END DeleteSection;
(** Return `TRUE` if section exists *)
PROCEDURE (this : Parser) HasSection*(section- : ARRAY OF CHAR): BOOLEAN;
BEGIN RETURN this.sections.In(section)
END HasSection;
(** Extract Vector of sections *)
PROCEDURE (this : Parser) Sections*(): Vector.VectorOfString;
BEGIN RETURN this.sections.Values()
END Sections;
(** Write data to Stream *)
PROCEDURE (this : Parser) Write*(fh : Object.Stream): BOOLEAN;
VAR
it : Dictionary.DictionaryStrStrIterator;
name, key, section : String.STRING;
sections : Vector.VectorOfString;
i, len : LONGINT;
BEGIN
IF fh.Closed() OR ~fh.Writeable() THEN RETURN FALSE END;
sections := this.Sections();
sections.Sort();
i := 0;
len := sections.Size();
WHILE i < len DO
section := sections.At(i);
fh.Format("[%s]\n", section^);
it := this.entries.Iterator();
WHILE it.Next() DO
key := it.Key();
IF Str.StartsWith(key^, section^) THEN
String.Extract(name, key^, Str.Length(section^) + 1, LEN(key^));
fh.Format("%s=%s\n", name^, it.Value()^);
END;
END;
INC(i);
END;
RETURN TRUE;
END Write;
(**
Read from Stream.
This operation will try to append the new data.
Clear the data before operation if this is not intended.
*)
PROCEDURE (this : Parser) Read*(fh : Object.Stream): LONGINT;
VAR
line, key, name, value, section : String.STRING;
i, j, slen : LONGINT;
ch : CHAR;
ret : BOOLEAN;
PROCEDURE Next;
BEGIN IF i < LEN(line^) THEN ch := line^[i]; INC(i) ELSE ch := 00X END
END Next;
PROCEDURE Skip();
BEGIN WHILE Char.IsSpace(ch) DO Next() END;
END Skip;
BEGIN
IF fh.Closed() OR ~fh.Readable() THEN RETURN -1 END;
String.Assign(key, "");
String.Assign(section, "");
String.Assign(name, "");
String.Assign(value, "");
j := 1; slen := 0;
WHILE fh.ReadLine(line) DO
i := 0;
Next; Skip();
IF (ch = "#") OR (ch = ";") OR (ch = 00X) THEN
;
ELSIF ch = "[" THEN
Str.Clear(section^);
Next;
WHILE (ch # "]") & (ch # 00X) DO
String.AppendChar(section, ch);
Next
END;
IF ch # "]" THEN RETURN j END;
Next; Skip();
IF ch # 00X THEN RETURN j END;
IF Str.Length(section^) = 0 THEN RETURN j END;
IF this.sections.In(section^) THEN RETURN j END;
ret := this.sections.Add(section^);
IF ret THEN INC(slen) ELSE RETURN -1 END;
ELSE
IF slen = 0 THEN RETURN j END;
Str.Clear(name^);
WHILE (ch # "=") & (ch # 00X) DO
String.AppendChar(name, ch);
Next
END;
Str.RightTrim(name^);
IF Str.Length(name^) = 0 THEN RETURN j END;
IF ch = "=" THEN Next END;
Skip();
Str.Clear(value^);
WHILE ch # 00X DO
String.AppendChar(value, ch);
Next
END;
Str.RightTrim(value^);
IF Str.Length(section^) = 0 THEN RETURN j END;
Str.Clear(key^);
Str.LowerCase(name^);
String.Format(key, "%s=%s", section^, name^);
ret := this.entries.Set(key^, value^);
IF ~ret THEN RETURN -2 END;
END;
INC(j);
END;
RETURN 0;
END Read;
END DataConfig.