1- // Prototype of an Immediate Deserialization idea.
1+ // Prototype of an Immediate Deserialization idea. Expect this API to change a lot.
22#ifndef JIMP_H_
33#define JIMP_H_
44
@@ -48,21 +48,49 @@ typedef struct {
4848 size_t string_count ;
4949 size_t string_capacity ;
5050 double number ;
51-
52- const char * member ;
51+ bool boolean ;
5352} Jimp ;
5453
5554// 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 `{`
5970bool 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).
6075bool jimp_object_member (Jimp * jimp );
76+
77+ /// Parses the end of the object `}`
6178bool 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.
6282void jimp_unknown_member (Jimp * jimp );
83+
84+ /// Parses the beginning of the array `[`
6385bool jimp_array_begin (Jimp * jimp );
86+
87+ /// Checks whether there is any more items in the array.
6488bool jimp_array_item (Jimp * jimp );
89+
90+ /// Parses the end of the array `]`
6591bool jimp_array_end (Jimp * jimp );
92+
93+ /// Prints diagnostic at the current position of the parser.
6694void jimp_diagf (Jimp * jimp , const char * fmt , ...);
6795
6896#endif // JIMP_H_
@@ -145,7 +173,7 @@ static bool jimp__get_token(Jimp *jimp)
145173 }
146174
147175 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
149177 if (jimp -> point != endptr ) {
150178 jimp -> point = endptr ;
151179 jimp -> token = JIMP_NUMBER ;
@@ -244,7 +272,7 @@ bool jimp_array_item(Jimp *jimp)
244272
245273void jimp_unknown_member (Jimp * jimp )
246274{
247- jimp_diagf (jimp , "ERROR: unexpected object member `%s`\n" , jimp -> member );
275+ jimp_diagf (jimp , "ERROR: unexpected object member `%s`\n" , jimp -> string );
248276}
249277
250278bool jimp_object_begin (Jimp * jimp )
@@ -258,7 +286,6 @@ bool jimp_object_member(Jimp *jimp)
258286 if (!jimp__get_token (jimp )) return false;
259287 if (jimp -> token == JIMP_COMMA ) {
260288 if (!jimp__get_and_expect_token (jimp , JIMP_STRING )) return false;
261- jimp -> member = strdup (jimp -> string ); // TODO: memory leak
262289 if (!jimp__get_and_expect_token (jimp , JIMP_COLON )) return false;
263290 return true;
264291 }
@@ -267,7 +294,6 @@ bool jimp_object_member(Jimp *jimp)
267294 return false;
268295 }
269296 if (!jimp__expect_token (jimp , JIMP_STRING )) return false;
270- jimp -> member = strdup (jimp -> string ); // TODO: memory leak
271297 if (!jimp__get_and_expect_token (jimp , JIMP_COLON )) return false;
272298 return true;
273299}
@@ -277,11 +303,9 @@ bool jimp_object_end(Jimp *jimp)
277303 return jimp__get_and_expect_token (jimp , JIMP_CCURLY );
278304}
279305
280- bool jimp_string (Jimp * jimp , const char * * string )
306+ bool jimp_string (Jimp * jimp )
281307{
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 );
285309}
286310
287311bool jimp_bool (Jimp * jimp , bool * boolean )
@@ -298,11 +322,9 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
298322 return true;
299323}
300324
301- bool jimp_number (Jimp * jimp , double * number )
325+ bool jimp_number (Jimp * jimp )
302326{
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 );
306328}
307329
308330static bool jimp__get_and_expect_token (Jimp * jimp , Jimp_Token token )
0 commit comments