소스 검색

jimp: push strdup onto the user

rexim 1 개월 전
부모
커밋
a465d7035f
3개의 변경된 파일57개의 추가작업 그리고 31개의 파일을 삭제
  1. 16 13
      examples/03_parsing_database.c
  2. 1 0
      jim2.h
  3. 40 18
      jimp.h

+ 16 - 13
examples/03_parsing_database.c

@@ -23,14 +23,18 @@ bool parse_person(Jimp *jimp, Person *p)
 {
     if (!jimp_object_begin(jimp)) return false;
     while (jimp_object_member(jimp)) {
-        if (strcmp(jimp->member, "name") == 0) {
-            if (!jimp_string(jimp, &p->name))       return false;
-        } else if (strcmp(jimp->member, "age") == 0) {
-            if (!jimp_number(jimp, &p->age))        return false;
-        } else if (strcmp(jimp->member, "location") == 0) {
-            if (!jimp_string(jimp, &p->location))   return false;
-        } else if (strcmp(jimp->member, "body_count") == 0) {
-            if (!jimp_number(jimp, &p->body_count)) return false;
+        if (strcmp(jimp->string, "name") == 0) {
+            if (!jimp_string(jimp)) return false;
+            p->name = strdup(jimp->string);
+        } else if (strcmp(jimp->string, "age") == 0) {
+            if (!jimp_number(jimp)) return false;
+            p->age = jimp->number;
+        } else if (strcmp(jimp->string, "location") == 0) {
+            if (!jimp_string(jimp)) return false;
+            p->location = strdup(jimp->string);
+        } else if (strcmp(jimp->string, "body_count") == 0) {
+            if (!jimp_number(jimp)) return false;
+            p->body_count = jimp->number;
         } else {
             jimp_unknown_member(jimp);
             return false;
@@ -87,14 +91,13 @@ int main()
     Numbers xs = {0};
     if (!jimp_object_begin(&jimp)) return 1;
     while (jimp_object_member(&jimp)) {
-        if (strcmp(jimp.member, "profile") == 0) {
+        if (strcmp(jimp.string, "profile") == 0) {
             if (!parse_people(&jimp, &ps)) return 1;
-        } else if (strcmp(jimp.member, "number") == 0) {
+        } else if (strcmp(jimp.string, "number") == 0) {
             if (!jimp_array_begin(&jimp)) return 1;
             while (jimp_array_item(&jimp)) {
-                double x = 0;
-                if (!jimp_number(&jimp, &x)) return 1;
-                da_append(&xs, x);
+                if (!jimp_number(&jimp)) return 1;
+                da_append(&xs, jimp.number);
             }
             if (!jimp_array_end(&jimp)) return 1;
         } else {

+ 1 - 0
jim2.h

@@ -43,6 +43,7 @@ typedef struct {
 void jim_null(Jim *jim);
 void jim_bool(Jim *jim, int boolean);
 void jim_integer(Jim *jim, long long int x);
+// 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
 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);

+ 40 - 18
jimp.h

@@ -1,4 +1,4 @@
-// Prototype of an Immediate Deserialization idea.
+// Prototype of an Immediate Deserialization idea. Expect this API to change a lot.
 #ifndef JIMP_H_
 #define JIMP_H_
 
@@ -48,21 +48,49 @@ typedef struct {
     size_t string_count;
     size_t string_capacity;
     double number;
-
-    const char *member;
+    bool boolean;
 } Jimp;
 
 // TODO: how do null-s fit into this entire system?
-bool jimp_bool(Jimp *jimp, bool *boolean);
-bool jimp_number(Jimp *jimp, double *number);
-bool jimp_string(Jimp *jimp, const char **string);
+
+/// If succeeds puts the freshly parsed boolean into jimp->boolean.
+/// Any consequent calls to the jimp_* functions may invalidate jimp->boolean.
+bool jimp_boolean(Jimp *jimp);
+
+/// If succeeds puts the freshly parsed number into jimp->number.
+/// Any consequent calls to the jimp_* functions may invalidate jimp->number.
+bool jimp_number(Jimp *jimp);
+
+/// If succeeds puts the freshly parsed string into jimp->string as a NULL-terminated string.
+/// Any consequent calls to the jimp_* functions may invalidate jimp->string.
+/// strdup it if you don't wanna lose it (memory management is on you at that point).
+bool jimp_string(Jimp *jimp);
+
+/// Parses the beginning of the object `{`
 bool jimp_object_begin(Jimp *jimp);
+
+/// If succeeds puts the key of the member into jimp->string as a NULL-terminated string.
+/// Any consequent calls to the jimp_* functions may invalidate jimp->string.
+/// strdup it if you don't wanna lose it (memory management is on you at that point).
 bool jimp_object_member(Jimp *jimp);
+
+/// Parses the end of the object `}`
 bool jimp_object_end(Jimp *jimp);
+
+/// Reports jimp->string as an unknown member. jimp->string is expected to be populated by
+/// jimp_object_member.
 void jimp_unknown_member(Jimp *jimp);
+
+/// Parses the beginning of the array `[`
 bool jimp_array_begin(Jimp *jimp);
+
+/// Checks whether there is any more items in the array.
 bool jimp_array_item(Jimp *jimp);
+
+/// Parses the end of the array `]`
 bool jimp_array_end(Jimp *jimp);
+
+/// Prints diagnostic at the current position of the parser.
 void jimp_diagf(Jimp *jimp, const char *fmt, ...);
 
 #endif // JIMP_H_
@@ -145,7 +173,7 @@ static bool jimp__get_token(Jimp *jimp)
     }
 
     char *endptr = NULL;
-    jimp->number = strtod(jimp->point, &endptr); // TODO: this implies that jimp->end is a valid address and *jimp->end == 0
+    jimp->number = strtod(jimp->point, &endptr); // TODO: This implies that jimp->end is a valid address and *jimp->end == 0
     if (jimp->point != endptr) {
         jimp->point = endptr;
         jimp->token = JIMP_NUMBER;
@@ -244,7 +272,7 @@ bool jimp_array_item(Jimp *jimp)
 
 void jimp_unknown_member(Jimp *jimp)
 {
-    jimp_diagf(jimp, "ERROR: unexpected object member `%s`\n", jimp->member);
+    jimp_diagf(jimp, "ERROR: unexpected object member `%s`\n", jimp->string);
 }
 
 bool jimp_object_begin(Jimp *jimp)
@@ -258,7 +286,6 @@ bool jimp_object_member(Jimp *jimp)
     if (!jimp__get_token(jimp)) return false;
     if (jimp->token == JIMP_COMMA) {
         if (!jimp__get_and_expect_token(jimp, JIMP_STRING)) return false;
-        jimp->member = strdup(jimp->string); // TODO: memory leak
         if (!jimp__get_and_expect_token(jimp, JIMP_COLON)) return false;
         return true;
     }
@@ -267,7 +294,6 @@ bool jimp_object_member(Jimp *jimp)
         return false;
     }
     if (!jimp__expect_token(jimp, JIMP_STRING)) return false;
-    jimp->member = strdup(jimp->string); // TODO: memory leak
     if (!jimp__get_and_expect_token(jimp, JIMP_COLON)) return false;
     return true;
 }
@@ -277,11 +303,9 @@ bool jimp_object_end(Jimp *jimp)
     return jimp__get_and_expect_token(jimp, JIMP_CCURLY);
 }
 
-bool jimp_string(Jimp *jimp, const char **string)
+bool jimp_string(Jimp *jimp)
 {
-    if (!jimp__get_and_expect_token(jimp, JIMP_STRING)) return false;
-    *string = strdup(jimp->string);
-    return true;
+    return jimp__get_and_expect_token(jimp, JIMP_STRING);
 }
 
 bool jimp_bool(Jimp *jimp, bool *boolean)
@@ -298,11 +322,9 @@ bool jimp_bool(Jimp *jimp, bool *boolean)
     return true;
 }
 
-bool jimp_number(Jimp *jimp, double *number)
+bool jimp_number(Jimp *jimp)
 {
-    if (!jimp__get_and_expect_token(jimp, JIMP_NUMBER)) return false;
-    *number = jimp->number;
-    return true;
+    return jimp__get_and_expect_token(jimp, JIMP_NUMBER);
 }
 
 static bool jimp__get_and_expect_token(Jimp *jimp, Jimp_Token token)