diff options
author | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-01-05 22:51:33 +0100 |
---|---|---|
committer | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-01-06 18:58:13 +0100 |
commit | e079370a92766181ff5281c5d6cea03a1fce5b93 (patch) | |
tree | c05ffce648ea7cb5e3c3ebb102ac4c80ff9b4c35 /src/internal.h |
initial release
Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'src/internal.h')
-rw-r--r-- | src/internal.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/internal.h b/src/internal.h new file mode 100644 index 0000000..7ea8a5e --- /dev/null +++ b/src/internal.h @@ -0,0 +1,74 @@ +#ifndef _JSON_INTERNAL_H_ +#define _JSON_INTERNAL_H_ + +#include <stdlib.h> + +#include <json.h> + +struct raw_json { + const char *data; + size_t size; + size_t index; +}; + +inline static void skip_ws(struct raw_json *raw) { + // empty loop to move i along until we hit a non-whitespace + while (raw->index < raw->size && + (raw->data[raw->index] == ' ' || + raw->data[raw->index] == '\n' || + raw->data[raw->index] == '\r' || + raw->data[raw->index] == '\t')) + { + raw->index++; + } +} + +inline static bool init_children(struct json *obj) { + const size_t starting_size = 4; + obj->children = calloc(sizeof(*obj->children) + (sizeof(*obj->children->items) * starting_size), 1); + if (!obj->children) + return false; + obj->children->maxitems = starting_size; + obj->children->nitems = 0; + return true; +} + + +inline static void add_children(struct json *dest, struct json *src) { + struct json_children *children = dest->children; + if (children->nitems + 1 >= children->maxitems) { + children->maxitems += 5; + children = realloc(children, sizeof(*children) + (sizeof(*children->items) * children->maxitems)); + if (!children) + return; + dest->children = children; + } + + src->parent = dest; + src->index = children->nitems; + + if (children->nitems > 0) { + children->items[children->nitems - 1]->next = src; + src->prev = children->items[children->nitems - 1]; + } + + children->items[children->nitems++] = src; + src->parent = dest; +} + +inline static void adjust_pointers(struct json* from, struct json* to) { + to->prev = from->prev; + to->next = from->next; + if (from->prev) from->prev->next = to; + if (from->next) from->next->prev = to; +} + + +enum json_parse_result parse_value(struct json **json_out, struct raw_json *raw, size_t depth); +enum json_parse_result parse_object(struct json **json_out, struct raw_json *raw, size_t depth); +enum json_parse_result parse_array(struct json **json_out, struct raw_json *raw, size_t depth); +enum json_parse_result parse_raw_string(char **str_out, struct raw_json *raw); +enum json_parse_result parse_string(struct json **json_out, struct raw_json *raw); +enum json_parse_result parse_literal(struct json **json_out, struct raw_json *raw); + +#endif |