From 1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 12 Apr 2024 12:18:03 +0200 Subject: libjson: new api a lot... changed... Signed-off-by: Anna (navi) Figueiredo Gomes --- src/array.c | 102 +++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 42 deletions(-) (limited to 'src/array.c') diff --git a/src/array.c b/src/array.c index c34a8e5..de14692 100644 --- a/src/array.c +++ b/src/array.c @@ -15,7 +15,7 @@ enum json_parse_result parse_array(struct json **json_out, struct raw_json *raw, goto err; } - json = json_new_array(); + json = json_array(); if (!json) return JSON_OOM; @@ -36,7 +36,7 @@ enum json_parse_result parse_array(struct json **json_out, struct raw_json *raw, goto err; } - json_array_append(json, obj); + json_append(json, obj); skip_ws(raw); obj = NULL; @@ -59,70 +59,88 @@ end: } -bool json_array_append(struct json *dest, struct json *src) { +bool json_append(struct json *dest, struct json *src) { if (!dest || !src) return false; + json_detach(src); add_children(dest, src); return true; } -struct json *json_array_get_mut(struct json *json, size_t index) { - if (json->type != JSON_ARRAY || index > json->children->nitems) - return NULL; +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 json->children->items[index]; + return true; } -const struct json *json_array_get_const(const struct json *array, size_t index) { - return json_array_get_mut((struct json *) array, index); -} +bool json_replace(struct json *dest, size_t index, struct json *src) { + if (!dest || !src) + return false; + json_detach(src); -struct json *json_array_add_object(struct json *dest) { - struct json *obj = json_new_object(); - json_array_append(dest, obj); - return obj; -} + 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); -struct json *json_array_add_array(struct json *dest) { - struct json *array = json_new_array(); - json_array_append(dest, array); - return array; -} -struct json *json_array_add_number(struct json *dest, double num) { - struct json *number = json_new_number(num); - json_array_append(dest, number); - return number; + return true; } -struct json *json_array_add_string(struct json *dest, const char *string) { - struct json *str = json_new_string_copy(string); - json_array_append(dest, str); - return str; -} +struct json *json_at_mut(struct json *json, size_t index) { + if (!json || json->type != JSON_ARRAY || index > json->children->nitems) + return NULL; -struct json *json_array_add_bool(struct json *dest, bool boolean) { - struct json *b = json_new_bool(boolean); - json_array_append(dest, b); - return b; + return json->children->items[index]; } -struct json *json_array_add_null(struct json *dest) { - struct json *n = json_new_null(); - json_array_append(dest, n); - return n; +const struct json *json_at_const(const struct json *array, size_t index) { + return json_at_mut((struct json *) array, index); } -struct json *json_array_detach(struct json *json, size_t index) { - if (json->type != JSON_ARRAY || index >= json->children->nitems) { +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; } -void json_array_delete(struct json *json, size_t index) { - struct json *tmp = json_array_detach(json, index); +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); } -- cgit v1.2.3