Skip to content

Commit a465d70

Browse files
committed
jimp: push strdup onto the user
1 parent 660c608 commit a465d70

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

examples/03_parsing_database.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@ bool parse_person(Jimp *jimp, Person *p)
2323
{
2424
if (!jimp_object_begin(jimp)) return false;
2525
while (jimp_object_member(jimp)) {
26-
if (strcmp(jimp->member, "name") == 0) {
27-
if (!jimp_string(jimp, &p->name)) return false;
28-
} else if (strcmp(jimp->member, "age") == 0) {
29-
if (!jimp_number(jimp, &p->age)) return false;
30-
} else if (strcmp(jimp->member, "location") == 0) {
31-
if (!jimp_string(jimp, &p->location)) return false;
32-
} else if (strcmp(jimp->member, "body_count") == 0) {
33-
if (!jimp_number(jimp, &p->body_count)) return false;
26+
if (strcmp(jimp->string, "name") == 0) {
27+
if (!jimp_string(jimp)) return false;
28+
p->name = strdup(jimp->string);
29+
} else if (strcmp(jimp->string, "age") == 0) {
30+
if (!jimp_number(jimp)) return false;
31+
p->age = jimp->number;
32+
} else if (strcmp(jimp->string, "location") == 0) {
33+
if (!jimp_string(jimp)) return false;
34+
p->location = strdup(jimp->string);
35+
} else if (strcmp(jimp->string, "body_count") == 0) {
36+
if (!jimp_number(jimp)) return false;
37+
p->body_count = jimp->number;
3438
} else {
3539
jimp_unknown_member(jimp);
3640
return false;
@@ -87,14 +91,13 @@ int main()
8791
Numbers xs = {0};
8892
if (!jimp_object_begin(&jimp)) return 1;
8993
while (jimp_object_member(&jimp)) {
90-
if (strcmp(jimp.member, "profile") == 0) {
94+
if (strcmp(jimp.string, "profile") == 0) {
9195
if (!parse_people(&jimp, &ps)) return 1;
92-
} else if (strcmp(jimp.member, "number") == 0) {
96+
} else if (strcmp(jimp.string, "number") == 0) {
9397
if (!jimp_array_begin(&jimp)) return 1;
9498
while (jimp_array_item(&jimp)) {
95-
double x = 0;
96-
if (!jimp_number(&jimp, &x)) return 1;
97-
da_append(&xs, x);
99+
if (!jimp_number(&jimp)) return 1;
100+
da_append(&xs, jimp.number);
98101
}
99102
if (!jimp_array_end(&jimp)) return 1;
100103
} else {

jim2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct {
4343
void jim_null(Jim *jim);
4444
void jim_bool(Jim *jim, int boolean);
4545
void jim_integer(Jim *jim, long long int x);
46+
// TODO: deprecate this version of jim_float introduce the one that does not require precision and uses something like sprintf from libc to render the floats
4647
void jim_float(Jim *jim, double x, int precision);
4748
void jim_string(Jim *jim, const char *str);
4849
void jim_string_sized(Jim *jim, const char *str, size_t size);

jimp.h

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 `{`
5970
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).
6075
bool jimp_object_member(Jimp *jimp);
76+
77+
/// Parses the end of the object `}`
6178
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.
6282
void jimp_unknown_member(Jimp *jimp);
83+
84+
/// Parses the beginning of the array `[`
6385
bool jimp_array_begin(Jimp *jimp);
86+
87+
/// Checks whether there is any more items in the array.
6488
bool jimp_array_item(Jimp *jimp);
89+
90+
/// Parses the end of the array `]`
6591
bool jimp_array_end(Jimp *jimp);
92+
93+
/// Prints diagnostic at the current position of the parser.
6694
void 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

245273
void 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

250278
bool 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

287311
bool 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

308330
static bool jimp__get_and_expect_token(Jimp *jimp, Jimp_Token token)

0 commit comments

Comments
 (0)