forked from gingerBill/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb_ini.h
425 lines (338 loc) · 9.67 KB
/
gb_ini.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
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
/* gb_ini.h - v0.93 - public domain ini file loader library - no warranty implied; use at your own risk
A Simple Ini File Loader Library for C and C++
Version History:
0.93 - C90 support
0.92 - ??
0.91 - New styling
0.91a - Error handling fix
0.91 - Add extern "C" if compiling as C++
0.90 - Initial Version
LICENSE
This software is in the public domain. Where that dedication is not
recognized, you are granted a perpetual, irrevocable license to copy,
distribute, and modify this file as you see fit.
How to use:
Do this:
#define GB_INI_IMPLEMENTATION
before you include this file in *one* C++ file to create the implementation
i.e. it should look like this:
#include ...
#include ...
#include ...
#define GB_INI_IMPLEMENTATION
#include "gb_ini.h"
If you prefer to use C++, you can use all the same functions in a
namespace instead, do this:
#define GB_INI_CPP
before you include the header file
i.e it should look like this:
#define GB_INI_CPP
#include "gb_ini.h"
*/
/* Examples: */
/* test.ini contents*/
#if 0
; This is a Comment
# This is also
name=gb_ini.h
version = 01337 # Look mum, a comment!
[license]
name = Public Domain
[author]
name = Ginger Bill
#endif
/* C example */
#if 0
#define GB_INI_IMPLEMENTATION
#include "gb_ini.h"
#include <stdio.h>
#include <stdlib.h>
struct Library {
char const *name;
int version;
char const *license;
char const *author;
};
// The below macro expands to this:
// static test_ini_handlerchar const *data, char const *section, char const *name, char const *value)
static GB_INI_HANDLER(test_ini_handler)
{
struct Library *lib = (struct Library *)data;
#define TEST(s, n) (strcmp(section, s) == 0 && strcmp(name, n) == 0)
if (TEST("", "name"))
lib->name = strdup(value);
else if (TEST("", "version"))
lib->version = atoi(value);
else if (TEST("license", "name"))
lib->license = strdup(value);
else if (TEST("author", "name"))
lib->author = strdup(value);
else
return 0;
#undef TEST
return 1;
}
int main(int argc, char **argv)
{
struct Library lib = {0};
gbIniError err = gb_ini_parse("test.ini", &test_ini_handler, &lib);
if (err.type != GB_INI_ERROR_NONE) {
if (err.line_num > 0)
printf("Line (%d): ", err.line_num);
printf("%s\n", gb_ini_error_string(err));
return 1;
}
printf("Name : %s\n", lib.name); // Name : gb_init.h
printf("Version : %d\n", lib.version); // Version : 1337
printf("License : %s\n", lib.license); // License : Public Domain
printf("Author : %s\n", lib.author); // Author : Ginger Bill
return 0;
}
#endif
/* C++ example */
#if 0
#define GB_INI_CPP
#define GB_INI_IMPLEMENTATION
#include "gb_ini.h"
#include <stdio.h>
#include <stdlib.h>
struct Library {
char const *name;
int version;
char const *license;
char const *author;
};
// The below macro expands to this:
// static test_ini_handler(void *data, char const *section, char const *name, char const *value)
static GB_INI_HANDLER(test_ini_handler)
{
Library* lib = (Library*)data;
#define TEST(s, n) (strcmp(section, s) == 0 && strcmp(name, n) == 0)
if (TEST("", "name"))
lib->name = strdup(value);
else if (TEST("", "version"))
lib->version = atoi(value);
else if (TEST("license", "name"))
lib->license = strdup(value);
else if (TEST("author", "name"))
lib->author = strdup(value);
else
return 0;
#undef TEST
return 1;
}
int main(int argc, char** argv)
{
Library lib = {};
using namespace gb;
Ini_Error err = ini_parse("test.ini", &test_ini_handler, &lib);
if (err.type != INI_ERROR_NONE) {
if (err.line_num > 0)
printf("Line (%d): ", err.line_num);
printf("%s\n", ini_error_string(err));
return 1;
}
printf("Name : %s\n", lib.name); // Name : gb_init.h
printf("Version : %d\n", lib.version); // Version : 90
printf("License : %s\n", lib.license); // License : Public Domain
printf("Author : %s\n", lib.author); // Author : Ginger Bill
return 0;
}
#endif
#ifndef GB_INI_INCLUDE_GB_INI_H
#define GB_INI_INCLUDE_GB_INI_H
#ifndef GB_INI_MAX_LINE_LENGTH
#define GB_INI_MAX_LINE_LENGTH 1024
#endif
#ifndef GB_INI_MAX_SECTION_LENGTH
#define GB_INI_MAX_SECTION_LENGTH 256
#endif
#ifndef GB_INI_MAX_NAME_LENGTH
#define GB_INI_MAX_NAME_LENGTH 256
#endif
#ifndef GB_INI_CHECK_FOR_UTF8_BOM
#define GB_INI_CHECK_FOR_UTF8_BOM 1
#endif
#ifndef _MSC_VER
#ifdef __cplusplus
#define gbini_inline inline
#else
#define gbini_inline
#endif
#else
#define gbini_inline __forceinline
#endif
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
enum {
GB_INI_ERROR_NONE = 0,
GB_INI_ERROR_FILE_ERROR,
GB_INI_ERROR_MISSING_SECTION_BRACKET,
GB_INI_ERROR_ASSIGNMENT_MISSING,
GB_INI_ERROR_HANDLER_ERROR,
GB_INI_ERROR_COUNT
};
typedef struct gbIniError {
int type;
size_t line_num;
} gbIniError;
#ifndef GB_INI_HANDLER_RETURN_TYPE
#define GB_INI_HANDLER_RETURN_TYPE
typedef int gbIniHRT;
#endif
/* This macro can be use to declare this type of function without
* needing to define all the of the parameters
*/
#define GB_INI_HANDLER(func_name) gbIniHRT func_name(void *data, char const *section, char const *name, char const *value)
typedef GB_INI_HANDLER(gbIniHandler);
extern char const *GB_ERROR_STRINGS[GB_INI_ERROR_COUNT];
gbIniError gb_ini_parse(char const *filename, gbIniHandler* handler_func, void *data);
gbIniError gb_ini_parse_file(FILE *file, gbIniHandler* handler_func, void *data);
gbini_inline char const *gb_ini_error_string(gbIniError const err) { return GB_ERROR_STRINGS[err.type]; }
#ifdef __cplusplus
}
#endif
#ifdef GB_INI_CPP
#if !defined(__cplusplus)
#error You need to compile as C++ for the C++ version of gb_ini.h to work
#endif
namespace gb
{
typedef gbIniError IniError;
typedef gbIniHandler IniHandler;
/* Just a copy but with the GB_ prefix stripped */
enum {
INI_ERROR_NONE = 0,
INI_ERROR_FILE_ERROR,
INI_ERROR_MISSING_SECTION_BRACKET,
INI_ERROR_ASSIGNMENT_MISSING,
INI_ERROR_HANDLER_ERROR
/* No need for enum count */
};
inline Ini_Error ini_parse(char const *filename, Ini_Handler *handler_func, void *data) { return gb_ini_parse(filename, handler_func, data); }
inline Ini_Error ini_parse(FILE *file, Ini_Handler *handler_func, void *data) { return gb_ini_parse_file(file, handler_func, data); }
inline char const *ini_error_string(const Ini_Error err) { return GB_ERROR_STRINGS[err.type]; }
} /* namespace gb */
#endif /* GB_INI_CPP */
#endif /* GB_INI_INCLUDE_GB_INI_H */
#ifdef GB_INI_IMPLEMENTATION
#include <ctype.h>
#include <string.h>
char const *GB_ERROR_STRINGS[GB_INI_ERROR_COUNT] = {
"",
"Error in opening file",
"Missing closing section bracket ']'",
"Missing assignment operator '='",
"Error in handler function",
};
static gbini_inline char*
gb__left_whitespace_skip(char const *str)
{
while (*str && isspace((unsigned char)(*str)))
str++;
return (char*)str;
}
static gbini_inline char*
gb__right_whitespace_strip(char* str)
{
char* end = str + strlen(str);
while (end > str && isspace((unsigned char)(*--end)))
*end = '\0';
return str;
}
static gbini_inline char*
gb__find_char_or_comment_in_string(char const *str, char c)
{
int was_whitespace = 0;
while (*str && *str != c && !(was_whitespace && *str == ';')) {
was_whitespace = isspace((unsigned char)(*str));
str++;
}
return (char*)str;
}
static gbini_inline char*
gb__string_copy(char* dst, char const *src, size_t size)
{
strncpy(dst, src, size);
dst[size - 1] = '\0';
return dst;
}
gbIniError
gb_ini_parse(char const *filename, gbIniHandler *handler_func, void *data)
{
gbIniError err = {GB_INI_ERROR_FILE_ERROR, 0};
FILE *file = fopen(filename, "r");
if (!file)
return err;
err = gb_ini_parse_file(file, handler_func, data);
fclose(file);
return err;
}
gbIniError
gb_ini_parse_file(FILE *file, gbIniHandler *handler_func, void *data)
{
char line[GB_INI_MAX_LINE_LENGTH] = "";
size_t line_num = 0;
char section[GB_INI_MAX_SECTION_LENGTH] = "";
char prev_name[GB_INI_MAX_NAME_LENGTH] = "";
char *start;
char *end;
struct gbIniError err = {GB_INI_ERROR_NONE, 0};
while (fgets(line, GB_INI_MAX_LINE_LENGTH, file) != 0) {
line_num++;
start = line;
#if GB_INI_CHECK_FOR_UTF8_BOM
/* Check for UTF-8 Byte Order Mark */
if (line_num == 1 &&
(unsigned char)start[0] == 0xef &&
(unsigned char)start[1] == 0xbb &&
(unsigned char)start[2] == 0xbf) {
start += 3;
}
#endif
start = gb__left_whitespace_skip(gb__right_whitespace_strip(start));
if (start[0] == ';' || start[0] == '#')
continue; /* Allow '#' and ';' comments at start of line */
if (start[0] == '[') { /* [section] */
end = gb__find_char_or_comment_in_string(start+1, ']');
if (*end == ']') {
char *sect = start + 1;
*end = '\0';
sect = gb__left_whitespace_skip(gb__right_whitespace_strip(sect));
gb__string_copy(section, sect, sizeof(section));
*prev_name = '\0';
} else if (!err.type) {
err.type = GB_INI_ERROR_MISSING_SECTION_BRACKET;
err.line_num = line_num;
}
} else if (start[0] && start[0] != ';') {
end = gb__find_char_or_comment_in_string(start, '=');
if (*end == '=') {
char *name, *value;
*end = '\0';
name = gb__right_whitespace_strip(start);
value = gb__left_whitespace_skip(end + 1);
end = gb__find_char_or_comment_in_string(value, '\0');
if (*end == ';')
*end = '\0';
gb__right_whitespace_strip(value);
gb__string_copy(prev_name, name, sizeof(prev_name));
if (!handler_func(data, section, name, value) && !err.type) {
err.type = GB_INI_ERROR_HANDLER_ERROR;
err.line_num = line_num;
}
} else if (!err.type) {
/* No '=' found on name=value line */
err.type = GB_INI_ERROR_ASSIGNMENT_MISSING;
err.line_num = line_num;
continue;
}
}
if (err.type)
break;
}
return err;
}
#endif /* GB_INI_IMPLEMENTATION */