Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 440 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2024 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 28 | ||
| 29 | #include "cx/json.h" | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
30 | #include "cx/kv_list.h" |
| 440 | 31 | |
| 32 | #include <string.h> | |
| 33 | #include <assert.h> | |
| 34 | #include <stdio.h> | |
| 35 | #include <inttypes.h> | |
| 845 | 36 | #include <ctype.h> |
| 440 | 37 | |
| 38 | /* | |
| 39 | * RFC 8259 | |
| 40 | * https://tools.ietf.org/html/rfc8259 | |
| 41 | */ | |
| 42 | ||
| 43 | static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING}; | |
| 44 | ||
| 45 | static void token_destroy(CxJsonToken *token) { | |
| 46 | if (token->allocated) { | |
| 47 | cx_strfree(&token->content); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
48 | token->allocated = false; |
| 440 | 49 | } |
| 50 | } | |
| 51 | ||
| 52 | static int num_isexp(const char *content, size_t length, size_t pos) { | |
| 53 | if (pos >= length) { | |
| 54 | return 0; | |
| 55 | } | |
| 56 | ||
| 57 | int ok = 0; | |
| 58 | for (size_t i = pos; i < length; i++) { | |
| 59 | char c = content[i]; | |
| 845 | 60 | if (isdigit((unsigned char)c)) { |
| 440 | 61 | ok = 1; |
| 62 | } else if (i == pos) { | |
| 63 | if (!(c == '+' || c == '-')) { | |
| 64 | return 0; | |
| 65 | } | |
| 66 | } else { | |
| 67 | return 0; | |
| 68 | } | |
| 69 | } | |
| 70 | ||
| 71 | return ok; | |
| 72 | } | |
| 73 | ||
| 74 | static CxJsonTokenType token_numbertype(const char *content, size_t length) { | |
| 75 | if (length == 0) return CX_JSON_TOKEN_ERROR; | |
| 76 | ||
| 845 | 77 | if (content[0] != '-' && !isdigit((unsigned char)content[0])) { |
| 440 | 78 | return CX_JSON_TOKEN_ERROR; |
| 79 | } | |
| 80 | ||
| 81 | CxJsonTokenType type = CX_JSON_TOKEN_INTEGER; | |
| 82 | for (size_t i = 1; i < length; i++) { | |
| 83 | if (content[i] == '.') { | |
| 84 | if (type == CX_JSON_TOKEN_NUMBER) { | |
| 85 | return CX_JSON_TOKEN_ERROR; // more than one decimal separator | |
| 86 | } | |
| 87 | type = CX_JSON_TOKEN_NUMBER; | |
| 88 | } else if (content[i] == 'e' || content[i] == 'E') { | |
| 89 | return num_isexp(content, length, i + 1) ? CX_JSON_TOKEN_NUMBER : CX_JSON_TOKEN_ERROR; | |
| 845 | 90 | } else if (!isdigit((unsigned char)content[i])) { |
| 440 | 91 | return CX_JSON_TOKEN_ERROR; // char is not a digit, decimal separator or exponent sep |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | return type; | |
| 96 | } | |
| 97 | ||
| 98 | static CxJsonToken token_create(CxJson *json, bool isstring, size_t start, size_t end) { | |
| 99 | cxmutstr str = cx_mutstrn(json->buffer.space + start, end - start); | |
| 100 | bool allocated = false; | |
| 101 | if (json->uncompleted.tokentype != CX_JSON_NO_TOKEN) { | |
| 102 | allocated = true; | |
| 103 | str = cx_strcat_m(json->uncompleted.content, 1, str); | |
| 104 | if (str.ptr == NULL) { // LCOV_EXCL_START | |
| 105 | return (CxJsonToken){CX_JSON_NO_TOKEN, false, {NULL, 0}}; | |
| 106 | } // LCOV_EXCL_STOP | |
| 107 | } | |
| 108 | json->uncompleted = (CxJsonToken){0}; | |
| 109 | CxJsonTokenType ttype; | |
| 110 | if (isstring) { | |
| 111 | ttype = CX_JSON_TOKEN_STRING; | |
| 112 | } else { | |
| 113 | cxstring s = cx_strcast(str); | |
| 1016 | 114 | if (!cx_strcmp(s, "true") || !cx_strcmp(s, "false") |
| 115 | || !cx_strcmp(s, "null")) { | |
| 440 | 116 | ttype = CX_JSON_TOKEN_LITERAL; |
| 117 | } else { | |
| 118 | ttype = token_numbertype(str.ptr, str.length); | |
| 119 | } | |
| 120 | } | |
| 121 | if (ttype == CX_JSON_TOKEN_ERROR) { | |
| 122 | if (allocated) { | |
| 123 | cx_strfree(&str); | |
| 124 | } | |
| 125 | return (CxJsonToken){CX_JSON_TOKEN_ERROR, false, {NULL, 0}}; | |
| 126 | } | |
| 127 | return (CxJsonToken){ttype, allocated, str}; | |
| 128 | } | |
| 129 | ||
| 130 | static CxJsonTokenType char2ttype(char c) { | |
| 131 | switch (c) { | |
| 132 | case '[': { | |
| 133 | return CX_JSON_TOKEN_BEGIN_ARRAY; | |
| 134 | } | |
| 135 | case '{': { | |
| 136 | return CX_JSON_TOKEN_BEGIN_OBJECT; | |
| 137 | } | |
| 138 | case ']': { | |
| 139 | return CX_JSON_TOKEN_END_ARRAY; | |
| 140 | } | |
| 141 | case '}': { | |
| 142 | return CX_JSON_TOKEN_END_OBJECT; | |
| 143 | } | |
| 144 | case ':': { | |
| 145 | return CX_JSON_TOKEN_NAME_SEPARATOR; | |
| 146 | } | |
| 147 | case ',': { | |
| 148 | return CX_JSON_TOKEN_VALUE_SEPARATOR; | |
| 149 | } | |
| 150 | case '"': { | |
| 151 | return CX_JSON_TOKEN_STRING; | |
| 152 | } | |
| 153 | default: { | |
| 845 | 154 | if (isspace((unsigned char)c)) { |
| 440 | 155 | return CX_JSON_TOKEN_SPACE; |
| 156 | } | |
| 157 | } | |
| 158 | } | |
| 159 | return CX_JSON_NO_TOKEN; | |
| 160 | } | |
| 161 | ||
| 162 | static enum cx_json_status token_parse_next(CxJson *json, CxJsonToken *result) { | |
| 163 | // check if there is data in the buffer | |
| 164 | if (cxBufferEof(&json->buffer)) { | |
| 165 | return json->uncompleted.tokentype == CX_JSON_NO_TOKEN ? | |
| 166 | CX_JSON_NO_DATA : CX_JSON_INCOMPLETE_DATA; | |
| 167 | } | |
| 168 | ||
| 169 | // current token type and start index | |
| 170 | CxJsonTokenType ttype = json->uncompleted.tokentype; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
171 | size_t token_part_start = json->buffer.pos; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
172 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
173 | bool escape_end_of_string = ttype == CX_JSON_TOKEN_STRING |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
174 | && json->uncompleted.content.ptr[json->uncompleted.content.length-1] == '\\'; |
| 440 | 175 | |
| 176 | for (size_t i = json->buffer.pos; i < json->buffer.size; i++) { | |
| 177 | char c = json->buffer.space[i]; | |
| 178 | if (ttype != CX_JSON_TOKEN_STRING) { | |
| 179 | // currently non-string token | |
| 180 | CxJsonTokenType ctype = char2ttype(c); // start of new token? | |
| 181 | if (ttype == CX_JSON_NO_TOKEN) { | |
| 182 | if (ctype == CX_JSON_TOKEN_SPACE) { | |
| 183 | json->buffer.pos++; | |
| 184 | continue; | |
| 185 | } else if (ctype == CX_JSON_TOKEN_STRING) { | |
| 186 | // begin string | |
| 187 | ttype = CX_JSON_TOKEN_STRING; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
188 | token_part_start = i; |
| 440 | 189 | } else if (ctype != CX_JSON_NO_TOKEN) { |
| 190 | // single-char token | |
| 191 | json->buffer.pos = i + 1; | |
| 192 | *result = (CxJsonToken){ctype, false, {NULL, 0}}; | |
| 193 | return CX_JSON_NO_ERROR; | |
| 194 | } else { | |
| 195 | ttype = CX_JSON_TOKEN_LITERAL; // number or literal | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
196 | token_part_start = i; |
| 440 | 197 | } |
| 198 | } else { | |
| 199 | // finish token | |
| 200 | if (ctype != CX_JSON_NO_TOKEN) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
201 | *result = token_create(json, false, token_part_start, i); |
| 440 | 202 | if (result->tokentype == CX_JSON_NO_TOKEN) { |
| 203 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 204 | } | |
| 205 | if (result->tokentype == CX_JSON_TOKEN_ERROR) { | |
| 206 | return CX_JSON_FORMAT_ERROR_NUMBER; | |
| 207 | } | |
| 208 | json->buffer.pos = i; | |
| 209 | return CX_JSON_NO_ERROR; | |
| 210 | } | |
| 211 | } | |
| 212 | } else { | |
| 213 | // currently inside a string | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
214 | if (escape_end_of_string) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
215 | escape_end_of_string = false; |
| 440 | 216 | } else { |
| 217 | if (c == '"') { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
218 | *result = token_create(json, true, token_part_start, i + 1); |
| 440 | 219 | if (result->tokentype == CX_JSON_NO_TOKEN) { |
| 220 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 221 | } | |
| 222 | json->buffer.pos = i + 1; | |
| 223 | return CX_JSON_NO_ERROR; | |
| 224 | } else if (c == '\\') { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
225 | escape_end_of_string = true; |
| 440 | 226 | } |
| 227 | } | |
| 228 | } | |
| 229 | } | |
| 230 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
231 | if (ttype == CX_JSON_NO_TOKEN) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
232 | return CX_JSON_NO_DATA; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
233 | } else { |
| 440 | 234 | // uncompleted token |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
235 | size_t uncompleted_len = json->buffer.size - token_part_start; |
| 440 | 236 | if (json->uncompleted.tokentype == CX_JSON_NO_TOKEN) { |
| 237 | // current token is uncompleted | |
| 238 | // save current token content | |
| 239 | CxJsonToken uncompleted = { | |
| 240 | ttype, true, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
241 | cx_strdup(cx_strn(json->buffer.space + token_part_start, uncompleted_len)) |
| 440 | 242 | }; |
| 243 | if (uncompleted.content.ptr == NULL) { | |
| 244 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 245 | } | |
| 246 | json->uncompleted = uncompleted; | |
| 247 | } else { | |
| 248 | // previously we also had an uncompleted token | |
| 249 | // combine the uncompleted token with the current token | |
| 250 | assert(json->uncompleted.allocated); | |
| 251 | cxmutstr str = cx_strcat_m(json->uncompleted.content, 1, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
252 | cx_strn(json->buffer.space + token_part_start, uncompleted_len)); |
| 440 | 253 | if (str.ptr == NULL) { |
| 254 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 255 | } | |
| 256 | json->uncompleted.content = str; | |
| 257 | } | |
| 258 | // advance the buffer position - we saved the stuff in the uncompleted token | |
| 259 | json->buffer.pos += uncompleted_len; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
260 | return CX_JSON_INCOMPLETE_DATA; |
| 440 | 261 | } |
| 262 | } | |
| 263 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
264 | // converts a Unicode codepoint to utf8 |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
265 | static unsigned codepoint_to_utf8(uint32_t codepoint, char *output_buf) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
266 | if (codepoint <= 0x7F) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
267 | *output_buf = (char)codepoint; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
268 | return 1; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
269 | } else if (codepoint <= 0x7FF) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
270 | output_buf[0] = (char)(0xC0 | ((codepoint >> 6) & 0x1F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
271 | output_buf[1] = (char)(0x80 | (codepoint & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
272 | return 2; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
273 | } else if (codepoint <= 0xFFFF) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
274 | output_buf[0] = (char)(0xE0 | ((codepoint >> 12) & 0x0F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
275 | output_buf[1] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
276 | output_buf[2] = (char)(0x80 | (codepoint & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
277 | return 3; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
278 | } else if (codepoint <= 0x10FFFF) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | output_buf[0] = (char)(0xF0 | ((codepoint >> 18) & 0x07)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
280 | output_buf[1] = (char)(0x80 | ((codepoint >> 12) & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
281 | output_buf[2] = (char)(0x80 | ((codepoint >> 6) & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
282 | output_buf[3] = (char)(0x80 | (codepoint & 0x3F)); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
283 | return 4; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
284 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
285 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
286 | return 0; // LCOV_EXCL_LINE |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
287 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
288 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
289 | // converts a utf16 surrogate pair to utf8 |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
290 | static inline uint32_t utf16pair_to_codepoint(uint16_t c0, uint16_t c1) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
291 | return ((c0 - 0xD800) << 10) + (c1 - 0xDC00) + 0x10000; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
292 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
293 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
294 | static unsigned unescape_unicode_string(cxstring str, char *utf8buf) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
295 | // str is supposed to start with "\uXXXX" or "\uXXXX\uXXXX" |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
296 | // remaining bytes in the string are ignored (str may be larger!) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
297 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
298 | if (str.length < 6 || str.ptr[0] != '\\' || str.ptr[1] != 'u') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
299 | return 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
300 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
301 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
302 | unsigned utf8len = 0; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
303 | cxstring ustr1 = { str.ptr + 2, 4}; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
304 | uint16_t utf16a, utf16b; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
305 | if (!cx_strtou16_lc(ustr1, &utf16a, 16, "")) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
306 | uint32_t codepoint; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
307 | if (utf16a < 0xD800 || utf16a > 0xE000) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
308 | // character is in the Basic Multilingual Plane |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
309 | // and encoded as a single utf16 char |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
310 | codepoint = utf16a; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
311 | utf8len = codepoint_to_utf8(codepoint, utf8buf); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
312 | } else if (utf16a >= 0xD800 && utf16a <= 0xDBFF) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
313 | // character is encoded as a surrogate pair |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
314 | // get next 6 bytes |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
315 | if (str.length >= 12) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
316 | if (str.ptr[6] == '\\' && str.ptr[7] == 'u') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
317 | cxstring ustr2 = { str.ptr+8, 4 }; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
318 | if (!cx_strtou16_lc(ustr2, &utf16b, 16, "") |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
319 | && utf16b >= 0xDC00 && utf16b <= 0xDFFF) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
320 | codepoint = utf16pair_to_codepoint(utf16a, utf16b); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
321 | utf8len = codepoint_to_utf8(codepoint, utf8buf); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
322 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
323 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
324 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
325 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
326 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
327 | return utf8len; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
328 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
329 | |
| 440 | 330 | static cxmutstr unescape_string(const CxAllocator *a, cxmutstr str) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
331 | // note: this function expects that str contains the enclosing quotes! |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
332 | |
| 440 | 333 | cxmutstr result; |
| 334 | result.length = 0; | |
| 335 | result.ptr = cxMalloc(a, str.length - 1); | |
| 336 | if (result.ptr == NULL) return result; // LCOV_EXCL_LINE | |
| 337 | ||
| 338 | bool u = false; | |
| 339 | for (size_t i = 1; i < str.length - 1; i++) { | |
| 340 | char c = str.ptr[i]; | |
| 341 | if (u) { | |
| 342 | u = false; | |
| 343 | if (c == 'n') { | |
| 344 | c = '\n'; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
345 | } else if (c == '"') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
346 | c = '"'; |
| 440 | 347 | } else if (c == 't') { |
| 348 | c = '\t'; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
349 | } else if (c == 'r') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
350 | c = '\r'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
351 | } else if (c == '\\') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
352 | c = '\\'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
353 | } else if (c == '/') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
354 | c = '/'; // always unescape, we don't need settings here |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
355 | } else if (c == 'f') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
356 | c = '\f'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
357 | } else if (c == 'b') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
358 | c = '\b'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
359 | } else if (c == 'u') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
360 | char utf8buf[4]; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
361 | unsigned utf8len = unescape_unicode_string( |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
362 | cx_strn(str.ptr + i - 1, str.length - i), |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
363 | utf8buf |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
364 | ); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
365 | if(utf8len > 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
366 | i += utf8len < 4 ? 4 : 10; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
367 | // add all bytes from utf8buf except the last char |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
368 | // to the result (last char will be added below) |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
369 | utf8len--; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
370 | c = utf8buf[utf8len]; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
371 | for (unsigned x = 0; x < utf8len; x++) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
372 | result.ptr[result.length++] = utf8buf[x]; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
373 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
374 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
375 | // decoding failed, ignore the entire sequence |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
376 | result.ptr[result.length++] = '\\'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
377 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
378 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
379 | // TODO: discuss the behavior for unrecognized escape sequences |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
380 | // most parsers throw an error here - we just ignore it |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
381 | result.ptr[result.length++] = '\\'; // LCOV_EXCL_LINE |
| 440 | 382 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
383 | |
| 440 | 384 | result.ptr[result.length++] = c; |
| 385 | } else { | |
| 386 | if (c == '\\') { | |
| 387 | u = true; | |
| 388 | } else { | |
| 389 | result.ptr[result.length++] = c; | |
| 390 | } | |
| 391 | } | |
| 392 | } | |
| 393 | result.ptr[result.length] = 0; | |
| 394 | ||
| 395 | return result; | |
| 396 | } | |
| 397 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
398 | static cxmutstr escape_string(cxstring str, bool escape_slash) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
399 | // note: this function produces the string without enclosing quotes |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
400 | // the reason is that we don't want to allocate memory just for that |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
401 | CxBuffer buf = {0}; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
402 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
403 | bool all_printable = true; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
404 | for (size_t i = 0; i < str.length; i++) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
405 | unsigned char c = str.ptr[i]; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
406 | bool escape = c < 0x20 || c == '\\' || c == '"' |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
407 | || (escape_slash && c == '/'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
408 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
409 | if (all_printable && escape) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
410 | size_t capa = str.length + 32; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
411 | char *space = cxMallocDefault(capa); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
412 | if (space == NULL) return cx_mutstrn(NULL, 0); |
| 1016 | 413 | cxBufferInit(&buf, NULL, space, capa, CX_BUFFER_AUTO_EXTEND); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
414 | cxBufferWrite(str.ptr, 1, i, &buf); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
415 | all_printable = false; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
416 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
417 | if (escape) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
418 | cxBufferPut(&buf, '\\'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
419 | if (c == '\"') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
420 | cxBufferPut(&buf, '\"'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
421 | } else if (c == '\n') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
422 | cxBufferPut(&buf, 'n'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
423 | } else if (c == '\t') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
424 | cxBufferPut(&buf, 't'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
425 | } else if (c == '\r') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
426 | cxBufferPut(&buf, 'r'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
427 | } else if (c == '\\') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
428 | cxBufferPut(&buf, '\\'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
429 | } else if (c == '/') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
430 | cxBufferPut(&buf, '/'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
431 | } else if (c == '\f') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
432 | cxBufferPut(&buf, 'f'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
433 | } else if (c == '\b') { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
434 | cxBufferPut(&buf, 'b'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
435 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
436 | char code[6]; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
437 | snprintf(code, sizeof(code), "u%04x", (unsigned int) c); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
438 | cxBufferPutString(&buf, code); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
439 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
440 | } else if (!all_printable) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
441 | cxBufferPut(&buf, c); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
442 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
443 | } |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
444 | cxmutstr ret; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
445 | if (all_printable) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
446 | // don't copy the string when we don't need to escape anything |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
447 | ret = cx_mutstrn((char*)str.ptr, str.length); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
448 | } else { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
449 | ret = cx_mutstrn(buf.space, buf.size); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
450 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
451 | cxBufferDestroy(&buf); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
452 | return ret; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
453 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
454 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
455 | static CxJsonObject json_create_object_map(const CxAllocator *allocator) { |
| 1016 | 456 | CxMap *map = cxKvListCreateAsMap(allocator, CX_STORE_POINTERS); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
457 | if (map == NULL) return NULL; // LCOV_EXCL_LINE |
| 1016 | 458 | cxSetCompareFunc(map, cxJsonCompare); |
| 459 | cxSetDestructor(map, cxJsonValueFree); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
460 | return map; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
461 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
462 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
463 | static void json_free_object_map(CxJsonObject obj) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
464 | cxMapFree(obj); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
465 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
466 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
467 | static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) { |
| 440 | 468 | CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue)); |
| 469 | if (v == NULL) return NULL; // LCOV_EXCL_LINE | |
| 470 | ||
| 471 | // initialize the value | |
| 472 | v->type = type; | |
| 473 | v->allocator = json->allocator; | |
| 474 | if (type == CX_JSON_ARRAY) { | |
| 1016 | 475 | if (cx_array_init_a(json->allocator, v->array, 16)) { |
| 476 | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 477 | } | |
| 440 | 478 | } else if (type == CX_JSON_OBJECT) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
479 | v->object = json_create_object_map(json->allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
480 | if (v->object == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 440 | 481 | } |
| 482 | ||
| 483 | // add the new value to a possible parent | |
| 1016 | 484 | if (json->vbuf.size > 0) { |
| 485 | CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1]; | |
| 440 | 486 | assert(parent != NULL); |
| 487 | if (parent->type == CX_JSON_ARRAY) { | |
| 1016 | 488 | if (cx_array_add_a(json->allocator, parent->array, &v)) { |
| 440 | 489 | goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 490 | } | |
| 491 | } else if (parent->type == CX_JSON_OBJECT) { | |
| 492 | // the member was already created after parsing the name | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
493 | // store the pointer of the uncompleted value in the map |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
494 | assert(json->uncompleted_member_name.ptr != NULL); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
495 | if (cxMapPut(parent->object, json->uncompleted_member_name, v)) { |
| 440 | 496 | goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 497 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
498 | cx_strfree_a(json->allocator, &json->uncompleted_member_name); |
| 440 | 499 | } else { |
| 500 | assert(false); // LCOV_EXCL_LINE | |
| 501 | } | |
| 502 | } | |
| 503 | ||
| 504 | // add the new value to the stack, if it is an array or object | |
| 505 | if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) { | |
| 1016 | 506 | if (json->vbuf.size >= json->vbuf.capacity) { |
| 507 | int alloc_error; | |
| 508 | if (json->vbuf.data == json->vbuf_internal) { | |
| 509 | alloc_error = cx_array_copy_to_new(json->vbuf, json->vbuf.size+1); | |
| 510 | } else { | |
| 511 | alloc_error = cx_array_reserve(json->vbuf, json->vbuf.size+1); | |
| 512 | } | |
| 513 | if (alloc_error) { | |
| 514 | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 515 | } | |
| 440 | 516 | } |
| 1016 | 517 | json->vbuf.data[json->vbuf.size] = v; |
| 518 | json->vbuf.size++; | |
| 440 | 519 | } |
| 520 | ||
| 521 | // if currently no value is parsed, this is now the value of interest | |
| 522 | if (json->parsed == NULL) { | |
| 523 | json->parsed = v; | |
| 524 | } | |
| 525 | ||
| 526 | return v; | |
| 527 | // LCOV_EXCL_START | |
| 528 | create_json_value_exit_error: | |
| 529 | cxJsonValueFree(v); | |
| 530 | return NULL; | |
| 531 | // LCOV_EXCL_STOP | |
| 532 | } | |
| 533 | ||
| 534 | #define JP_STATE_VALUE_BEGIN 0 | |
| 535 | #define JP_STATE_VALUE_END 10 | |
| 536 | #define JP_STATE_VALUE_BEGIN_OBJ 1 | |
| 537 | #define JP_STATE_OBJ_SEP_OR_CLOSE 11 | |
| 538 | #define JP_STATE_VALUE_BEGIN_AR 2 | |
| 539 | #define JP_STATE_ARRAY_SEP_OR_CLOSE 12 | |
| 540 | #define JP_STATE_OBJ_NAME_OR_CLOSE 5 | |
| 541 | #define JP_STATE_OBJ_NAME 6 | |
| 542 | #define JP_STATE_OBJ_COLON 7 | |
| 543 | ||
| 544 | void cxJsonInit(CxJson *json, const CxAllocator *allocator) { | |
| 545 | if (allocator == NULL) { | |
| 546 | allocator = cxDefaultAllocator; | |
| 547 | } | |
| 548 | ||
| 549 | memset(json, 0, sizeof(CxJson)); | |
| 550 | json->allocator = allocator; | |
| 551 | ||
| 1016 | 552 | cx_array_init_fixed(json->states, json->states_internal, 1); |
| 553 | json->states.data[0] = JP_STATE_VALUE_BEGIN; | |
| 554 | cx_array_init_fixed(json->vbuf, json->vbuf_internal, 0); | |
| 440 | 555 | } |
| 556 | ||
| 557 | void cxJsonDestroy(CxJson *json) { | |
| 558 | cxBufferDestroy(&json->buffer); | |
| 1016 | 559 | if (json->states.data != json->states_internal) { |
| 560 | cx_array_free(json->states); | |
| 440 | 561 | } |
| 1016 | 562 | if (json->vbuf.data != json->vbuf_internal) { |
| 563 | cx_array_free(json->vbuf); | |
| 440 | 564 | } |
| 565 | cxJsonValueFree(json->parsed); | |
| 566 | json->parsed = NULL; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
567 | token_destroy(&json->uncompleted); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
568 | cx_strfree_a(json->allocator, &json->uncompleted_member_name); |
| 440 | 569 | } |
| 570 | ||
| 870 | 571 | void cxJsonReset(CxJson *json) { |
| 572 | const CxAllocator *allocator = json->allocator; | |
| 573 | cxJsonDestroy(json); | |
| 574 | cxJsonInit(json, allocator); | |
| 575 | } | |
| 576 | ||
| 440 | 577 | int cxJsonFilln(CxJson *json, const char *buf, size_t size) { |
| 578 | if (cxBufferEof(&json->buffer)) { | |
| 579 | // reinitialize the buffer | |
| 580 | cxBufferDestroy(&json->buffer); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
581 | if (buf == NULL) buf = ""; // buffer must not be initialized with NULL |
| 1016 | 582 | cxBufferInit(&json->buffer, NULL, (char*) buf, |
| 583 | size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); | |
| 440 | 584 | json->buffer.size = size; |
| 585 | return 0; | |
| 586 | } else { | |
| 587 | return size != cxBufferAppend(buf, 1, size, &json->buffer); | |
| 588 | } | |
| 589 | } | |
| 590 | ||
| 591 | static void json_add_state(CxJson *json, int state) { | |
| 1016 | 592 | // we have guaranteed the necessary space |
| 440 | 593 | // therefore, we can safely add the state in the simplest way possible |
| 1016 | 594 | json->states.data[json->states.size++] = state; |
| 440 | 595 | } |
| 596 | ||
| 597 | #define return_rec(code) \ | |
| 598 | token_destroy(&token); \ | |
| 599 | return code | |
| 600 | ||
| 601 | static enum cx_json_status json_parse(CxJson *json) { | |
| 602 | // Reserve a pointer for a possibly read value | |
| 603 | CxJsonValue *vbuf = NULL; | |
| 604 | ||
| 605 | // grab the next token | |
| 606 | CxJsonToken token; | |
| 607 | { | |
| 608 | enum cx_json_status ret = token_parse_next(json, &token); | |
| 609 | if (ret != CX_JSON_NO_ERROR) { | |
| 610 | return ret; | |
| 611 | } | |
| 612 | } | |
| 613 | ||
| 614 | // pop the current state | |
| 1016 | 615 | assert(json->states.size > 0); |
| 616 | int state = json->states.data[--json->states.size]; | |
| 440 | 617 | |
| 1016 | 618 | // guarantee that at least two more states fit into the array |
| 619 | const size_t required_states_depth = json->states.size + 2; | |
| 620 | if (required_states_depth >= json->states.capacity) { | |
| 621 | int alloc_error; | |
| 622 | if (json->states.data == json->states_internal) { | |
| 623 | alloc_error = cx_array_copy_to_new(json->states, required_states_depth); | |
| 624 | } else { | |
| 625 | alloc_error = cx_array_reserve(json->states, required_states_depth); | |
| 626 | } | |
| 627 | if (alloc_error) { | |
| 628 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 629 | } | |
| 440 | 630 | } |
| 631 | ||
| 632 | ||
| 633 | // 0 JP_STATE_VALUE_BEGIN value begin | |
| 634 | // 10 JP_STATE_VALUE_END expect value end | |
| 635 | // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object) | |
| 636 | // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose | |
| 637 | // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array) | |
| 638 | // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose | |
| 639 | // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose | |
| 640 | // 6 JP_STATE_OBJ_NAME object, expect name | |
| 641 | // 7 JP_STATE_OBJ_COLON object, expect ':' | |
| 642 | ||
| 643 | if (state < 3) { | |
| 644 | // push expected end state to the stack | |
| 645 | json_add_state(json, 10 + state); | |
| 646 | switch (token.tokentype) { | |
| 647 | case CX_JSON_TOKEN_BEGIN_ARRAY: { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
648 | if (json_create_value(json, CX_JSON_ARRAY) == NULL) { |
| 440 | 649 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 650 | } | |
| 651 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 652 | return_rec(CX_JSON_NO_ERROR); | |
| 653 | } | |
| 654 | case CX_JSON_TOKEN_BEGIN_OBJECT: { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
655 | if (json_create_value(json, CX_JSON_OBJECT) == NULL) { |
| 440 | 656 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 657 | } | |
| 658 | json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE); | |
| 659 | return_rec(CX_JSON_NO_ERROR); | |
| 660 | } | |
| 1016 | 661 | case CX_JSON_TOKEN_END_ARRAY: { |
| 662 | if (state == JP_STATE_VALUE_BEGIN_AR) { | |
| 663 | // discard the array from the value buffer | |
| 664 | json->vbuf.size--; | |
| 665 | json->states.size--; | |
| 666 | return_rec(CX_JSON_NO_ERROR); | |
| 667 | } else { | |
| 668 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 669 | } | |
| 670 | } | |
| 440 | 671 | case CX_JSON_TOKEN_STRING: { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
672 | if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) { |
| 440 | 673 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 674 | } | |
| 675 | cxmutstr str = unescape_string(json->allocator, token.content); | |
| 676 | if (str.ptr == NULL) { | |
| 677 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 678 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
679 | vbuf->string = str; |
| 440 | 680 | return_rec(CX_JSON_NO_ERROR); |
| 681 | } | |
| 682 | case CX_JSON_TOKEN_INTEGER: | |
| 683 | case CX_JSON_TOKEN_NUMBER: { | |
| 684 | int type = token.tokentype == CX_JSON_TOKEN_INTEGER ? CX_JSON_INTEGER : CX_JSON_NUMBER; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
685 | if (NULL == (vbuf = json_create_value(json, type))) { |
| 440 | 686 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 687 | } | |
| 688 | if (type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
689 | if (cx_strtoi64(token.content, &vbuf->integer, 10)) { |
| 440 | 690 | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); |
| 691 | } | |
| 692 | } else { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
693 | if (cx_strtod(token.content, &vbuf->number)) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
694 | // TODO: at the moment this is unreachable, because the tokenizer is already stricter than cx_strtod() |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
695 | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); // LCOV_EXCL_LINE |
| 440 | 696 | } |
| 697 | } | |
| 698 | return_rec(CX_JSON_NO_ERROR); | |
| 699 | } | |
| 700 | case CX_JSON_TOKEN_LITERAL: { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
701 | if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) { |
| 440 | 702 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 703 | } | |
| 1016 | 704 | if (0 == cx_strcmp(token.content, "true")) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
705 | vbuf->literal = CX_JSON_TRUE; |
| 1016 | 706 | } else if (0 == cx_strcmp(token.content, "false")) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
707 | vbuf->literal = CX_JSON_FALSE; |
| 440 | 708 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
709 | vbuf->literal = CX_JSON_NULL; |
| 440 | 710 | } |
| 711 | return_rec(CX_JSON_NO_ERROR); | |
| 712 | } | |
| 713 | default: { | |
| 714 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 715 | } | |
| 716 | } | |
| 717 | } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) { | |
| 718 | // expect ',' or ']' | |
| 719 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { | |
| 720 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 721 | return_rec(CX_JSON_NO_ERROR); | |
| 722 | } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { | |
| 723 | // discard the array from the value buffer | |
| 1016 | 724 | json->vbuf.size--; |
| 440 | 725 | return_rec(CX_JSON_NO_ERROR); |
| 726 | } else { | |
| 727 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 728 | } | |
| 729 | } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { | |
| 730 | if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { | |
| 731 | // discard the obj from the value buffer | |
| 1016 | 732 | json->vbuf.size--; |
| 440 | 733 | return_rec(CX_JSON_NO_ERROR); |
| 734 | } else { | |
| 735 | // expect string | |
| 736 | if (token.tokentype != CX_JSON_TOKEN_STRING) { | |
| 737 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 738 | } | |
| 739 | ||
| 740 | // add new entry | |
| 741 | cxmutstr name = unescape_string(json->allocator, token.content); | |
| 742 | if (name.ptr == NULL) { | |
| 743 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 744 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
745 | assert(json->uncompleted_member_name.ptr == NULL); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
746 | json->uncompleted_member_name = name; |
| 1016 | 747 | assert(json->vbuf.size > 0); |
| 440 | 748 | |
| 749 | // next state | |
| 750 | json_add_state(json, JP_STATE_OBJ_COLON); | |
| 751 | return_rec(CX_JSON_NO_ERROR); | |
| 752 | } | |
| 753 | } else if (state == JP_STATE_OBJ_COLON) { | |
| 754 | // expect ':' | |
| 755 | if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) { | |
| 756 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 757 | } | |
| 758 | // next state | |
| 759 | json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ); | |
| 760 | return_rec(CX_JSON_NO_ERROR); | |
| 761 | } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) { | |
| 762 | // expect ',' or '}' | |
| 763 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { | |
| 764 | json_add_state(json, JP_STATE_OBJ_NAME); | |
| 765 | return_rec(CX_JSON_NO_ERROR); | |
| 766 | } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { | |
| 767 | // discard the obj from the value buffer | |
| 1016 | 768 | json->vbuf.size--; |
| 440 | 769 | return_rec(CX_JSON_NO_ERROR); |
| 770 | } else { | |
| 771 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 772 | } | |
| 773 | } else { | |
| 774 | // should be unreachable | |
| 775 | assert(false); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
776 | return_rec(-1); // LCOV_EXCL_LINE |
| 440 | 777 | } |
| 778 | } | |
| 779 | ||
| 780 | CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) { | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
781 | // initialize output value |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
782 | *value = &cx_json_value_nothing; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
783 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
784 | // check if the buffer has been filled |
| 440 | 785 | if (json->buffer.space == NULL) { |
| 786 | return CX_JSON_NULL_DATA; | |
| 787 | } | |
| 788 | ||
| 789 | // parse data | |
| 790 | CxJsonStatus result; | |
| 791 | do { | |
| 792 | result = json_parse(json); | |
| 1016 | 793 | if (result == CX_JSON_NO_ERROR && json->states.size == 1) { |
| 440 | 794 | // final state reached |
| 1016 | 795 | assert(json->states.data[0] == JP_STATE_VALUE_END); |
| 796 | assert(json->vbuf.size == 0); | |
| 440 | 797 | |
| 798 | // write output value | |
| 799 | *value = json->parsed; | |
| 800 | json->parsed = NULL; | |
| 801 | ||
| 802 | // re-initialize state machine | |
| 1016 | 803 | json->states.data[0] = JP_STATE_VALUE_BEGIN; |
| 440 | 804 | |
| 805 | return CX_JSON_NO_ERROR; | |
| 806 | } | |
| 807 | } while (result == CX_JSON_NO_ERROR); | |
| 808 | ||
| 809 | // the parser might think there is no data | |
| 810 | // but when we did not reach the final state, | |
| 811 | // we know that there must be more to come | |
| 1016 | 812 | if (result == CX_JSON_NO_DATA && json->states.size > 1) { |
| 440 | 813 | return CX_JSON_INCOMPLETE_DATA; |
| 814 | } | |
| 815 | ||
| 816 | return result; | |
| 817 | } | |
| 818 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
819 | CxJsonStatus cx_json_from_string(const CxAllocator *allocator, |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
820 | cxstring str, CxJsonValue **value) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
821 | *value = &cx_json_value_nothing; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
822 | CxJson parser; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
823 | cxJsonInit(&parser, allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
824 | if (cxJsonFill(&parser, str)) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
825 | // LCOV_EXCL_START |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
826 | cxJsonDestroy(&parser); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
827 | return CX_JSON_BUFFER_ALLOC_FAILED; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
828 | // LCOV_EXCL_STOP |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
829 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
830 | CxJsonStatus status = cxJsonNext(&parser, value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
831 | // check if we consume the total string |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
832 | CxJsonValue *chk_value = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
833 | CxJsonStatus chk_status = CX_JSON_NO_DATA; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
834 | if (status == CX_JSON_NO_ERROR) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
835 | chk_status = cxJsonNext(&parser, &chk_value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
836 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
837 | cxJsonDestroy(&parser); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
838 | if (chk_status == CX_JSON_NO_DATA) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
839 | return status; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
840 | } else { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
841 | cxJsonValueFree(*value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
842 | // if chk_value is nothing, the free is harmless |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
843 | cxJsonValueFree(chk_value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
844 | *value = &cx_json_value_nothing; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
845 | return CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
846 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
847 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
848 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
849 | |
| 440 | 850 | void cxJsonValueFree(CxJsonValue *value) { |
| 851 | if (value == NULL || value->type == CX_JSON_NOTHING) return; | |
| 852 | switch (value->type) { | |
| 853 | case CX_JSON_OBJECT: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
854 | json_free_object_map(value->object); |
| 440 | 855 | break; |
| 856 | } | |
| 857 | case CX_JSON_ARRAY: { | |
| 1016 | 858 | for (size_t i = 0; i < value->array.size; i++) { |
| 859 | cxJsonValueFree(value->array.data[i]); | |
| 440 | 860 | } |
| 1016 | 861 | cx_array_free_a(value->allocator, value->array); |
| 440 | 862 | break; |
| 863 | } | |
| 864 | case CX_JSON_STRING: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
865 | cxFree(value->allocator, value->string.ptr); |
| 440 | 866 | break; |
| 867 | } | |
| 868 | default: { | |
| 869 | break; | |
| 870 | } | |
| 871 | } | |
| 872 | cxFree(value->allocator, value); | |
| 873 | } | |
| 874 | ||
| 875 | CxJsonValue* cxJsonCreateObj(const CxAllocator* allocator) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
876 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 877 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 878 | if (v == NULL) return NULL; | |
| 879 | v->allocator = allocator; | |
| 880 | v->type = CX_JSON_OBJECT; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
881 | v->object = json_create_object_map(allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
882 | if (v->object == NULL) { // LCOV_EXCL_START |
| 440 | 883 | cxFree(allocator, v); |
| 884 | return NULL; | |
| 885 | // LCOV_EXCL_STOP | |
| 886 | } | |
| 887 | return v; | |
| 888 | } | |
| 889 | ||
| 1016 | 890 | CxJsonValue* cxJsonCreateArr(const CxAllocator* allocator, size_t capacity) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
891 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 892 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 893 | if (v == NULL) return NULL; | |
| 894 | v->allocator = allocator; | |
| 895 | v->type = CX_JSON_ARRAY; | |
| 1016 | 896 | if (capacity > 0) { |
| 897 | if (cx_array_init_a(allocator, v->array, capacity)) { | |
| 898 | // LCOV_EXCL_START | |
| 899 | cxFree(allocator, v); | |
| 900 | return NULL; | |
| 901 | // LCOV_EXCL_STOP | |
| 902 | } | |
| 903 | } else { | |
| 904 | v->array.data = NULL; | |
| 905 | v->array.size = v->array.capacity = 0; | |
| 906 | } | |
| 440 | 907 | return v; |
| 908 | } | |
| 909 | ||
| 910 | CxJsonValue* cxJsonCreateNumber(const CxAllocator* allocator, double num) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
911 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 912 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 913 | if (v == NULL) return NULL; | |
| 914 | v->allocator = allocator; | |
| 915 | v->type = CX_JSON_NUMBER; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
916 | v->number = num; |
| 440 | 917 | return v; |
| 918 | } | |
| 919 | ||
| 920 | CxJsonValue* cxJsonCreateInteger(const CxAllocator* allocator, int64_t num) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
921 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 922 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 923 | if (v == NULL) return NULL; | |
| 924 | v->allocator = allocator; | |
| 925 | v->type = CX_JSON_INTEGER; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
926 | v->integer = num; |
| 440 | 927 | return v; |
| 928 | } | |
| 929 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
930 | CxJsonValue* cx_json_create_string(const CxAllocator* allocator, cxstring str) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
931 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 932 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 933 | if (v == NULL) return NULL; | |
| 934 | v->allocator = allocator; | |
| 935 | v->type = CX_JSON_STRING; | |
| 936 | cxmutstr s = cx_strdup_a(allocator, str); | |
| 937 | if (s.ptr == NULL) { cxFree(allocator, v); return NULL; } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
938 | v->string = s; |
| 440 | 939 | return v; |
| 940 | } | |
| 941 | ||
| 942 | CxJsonValue* cxJsonCreateLiteral(const CxAllocator* allocator, CxJsonLiteral lit) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
943 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 944 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 945 | if (v == NULL) return NULL; | |
| 946 | v->allocator = allocator; | |
| 947 | v->type = CX_JSON_LITERAL; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
948 | v->literal = lit; |
| 440 | 949 | return v; |
| 950 | } | |
| 951 | ||
| 952 | // LCOV_EXCL_START | |
| 953 | // never called as long as malloc() does not return NULL | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
954 | static void json_arr_free_temp(CxJsonValue** values, size_t count) { |
| 440 | 955 | for (size_t i = 0; i < count; i++) { |
| 956 | if (values[i] == NULL) break; | |
| 957 | cxJsonValueFree(values[i]); | |
| 958 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
959 | cxFreeDefault(values); |
| 440 | 960 | } |
| 961 | // LCOV_EXCL_STOP | |
| 962 | ||
| 963 | int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
964 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 965 | if (values == NULL) return -1; |
| 966 | for (size_t i = 0; i < count; i++) { | |
| 967 | values[i] = cxJsonCreateNumber(arr->allocator, num[i]); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
968 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 969 | } |
| 970 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
971 | cxFreeDefault(values); |
| 440 | 972 | return ret; |
| 973 | } | |
| 974 | ||
| 975 | int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
976 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 977 | if (values == NULL) return -1; |
| 978 | for (size_t i = 0; i < count; i++) { | |
| 979 | values[i] = cxJsonCreateInteger(arr->allocator, num[i]); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
980 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 981 | } |
| 982 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
983 | cxFreeDefault(values); |
| 440 | 984 | return ret; |
| 985 | } | |
| 986 | ||
| 987 | int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
988 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 989 | if (values == NULL) return -1; |
| 990 | for (size_t i = 0; i < count; i++) { | |
| 991 | values[i] = cxJsonCreateString(arr->allocator, str[i]); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
992 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 993 | } |
| 994 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
995 | cxFreeDefault(values); |
| 440 | 996 | return ret; |
| 997 | } | |
| 998 | ||
| 999 | int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1000 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 1001 | if (values == NULL) return -1; |
| 1002 | for (size_t i = 0; i < count; i++) { | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1003 | values[i] = cxJsonCreateString(arr->allocator, str[i]); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1004 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 1005 | } |
| 1006 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1007 | cxFreeDefault(values); |
| 440 | 1008 | return ret; |
| 1009 | } | |
| 1010 | ||
| 1011 | int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1012 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 1013 | if (values == NULL) return -1; |
| 1014 | for (size_t i = 0; i < count; i++) { | |
| 1015 | values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]); | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1016 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 1017 | } |
| 1018 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1019 | cxFreeDefault(values); |
| 440 | 1020 | return ret; |
| 1021 | } | |
| 1022 | ||
| 1023 | int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) { | |
| 1024 | assert(arr->type == CX_JSON_ARRAY); | |
| 1016 | 1025 | return cx_array_add_array_a(arr->allocator, arr->array, val, count); |
| 440 | 1026 | } |
| 1027 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1028 | int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1029 | return cxMapPut(obj->object, name, child); |
| 440 | 1030 | } |
| 1031 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1032 | CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name) { |
| 440 | 1033 | CxJsonValue* v = cxJsonCreateObj(obj->allocator); |
| 1034 | if (v == NULL) return NULL; | |
| 1035 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1036 | return v; | |
| 1037 | } | |
| 1038 | ||
| 1016 | 1039 | CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity) { |
| 1040 | CxJsonValue* v = cxJsonCreateArr(obj->allocator, capacity); | |
| 440 | 1041 | if (v == NULL) return NULL; |
| 1042 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1043 | return v; | |
| 1044 | } | |
| 1045 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1046 | CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name, double num) { |
| 440 | 1047 | CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num); |
| 1048 | if (v == NULL) return NULL; | |
| 1049 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1050 | return v; | |
| 1051 | } | |
| 1052 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1053 | CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name, int64_t num) { |
| 440 | 1054 | CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num); |
| 1055 | if (v == NULL) return NULL; | |
| 1056 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1057 | return v; | |
| 1058 | } | |
| 1059 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1060 | CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str) { |
| 440 | 1061 | CxJsonValue* v = cxJsonCreateString(obj->allocator, str); |
| 1062 | if (v == NULL) return NULL; | |
| 1063 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1064 | return v; | |
| 1065 | } | |
| 1066 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1067 | CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) { |
| 440 | 1068 | CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit); |
| 1069 | if (v == NULL) return NULL; | |
| 1070 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;} | |
| 1071 | return v; | |
| 1072 | } | |
| 1073 | ||
| 1074 | CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) { | |
| 1016 | 1075 | if (index >= value->array.size) { |
| 440 | 1076 | return &cx_json_value_nothing; |
| 1077 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1078 | return value->array.data[index]; |
| 440 | 1079 | } |
| 1080 | ||
| 845 | 1081 | CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index) { |
| 1016 | 1082 | if (index >= value->array.size) { |
| 845 | 1083 | return NULL; |
| 1084 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1085 | CxJsonValue *ret = value->array.data[index]; |
| 845 | 1086 | // TODO: replace with a low level cx_array_remove() |
| 1016 | 1087 | size_t count = value->array.size - index - 1; |
| 845 | 1088 | if (count > 0) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1089 | memmove(value->array.data + index, value->array.data + index + 1, count * sizeof(CxJsonValue*)); |
| 845 | 1090 | } |
| 1016 | 1091 | value->array.size--; |
| 845 | 1092 | return ret; |
| 1093 | } | |
| 1094 | ||
| 870 | 1095 | char *cxJsonAsString(const CxJsonValue *value) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1096 | return value->string.ptr; |
| 870 | 1097 | } |
| 1098 | ||
| 1099 | cxstring cxJsonAsCxString(const CxJsonValue *value) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1100 | return cx_strcast(value->string); |
| 870 | 1101 | } |
| 1102 | ||
| 1103 | cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1104 | return value->string; |
| 870 | 1105 | } |
| 1106 | ||
| 1107 | double cxJsonAsDouble(const CxJsonValue *value) { | |
| 1108 | if (value->type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1109 | return (double) value->integer; |
| 870 | 1110 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1111 | return value->number; |
| 870 | 1112 | } |
| 1113 | } | |
| 1114 | ||
| 1115 | int64_t cxJsonAsInteger(const CxJsonValue *value) { | |
| 1116 | if (value->type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1117 | return value->integer; |
| 870 | 1118 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1119 | return (int64_t) value->number; |
| 870 | 1120 | } |
| 1121 | } | |
| 1122 | ||
| 440 | 1123 | CxIterator cxJsonArrIter(const CxJsonValue *value) { |
| 1016 | 1124 | return cx_array_iterator_ptr(value->array); |
| 440 | 1125 | } |
| 1126 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1127 | CxMapIterator cxJsonObjIter(const CxJsonValue *value) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1128 | return cxMapIterator(value->object); |
| 440 | 1129 | } |
| 1130 | ||
| 845 | 1131 | CxJsonValue *cx_json_obj_get(const CxJsonValue *value, cxstring name) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1132 | CxJsonValue *v = cxMapGet(value->object, name); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1133 | if (v == NULL) { |
| 440 | 1134 | return &cx_json_value_nothing; |
| 1135 | } else { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1136 | return v; |
| 845 | 1137 | } |
| 1138 | } | |
| 1139 | ||
| 1140 | CxJsonValue *cx_json_obj_remove(CxJsonValue *value, cxstring name) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1141 | CxJsonValue *v = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1142 | cxMapRemoveAndGet(value->object, name, &v); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1143 | return v; |
| 440 | 1144 | } |
| 1145 | ||
| 1146 | CxJsonWriter cxJsonWriterCompact(void) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1147 | return (CxJsonWriter) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1148 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1149 | 6, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1150 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1151 | 4, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1152 | false |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1153 | }; |
| 440 | 1154 | } |
| 1155 | ||
| 1156 | CxJsonWriter cxJsonWriterPretty(bool use_spaces) { | |
| 1157 | return (CxJsonWriter) { | |
| 1158 | true, | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1159 | 6, |
| 440 | 1160 | use_spaces, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1161 | 4, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1162 | false |
| 440 | 1163 | }; |
| 1164 | } | |
| 1165 | ||
| 1166 | static int cx_json_writer_indent( | |
| 1167 | void *target, | |
| 1168 | cx_write_func wfunc, | |
| 1169 | const CxJsonWriter *settings, | |
| 1170 | unsigned int depth | |
| 1171 | ) { | |
| 1172 | if (depth == 0) return 0; | |
| 1173 | ||
| 1174 | // determine the width and characters to use | |
| 1175 | const char* indent; // for 32 prepared chars | |
| 1176 | size_t width = depth; | |
| 1177 | if (settings->indent_space) { | |
| 1178 | if (settings->indent == 0) return 0; | |
| 1179 | width *= settings->indent; | |
| 1180 | indent = " "; | |
| 1181 | } else { | |
| 1182 | indent = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; | |
| 1183 | } | |
| 1184 | ||
| 1185 | // calculate the number of write calls and write | |
| 1186 | size_t full = width / 32; | |
| 1187 | size_t remaining = width % 32; | |
| 1188 | for (size_t i = 0; i < full; i++) { | |
| 1189 | if (32 != wfunc(indent, 1, 32, target)) return 1; | |
| 1190 | } | |
| 1191 | if (remaining != wfunc(indent, 1, remaining, target)) return 1; | |
| 1192 | ||
| 1193 | return 0; | |
| 1194 | } | |
| 1195 | ||
| 1196 | ||
| 1197 | int cx_json_write_rec( | |
| 1198 | void *target, | |
| 1199 | const CxJsonValue *value, | |
| 1200 | cx_write_func wfunc, | |
| 1201 | const CxJsonWriter *settings, | |
| 1202 | unsigned int depth | |
| 1203 | ) { | |
| 1204 | // keep track of written items | |
| 1205 | // the idea is to reduce the number of jumps for error checking | |
| 1206 | size_t actual = 0, expected = 0; | |
| 1207 | ||
| 1208 | // small buffer for number to string conversions | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1209 | char numbuf[40]; |
| 440 | 1210 | |
| 1211 | // recursively write the values | |
| 1212 | switch (value->type) { | |
| 1213 | case CX_JSON_OBJECT: { | |
| 1214 | const char *begin_obj = "{\n"; | |
| 1215 | if (settings->pretty) { | |
| 1216 | actual += wfunc(begin_obj, 1, 2, target); | |
| 1217 | expected += 2; | |
| 1218 | } else { | |
| 1219 | actual += wfunc(begin_obj, 1, 1, target); | |
| 1220 | expected++; | |
| 1221 | } | |
| 1222 | depth++; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1223 | CxMapIterator member_iter = cxJsonObjIter(value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1224 | cx_foreach(const CxMapEntry *, member, member_iter) { |
| 440 | 1225 | // possible indentation |
| 1226 | if (settings->pretty) { | |
| 1227 | if (cx_json_writer_indent(target, wfunc, settings, depth)) { | |
| 1228 | return 1; // LCOV_EXCL_LINE | |
| 1229 | } | |
| 1230 | } | |
| 1231 | ||
| 1232 | // the name | |
| 1233 | actual += wfunc("\"", 1, 1, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1234 | cxstring key = cx_strn(member->key->data, member->key->len); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1235 | cxmutstr name = escape_string(key, settings->escape_slash); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1236 | actual += wfunc(name.ptr, 1, name.length, target); |
| 440 | 1237 | actual += wfunc("\"", 1, 1, target); |
| 1238 | const char *obj_name_sep = ": "; | |
| 1239 | if (settings->pretty) { | |
| 1240 | actual += wfunc(obj_name_sep, 1, 2, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1241 | expected += 4 + name.length; |
| 440 | 1242 | } else { |
| 1243 | actual += wfunc(obj_name_sep, 1, 1, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1244 | expected += 3 + name.length; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1245 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1246 | if (name.ptr != key.ptr) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1247 | cx_strfree(&name); |
| 440 | 1248 | } |
| 1249 | ||
| 1250 | // the value | |
| 1251 | if (cx_json_write_rec(target, member->value, wfunc, settings, depth)) return 1; | |
| 1252 | ||
| 1253 | // end of object-value | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1254 | if (member_iter.index < member_iter.elem_count - 1) { |
| 440 | 1255 | const char *obj_value_sep = ",\n"; |
| 1256 | if (settings->pretty) { | |
| 1257 | actual += wfunc(obj_value_sep, 1, 2, target); | |
| 1258 | expected += 2; | |
| 1259 | } else { | |
| 1260 | actual += wfunc(obj_value_sep, 1, 1, target); | |
| 1261 | expected++; | |
| 1262 | } | |
| 1263 | } else { | |
| 1264 | if (settings->pretty) { | |
| 1265 | actual += wfunc("\n", 1, 1, target); | |
| 1266 | expected ++; | |
| 1267 | } | |
| 1268 | } | |
| 1269 | } | |
| 1270 | depth--; | |
| 1271 | if (settings->pretty) { | |
| 1272 | if (cx_json_writer_indent(target, wfunc, settings, depth)) return 1; | |
| 1273 | } | |
| 1274 | actual += wfunc("}", 1, 1, target); | |
| 1275 | expected++; | |
| 1276 | break; | |
| 1277 | } | |
| 1278 | case CX_JSON_ARRAY: { | |
| 1279 | actual += wfunc("[", 1, 1, target); | |
| 1280 | expected++; | |
| 1281 | CxIterator iter = cxJsonArrIter(value); | |
| 1282 | cx_foreach(CxJsonValue*, element, iter) { | |
| 1283 | if (cx_json_write_rec( | |
| 1284 | target, element, | |
| 1285 | wfunc, settings, depth) | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1286 | ) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1287 | return 1; // LCOV_EXCL_LINE |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1288 | } |
| 440 | 1289 | |
| 1290 | if (iter.index < iter.elem_count - 1) { | |
| 1291 | const char *arr_value_sep = ", "; | |
| 1292 | if (settings->pretty) { | |
| 1293 | actual += wfunc(arr_value_sep, 1, 2, target); | |
| 1294 | expected += 2; | |
| 1295 | } else { | |
| 1296 | actual += wfunc(arr_value_sep, 1, 1, target); | |
| 1297 | expected++; | |
| 1298 | } | |
| 1299 | } | |
| 1300 | } | |
| 1301 | actual += wfunc("]", 1, 1, target); | |
| 1302 | expected++; | |
| 1303 | break; | |
| 1304 | } | |
| 1305 | case CX_JSON_STRING: { | |
| 1306 | actual += wfunc("\"", 1, 1, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1307 | cxmutstr str = escape_string(cx_strcast(value->string), |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1308 | settings->escape_slash); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1309 | actual += wfunc(str.ptr, 1, str.length, target); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1310 | actual += wfunc("\"", 1, 1, target); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1311 | expected += 2 + str.length; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1312 | if (str.ptr != value->string.ptr) { |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1313 | cx_strfree(&str); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1314 | } |
| 440 | 1315 | break; |
| 1316 | } | |
| 1317 | case CX_JSON_NUMBER: { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1318 | int precision = settings->frac_max_digits; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1319 | // because of the way how %g is defined, we need to |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1320 | // double the precision and truncate ourselves |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1321 | precision = 1 + (precision > 15 ? 30 : 2 * precision); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1322 | snprintf(numbuf, 40, "%.*g", precision, value->number); |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1323 | char *dot, *exp; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1324 | unsigned char max_digits; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1325 | // find the decimal separator and hope that it's one of . or , |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1326 | dot = strchr(numbuf, '.'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1327 | if (dot == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1328 | dot = strchr(numbuf, ','); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1329 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1330 | if (dot == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1331 | // no decimal separator found |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1332 | // output everything until a possible exponent |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1333 | max_digits = 30; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1334 | dot = numbuf; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1335 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1336 | // found a decimal separator |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1337 | // output everything until the separator |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1338 | // and set max digits to what the settings say |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1339 | size_t len = dot - numbuf; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1340 | actual += wfunc(numbuf, 1, len, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1341 | expected += len; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1342 | max_digits = settings->frac_max_digits; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1343 | if (max_digits > 15) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1344 | max_digits = 15; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1345 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1346 | // locale independent separator |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1347 | if (max_digits > 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1348 | actual += wfunc(".", 1, 1, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1349 | expected++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1350 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1351 | dot++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1352 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1353 | // find the exponent |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1354 | exp = strchr(dot, 'e'); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1355 | if (exp == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1356 | // no exponent - output the rest |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1357 | if (max_digits > 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1358 | size_t len = strlen(dot); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1359 | if (len > max_digits) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1360 | len = max_digits; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1361 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1362 | actual += wfunc(dot, 1, len, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1363 | expected += len; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1364 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1365 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1366 | // exponent found - truncate the frac digits |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1367 | // and then output the rest |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1368 | if (max_digits > 0) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1369 | size_t len = exp - dot - 1; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1370 | if (len > max_digits) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1371 | len = max_digits; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1372 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1373 | actual += wfunc(dot, 1, len, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1374 | expected += len; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1375 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1376 | actual += wfunc("e", 1, 1, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1377 | expected++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1378 | exp++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1379 | size_t len = strlen(exp); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1380 | actual += wfunc(exp, 1, len, target); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1381 | expected += len; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1382 | } |
| 440 | 1383 | break; |
| 1384 | } | |
| 1385 | case CX_JSON_INTEGER: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1386 | snprintf(numbuf, 32, "%" PRIi64, value->integer); |
| 440 | 1387 | size_t len = strlen(numbuf); |
| 1388 | actual += wfunc(numbuf, 1, len, target); | |
| 1389 | expected += len; | |
| 1390 | break; | |
| 1391 | } | |
| 1392 | case CX_JSON_LITERAL: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1393 | if (value->literal == CX_JSON_TRUE) { |
| 440 | 1394 | actual += wfunc("true", 1, 4, target); |
| 1395 | expected += 4; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1396 | } else if (value->literal == CX_JSON_FALSE) { |
| 440 | 1397 | actual += wfunc("false", 1, 5, target); |
| 1398 | expected += 5; | |
| 1399 | } else { | |
| 1400 | actual += wfunc("null", 1, 4, target); | |
| 1401 | expected += 4; | |
| 1402 | } | |
| 1403 | break; | |
| 1404 | } | |
| 1405 | case CX_JSON_NOTHING: { | |
| 1406 | // deliberately supported as an empty string! | |
| 1407 | // users might want to just write the result | |
| 1408 | // of a get operation without testing the value | |
| 1409 | // and therefore this should not blow up | |
| 1410 | break; | |
| 1411 | } | |
| 1412 | default: assert(false); // LCOV_EXCL_LINE | |
| 1413 | } | |
| 1414 | ||
| 1415 | return expected != actual; | |
| 1416 | } | |
| 1417 | ||
| 1418 | int cxJsonWrite( | |
| 1419 | void *target, | |
| 1420 | const CxJsonValue *value, | |
| 1421 | cx_write_func wfunc, | |
| 1422 | const CxJsonWriter *settings | |
| 1423 | ) { | |
| 1424 | assert(target != NULL); | |
| 1425 | assert(value != NULL); | |
| 1426 | assert(wfunc != NULL); | |
| 1427 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1428 | CxJsonWriter writer_default = cxJsonWriterCompact(); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1429 | if (settings == NULL) { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1430 | settings = &writer_default; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1431 | } |
| 440 | 1432 | return cx_json_write_rec(target, value, wfunc, settings, 0); |
| 1433 | } | |
| 995 | 1434 | |
| 1435 | static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) { | |
| 1436 | if (allocator == NULL) allocator = cxDefaultAllocator; | |
| 1437 | CxBuffer buffer; | |
| 1016 | 1438 | if (cxBufferInit(&buffer, allocator, NULL, 128, |
| 1439 | CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) { | |
| 995 | 1440 | return (cxmutstr){NULL, 0}; |
| 1441 | } | |
| 1442 | if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0) | |
| 1443 | || cxBufferTerminate(&buffer)) { | |
| 1444 | // LCOV_EXCL_START | |
| 1445 | buffer.flags &= ~CX_BUFFER_DO_NOT_FREE; | |
| 1446 | cxBufferDestroy(&buffer); | |
| 1447 | return (cxmutstr){NULL, 0}; | |
| 1448 | // LCOV_EXCL_STOP | |
| 1449 | } else { | |
| 1450 | cxmutstr str = cx_mutstrn(buffer.space, buffer.size); | |
| 1451 | cxBufferDestroy(&buffer); | |
| 1452 | return str; | |
| 1453 | } | |
| 1454 | ||
| 1455 | } | |
| 1456 | ||
| 1016 | 1457 | cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) { |
| 995 | 1458 | CxJsonWriter writer = cxJsonWriterCompact(); |
| 1459 | return cx_json_to_string(value, allocator, &writer); | |
| 1460 | } | |
| 1461 | ||
| 1016 | 1462 | cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) { |
| 995 | 1463 | CxJsonWriter writer = cxJsonWriterPretty(true); |
| 1464 | return cx_json_to_string(value, allocator, &writer); | |
| 1465 | } | |
| 1016 | 1466 | |
| 1467 | int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) { | |
| 1468 | if (json == other) return 0; | |
| 1469 | if (json == NULL || other == NULL) return -1; | |
| 1470 | if (json->type != other->type) { | |
| 1471 | if (!cxJsonIsNumber(json)) return -1; | |
| 1472 | if (!cxJsonIsNumber(other)) return -1; | |
| 1473 | } | |
| 1474 | switch (json->type) { | |
| 1475 | case CX_JSON_NOTHING: | |
| 1476 | return 0; | |
| 1477 | case CX_JSON_OBJECT: | |
| 1478 | return cxMapCompare(json->object, other->object); | |
| 1479 | case CX_JSON_ARRAY: | |
| 1480 | if (json->array.size != other->array.size) return -1; | |
| 1481 | for (size_t i = 0; i < json->array.size; i++) { | |
| 1482 | const int d = cxJsonCompare(json->array.data[i], other->array.data[i]); | |
| 1483 | if (d != 0) return d; | |
| 1484 | } | |
| 1485 | return 0; | |
| 1486 | case CX_JSON_STRING: | |
| 1487 | return cx_strcmp(json->string, other->string); | |
| 1488 | case CX_JSON_INTEGER: | |
| 1489 | if (other->type == CX_JSON_INTEGER) { | |
| 1490 | return cx_vcmp_int64(json->integer, other->integer); | |
| 1491 | } else { | |
| 1492 | return cx_vcmp_double(cxJsonAsDouble(json), other->number); | |
| 1493 | } | |
| 1494 | case CX_JSON_NUMBER: | |
| 1495 | return cx_vcmp_double(json->number, cxJsonAsDouble(other)); | |
| 1496 | case CX_JSON_LITERAL: | |
| 1497 | return json->literal == other->literal ? 0 : -1; | |
| 1498 | default: | |
| 1499 | // LCOV_EXCL_START | |
| 1500 | // unreachable | |
| 1501 | assert(false); | |
| 1502 | return -1; | |
| 1503 | // LCOV_EXCL_STOP | |
| 1504 | } | |
| 1505 | } | |
| 1506 | ||
| 1507 | CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) { | |
| 1508 | return cx_json_clone_func(NULL, value, allocator, NULL); | |
| 1509 | } | |
| 1510 | ||
| 1511 | CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source, | |
| 1512 | const CxAllocator* allocator, cx_attr_unused void *data) { | |
| 1513 | if (source == NULL || source->type == CX_JSON_NOTHING) { | |
| 1514 | return &cx_json_value_nothing; | |
| 1515 | } | |
| 1516 | if (allocator == NULL) allocator = cxDefaultAllocator; | |
| 1517 | ||
| 1518 | #define return_value(v) { \ | |
| 1519 | CxJsonValue *ret = v; \ | |
| 1520 | if (target == NULL) { \ | |
| 1521 | return ret; \ | |
| 1522 | } else { \ | |
| 1523 | *target = *ret; \ | |
| 1524 | cxFree(allocator, ret); \ | |
| 1525 | return target; \ | |
| 1526 | } \ | |
| 1527 | } | |
| 1528 | ||
| 1529 | switch (source->type) { | |
| 1530 | case CX_JSON_OBJECT: { | |
| 1531 | CxJsonValue *obj = cxJsonCreateObj(allocator); | |
| 1532 | if (obj == NULL) return NULL; // LCOV_EXCL_LINE | |
| 1533 | if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) { | |
| 1534 | // LCOV_EXCL_START | |
| 1535 | cxJsonValueFree(obj); | |
| 1536 | return NULL; | |
| 1537 | // LCOV_EXCL_STOP | |
| 1538 | } | |
| 1539 | return_value(obj); | |
| 1540 | } | |
| 1541 | case CX_JSON_ARRAY: { | |
| 1542 | const size_t elem_count = source->array.size; | |
| 1543 | CxJsonValue *arr = cxJsonCreateArr(allocator, elem_count); | |
| 1544 | if (arr == NULL) return NULL; // LCOV_EXCL_LINE | |
| 1545 | arr->array.size = elem_count; | |
| 1546 | for (size_t i = 0 ; i < elem_count ; i++) { | |
| 1547 | CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL); | |
| 1548 | if (e == NULL) { | |
| 1549 | // LCOV_EXCL_START | |
| 1550 | cxJsonValueFree(arr); | |
| 1551 | return NULL; | |
| 1552 | // LCOV_EXCL_STOP | |
| 1553 | } | |
| 1554 | arr->array.data[i] = e; | |
| 1555 | } | |
| 1556 | return_value(arr); | |
| 1557 | } | |
| 1558 | case CX_JSON_STRING: | |
| 1559 | return_value(cxJsonCreateString(allocator, source->string)); | |
| 1560 | case CX_JSON_INTEGER: | |
| 1561 | return_value(cxJsonCreateInteger(allocator, source->integer)); | |
| 1562 | case CX_JSON_NUMBER: | |
| 1563 | return_value(cxJsonCreateNumber(allocator, source->number)); | |
| 1564 | case CX_JSON_LITERAL: | |
| 1565 | return_value(cxJsonCreateLiteral(allocator, source->literal)); | |
| 1566 | default: | |
| 1567 | // LCOV_EXCL_START | |
| 1568 | // unreachable | |
| 1569 | assert(false); | |
| 1570 | return NULL; | |
| 1571 | // LCOV_EXCL_STOP | |
| 1572 | } | |
| 1573 | #undef return_value | |
| 1574 | } |