-
-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathtomlparser.c
More file actions
346 lines (315 loc) · 9.07 KB
/
Copy pathtomlparser.c
File metadata and controls
346 lines (315 loc) · 9.07 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
/* See LICENSE file for copyright and license details.
* Minimal TOML parser for dwm-titus hot-reload configuration.
*/
#include "tomlparser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
static void
copystr(char *dst, size_t dstsz, const char *src)
{
size_t len;
if (dstsz == 0)
return;
len = strlen(src);
if (len >= dstsz)
len = dstsz - 1;
memcpy(dst, src, len);
dst[len] = '\0';
}
static char *
strtrim(char *s)
{
while (isspace((unsigned char)*s)) s++;
if (*s) {
char *e = s + strlen(s) - 1;
while (e > s && isspace((unsigned char)*e)) *e-- = '\0';
}
return s;
}
int
toml_table_count(const TomlDoc *doc, const char *section)
{
int max = -1, i;
for (i = 0; i < doc->n; i++)
if (doc->entries[i].table_idx >= 0
&& strcmp(doc->entries[i].section, section) == 0
&& doc->entries[i].table_idx > max)
max = doc->entries[i].table_idx;
return max + 1;
}
/* ── Inline string un-escaping ───────────────────────────────────────────── */
/* Copies unescaped content from src (pointing just after opening '"') into
* dst, stopping before the closing '"'. Returns pointer to the closing '"'
* (or end-of-string if unterminated). */
static const char *
unescape_into(const char *src, char *dst, int maxlen)
{
int si = 0;
while (*src && *src != '"' && si < maxlen - 1) {
if (*src == '\\' && *(src + 1)) {
src++;
switch (*src) {
case 'n': dst[si++] = '\n'; break;
case 't': dst[si++] = '\t'; break;
case '\\': dst[si++] = '\\'; break;
case '"': dst[si++] = '"'; break;
default: dst[si++] = *src; break;
}
} else {
dst[si++] = *src;
}
src++;
}
dst[si] = '\0';
return src; /* points to closing '"' or '\0' */
}
/* ── Inline table parser ─────────────────────────────────────────────────── */
/* Parses key=value pairs from content between { and }.
* p points to first character *after* the opening '{'.
* Returns pointer to first character *after* the closing '}'.
* Each pair is stored as a TomlEntry with the given section and tidx. */
static const char *
parse_inline_table(const char *p, TomlDoc *doc, const char *section, int tidx)
{
while (*p) {
/* skip whitespace and commas between pairs */
while (isspace((unsigned char)*p) || *p == ',') p++;
if (*p == '}' || *p == '\0') break;
if (doc->n >= TOML_MAX_ENTRIES) break;
/* key (unquoted identifier) */
const char *kstart = p;
while (*p && *p != '=' && !isspace((unsigned char)*p) && *p != '}') p++;
int klen = (int)(p - kstart);
if (klen <= 0 || klen >= TOML_MAX_STR) break;
while (isspace((unsigned char)*p)) p++;
if (*p != '=') break;
p++; /* skip '=' */
while (isspace((unsigned char)*p)) p++;
TomlEntry *ent = &doc->entries[doc->n];
strncpy(ent->section, section, TOML_MAX_STR - 1);
ent->section[TOML_MAX_STR - 1] = '\0';
ent->table_idx = tidx;
strncpy(ent->key, kstart, (size_t)klen);
ent->key[klen] = '\0';
if (*p == '"') {
ent->val.type = TOML_STRING;
p = unescape_into(p + 1, ent->val.s, TOML_MAX_STR);
if (*p == '"') p++; /* skip closing quote */
} else if (*p == '[') {
ent->val.type = TOML_ARRAY;
ent->val.a.len = 0;
p++; /* skip '[' */
while (*p && *p != ']' && ent->val.a.len < TOML_MAX_ARR) {
while (isspace((unsigned char)*p) || *p == ',') p++;
if (*p == ']' || *p == '\0') break;
if (*p == '"') {
char *dst = ent->val.a.items[ent->val.a.len];
p = unescape_into(p + 1, dst, TOML_MAX_STR);
if (*p == '"') p++;
ent->val.a.len++;
} else {
p++;
}
}
if (*p == ']') p++;
} else {
/* integer or float */
char nbuf[64];
int ni = 0;
while (*p && *p != ',' && *p != '}' && !isspace((unsigned char)*p) && ni < 63)
nbuf[ni++] = *p++;
nbuf[ni] = '\0';
char *ep;
long iv = strtol(nbuf, &ep, 10);
if (ep != nbuf && *ep == '\0') {
ent->val.type = TOML_INT;
ent->val.i = iv;
} else {
double dv = strtod(nbuf, &ep);
ent->val.type = TOML_FLOAT;
ent->val.d = dv;
}
}
doc->n++;
/* advance past any trailing chars before next comma/brace */
while (*p && *p != ',' && *p != '}') p++;
}
if (*p == '}') p++;
return p;
}
int
toml_parse(const char *path, TomlDoc *doc)
{
FILE *f = fopen(path, "r");
if (!f) return 0;
doc->n = 0;
char line[4096];
char cur_section[TOML_MAX_STR] = "";
int cur_tidx = -1;
/* multi-line array-of-inline-tables state (compact format: key = [...]) */
int ml_active = 0;
char ml_section[TOML_MAX_STR] = "";
int ml_tidx = 0;
while (fgets(line, sizeof(line), f)) {
char *p = strtrim(line);
/* ── multi-line array mode ── */
if (ml_active) {
if (!*p || *p == '#') continue; /* blank / comment */
if (p[0] == ']') { ml_active = 0; continue; } /* end of array */
/* parse every { ... } block on this line */
const char *sp = p;
while (*sp) {
while (*sp && *sp != '{') sp++;
if (!*sp) break;
sp++; /* skip '{' */
sp = parse_inline_table(sp, doc, ml_section, ml_tidx);
ml_tidx++;
}
continue;
}
if (!*p || *p == '#') continue;
/* [[array-of-tables]] */
if (p[0] == '[' && p[1] == '[') {
char *end = strstr(p + 2, "]]");
if (!end) continue;
int len = (int)(end - (p + 2));
if (len >= TOML_MAX_STR) len = TOML_MAX_STR - 1;
strncpy(cur_section, p + 2, len);
cur_section[len] = '\0';
cur_tidx = toml_table_count(doc, cur_section);
continue;
}
/* [section] */
if (p[0] == '[') {
char *end = strchr(p + 1, ']');
if (!end) continue;
int len = (int)(end - (p + 1));
if (len >= TOML_MAX_STR) len = TOML_MAX_STR - 1;
strncpy(cur_section, p + 1, len);
cur_section[len] = '\0';
cur_tidx = -1;
continue;
}
/* key = value */
char *eq = strchr(p, '=');
if (!eq) continue;
/* extract key name into local buf first */
int klen = (int)(eq - p);
while (klen > 0 && isspace((unsigned char)p[klen - 1])) klen--;
if (klen <= 0 || klen >= TOML_MAX_STR) continue;
char key[TOML_MAX_STR];
strncpy(key, p, (size_t)klen);
key[klen] = '\0';
char *v = strtrim(eq + 1);
/* strip inline comment (outside strings) */
{
int in_str = 0;
for (char *cp = v; *cp; cp++) {
if (*cp == '"') in_str = !in_str;
if (!in_str && *cp == '#') { *cp = '\0'; break; }
}
v = strtrim(v);
}
/* ── compact array-of-tables: key = [ or key = [{...},...] ── */
if (*v == '[') {
const char *after = v + 1;
while (isspace((unsigned char)*after)) after++;
if (*after == '\0') {
/* multi-line array: opening '[' on its own */
strncpy(ml_section, key, TOML_MAX_STR - 1);
ml_section[TOML_MAX_STR - 1] = '\0';
ml_tidx = 0;
ml_active = 1;
continue;
}
if (*after == '{') {
/* single-line array of inline tables: key = [{...}, ...] */
const char *sp = v + 1;
int tidx_local = 0;
while (*sp && *sp != ']') {
while (*sp && *sp != '{' && *sp != ']') sp++;
if (*sp == '{') {
sp++;
sp = parse_inline_table(sp, doc, key, tidx_local++);
}
}
continue;
}
}
/* ── standard scalar / array value ── */
if (doc->n >= TOML_MAX_ENTRIES) continue;
TomlEntry *ent = &doc->entries[doc->n];
copystr(ent->section, sizeof(ent->section), cur_section);
ent->table_idx = cur_tidx;
copystr(ent->key, sizeof(ent->key), key);
if (*v == '"') {
ent->val.type = TOML_STRING;
const char *sp = unescape_into(v + 1, ent->val.s, TOML_MAX_STR);
(void)sp;
} else if (*v == '[') {
ent->val.type = TOML_ARRAY;
ent->val.a.len = 0;
const char *vp = v + 1;
while (*vp && *vp != ']' && ent->val.a.len < TOML_MAX_ARR) {
while (isspace((unsigned char)*vp) || *vp == ',') vp++;
if (*vp == ']' || *vp == '\0') break;
if (*vp == '"') {
char *dst = ent->val.a.items[ent->val.a.len];
vp = unescape_into(vp + 1, dst, TOML_MAX_STR);
if (*vp == '"') vp++;
ent->val.a.len++;
} else {
vp++;
}
}
} else {
char *ep;
long iv = strtol(v, &ep, 10);
if (ep != v && (*ep == '\0' || *ep == '#' || isspace((unsigned char)*ep))) {
ent->val.type = TOML_INT;
ent->val.i = iv;
} else {
double dv = strtod(v, &ep);
if (ep != v) {
ent->val.type = TOML_FLOAT;
ent->val.d = dv;
} else {
ent->val.type = TOML_STRING;
strncpy(ent->val.s, v, TOML_MAX_STR - 1);
ent->val.s[TOML_MAX_STR - 1] = '\0';
}
}
}
doc->n++;
}
fclose(f);
return 1;
}
const TomlValue *
toml_get(const TomlDoc *doc, const char *section, const char *key)
{
int i;
for (i = 0; i < doc->n; i++) {
const TomlEntry *e = &doc->entries[i];
if (e->table_idx < 0
&& strcmp(e->section, section) == 0
&& strcmp(e->key, key) == 0)
return &e->val;
}
return NULL;
}
const TomlValue *
toml_table_get(const TomlDoc *doc, const char *section, int idx, const char *key)
{
int i;
for (i = 0; i < doc->n; i++) {
const TomlEntry *e = &doc->entries[i];
if (e->table_idx == idx
&& strcmp(e->section, section) == 0
&& strcmp(e->key, key) == 0)
return &e->val;
}
return NULL;
}