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