-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjj_ini.h
More file actions
370 lines (311 loc) · 8.83 KB
/
jj_ini.h
File metadata and controls
370 lines (311 loc) · 8.83 KB
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
#ifndef __INI_H__
#define __INI_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
typedef int (*ini_handler)(void *user, const char *section,
const char *name, const char *value);
typedef char *(*ini_reader)(char *str, int num, void *stream);
int ini_parse(const char *filename, ini_handler handler, void *user);
int ini_parse_file(FILE *file, ini_handler handler, void *user);
int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
void *user);
#ifndef INI_ALLOW_MULTILINE
#define INI_ALLOW_MULTILINE 1
#endif
#ifndef INI_ALLOW_BOM
#define INI_ALLOW_BOM 1
#endif
#ifndef INI_ALLOW_INLINE_COMMENTS
#define INI_ALLOW_INLINE_COMMENTS 1
#endif
#ifndef INI_INLINE_COMMENT_PREFIXES
#define INI_INLINE_COMMENT_PREFIXES ";"
#endif
#ifndef INI_USE_STACK
#define INI_USE_STACK 1
#endif
#ifndef INI_STOP_ON_FIRST_ERROR
#define INI_STOP_ON_FIRST_ERROR 0
#endif
#ifndef INI_MAX_LINE
#define INI_MAX_LINE 200
#endif
#ifdef __cplusplus
}
#endif
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#if !INI_USE_STACK
#include <stdlib.h>
#endif
#define MAX_SECTION 50
#define MAX_NAME 50
inline static char *rstrip(char *s)
{
char *p = s + strlen(s);
while (p > s && isspace((unsigned char)(*--p)))
*p = '\0';
return s;
}
inline static char *lskip(const char *s)
{
while (*s && isspace((unsigned char)(*s)))
s++;
return (char *)s;
}
inline static char *find_chars_or_comment(const char *s, const char *chars)
{
#if INI_ALLOW_INLINE_COMMENTS
int was_space = 0;
while (*s && (!chars || !strchr(chars, *s)) &&
!(was_space && strchr(INI_INLINE_COMMENT_PREFIXES, *s)))
{
was_space = isspace((unsigned char)(*s));
s++;
}
#else
while (*s && (!chars || !strchr(chars, *s)))
{
s++;
}
#endif
return (char *)s;
}
inline static char *strncpy0(char *dest, const char *src, size_t size)
{
if (size > 0)
{
sprintf(dest, "%.*s", (int)(size - 1), src);
}
dest[size - 1] = '\0';
return dest;
}
inline int ini_parse_stream(ini_reader reader, void *stream, ini_handler handler,
void *user)
{
#if INI_USE_STACK
char line[INI_MAX_LINE];
#else
char *line;
#endif
char section[MAX_SECTION] = "";
char prev_name[MAX_NAME] = "";
char *start;
char *end;
char *name;
char *value;
int lineno = 0;
int error = 0;
#if !INI_USE_STACK
line = (char *)malloc(INI_MAX_LINE);
if (!line)
{
return -2;
}
#endif
while (reader(line, INI_MAX_LINE, stream) != NULL)
{
lineno++;
start = line;
#if INI_ALLOW_BOM
if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
(unsigned char)start[1] == 0xBB &&
(unsigned char)start[2] == 0xBF)
{
start += 3;
}
#endif
start = lskip(rstrip(start));
if (*start == ';' || *start == '#')
{
}
#if INI_ALLOW_MULTILINE
else if (*prev_name && *start && start > line)
{
#if INI_ALLOW_INLINE_COMMENTS
end = find_chars_or_comment(start, NULL);
if (*end)
*end = '\0';
rstrip(start);
#endif
if (!handler(user, section, prev_name, start) && !error)
error = lineno;
}
#endif
else if (*start == '[')
{
end = find_chars_or_comment(start + 1, "]");
if (*end == ']')
{
*end = '\0';
strncpy0(section, start + 1, sizeof(section));
*prev_name = '\0';
}
else if (!error)
{
error = lineno;
}
}
else if (*start)
{
end = find_chars_or_comment(start, "=:");
if (*end == '=' || *end == ':')
{
*end = '\0';
name = rstrip(start);
value = lskip(end + 1);
#if INI_ALLOW_INLINE_COMMENTS
end = find_chars_or_comment(value, NULL);
if (*end)
*end = '\0';
#endif
rstrip(value);
strncpy0(prev_name, name, sizeof(prev_name));
if (!handler(user, section, name, value) && !error)
error = lineno;
}
else if (!error)
{
error = lineno;
}
}
#if INI_STOP_ON_FIRST_ERROR
if (error)
break;
#endif
}
#if !INI_USE_STACK
free(line);
#endif
return error;
}
inline int ini_parse_file(FILE *file, ini_handler handler, void *user)
{
return ini_parse_stream((ini_reader)fgets, file, handler, user);
}
inline int ini_parse(const char *filename, ini_handler handler, void *user)
{
FILE *file;
int error;
file = fopen(filename, "r");
if (!file)
return -1;
error = ini_parse_file(file, handler, user);
fclose(file);
return error;
}
#endif
#ifndef __JJINI_H__
#define __JJINI_H__
#include <map>
#include <set>
#include <string>
class JJINI
{
public:
JJINI(){};
explicit JJINI(const std::string &filename);
explicit JJINI(FILE *file);
int ParseError() const;
const std::set<std::string> &Sections() const;
std::string Get(const std::string §ion, const std::string &name,
const std::string &default_value) const;
long GetInteger(const std::string §ion, const std::string &name, long default_value) const;
double GetReal(const std::string §ion, const std::string &name, double default_value) const;
float GetFloat(const std::string §ion, const std::string &name, float default_value) const;
bool GetBoolean(const std::string §ion, const std::string &name, bool default_value) const;
protected:
int _error;
std::map<std::string, std::string> _values;
std::set<std::string> _sections;
static std::string MakeKey(const std::string §ion, const std::string &name);
static int ValueHandler(void *user, const char *section, const char *name,
const char *value);
};
#endif
#ifndef __JJINI__
#define __JJINI__
#include <algorithm>
#include <cctype>
#include <cstdlib>
inline JJINI::JJINI(const std::string &filename)
{
_error = ini_parse(filename.c_str(), ValueHandler, this);
}
inline JJINI::JJINI(FILE *file)
{
_error = ini_parse_file(file, ValueHandler, this);
}
inline int JJINI::ParseError() const
{
return _error;
}
inline const std::set<std::string> &JJINI::Sections() const
{
return _sections;
}
inline std::string JJINI::Get(const std::string §ion, const std::string &name, const std::string &default_value) const
{
std::string key = MakeKey(section, name);
return _values.count(key) ? _values.at(key) : default_value;
}
inline long JJINI::GetInteger(const std::string §ion, const std::string &name, long default_value) const
{
std::string valstr = Get(section, name, "");
const char *value = valstr.c_str();
char *end;
long n = strtol(value, &end, 0);
return end > value ? n : default_value;
}
inline double JJINI::GetReal(const std::string §ion, const std::string &name, double default_value) const
{
std::string valstr = Get(section, name, "");
const char *value = valstr.c_str();
char *end;
double n = strtod(value, &end);
return end > value ? n : default_value;
}
inline float JJINI::GetFloat(const std::string §ion, const std::string &name, float default_value) const
{
std::string valstr = Get(section, name, "");
const char *value = valstr.c_str();
char *end;
float n = strtof(value, &end);
return end > value ? n : default_value;
}
inline bool JJINI::GetBoolean(const std::string §ion, const std::string &name, bool default_value) const
{
std::string valstr = Get(section, name, "");
std::transform(valstr.begin(), valstr.end(), valstr.begin(), ::tolower);
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
return true;
else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0")
return false;
else
return default_value;
}
inline std::string JJINI::MakeKey(const std::string §ion, const std::string &name)
{
std::string key = section + "=" + name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return key;
}
inline int JJINI::ValueHandler(void *user, const char *section, const char *name,
const char *value)
{
JJINI *reader = (JJINI *)user;
std::string key = MakeKey(section, name);
if (reader->_values[key].size() > 0)
reader->_values[key] += "\n";
reader->_values[key] += value;
reader->_sections.insert(section);
return 1;
}
#endif