-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.c
292 lines (254 loc) · 7.02 KB
/
ini.c
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
/**
* Copyright (c) 2016 rxi
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "ini.h"
struct ini_t {
char *data;
char *end;
};
// Case insensitive string compare
static int strcmpci(const char *a, const char *b)
{
for (;;) {
int d = tolower(*a) - tolower(*b);
if ((d != 0) || !*a) {
return d;
}
a++, b++;
}
}
// Returns the next string in the split data
static char *next(ini_t *ini, char *p)
{
p += strlen(p);
while ((p < ini->end) && (*p == 0)) {
p++;
}
return p;
}
static void trim_back(ini_t *ini, char *p)
{
while ((p >= ini->data) && ((*p == ' ') || (*p == '\t') || (*p == '\r'))) {
*p-- = '\0';
}
}
static char *discard_line(ini_t *ini, char *p)
{
while ((p < ini->end) && (*p != '\n')) {
*p++ = 0;
}
return p;
}
static char *unescape_quoted_value(ini_t *ini, char *p)
{
/* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q`
* as escape sequences are always larger than their resultant data */
char *q = p;
int done = 0;
p++;
while ((p < ini->end) && (*p != '"') && (*p != '\r') && (*p != '\n')) {
if (*p == '\\') {
// Handle escaped char
p++;
switch (*p) {
default:
*q = *p;
break;
case 'r':
*q = '\r';
break;
case 'n':
*q = '\n';
break;
case 't':
*q = '\t';
break;
case '\r':
case '\n':
case '\0' :
done = 1;
}
} else {
// Handle normal char
*q = *p;
}
if (done) {
break;
}
q++;
p++;
}
return q;
}
/* Splits data in place into strings containing section-headers, keys and
* values using one or more '\0' as a delimiter. Unescapes quoted values */
static void split_data(ini_t *ini)
{
char *value_start, *line_start;
char *p = ini->data;
while (p < ini->end) {
switch (*p) {
case '\r':
case '\n':
case '\t':
case ' ':
*p = '\0';
// Fall through
case '\0':
p++;
break;
case '[':
p += strcspn(p, "]\n");
*p = '\0';
break;
case ';':
p = discard_line(ini, p);
break;
default:
line_start = p;
p += strcspn(p, "=\n");
// Is line missing a '='?
if (*p != '=') {
p = discard_line(ini, line_start);
break;
}
trim_back(ini, p - 1);
// Replace '=' and whitespace after it with '\0'
do {
*p++ = '\0';
} while ((*p == ' ') || (*p == '\r') || (*p == '\t'));
// Is a value after '=' missing?
if ((*p == '\n') || (*p == '\0')) {
p = discard_line(ini, line_start);
break;
}
if (*p == '"') {
// Handle quoted string value
value_start = p;
p = unescape_quoted_value(ini, p);
// Was the string empty?
if (p == value_start) {
p = discard_line(ini, line_start);
break;
}
// Discard the rest of the line after the string value
p = discard_line(ini, p);
} else {
// Handle normal value
p += strcspn(p, "\n");
trim_back(ini, p - 1);
}
break;
}
}
}
ini_t *ini_load(const char *filename)
{
ini_t *ini = NULL;
FILE *fp = NULL;
int n, sz;
// Init ini struct
ini = malloc(sizeof(*ini));
do {
if (!ini) {
break;
}
memset(ini, 0, sizeof(*ini));
// Open file
fp = fopen(filename, "rb");
if (!fp) {
break;
}
// Get file size
fseek(fp, 0, SEEK_END);
sz = ftell(fp);
rewind(fp);
// Load file content into memory, null terminate, init end var
ini->data = malloc(sz + 1);
ini->data[sz] = '\0';
ini->end = ini->data + sz;
n = fread(ini->data, 1, sz, fp);
if (n != sz) {
break;
}
// Prepare data
split_data(ini);
// Clean up and return
fclose(fp);
return ini;
} while (0);
// fail
if (fp) {
fclose(fp);
}
if (ini) {
ini_free(ini);
}
return NULL;
}
void ini_free(ini_t *ini)
{
free(ini->data);
free(ini);
}
const char *ini_get(ini_t *ini, const char *section, const char *key)
{
char *current_section = "";
char *val;
char *p = ini->data;
if (*p == '\0') {
p = next(ini, p);
}
while (p < ini->end) {
if (*p == '[') {
// Handle section
current_section = p + 1;
} else {
// Handle key
val = next(ini, p);
if (!section || !strcmpci(section, current_section)) {
if (!strcmpci(p, key)) {
return val;
}
}
p = val;
}
p = next(ini, p);
}
return NULL;
}
int ini_sget(ini_t *ini, const char *section, const char *key, const char *scanfmt, void *dst)
{
const char *val = ini_get(ini, section, key);
if (!val) {
return 0;
}
if (scanfmt) {
sscanf(val, scanfmt, dst);
} else {
*((const char**) dst) = val;
}
return 1;
}