-
Notifications
You must be signed in to change notification settings - Fork 2
/
vson.h
290 lines (243 loc) · 6.43 KB
/
vson.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
#ifndef VIOLET_VSON_H
#define VIOLET_VSON_H
#include "violet/core.h"
#ifndef VSON_LABEL_SZ
#define VSON_LABEL_SZ 64
#endif
#ifndef VSON_VALUE_SZ
#define VSON_VALUE_SZ 64
#endif
b32 vson_read_header(FILE *fp, const char *label);
b32 vson_read_b8(FILE *fp, const char *label, b8 *val);
b32 vson_read_u8(FILE *fp, const char *label, u8 *val);
b32 vson_read_s8(FILE *fp, const char *label, s8 *val);
b32 vson_read_char(FILE *fp, const char *label, char *val);
b32 vson_read_u16(FILE *fp, const char *label, u16 *val);
b32 vson_read_s16(FILE *fp, const char *label, s16 *val);
b32 vson_read_b32(FILE *fp, const char *label, b32 *val);
b32 vson_read_s32(FILE *fp, const char *label, s32 *val);
b32 vson_read_u32(FILE *fp, const char *label, u32 *val);
b32 vson_read_r32(FILE *fp, const char *label, r32 *val);
b32 vson_read_s64(FILE *fp, const char *label, s64 *val);
b32 vson_read_u64(FILE *fp, const char *label, u64 *val);
b32 vson_read_r64(FILE *fp, const char *label, r64 *val);
b32 vson_read_str(FILE *fp, const char *label, char *val, u32 sz);
void vson_write_header(FILE *fp, const char *label);
void vson_write_b32(FILE *fp, const char *label, b32 val);
void vson_write_s32(FILE *fp, const char *label, s32 val);
void vson_write_u32(FILE *fp, const char *label, u32 val);
void vson_write_r32(FILE *fp, const char *label, r32 val);
void vson_write_s64(FILE *fp, const char *label, s64 val);
void vson_write_u64(FILE *fp, const char *label, u64 val);
void vson_write_r64(FILE *fp, const char *label, r64 val);
void vson_write_char(FILE *fp, const char *label, char val);
void vson_write_str(FILE *fp, const char *label, const char *val);
#endif
/* Implementation */
#ifdef VSON_IMPLEMENTATION
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
static b32 vson__read_label(FILE *fp, const char *label)
{
b32 retval = false;
char c = 0;
char label_buf[VSON_LABEL_SZ], *bufp = label_buf;
while ((c = fgetc(fp)) != EOF && isspace((u8)c))
;
if (isspace((u8)c)) {
log_error("vson: failed reading label %s", label);
goto out;
}
*(bufp++) = c;
while (bufp - label_buf < VSON_LABEL_SZ && (c = fgetc(fp)) != EOF && c != ':')
*(bufp++) = c;
if (c != ':') {
log_error("vson: missing colon after label %s", label);
goto out;
}
*bufp = '\0';
if (strcmp(label_buf, label) != 0) {
log_error("vson: expected %s, got %s", label, label_buf);
goto out;
}
retval = fgetc(fp) == ' ';
out:
return retval;
}
static b32 vson__skip_rest_of_line(FILE *fp)
{
char c;
while ((c = fgetc(fp)) != EOF && c != '\n')
;
return c == '\n';
}
static u32 vson__read_rest_of_line(FILE *fp, char *str, u32 n)
{
char *p = str;
char c = 0;
while ((u32)(p - str) < n && (c = fgetc(fp)) != EOF && c != '\n')
*(p++) = c;
if (c != '\n') {
vson__skip_rest_of_line(fp);
return 0;
}
if (p > str && p[-1] == '\r')
p[-1] = '\0';
else
*p = '\0';
return (u32)(p - str);
}
#define VSON_READ_VAL(expr) \
char buf[VSON_VALUE_SZ]; \
if (!vson__read_label(fp, label)) { \
vson__skip_rest_of_line(fp); \
return false; \
} \
if (vson__read_rest_of_line(fp, buf, VSON_VALUE_SZ) == 0) \
return false; \
*val = expr; \
return true
b32 vson_read_header(FILE *fp, const char *label)
{
const b32 ret = vson__read_label(fp, label);
vson__skip_rest_of_line(fp);
return ret;
}
b32 vson_read_b8(FILE *fp, const char *label, b8 *val)
{
b32 val_;
if (vson_read_b32(fp, label, &val_)) {
*val = val_;
return true;
}
return false;
}
b32 vson_read_u8(FILE *fp, const char *label, u8 *val)
{
u32 val_;
if (vson_read_u32(fp, label, &val_) && val_ <= UINT8_MAX) {
*val = val_;
return true;
}
return false;
}
b32 vson_read_s8(FILE *fp, const char *label, s8 *val)
{
s32 val_;
if (vson_read_s32(fp, label, &val_) && val_ >= INT8_MIN && val_ <= INT8_MAX) {
*val = val_;
return true;
}
return false;
}
b32 vson_read_char(FILE *fp, const char *label, char *val)
{
VSON_READ_VAL(buf[0]);
}
b32 vson_read_u16(FILE *fp, const char *label, u16 *val)
{
u32 val_;
if (vson_read_u32(fp, label, &val_) && val_ <= UINT16_MAX) {
*val = val_;
return true;
}
return false;
}
b32 vson_read_s16(FILE *fp, const char *label, s16 *val)
{
s32 val_;
if ( vson_read_s32(fp, label, &val_)
&& val_ >= INT16_MIN
&& val_ <= INT16_MAX) {
*val = val_;
return true;
}
return false;
}
b32 vson_read_b32(FILE *fp, const char *label, b32 *val)
{
VSON_READ_VAL(buf[0] != '0' && buf[0] != 'f' && buf[0] != ' ');
}
b32 vson_read_s32(FILE *fp, const char *label, s32 *val)
{
VSON_READ_VAL((s32)strtol(buf, NULL, 10));
}
b32 vson_read_u32(FILE *fp, const char *label, u32 *val)
{
VSON_READ_VAL((u32)strtoul(buf, NULL, 10));
}
b32 vson_read_r32(FILE *fp, const char *label, r32 *val)
{
VSON_READ_VAL(strtof(buf, NULL));
}
b32 vson_read_s64(FILE *fp, const char *label, s64 *val)
{
VSON_READ_VAL((s64)strtoll(buf, NULL, 10));
}
b32 vson_read_u64(FILE *fp, const char *label, u64 *val)
{
VSON_READ_VAL((u64)strtoull(buf, NULL, 10));
}
b32 vson_read_r64(FILE *fp, const char *label, r64 *val)
{
VSON_READ_VAL(strtod(buf, NULL));
}
b32 vson_read_str(FILE *fp, const char *label, char *val, u32 sz)
{
char c = 0, *p = val;
if (!vson__read_label(fp, label)) {
vson__skip_rest_of_line(fp);
return false;
}
while (p < sz + val && (c = fgetc(fp)) != EOF && c != '\n' && c != '\r')
*(p++) = c;
if (p < val + sz && (c == EOF || c == '\r' || c == '\n')) {
*p = '\0';
return true;
} else {
return false;
}
}
void vson_write_header(FILE *fp, const char *label)
{
fprintf(fp, "\n%s: \n", label);
}
void vson_write_b32(FILE *fp, const char *label, b32 val)
{
fprintf(fp, "%s: %c\n", label, val ? 't' : 'f');
}
void vson_write_s32(FILE *fp, const char *label, s32 val)
{
fprintf(fp, "%s: %d\n", label, val);
}
void vson_write_u32(FILE *fp, const char *label, u32 val)
{
fprintf(fp, "%s: %u\n", label, val);
}
void vson_write_s64(FILE *fp, const char *label, s64 val)
{
fprintf(fp, "%s: %" PRId64 "\n", label, val);
}
void vson_write_u64(FILE *fp, const char *label, u64 val)
{
fprintf(fp, "%s: %" PRIu64 "\n", label, val);
}
void vson_write_r32(FILE *fp, const char *label, r32 val)
{
fprintf(fp, "%s: %f\n", label, val);
}
void vson_write_r64(FILE *fp, const char *label, r64 val)
{
fprintf(fp, "%s: %f\n", label, val);
}
void vson_write_char(FILE *fp, const char *label, char val)
{
fprintf(fp, "%s: %c\n", label, val);
}
void vson_write_str(FILE *fp, const char *label, const char *val)
{
fprintf(fp, "%s: %s\n", label, val);
}
#undef VSON_IMPLEMENTATION
#endif // VSON_IMPLEMENTATION