1
- // Prototype of an Immediate Deserialization idea.
1
+ // Prototype of an Immediate Deserialization idea. Expect this API to change a lot.
2
2
#ifndef JIMP_H_
3
3
#define JIMP_H_
4
4
@@ -48,21 +48,49 @@ typedef struct {
48
48
size_t string_count ;
49
49
size_t string_capacity ;
50
50
double number ;
51
-
52
- const char * member ;
51
+ bool boolean ;
53
52
} Jimp ;
54
53
55
54
// TODO: how do null-s fit into this entire system?
56
- bool jimp_bool (Jimp * jimp , bool * boolean );
57
- bool jimp_number (Jimp * jimp , double * number );
58
- bool jimp_string (Jimp * jimp , const char * * string );
55
+
56
+ /// If succeeds puts the freshly parsed boolean into jimp->boolean.
57
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->boolean.
58
+ bool jimp_boolean (Jimp * jimp );
59
+
60
+ /// If succeeds puts the freshly parsed number into jimp->number.
61
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->number.
62
+ bool jimp_number (Jimp * jimp );
63
+
64
+ /// If succeeds puts the freshly parsed string into jimp->string as a NULL-terminated string.
65
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->string.
66
+ /// strdup it if you don't wanna lose it (memory management is on you at that point).
67
+ bool jimp_string (Jimp * jimp );
68
+
69
+ /// Parses the beginning of the object `{`
59
70
bool jimp_object_begin (Jimp * jimp );
71
+
72
+ /// If succeeds puts the key of the member into jimp->string as a NULL-terminated string.
73
+ /// Any consequent calls to the jimp_* functions may invalidate jimp->string.
74
+ /// strdup it if you don't wanna lose it (memory management is on you at that point).
60
75
bool jimp_object_member (Jimp * jimp );
76
+
77
+ /// Parses the end of the object `}`
61
78
bool jimp_object_end (Jimp * jimp );
79
+
80
+ /// Reports jimp->string as an unknown member. jimp->string is expected to be populated by
81
+ /// jimp_object_member.
62
82
void jimp_unknown_member (Jimp * jimp );
83
+
84
+ /// Parses the beginning of the array `[`
63
85
bool jimp_array_begin (Jimp * jimp );
86
+
87
+ /// Checks whether there is any more items in the array.
64
88
bool jimp_array_item (Jimp * jimp );
89
+
90
+ /// Parses the end of the array `]`
65
91
bool jimp_array_end (Jimp * jimp );
92
+
93
+ /// Prints diagnostic at the current position of the parser.
66
94
void jimp_diagf (Jimp * jimp , const char * fmt , ...);
67
95
68
96
#endif // JIMP_H_
@@ -145,7 +173,7 @@ static bool jimp__get_token(Jimp *jimp)
145
173
}
146
174
147
175
char * endptr = NULL ;
148
- jimp -> number = strtod (jimp -> point , & endptr ); // TODO: this implies that jimp->end is a valid address and *jimp->end == 0
176
+ jimp -> number = strtod (jimp -> point , & endptr ); // TODO: This implies that jimp->end is a valid address and *jimp->end == 0
149
177
if (jimp -> point != endptr ) {
150
178
jimp -> point = endptr ;
151
179
jimp -> token = JIMP_NUMBER ;
@@ -244,7 +272,7 @@ bool jimp_array_item(Jimp *jimp)
244
272
245
273
void jimp_unknown_member (Jimp * jimp )
246
274
{
247
- jimp_diagf (jimp , "ERROR: unexpected object member `%s`\n" , jimp -> member );
275
+ jimp_diagf (jimp , "ERROR: unexpected object member `%s`\n" , jimp -> string );
248
276
}
249
277
250
278
bool jimp_object_begin (Jimp * jimp )
@@ -258,7 +286,6 @@ bool jimp_object_member(Jimp *jimp)
258
286
if (!jimp__get_token (jimp )) return false;
259
287
if (jimp -> token == JIMP_COMMA ) {
260
288
if (!jimp__get_and_expect_token (jimp , JIMP_STRING )) return false;
261
- jimp -> member = strdup (jimp -> string ); // TODO: memory leak
262
289
if (!jimp__get_and_expect_token (jimp , JIMP_COLON )) return false;
263
290
return true;
264
291
}
@@ -267,7 +294,6 @@ bool jimp_object_member(Jimp *jimp)
267
294
return false;
268
295
}
269
296
if (!jimp__expect_token (jimp , JIMP_STRING )) return false;
270
- jimp -> member = strdup (jimp -> string ); // TODO: memory leak
271
297
if (!jimp__get_and_expect_token (jimp , JIMP_COLON )) return false;
272
298
return true;
273
299
}
@@ -277,11 +303,9 @@ bool jimp_object_end(Jimp *jimp)
277
303
return jimp__get_and_expect_token (jimp , JIMP_CCURLY );
278
304
}
279
305
280
- bool jimp_string (Jimp * jimp , const char * * string )
306
+ bool jimp_string (Jimp * jimp )
281
307
{
282
- if (!jimp__get_and_expect_token (jimp , JIMP_STRING )) return false;
283
- * string = strdup (jimp -> string );
284
- return true;
308
+ return jimp__get_and_expect_token (jimp , JIMP_STRING );
285
309
}
286
310
287
311
bool jimp_bool (Jimp * jimp , bool * boolean )
@@ -298,11 +322,9 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
298
322
return true;
299
323
}
300
324
301
- bool jimp_number (Jimp * jimp , double * number )
325
+ bool jimp_number (Jimp * jimp )
302
326
{
303
- if (!jimp__get_and_expect_token (jimp , JIMP_NUMBER )) return false;
304
- * number = jimp -> number ;
305
- return true;
327
+ return jimp__get_and_expect_token (jimp , JIMP_NUMBER );
306
328
}
307
329
308
330
static bool jimp__get_and_expect_token (Jimp * jimp , Jimp_Token token )
0 commit comments