-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyValueFile.h
380 lines (316 loc) · 8.89 KB
/
keyValueFile.h
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
#pragma once
#include <string.h>
#include <string>
#include <vector>
#include <fstream>
#include <streambuf>
//file example:
//
// ; comment
// include ../some_file.inc
// boolean_value = yes ; comment
// code = fn1(); fn2(); ; comment
//
// ; use '\' for concatination with the next line
// multiline = line1 \
// line1 \ ; valid comment
// line1 \
// line1
//
// list = value1 ; call getValuesList("list") to get all these values in std::vector
// list = value2
// list = value3 value4 value5
// list = value6
//
//
class KeyValueFile
{
public:
typedef void (*PrintErrorFunc)(const char *);
PrintErrorFunc printErrorFunc = nullptr;
struct StringKeyValue
{
std::string key;
std::string value;
};
private:
std::string fileName;
std::vector<StringKeyValue> kv;
int includeDepth = 0;
bool isUtf8Bom(const char * it)
{
return (
(unsigned char)(*it++) == 0xef &&
(unsigned char)(*it++) == 0xbb &&
(unsigned char)(*it) == 0xbf
);
}
bool isSpace(char c) const
{
return c == ' ' || c == '\t';
}
std::string trimStr(std::string s)
{
int from = 0;
int to = int(s.length()) - 1;
while (to > 0 && isSpace(s[to]))
to--;
while (from <= to && isSpace(s[from]))
from++;
if (to < from)
return std::string("");
return std::string(&(s[from]), to - from + 1);
}
void errorParam(const char * msg, const char * key_name) const
{
if (printErrorFunc)
{
std::string err = std::string("ERROR: ") + msg + ", at file '" + fileName + "', key '" + std::string(key_name) + "'\n";
printErrorFunc(err.c_str());
}
}
bool includeFile(const char * inc_file_name, int include_depth)
{
if (include_depth >= 16)
{
if (printErrorFunc)
printErrorFunc(
(std::string("ERROR: Maximum include depth exceeded on file '") + inc_file_name + "'\n").c_str());
return false;
}
int len = int(fileName.length()) - 1;
while (len >= 0)
{
if (fileName[len] == '\\' || fileName[len] == '/')
break;
len--;
}
std::string combinedFileName(fileName.c_str(), len + 1);
combinedFileName += inc_file_name;
KeyValueFile kvFile;
kvFile.includeDepth = include_depth + 1;
kvFile.printErrorFunc = printErrorFunc;
bool ok = kvFile.loadFromFile(combinedFileName.c_str());
if (ok)
for (auto && value : kvFile.kv)
kv.push_back(value);
return ok;
}
public:
bool loadFromFile(const char * file_name)
{
if (!file_name)
return false;
std::ifstream stream(file_name);
if (stream.fail())
{
if (printErrorFunc)
printErrorFunc(
(std::string("ERROR: Cannot open file '") + file_name + "'\n").c_str());
return false;
}
std::string buf((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
return loadFromString(buf.c_str(), file_name);
}
bool loadFromString(const char * data, const char * debug_file_name = "")
{
fileName = debug_file_name;
if (!data)
return false;
if (isUtf8Bom(data))
data += 3;
int len = int(strlen(data));
StringKeyValue x;
bool comment = false;
bool readKey = true;
bool eqFound = false;
bool error = false;
bool include = false;
int line = 1;
for (int i = 0; i <= len; i++)
{
char c = data[i];
if (c == '\\')
{
if (i == len - 1)
i++;
else
{
bool commentAfterConcat = false;
for (int j = i + 1; j < len; j++)
{
if (data[j] == ';' && isSpace(data[j - 1]))
commentAfterConcat = true;
if (!commentAfterConcat && data[j] > ' ')
break;
bool concat = false;
if (data[j] == 0x0d && data[j + 1] == 0x0a)
{
line++;
i = j + 2;
concat = true;
}
else if (data[j] == 0x0a)
{
line++;
i = j + 1;
concat = true;
}
if (concat)
{
if (comment)
if (printErrorFunc)
printErrorFunc((std::string("ERROR: Line concatenation inside comment. At file '") +
fileName + "' line " + std::to_string(line - 1) + "\n").c_str());
if (data[i] > ' ' && data[i] != ';')
if (printErrorFunc)
printErrorFunc((
std::string("ERROR: The next line after the line concatenation symbol '\\' must be started with space. At file '") +
fileName + "' line " + std::to_string(line) + "\n").c_str());
c = data[i];
break;
}
}
}
}
if (c == 0x0d || c == 0x0a || c == 0)
{
x.key = trimStr(x.key);
x.value = trimStr(x.value);
if (!x.key.empty() && !x.value.empty() && eqFound && !include)
{
//printf("%s = \"%s\"\n", x.key.c_str(), x.value.c_str());
kv.push_back(x);
}
else if (!x.key.empty())
{
if (include)
{
if (!debug_file_name || !debug_file_name[0])
{
if (printErrorFunc)
printErrorFunc(
(std::string("ERROR: File name must be passed to loadFromString() when you are using 'include'. At file '") +
fileName + "' line " + std::to_string(line) + "\n").c_str());
error = true;
}
else
{
bool ok = includeFile(x.value.c_str(), includeDepth);
error |= !ok;
}
}
else if (!eqFound)
{
if (printErrorFunc)
printErrorFunc(
(std::string("ERROR: Expected '=' at file '") + fileName + "' line " + std::to_string(line) + "\n").c_str());
error = true;
}
else if (x.value.empty())
{
if (printErrorFunc)
printErrorFunc(
(std::string("ERROR: Expected value after '=' at file '") + fileName + "' line " + std::to_string(line) + "\n").c_str());
error = true;
}
}
comment = false;
readKey = true;
eqFound = false;
include = false;
x.key.clear();
x.value.clear();
if (c == 0x0a)
line++;
}
if (c == ';' && (i == 0 || isSpace(data[i - 1]) || data[i - 1] == 0x0a))
comment = true;
if (comment)
continue;
if (readKey)
{
if (!isSpace(c) && c > ' ' && c != '=')
x.key += c;
else if (!x.key.empty())
{
readKey = false;
if (c == '=')
eqFound = true;
if (x.key == "include")
include = true;
}
}
else
{
if (c == '=' && !eqFound)
eqFound = true;
else if ((eqFound && c >= ' ') || include)
x.value += c;
}
}
return !error;
}
int count() const
{
return int(kv.size());
}
const char * getStr(const char * key, const char * default_value = "") const
{
if (!key)
return default_value;
const char *res = default_value;
for (int i = 0; i < int(kv.size()); i++)
if (!strcmp(key, kv[i].key.c_str()))
{
if (res != default_value)
errorParam("More than one key found", key);
res = kv[i].value.c_str();
}
return res;
}
bool getBool(const char * key, bool default_value) const
{
if (!key)
return default_value;
for (int i = 0; i < int(kv.size()); i++)
if (!strcmp(key, kv[i].key.c_str()))
{
if (kv[i].value.empty())
errorParam("Value is empty", key);
const char * v = kv[i].value.c_str();
bool isTrue = !strcmp(v, "1") || !strncmp(v, "yes", 3) || !strncmp(v, "true", 4);
bool isFalse = !strcmp(v, "0") || !strncmp(v, "no", 2) || !strncmp(v, "false", 5);
if (!isTrue && !isFalse)
errorParam("Invalid boolean value, expected yes,true,1 or no,false,0", key);
return isTrue;
}
return default_value;
}
std::vector<std::string> getValuesList(const char * key) const // separated by spaces
{
std::vector<std::string> res;
for (auto && x : kv)
if (!strcmp(key, x.key.c_str()))
{
const char * s = x.value.c_str();
int len = int(x.value.length());
int i = 0;
while (i < len)
{
while (i < len && (isSpace(s[i]) || s[i] < ' '))
i++;
int wordStart = i;
while (i < len && s[i] > ' ')
i++;
if (i != wordStart)
res.push_back(std::string(s + wordStart, i - wordStart));
}
}
return res;
}
const StringKeyValue & keyValue(int index) const
{
return kv[index];
}
};