summaryrefslogtreecommitdiff
path: root/src/array.c
blob: de14692e767adc8d1b41bd051616dfb0dd1ecfb4 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <json.h>
#include "internal.h"

enum json_parse_result parse_array(struct json **json_out, struct raw_json *raw, size_t depth) {
	assert(raw->data[raw->index] == '[');
	enum json_parse_result ret = JSON_PARSE_OK;
	struct json *json = NULL;
	struct json *obj = NULL;

	if (depth++ > 9) {
		ret = JSON_PARSE_MAX_DEPTH_ERR;
		goto err;
	}

	json = json_array();
	if (!json)
		return JSON_OOM;

	do {
		raw->index++;
		skip_ws(raw);

		if (raw->data[raw->index] == ']') {
			if (json->children->nitems == 0) {
				break;
			}
			ret = JSON_PARSE_EXPECTED_VALUE_ERR;
			goto err;
		}

		ret = parse_value(&obj, raw, depth);
		if (ret != JSON_PARSE_OK) {
			goto err;
		}

		json_append(json, obj);
		skip_ws(raw);

		obj = NULL;
	} while (raw->index < raw->size && raw->data[raw->index] == ',');

	if (raw->data[raw->index] != ']') {
		ret = JSON_PARSE_INVALID_TOKEN_ERR;
		goto err;
	}
	raw->index++;

	*json_out = json;
	goto end;
err:
	json_delete(json);
	json_delete(obj);
	
end:
	return ret;
}


bool json_append(struct json *dest, struct json *src) {
	if (!dest || !src)
		return false;
	json_detach(src);
	add_children(dest, src);
	return true;
}

bool json_insert(struct json *dest, size_t index, struct json *src) {
	if (!dest || !src)
		return false;
	json_detach(src);
	if (index >= dest->children->nitems) {
		add_children(dest, src);
		return true;
	}
	if (!ensure_size(dest, 1))
		return false;
	struct json_children *chld = dest->children;
	if (index > 0) {
		chld->items[index]->prev = src;
		chld->items[index - 1]->next = src;
	}
	for (size_t i = index; i < chld->nitems - 1; i++)
		chld->items[i + 1] = chld->items[i];
	chld->items[index] = src;

	return true;
}

bool json_replace(struct json *dest, size_t index, struct json *src) {
	if (!dest || !src)
		return false;
	json_detach(src);

	struct json_children *chld = dest->children;
	if (index >= chld->nitems) {
		add_children(dest, src);
		return true;
	}
	struct json *old = json_swap_index(dest, index, src);
	json_clear(old);
	free(old);

	return true;
}

struct json *json_at_mut(struct json *json, size_t index) {
	if (!json || json->type != JSON_ARRAY || index > json->children->nitems)
		return NULL;

	return json->children->items[index];
}

const struct json *json_at_const(const struct json *array, size_t index) {
	return json_at_mut((struct json *) array, index);
}

struct json *json_detach_index(struct json *json, size_t index) {
	if (!json_is_array(json) || index >= json->children->nitems)
		return NULL;

	struct json *tmp = json->children->items[index];
	json_detach(tmp);
	return tmp;
}

struct json *json_swap_index(struct json *json, size_t index, struct json *src) {
	if (!json_is_array(json) || index >= json->children->nitems || !src)
		return NULL;
	json_detach(src);
	struct json_children *chld = json->children;
	if (index > 0)
		chld->items[index - 1]->next = src;
	if (index < chld->nitems - 1)
		chld->items[index + 1]->prev = src;
	struct json *old = chld->items[index];
	chld->items[index] = src;
	return old;
}

void json_delete_index(struct json *json, size_t index) {
	struct json *tmp = json_detach_index(json, index);
	json_delete(tmp);
}