-
Notifications
You must be signed in to change notification settings - Fork 11
/
jim.h
386 lines (335 loc) · 9.08 KB
/
jim.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
#ifndef JIM_H_
#define JIM_H_
#ifndef JIM_SCOPES_CAPACITY
#define JIM_SCOPES_CAPACITY 128
#endif // JIM_SCOPES_CAPACITY
typedef void* Jim_Sink;
typedef size_t (*Jim_Write)(const void *ptr, size_t size, size_t nmemb, Jim_Sink sink);
typedef enum {
JIM_OK = 0,
JIM_WRITE_ERROR,
JIM_SCOPES_OVERFLOW,
JIM_SCOPES_UNDERFLOW,
JIM_OUT_OF_SCOPE_KEY,
JIM_DOUBLE_KEY
} Jim_Error;
const char *jim_error_string(Jim_Error error);
typedef enum {
JIM_ARRAY_SCOPE,
JIM_OBJECT_SCOPE,
} Jim_Scope_Kind;
typedef struct {
Jim_Scope_Kind kind;
int tail;
int key;
} Jim_Scope;
typedef struct {
Jim_Sink sink;
Jim_Write write;
Jim_Error error;
Jim_Scope scopes[JIM_SCOPES_CAPACITY];
size_t scopes_size;
} Jim;
void jim_null(Jim *jim);
void jim_bool(Jim *jim, int boolean);
void jim_integer(Jim *jim, long long int x);
void jim_float(Jim *jim, double x, int precision);
void jim_string(Jim *jim, const char *str);
void jim_string_sized(Jim *jim, const char *str, size_t size);
void jim_element_begin(Jim *jim);
void jim_element_end(Jim *jim);
void jim_array_begin(Jim *jim);
void jim_array_end(Jim *jim);
void jim_object_begin(Jim *jim);
void jim_member_key(Jim *jim, const char *str);
void jim_member_key_sized(Jim *jim, const char *str, size_t size);
void jim_object_end(Jim *jim);
#endif // JIM_H_
#ifdef JIM_IMPLEMENTATION
static size_t jim_strlen(const char *s)
{
size_t count = 0;
while (*(s + count)) {
count += 1;
}
return count;
}
static void jim_scope_push(Jim *jim, Jim_Scope_Kind kind)
{
if (jim->error == JIM_OK) {
if (jim->scopes_size < JIM_SCOPES_CAPACITY) {
jim->scopes[jim->scopes_size].kind = kind;
jim->scopes[jim->scopes_size].tail = 0;
jim->scopes[jim->scopes_size].key = 0;
jim->scopes_size += 1;
} else {
jim->error = JIM_SCOPES_OVERFLOW;
}
}
}
static void jim_scope_pop(Jim *jim)
{
if (jim->error == JIM_OK) {
if (jim->scopes_size > 0) {
jim->scopes_size--;
} else {
jim->error = JIM_SCOPES_UNDERFLOW;
}
}
}
static Jim_Scope *jim_current_scope(Jim *jim)
{
if (jim->error == JIM_OK) {
if (jim->scopes_size > 0) {
return &jim->scopes[jim->scopes_size - 1];
}
}
return NULL;
}
static void jim_write(Jim *jim, const char *buffer, size_t size)
{
if (jim->error == JIM_OK) {
if (jim->write(buffer, 1, size, jim->sink) < size) {
jim->error = 1;
}
}
}
static void jim_write_cstr(Jim *jim, const char *cstr)
{
if (jim->error == JIM_OK) {
jim_write(jim, cstr, jim_strlen(cstr));
}
}
static int jim_get_utf8_char_len(unsigned char ch)
{
if ((ch & 0x80) == 0) return 1;
switch (ch & 0xf0) {
case 0xf0:
return 4;
case 0xe0:
return 3;
default:
return 2;
}
}
void jim_element_begin(Jim *jim)
{
if (jim->error == JIM_OK) {
Jim_Scope *scope = jim_current_scope(jim);
if (scope && scope->tail && !scope->key) {
jim_write_cstr(jim, ",");
}
}
}
void jim_element_end(Jim *jim)
{
if (jim->error == JIM_OK) {
Jim_Scope *scope = jim_current_scope(jim);
if (scope) {
scope->tail = 1;
scope->key = 0;
}
}
}
const char *jim_error_string(Jim_Error error)
{
// TODO(#1): error strings are not particularly useful
switch (error) {
case JIM_OK:
return "There is no error. The developer of this software just had a case of \"Task failed successfully\" https://i.imgur.com/Bdb3rkq.jpg - Please contact the developer and tell them that they are very lazy for not checking errors properly.";
case JIM_WRITE_ERROR:
return "Write error";
case JIM_SCOPES_OVERFLOW:
return "Stack of Scopes Overflow";
case JIM_SCOPES_UNDERFLOW:
return "Stack of Scopes Underflow";
case JIM_OUT_OF_SCOPE_KEY:
return "Out of Scope key";
case JIM_DOUBLE_KEY:
return "Tried to set the member key twice";
default:
return NULL;
}
}
void jim_null(Jim *jim)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
jim_write_cstr(jim, "null");
jim_element_end(jim);
}
}
void jim_bool(Jim *jim, int boolean)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
if (boolean) {
jim_write_cstr(jim, "true");
} else {
jim_write_cstr(jim, "false");
}
jim_element_end(jim);
}
}
static void jim_integer_no_element(Jim *jim, long long int x)
{
if (jim->error == JIM_OK) {
if (x < 0) {
jim_write_cstr(jim, "-");
x = -x;
}
if (x == 0) {
jim_write_cstr(jim, "0");
} else {
char buffer[64];
size_t count = 0;
while (x > 0) {
buffer[count++] = (x % 10) + '0';
x /= 10;
}
for (size_t i = 0; i < count / 2; ++i) {
char t = buffer[i];
buffer[i] = buffer[count - i - 1];
buffer[count - i - 1] = t;
}
jim_write(jim, buffer, count);
}
}
}
void jim_integer(Jim *jim, long long int x)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
jim_integer_no_element(jim, x);
jim_element_end(jim);
}
}
static int is_nan_or_inf(double x)
{
unsigned long long int mask = (1ULL << 11ULL) - 1ULL;
return (((*(unsigned long long int*) &x) >> 52ULL) & mask) == mask;
}
void jim_float(Jim *jim, double x, int precision)
{
if (jim->error == JIM_OK) {
if (is_nan_or_inf(x)) {
jim_null(jim);
} else {
jim_element_begin(jim);
jim_integer_no_element(jim, (long long int) x);
x -= (double) (long long int) x;
while (precision-- > 0) {
x *= 10.0;
}
jim_write_cstr(jim, ".");
long long int y = (long long int) x;
if (y < 0) {
y = -y;
}
jim_integer_no_element(jim, y);
jim_element_end(jim);
}
}
}
static void jim_string_sized_no_element(Jim *jim, const char *str, size_t size)
{
if (jim->error == JIM_OK) {
const char *hex_digits = "0123456789abcdef";
const char *specials = "btnvfr";
const char *p = str;
size_t len = size;
jim_write_cstr(jim, "\"");
size_t cl;
for (size_t i = 0; i < len; i++) {
unsigned char ch = ((unsigned char *) p)[i];
if (ch == '"' || ch == '\\') {
jim_write(jim, "\\", 1);
jim_write(jim, p + i, 1);
} else if (ch >= '\b' && ch <= '\r') {
jim_write(jim, "\\", 1);
jim_write(jim, &specials[ch - '\b'], 1);
} else if (0x20 <= ch && ch <= 0x7F) { // is printable
jim_write(jim, p + i, 1);
} else if ((cl = jim_get_utf8_char_len(ch)) == 1) {
jim_write(jim, "\\u00", 4);
jim_write(jim, &hex_digits[(ch >> 4) % 0xf], 1);
jim_write(jim, &hex_digits[ch % 0xf], 1);
} else {
jim_write(jim, p + i, cl);
i += cl - 1;
}
}
jim_write_cstr(jim, "\"");
}
}
void jim_string_sized(Jim *jim, const char *str, size_t size)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
jim_string_sized_no_element(jim, str, size);
jim_element_end(jim);
}
}
void jim_string(Jim *jim, const char *str)
{
if (jim->error == JIM_OK) {
jim_string_sized(jim, str, jim_strlen(str));
}
}
void jim_array_begin(Jim *jim)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
jim_write_cstr(jim, "[");
jim_scope_push(jim, JIM_ARRAY_SCOPE);
}
}
void jim_array_end(Jim *jim)
{
if (jim->error == JIM_OK) {
jim_write_cstr(jim, "]");
jim_scope_pop(jim);
jim_element_end(jim);
}
}
void jim_object_begin(Jim *jim)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
jim_write_cstr(jim, "{");
jim_scope_push(jim, JIM_OBJECT_SCOPE);
}
}
void jim_member_key(Jim *jim, const char *str)
{
if (jim->error == JIM_OK) {
jim_member_key_sized(jim, str, jim_strlen(str));
}
}
void jim_member_key_sized(Jim *jim, const char *str, size_t size)
{
if (jim->error == JIM_OK) {
jim_element_begin(jim);
Jim_Scope *scope = jim_current_scope(jim);
if (scope && scope->kind == JIM_OBJECT_SCOPE) {
if (!scope->key) {
jim_string_sized_no_element(jim, str, size);
jim_write_cstr(jim, ":");
scope->key = 1;
} else {
jim->error = JIM_DOUBLE_KEY;
}
} else {
jim->error = JIM_OUT_OF_SCOPE_KEY;
}
}
}
void jim_object_end(Jim *jim)
{
if (jim->error == JIM_OK) {
jim_write_cstr(jim, "}");
jim_scope_pop(jim);
jim_element_end(jim);
}
}
#endif // JIM_IMPLEMENTATION