chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4a5f9dbe0bf279a628d6a09f42add4c570e34509
parent f393436c1d5676d131096e2cd410ef219a014d3e
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 18 Dec 2020 10:49:48 -0800

array progress

Diffstat:
Msrc/json.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Msrc/test_json.c | 13+++++++------
2 files changed, 84 insertions(+), 10 deletions(-)

diff --git a/src/json.c b/src/json.c @@ -325,10 +325,22 @@ static int parse_kv(struct json_parser *p) return parse_value(p); } -static inline int finalize_object(struct json_parser *p, unsigned int *len) + +static inline int finalize_container(struct json_parser *p, unsigned int *len, char end) { *len = p->ubjson.p + 1 - ((unsigned char*)len)-4; - return push_byte(&p->ubjson, '}'); + p->cur.p++; + return push_byte(&p->ubjson, end); +} + +static inline int finalize_array(struct json_parser *p, unsigned int *len) +{ + return finalize_container(p, len, ']'); +} + +static inline int finalize_object(struct json_parser *p, unsigned int *len) +{ + return finalize_container(p, len, '}'); } static int parse_object(struct json_parser *p) @@ -402,8 +414,69 @@ static int parse_object(struct json_parser *p) static int parse_array(struct json_parser *p) { - (void)p; - note_error(&p->errs, "implement parse_array"); + char c; + unsigned int *len; + + c = 0; + + if (!parse_char(&p->cur, &c, '[')) { + note_error(&p->errs, "expected '[', got '%c'", c); + return 0; + } + + if (!push_byte(&p->ubjson, '[')) { + note_error(&p->errs, "push ubjson '[' oob"); + return 0; + } + + if (!push_byte(&p->ubjson, 'l')) { + note_error(&p->errs, "push ubjson 'l' oob"); + return 0; + } + + /* fill this in later */ + len = (unsigned int *)p->ubjson.p; + + if (!push_int(&p->ubjson, 0)) { + note_error(&p->errs, "push ubjson array len oob"); + return 0; + } + + consume_whitespace(&p->cur); + + /* empty array case */ + if (!peek_char(&p->cur, &c)) { + note_error(&p->errs, "empty array peek oob"); + return 0; + } + + if (c == ']') { + finalize_array(p, len); + return 1; + } + + while(1) { + if (!parse_value(p)) { + note_error(&p->errs, "expected json value"); + return 0; + } + + if (!peek_char(&p->cur, &c)) { + note_error(&p->errs, "oob"); + return 0; + } + + if (c == ']') { + return finalize_array(p, len); + } + + if (c != ',') { + note_error(&p->errs, "expected ',', got '%c'", c); + return 0; + } + } + + return 0; } diff --git a/src/test_json.c b/src/test_json.c @@ -9,6 +9,7 @@ #include <sys/types.h> #include <fcntl.h> #include <unistd.h> +#include <stdlib.h> int map_file(const char *filename, unsigned char **p, size_t *flen) { @@ -36,17 +37,16 @@ static void write_data(unsigned char *data, int data_size) int main(int argc, char *argv[]) { - static unsigned char out[1024] = {0}; + unsigned char *out; unsigned char *p; struct ubjson ubjson; struct json val; - size_t flen; + size_t flen, outlen; - init_ubjson(&ubjson, out, sizeof(out)); + outlen = 1024 * 1024 * 80; + out = malloc(outlen); - //unsigned char bad[] = "{a}"; - //assert(!parse_json(bad, sizeof(bad), &ubjson)); - //ubjson.data_size = sizeof(out); + init_ubjson(&ubjson, out, outlen); unsigned char empty[] = "{}"; assert(parse_json(empty, sizeof(empty), &ubjson)); @@ -75,4 +75,5 @@ int main(int argc, char *argv[]) print_value(&val); printf("\n"); + free(out); }