summaryrefslogtreecommitdiff
path: root/src/internal.h
blob: f1e08dcc8722bb26ab6f3734df1a32b0b13e0139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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;
}

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