Wed, 31 Dec 2025 16:40:12 +0100
update ucx to version 4.0
| 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; | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
101 | if (json->uncompleted_tokentype != CX_JSON_NO_TOKEN) { |
| 440 | 102 | allocated = true; |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
103 | str = cx_strcat(json->uncompleted_content, 1, str); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
104 | if (str.ptr == NULL) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
105 | return (CxJsonToken){CX_JSON_NO_TOKEN, false, CX_NULLSTR}; // LCOV_EXCL_LINE |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
106 | } |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
107 | json->uncompleted_content = CX_NULLSTR; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
108 | json->uncompleted_tokentype = CX_JSON_NO_TOKEN; |
| 440 | 109 | } |
| 110 | CxJsonTokenType ttype; | |
| 111 | if (isstring) { | |
| 112 | ttype = CX_JSON_TOKEN_STRING; | |
| 113 | } else { | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
114 | if (!cx_strcmp(str, "true") || !cx_strcmp(str, "false") |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
115 | || !cx_strcmp(str, "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 | } | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
125 | return (CxJsonToken){CX_JSON_TOKEN_ERROR, false, CX_NULLSTR}; |
| 440 | 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)) { | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
165 | return json->uncompleted_tokentype == CX_JSON_NO_TOKEN ? |
| 440 | 166 | CX_JSON_NO_DATA : CX_JSON_INCOMPLETE_DATA; |
| 167 | } | |
| 168 | ||
| 169 | // current token type and start index | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
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 |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
174 | && cx_strat(json->uncompleted_content, -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; | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
192 | *result = (CxJsonToken){ctype, false, CX_NULLSTR}; |
| 440 | 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 |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
235 | cxstring uncompleted = cx_strn(json->buffer.space + token_part_start, json->buffer.size - token_part_start); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
236 | if (json->uncompleted_tokentype == CX_JSON_NO_TOKEN) { |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
237 | assert(json->uncompleted_content.ptr == NULL); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
238 | json->uncompleted_content = cx_strdup(uncompleted); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
239 | if (json->uncompleted_content.ptr == NULL) { |
| 440 | 240 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE |
| 241 | } | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
242 | json->uncompleted_tokentype = ttype; |
| 440 | 243 | } else { |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
244 | // previously we already had an uncompleted token |
| 440 | 245 | // combine the uncompleted token with the current token |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
246 | cxmutstr s = cx_strcat(json->uncompleted_content, 1, uncompleted); |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
247 | if (s.ptr == NULL) { |
| 440 | 248 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE |
| 249 | } | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
250 | json->uncompleted_content = s; |
| 440 | 251 | } |
| 252 | // advance the buffer position - we saved the stuff in the uncompleted token | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
253 | json->buffer.pos += uncompleted.length; |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
254 | return CX_JSON_INCOMPLETE_DATA; |
| 440 | 255 | } |
| 256 | } | |
| 257 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
258 | // 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
|
259 | 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
|
260 | 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
|
261 | *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
|
262 | 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
|
263 | } 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
|
264 | 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
|
265 | 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
|
266 | 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
|
267 | } 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
|
268 | 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
|
269 | 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
|
270 | 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
|
271 | 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
|
272 | } 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
|
273 | 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
|
274 | 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
|
275 | 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
|
276 | 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
|
277 | 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
|
278 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
279 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
280 | 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
|
281 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
282 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
283 | // 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
|
284 | 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
|
285 | 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
|
286 | } |
|
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 | 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
|
289 | // 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
|
290 | // 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
|
291 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
292 | 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
|
293 | 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
|
294 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
295 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
296 | 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
|
297 | 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
|
298 | 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
|
299 | 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
|
300 | 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
|
301 | 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
|
302 | // 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
|
303 | // 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
|
304 | 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
|
305 | 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
|
306 | } 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
|
307 | // 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
|
308 | // 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
|
309 | 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
|
310 | 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
|
311 | 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
|
312 | 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
|
313 | && 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
|
314 | 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
|
315 | 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
|
316 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
317 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
318 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
319 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
320 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
321 | 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
|
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 | |
| 440 | 324 | 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
|
325 | // 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
|
326 | |
| 440 | 327 | cxmutstr result; |
| 328 | result.length = 0; | |
| 329 | result.ptr = cxMalloc(a, str.length - 1); | |
| 330 | if (result.ptr == NULL) return result; // LCOV_EXCL_LINE | |
| 331 | ||
| 332 | bool u = false; | |
| 333 | for (size_t i = 1; i < str.length - 1; i++) { | |
| 334 | char c = str.ptr[i]; | |
| 335 | if (u) { | |
| 336 | u = false; | |
| 337 | if (c == 'n') { | |
| 338 | 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
|
339 | } 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
|
340 | c = '"'; |
| 440 | 341 | } else if (c == 't') { |
| 342 | 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
|
343 | } 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
|
344 | 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
|
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 = '\\'; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
347 | } 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
|
348 | 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
|
349 | } 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
|
350 | 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
|
351 | } 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
|
352 | 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
|
353 | } 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
|
354 | 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
|
355 | unsigned utf8len = unescape_unicode_string( |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
356 | 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
|
357 | utf8buf |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
358 | ); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
359 | 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
|
360 | 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
|
361 | // 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
|
362 | // 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
|
363 | utf8len--; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
364 | 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
|
365 | 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
|
366 | 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
|
367 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
368 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
369 | // 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
|
370 | 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
|
371 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
372 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
373 | // 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
|
374 | // 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
|
375 | result.ptr[result.length++] = '\\'; // LCOV_EXCL_LINE |
| 440 | 376 | } |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
377 | |
| 440 | 378 | result.ptr[result.length++] = c; |
| 379 | } else { | |
| 380 | if (c == '\\') { | |
| 381 | u = true; | |
| 382 | } else { | |
| 383 | result.ptr[result.length++] = c; | |
| 384 | } | |
| 385 | } | |
| 386 | } | |
| 387 | result.ptr[result.length] = 0; | |
| 388 | ||
| 389 | return result; | |
| 390 | } | |
| 391 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
392 | 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
|
393 | // 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
|
394 | // 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
|
395 | 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
|
396 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
397 | 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
|
398 | 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
|
399 | 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
|
400 | 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
|
401 | || (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
|
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 | 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
|
404 | size_t capa = str.length + 32; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
405 | 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
|
406 | if (space == NULL) return cx_mutstrn(NULL, 0); |
| 1016 | 407 | 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
|
408 | 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
|
409 | 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
|
410 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
411 | 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
|
412 | 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
|
413 | 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
|
414 | 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
|
415 | } 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
|
416 | 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
|
417 | } 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
|
418 | 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
|
419 | } 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
|
420 | 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
|
421 | } 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
|
422 | 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
|
423 | } 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
|
424 | 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
|
425 | } 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
|
426 | 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
|
427 | } 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
|
428 | 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
|
429 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
430 | 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
|
431 | 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
|
432 | 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
|
433 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
434 | } 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
|
435 | 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
|
436 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
437 | } |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
438 | cxmutstr ret; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
439 | if (all_printable) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
440 | // 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
|
441 | ret = cx_mutstrn((char*)str.ptr, str.length); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
442 | } else { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
443 | 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
|
444 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
445 | cxBufferDestroy(&buf); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
446 | return ret; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
447 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
448 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
449 | static CxJsonObject json_create_object_map(const CxAllocator *allocator) { |
| 1016 | 450 | CxMap *map = cxKvListCreateAsMap(allocator, CX_STORE_POINTERS); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
451 | if (map == NULL) return NULL; // LCOV_EXCL_LINE |
| 1016 | 452 | cxSetCompareFunc(map, cxJsonCompare); |
| 453 | cxSetDestructor(map, cxJsonValueFree); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
454 | return map; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
455 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
456 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
457 | static void json_free_object_map(CxJsonObject obj) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
458 | 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
|
459 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
460 | |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
461 | static CxJsonValue* json_create_value(CxJson *json, CxJsonValueType type) { |
| 440 | 462 | CxJsonValue *v = cxCalloc(json->allocator, 1, sizeof(CxJsonValue)); |
| 463 | if (v == NULL) return NULL; // LCOV_EXCL_LINE | |
| 464 | ||
| 465 | // initialize the value | |
| 466 | v->type = type; | |
| 467 | v->allocator = json->allocator; | |
| 468 | if (type == CX_JSON_ARRAY) { | |
| 1016 | 469 | if (cx_array_init_a(json->allocator, v->array, 16)) { |
| 470 | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 471 | } | |
| 440 | 472 | } else if (type == CX_JSON_OBJECT) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
473 | v->object = json_create_object_map(json->allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
474 | if (v->object == NULL) goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 440 | 475 | } |
| 476 | ||
| 477 | // add the new value to a possible parent | |
| 1016 | 478 | if (json->vbuf.size > 0) { |
| 479 | CxJsonValue *parent = json->vbuf.data[json->vbuf.size - 1]; | |
| 440 | 480 | assert(parent != NULL); |
| 481 | if (parent->type == CX_JSON_ARRAY) { | |
|
1035
86d3a45dc928
implement menu item events (Win32)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1016
diff
changeset
|
482 | if (cx_array_add_a(json->allocator, parent->array, v)) { |
| 440 | 483 | goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 484 | } | |
| 485 | } else if (parent->type == CX_JSON_OBJECT) { | |
| 486 | // 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
|
487 | // 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
|
488 | assert(json->uncompleted_member_name.ptr != NULL); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
489 | if (cxMapPut(parent->object, json->uncompleted_member_name, v)) { |
| 440 | 490 | goto create_json_value_exit_error; // LCOV_EXCL_LINE |
| 491 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
492 | cx_strfree_a(json->allocator, &json->uncompleted_member_name); |
| 440 | 493 | } else { |
| 494 | assert(false); // LCOV_EXCL_LINE | |
| 495 | } | |
| 496 | } | |
| 497 | ||
| 498 | // add the new value to the stack, if it is an array or object | |
| 499 | if (type == CX_JSON_ARRAY || type == CX_JSON_OBJECT) { | |
| 1016 | 500 | if (json->vbuf.size >= json->vbuf.capacity) { |
| 501 | int alloc_error; | |
| 502 | if (json->vbuf.data == json->vbuf_internal) { | |
| 503 | alloc_error = cx_array_copy_to_new(json->vbuf, json->vbuf.size+1); | |
| 504 | } else { | |
| 505 | alloc_error = cx_array_reserve(json->vbuf, json->vbuf.size+1); | |
| 506 | } | |
| 507 | if (alloc_error) { | |
| 508 | goto create_json_value_exit_error; // LCOV_EXCL_LINE | |
| 509 | } | |
| 440 | 510 | } |
| 1016 | 511 | json->vbuf.data[json->vbuf.size] = v; |
| 512 | json->vbuf.size++; | |
| 440 | 513 | } |
| 514 | ||
| 515 | // if currently no value is parsed, this is now the value of interest | |
| 516 | if (json->parsed == NULL) { | |
| 517 | json->parsed = v; | |
| 518 | } | |
| 519 | ||
| 520 | return v; | |
| 521 | // LCOV_EXCL_START | |
| 522 | create_json_value_exit_error: | |
| 523 | cxJsonValueFree(v); | |
| 524 | return NULL; | |
| 525 | // LCOV_EXCL_STOP | |
| 526 | } | |
| 527 | ||
| 528 | #define JP_STATE_VALUE_BEGIN 0 | |
| 529 | #define JP_STATE_VALUE_END 10 | |
| 530 | #define JP_STATE_VALUE_BEGIN_OBJ 1 | |
| 531 | #define JP_STATE_OBJ_SEP_OR_CLOSE 11 | |
| 532 | #define JP_STATE_VALUE_BEGIN_AR 2 | |
| 533 | #define JP_STATE_ARRAY_SEP_OR_CLOSE 12 | |
| 534 | #define JP_STATE_OBJ_NAME_OR_CLOSE 5 | |
| 535 | #define JP_STATE_OBJ_NAME 6 | |
| 536 | #define JP_STATE_OBJ_COLON 7 | |
| 537 | ||
| 538 | void cxJsonInit(CxJson *json, const CxAllocator *allocator) { | |
| 539 | if (allocator == NULL) { | |
| 540 | allocator = cxDefaultAllocator; | |
| 541 | } | |
| 542 | ||
| 543 | memset(json, 0, sizeof(CxJson)); | |
| 544 | json->allocator = allocator; | |
| 545 | ||
| 1016 | 546 | cx_array_init_fixed(json->states, json->states_internal, 1); |
| 547 | json->states.data[0] = JP_STATE_VALUE_BEGIN; | |
| 548 | cx_array_init_fixed(json->vbuf, json->vbuf_internal, 0); | |
| 440 | 549 | } |
| 550 | ||
| 551 | void cxJsonDestroy(CxJson *json) { | |
| 552 | cxBufferDestroy(&json->buffer); | |
| 1016 | 553 | if (json->states.data != json->states_internal) { |
| 554 | cx_array_free(json->states); | |
| 440 | 555 | } |
| 1016 | 556 | if (json->vbuf.data != json->vbuf_internal) { |
| 557 | cx_array_free(json->vbuf); | |
| 440 | 558 | } |
| 559 | cxJsonValueFree(json->parsed); | |
| 560 | json->parsed = NULL; | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
561 | json->uncompleted_tokentype = CX_JSON_NO_TOKEN; |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
562 | cx_strfree(&json->uncompleted_content); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
563 | cx_strfree_a(json->allocator, &json->uncompleted_member_name); |
| 440 | 564 | } |
| 565 | ||
| 870 | 566 | void cxJsonReset(CxJson *json) { |
| 567 | const CxAllocator *allocator = json->allocator; | |
| 568 | cxJsonDestroy(json); | |
| 569 | cxJsonInit(json, allocator); | |
| 570 | } | |
| 571 | ||
| 440 | 572 | int cxJsonFilln(CxJson *json, const char *buf, size_t size) { |
| 573 | if (cxBufferEof(&json->buffer)) { | |
| 574 | // reinitialize the buffer | |
| 575 | cxBufferDestroy(&json->buffer); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
576 | if (buf == NULL) buf = ""; // buffer must not be initialized with NULL |
| 1016 | 577 | cxBufferInit(&json->buffer, NULL, (char*) buf, |
| 578 | size, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_COPY_ON_WRITE); | |
| 440 | 579 | json->buffer.size = size; |
| 580 | return 0; | |
| 581 | } else { | |
| 582 | return size != cxBufferAppend(buf, 1, size, &json->buffer); | |
| 583 | } | |
| 584 | } | |
| 585 | ||
| 586 | static void json_add_state(CxJson *json, int state) { | |
| 1016 | 587 | // we have guaranteed the necessary space |
| 440 | 588 | // therefore, we can safely add the state in the simplest way possible |
| 1016 | 589 | json->states.data[json->states.size++] = state; |
| 440 | 590 | } |
| 591 | ||
| 592 | #define return_rec(code) \ | |
| 593 | token_destroy(&token); \ | |
| 594 | return code | |
| 595 | ||
| 596 | static enum cx_json_status json_parse(CxJson *json) { | |
| 597 | // Reserve a pointer for a possibly read value | |
| 598 | CxJsonValue *vbuf = NULL; | |
| 599 | ||
| 600 | // grab the next token | |
| 601 | CxJsonToken token; | |
| 602 | { | |
| 603 | enum cx_json_status ret = token_parse_next(json, &token); | |
| 604 | if (ret != CX_JSON_NO_ERROR) { | |
| 605 | return ret; | |
| 606 | } | |
| 607 | } | |
| 608 | ||
| 609 | // pop the current state | |
| 1016 | 610 | assert(json->states.size > 0); |
| 611 | int state = json->states.data[--json->states.size]; | |
| 440 | 612 | |
| 1016 | 613 | // guarantee that at least two more states fit into the array |
| 614 | const size_t required_states_depth = json->states.size + 2; | |
| 615 | if (required_states_depth >= json->states.capacity) { | |
| 616 | int alloc_error; | |
| 617 | if (json->states.data == json->states_internal) { | |
| 618 | alloc_error = cx_array_copy_to_new(json->states, required_states_depth); | |
| 619 | } else { | |
| 620 | alloc_error = cx_array_reserve(json->states, required_states_depth); | |
| 621 | } | |
| 622 | if (alloc_error) { | |
| 623 | return CX_JSON_BUFFER_ALLOC_FAILED; // LCOV_EXCL_LINE | |
| 624 | } | |
| 440 | 625 | } |
| 626 | ||
| 627 | ||
| 628 | // 0 JP_STATE_VALUE_BEGIN value begin | |
| 629 | // 10 JP_STATE_VALUE_END expect value end | |
| 630 | // 1 JP_STATE_VALUE_BEGIN_OBJ value begin (inside object) | |
| 631 | // 11 JP_STATE_OBJ_SEP_OR_CLOSE object, expect separator, objclose | |
| 632 | // 2 JP_STATE_VALUE_BEGIN_AR value begin (inside array) | |
| 633 | // 12 JP_STATE_ARRAY_SEP_OR_CLOSE array, expect separator or arrayclose | |
| 634 | // 5 JP_STATE_OBJ_NAME_OR_CLOSE object, expect name or objclose | |
| 635 | // 6 JP_STATE_OBJ_NAME object, expect name | |
| 636 | // 7 JP_STATE_OBJ_COLON object, expect ':' | |
| 637 | ||
| 638 | if (state < 3) { | |
| 639 | // push expected end state to the stack | |
| 640 | json_add_state(json, 10 + state); | |
| 641 | switch (token.tokentype) { | |
| 642 | 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
|
643 | if (json_create_value(json, CX_JSON_ARRAY) == NULL) { |
| 440 | 644 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 645 | } | |
| 646 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 647 | return_rec(CX_JSON_NO_ERROR); | |
| 648 | } | |
| 649 | 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
|
650 | if (json_create_value(json, CX_JSON_OBJECT) == NULL) { |
| 440 | 651 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 652 | } | |
| 653 | json_add_state(json, JP_STATE_OBJ_NAME_OR_CLOSE); | |
| 654 | return_rec(CX_JSON_NO_ERROR); | |
| 655 | } | |
| 1016 | 656 | case CX_JSON_TOKEN_END_ARRAY: { |
| 657 | if (state == JP_STATE_VALUE_BEGIN_AR) { | |
| 658 | // discard the array from the value buffer | |
| 659 | json->vbuf.size--; | |
| 660 | json->states.size--; | |
| 661 | return_rec(CX_JSON_NO_ERROR); | |
| 662 | } else { | |
| 663 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 664 | } | |
| 665 | } | |
| 440 | 666 | 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
|
667 | if ((vbuf = json_create_value(json, CX_JSON_STRING)) == NULL) { |
| 440 | 668 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 669 | } | |
| 670 | cxmutstr str = unescape_string(json->allocator, token.content); | |
| 671 | if (str.ptr == NULL) { | |
| 672 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 673 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
674 | vbuf->string = str; |
| 440 | 675 | return_rec(CX_JSON_NO_ERROR); |
| 676 | } | |
| 677 | case CX_JSON_TOKEN_INTEGER: | |
| 678 | case CX_JSON_TOKEN_NUMBER: { | |
| 679 | 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
|
680 | if (NULL == (vbuf = json_create_value(json, type))) { |
| 440 | 681 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 682 | } | |
| 683 | if (type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
684 | if (cx_strtoi64(token.content, &vbuf->integer, 10)) { |
| 440 | 685 | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); |
| 686 | } | |
| 687 | } else { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
688 | 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
|
689 | // 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
|
690 | return_rec(CX_JSON_FORMAT_ERROR_NUMBER); // LCOV_EXCL_LINE |
| 440 | 691 | } |
| 692 | } | |
| 693 | return_rec(CX_JSON_NO_ERROR); | |
| 694 | } | |
| 695 | 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
|
696 | if ((vbuf = json_create_value(json, CX_JSON_LITERAL)) == NULL) { |
| 440 | 697 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE |
| 698 | } | |
| 1016 | 699 | if (0 == cx_strcmp(token.content, "true")) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
700 | vbuf->literal = CX_JSON_TRUE; |
| 1016 | 701 | } else if (0 == cx_strcmp(token.content, "false")) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
702 | vbuf->literal = CX_JSON_FALSE; |
| 440 | 703 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
704 | vbuf->literal = CX_JSON_NULL; |
| 440 | 705 | } |
| 706 | return_rec(CX_JSON_NO_ERROR); | |
| 707 | } | |
| 708 | default: { | |
| 709 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 710 | } | |
| 711 | } | |
| 712 | } else if (state == JP_STATE_ARRAY_SEP_OR_CLOSE) { | |
| 713 | // expect ',' or ']' | |
| 714 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { | |
| 715 | json_add_state(json, JP_STATE_VALUE_BEGIN_AR); | |
| 716 | return_rec(CX_JSON_NO_ERROR); | |
| 717 | } else if (token.tokentype == CX_JSON_TOKEN_END_ARRAY) { | |
| 718 | // discard the array from the value buffer | |
| 1016 | 719 | json->vbuf.size--; |
| 440 | 720 | return_rec(CX_JSON_NO_ERROR); |
| 721 | } else { | |
| 722 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 723 | } | |
| 724 | } else if (state == JP_STATE_OBJ_NAME_OR_CLOSE || state == JP_STATE_OBJ_NAME) { | |
| 725 | if (state == JP_STATE_OBJ_NAME_OR_CLOSE && token.tokentype == CX_JSON_TOKEN_END_OBJECT) { | |
| 726 | // discard the obj from the value buffer | |
| 1016 | 727 | json->vbuf.size--; |
| 440 | 728 | return_rec(CX_JSON_NO_ERROR); |
| 729 | } else { | |
| 730 | // expect string | |
| 731 | if (token.tokentype != CX_JSON_TOKEN_STRING) { | |
| 732 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 733 | } | |
| 734 | ||
| 735 | // add new entry | |
| 736 | cxmutstr name = unescape_string(json->allocator, token.content); | |
| 737 | if (name.ptr == NULL) { | |
| 738 | return_rec(CX_JSON_VALUE_ALLOC_FAILED); // LCOV_EXCL_LINE | |
| 739 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
740 | assert(json->uncompleted_member_name.ptr == NULL); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
741 | json->uncompleted_member_name = name; |
| 1016 | 742 | assert(json->vbuf.size > 0); |
| 440 | 743 | |
| 744 | // next state | |
| 745 | json_add_state(json, JP_STATE_OBJ_COLON); | |
| 746 | return_rec(CX_JSON_NO_ERROR); | |
| 747 | } | |
| 748 | } else if (state == JP_STATE_OBJ_COLON) { | |
| 749 | // expect ':' | |
| 750 | if (token.tokentype != CX_JSON_TOKEN_NAME_SEPARATOR) { | |
| 751 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 752 | } | |
| 753 | // next state | |
| 754 | json_add_state(json, JP_STATE_VALUE_BEGIN_OBJ); | |
| 755 | return_rec(CX_JSON_NO_ERROR); | |
| 756 | } else if (state == JP_STATE_OBJ_SEP_OR_CLOSE) { | |
| 757 | // expect ',' or '}' | |
| 758 | if (token.tokentype == CX_JSON_TOKEN_VALUE_SEPARATOR) { | |
| 759 | json_add_state(json, JP_STATE_OBJ_NAME); | |
| 760 | return_rec(CX_JSON_NO_ERROR); | |
| 761 | } else if (token.tokentype == CX_JSON_TOKEN_END_OBJECT) { | |
| 762 | // discard the obj from the value buffer | |
| 1016 | 763 | json->vbuf.size--; |
| 440 | 764 | return_rec(CX_JSON_NO_ERROR); |
| 765 | } else { | |
| 766 | return_rec(CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN); | |
| 767 | } | |
| 768 | } else { | |
| 769 | // should be unreachable | |
| 770 | assert(false); | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
771 | return_rec(-1); // LCOV_EXCL_LINE |
| 440 | 772 | } |
| 773 | } | |
| 774 | ||
| 775 | CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value) { | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
776 | // initialize output value |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
777 | *value = &cx_json_value_nothing; |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
778 | |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
779 | // check if the buffer has been filled |
| 440 | 780 | if (json->buffer.space == NULL) { |
| 781 | return CX_JSON_NULL_DATA; | |
| 782 | } | |
| 783 | ||
| 784 | // parse data | |
| 785 | CxJsonStatus result; | |
| 786 | do { | |
| 787 | result = json_parse(json); | |
| 1016 | 788 | if (result == CX_JSON_NO_ERROR && json->states.size == 1) { |
| 440 | 789 | // final state reached |
| 1016 | 790 | assert(json->states.data[0] == JP_STATE_VALUE_END); |
| 791 | assert(json->vbuf.size == 0); | |
| 440 | 792 | |
| 793 | // write output value | |
| 794 | *value = json->parsed; | |
| 795 | json->parsed = NULL; | |
| 796 | ||
| 797 | // re-initialize state machine | |
| 1016 | 798 | json->states.data[0] = JP_STATE_VALUE_BEGIN; |
| 440 | 799 | |
| 800 | return CX_JSON_NO_ERROR; | |
| 801 | } | |
| 802 | } while (result == CX_JSON_NO_ERROR); | |
| 803 | ||
| 804 | // the parser might think there is no data | |
| 805 | // but when we did not reach the final state, | |
| 806 | // we know that there must be more to come | |
| 1016 | 807 | if (result == CX_JSON_NO_DATA && json->states.size > 1) { |
| 440 | 808 | return CX_JSON_INCOMPLETE_DATA; |
| 809 | } | |
| 810 | ||
| 811 | return result; | |
| 812 | } | |
| 813 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
814 | CxJsonStatus cx_json_from_string(const CxAllocator *allocator, |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
815 | cxstring str, CxJsonValue **value) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
816 | *value = &cx_json_value_nothing; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
817 | CxJson parser; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
818 | cxJsonInit(&parser, allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
819 | if (cxJsonFill(&parser, str)) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
820 | // LCOV_EXCL_START |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
821 | cxJsonDestroy(&parser); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
822 | return CX_JSON_BUFFER_ALLOC_FAILED; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
823 | // LCOV_EXCL_STOP |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
824 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
825 | CxJsonStatus status = cxJsonNext(&parser, value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
826 | // check if we consume the total string |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
827 | CxJsonValue *chk_value = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
828 | CxJsonStatus chk_status = CX_JSON_NO_DATA; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
829 | if (status == CX_JSON_NO_ERROR) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
830 | chk_status = cxJsonNext(&parser, &chk_value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
831 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
832 | cxJsonDestroy(&parser); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
833 | if (chk_status == CX_JSON_NO_DATA) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
834 | return status; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
835 | } else { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
836 | cxJsonValueFree(*value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
837 | // if chk_value is nothing, the free is harmless |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
838 | cxJsonValueFree(chk_value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
839 | *value = &cx_json_value_nothing; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
840 | return CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
841 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
842 | |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
843 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
844 | |
| 440 | 845 | void cxJsonValueFree(CxJsonValue *value) { |
| 846 | if (value == NULL || value->type == CX_JSON_NOTHING) return; | |
| 847 | switch (value->type) { | |
| 848 | case CX_JSON_OBJECT: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
849 | json_free_object_map(value->object); |
| 440 | 850 | break; |
| 851 | } | |
| 852 | case CX_JSON_ARRAY: { | |
| 1016 | 853 | for (size_t i = 0; i < value->array.size; i++) { |
| 854 | cxJsonValueFree(value->array.data[i]); | |
| 440 | 855 | } |
| 1016 | 856 | cx_array_free_a(value->allocator, value->array); |
| 440 | 857 | break; |
| 858 | } | |
| 859 | case CX_JSON_STRING: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
860 | cxFree(value->allocator, value->string.ptr); |
| 440 | 861 | break; |
| 862 | } | |
| 863 | default: { | |
| 864 | break; | |
| 865 | } | |
| 866 | } | |
| 867 | cxFree(value->allocator, value); | |
| 868 | } | |
| 869 | ||
| 870 | 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
|
871 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 872 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 873 | if (v == NULL) return NULL; | |
| 874 | v->allocator = allocator; | |
| 875 | v->type = CX_JSON_OBJECT; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
876 | v->object = json_create_object_map(allocator); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
877 | if (v->object == NULL) { // LCOV_EXCL_START |
| 440 | 878 | cxFree(allocator, v); |
| 879 | return NULL; | |
| 880 | // LCOV_EXCL_STOP | |
| 881 | } | |
| 882 | return v; | |
| 883 | } | |
| 884 | ||
| 1016 | 885 | 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
|
886 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 887 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 888 | if (v == NULL) return NULL; | |
| 889 | v->allocator = allocator; | |
| 890 | v->type = CX_JSON_ARRAY; | |
| 1016 | 891 | if (capacity > 0) { |
| 892 | if (cx_array_init_a(allocator, v->array, capacity)) { | |
| 893 | // LCOV_EXCL_START | |
| 894 | cxFree(allocator, v); | |
| 895 | return NULL; | |
| 896 | // LCOV_EXCL_STOP | |
| 897 | } | |
| 898 | } else { | |
| 899 | v->array.data = NULL; | |
| 900 | v->array.size = v->array.capacity = 0; | |
| 901 | } | |
| 440 | 902 | return v; |
| 903 | } | |
| 904 | ||
| 905 | 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
|
906 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 907 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 908 | if (v == NULL) return NULL; | |
| 909 | v->allocator = allocator; | |
| 910 | v->type = CX_JSON_NUMBER; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
911 | v->number = num; |
| 440 | 912 | return v; |
| 913 | } | |
| 914 | ||
| 915 | 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
|
916 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 917 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 918 | if (v == NULL) return NULL; | |
| 919 | v->allocator = allocator; | |
| 920 | v->type = CX_JSON_INTEGER; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
921 | v->integer = num; |
| 440 | 922 | return v; |
| 923 | } | |
| 924 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
925 | 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
|
926 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 927 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 928 | if (v == NULL) return NULL; | |
| 929 | v->allocator = allocator; | |
| 930 | v->type = CX_JSON_STRING; | |
| 931 | cxmutstr s = cx_strdup_a(allocator, str); | |
| 932 | 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
|
933 | v->string = s; |
| 440 | 934 | return v; |
| 935 | } | |
| 936 | ||
| 937 | 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
|
938 | if (allocator == NULL) allocator = cxDefaultAllocator; |
| 440 | 939 | CxJsonValue* v = cxMalloc(allocator, sizeof(CxJsonValue)); |
| 940 | if (v == NULL) return NULL; | |
| 941 | v->allocator = allocator; | |
| 942 | v->type = CX_JSON_LITERAL; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
943 | v->literal = lit; |
| 440 | 944 | return v; |
| 945 | } | |
| 946 | ||
| 947 | // LCOV_EXCL_START | |
| 948 | // 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
|
949 | static void json_arr_free_temp(CxJsonValue** values, size_t count) { |
| 440 | 950 | for (size_t i = 0; i < count; i++) { |
| 951 | if (values[i] == NULL) break; | |
| 952 | cxJsonValueFree(values[i]); | |
| 953 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
954 | cxFreeDefault(values); |
| 440 | 955 | } |
| 956 | // LCOV_EXCL_STOP | |
| 957 | ||
| 958 | 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
|
959 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 960 | if (values == NULL) return -1; |
| 961 | for (size_t i = 0; i < count; i++) { | |
| 962 | 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
|
963 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 964 | } |
| 965 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
966 | cxFreeDefault(values); |
| 440 | 967 | return ret; |
| 968 | } | |
| 969 | ||
| 970 | 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
|
971 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 972 | if (values == NULL) return -1; |
| 973 | for (size_t i = 0; i < count; i++) { | |
| 974 | 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
|
975 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 976 | } |
| 977 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
978 | cxFreeDefault(values); |
| 440 | 979 | return ret; |
| 980 | } | |
| 981 | ||
| 982 | 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
|
983 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 984 | if (values == NULL) return -1; |
| 985 | for (size_t i = 0; i < count; i++) { | |
| 986 | 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
|
987 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 988 | } |
| 989 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
990 | cxFreeDefault(values); |
| 440 | 991 | return ret; |
| 992 | } | |
| 993 | ||
| 994 | 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
|
995 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 996 | if (values == NULL) return -1; |
| 997 | 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
|
998 | 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
|
999 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 1000 | } |
| 1001 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1002 | cxFreeDefault(values); |
| 440 | 1003 | return ret; |
| 1004 | } | |
| 1005 | ||
| 1006 | 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
|
1007 | CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*)); |
| 440 | 1008 | if (values == NULL) return -1; |
| 1009 | for (size_t i = 0; i < count; i++) { | |
| 1010 | 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
|
1011 | if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; } |
| 440 | 1012 | } |
| 1013 | int ret = cxJsonArrAddValues(arr, values, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
1014 | cxFreeDefault(values); |
| 440 | 1015 | return ret; |
| 1016 | } | |
| 1017 | ||
| 1018 | int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue* const* val, size_t count) { | |
| 1019 | assert(arr->type == CX_JSON_ARRAY); | |
| 1016 | 1020 | return cx_array_add_array_a(arr->allocator, arr->array, val, count); |
| 440 | 1021 | } |
| 1022 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1023 | 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
|
1024 | return cxMapPut(obj->object, name, child); |
| 440 | 1025 | } |
| 1026 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1027 | CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name) { |
| 440 | 1028 | CxJsonValue* v = cxJsonCreateObj(obj->allocator); |
| 1029 | if (v == NULL) return NULL; | |
| 1030 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1031 | return v; | |
| 1032 | } | |
| 1033 | ||
| 1016 | 1034 | CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name, size_t capacity) { |
| 1035 | CxJsonValue* v = cxJsonCreateArr(obj->allocator, capacity); | |
| 440 | 1036 | if (v == NULL) return NULL; |
| 1037 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1038 | return v; | |
| 1039 | } | |
| 1040 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1041 | CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name, double num) { |
| 440 | 1042 | CxJsonValue* v = cxJsonCreateNumber(obj->allocator, num); |
| 1043 | if (v == NULL) return NULL; | |
| 1044 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1045 | return v; | |
| 1046 | } | |
| 1047 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1048 | CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name, int64_t num) { |
| 440 | 1049 | CxJsonValue* v = cxJsonCreateInteger(obj->allocator, num); |
| 1050 | if (v == NULL) return NULL; | |
| 1051 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1052 | return v; | |
| 1053 | } | |
| 1054 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1055 | CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str) { |
| 440 | 1056 | CxJsonValue* v = cxJsonCreateString(obj->allocator, str); |
| 1057 | if (v == NULL) return NULL; | |
| 1058 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL; } | |
| 1059 | return v; | |
| 1060 | } | |
| 1061 | ||
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1062 | CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit) { |
| 440 | 1063 | CxJsonValue* v = cxJsonCreateLiteral(obj->allocator, lit); |
| 1064 | if (v == NULL) return NULL; | |
| 1065 | if (cxJsonObjPut(obj, name, v)) { cxJsonValueFree(v); return NULL;} | |
| 1066 | return v; | |
| 1067 | } | |
| 1068 | ||
| 1069 | CxJsonValue *cxJsonArrGet(const CxJsonValue *value, size_t index) { | |
| 1016 | 1070 | if (index >= value->array.size) { |
| 440 | 1071 | return &cx_json_value_nothing; |
| 1072 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1073 | return value->array.data[index]; |
| 440 | 1074 | } |
| 1075 | ||
| 845 | 1076 | CxJsonValue *cxJsonArrRemove(CxJsonValue *value, size_t index) { |
| 1016 | 1077 | if (index >= value->array.size) { |
| 845 | 1078 | return NULL; |
| 1079 | } | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1080 | CxJsonValue *ret = value->array.data[index]; |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1081 | cx_array_remove(value->array, index); |
| 845 | 1082 | return ret; |
| 1083 | } | |
| 1084 | ||
| 870 | 1085 | char *cxJsonAsString(const CxJsonValue *value) { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1086 | return value->string.ptr; |
| 870 | 1087 | } |
| 1088 | ||
| 1089 | cxstring cxJsonAsCxString(const CxJsonValue *value) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1090 | return cx_strcast(value->string); |
| 870 | 1091 | } |
| 1092 | ||
| 1093 | cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1094 | return value->string; |
| 870 | 1095 | } |
| 1096 | ||
| 1097 | double cxJsonAsDouble(const CxJsonValue *value) { | |
| 1098 | if (value->type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1099 | return (double) value->integer; |
| 870 | 1100 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1101 | return value->number; |
| 870 | 1102 | } |
| 1103 | } | |
| 1104 | ||
| 1105 | int64_t cxJsonAsInteger(const CxJsonValue *value) { | |
| 1106 | if (value->type == CX_JSON_INTEGER) { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1107 | return value->integer; |
| 870 | 1108 | } else { |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1109 | return (int64_t) value->number; |
| 870 | 1110 | } |
| 1111 | } | |
| 1112 | ||
| 440 | 1113 | CxIterator cxJsonArrIter(const CxJsonValue *value) { |
| 1016 | 1114 | return cx_array_iterator_ptr(value->array); |
| 440 | 1115 | } |
| 1116 | ||
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1117 | CxMapIterator cxJsonObjIter(const CxJsonValue *value) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1118 | return cxMapIterator(value->object); |
| 440 | 1119 | } |
| 1120 | ||
| 845 | 1121 | 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
|
1122 | CxJsonValue *v = cxMapGet(value->object, name); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1123 | if (v == NULL) { |
| 440 | 1124 | return &cx_json_value_nothing; |
| 1125 | } else { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1126 | return v; |
| 845 | 1127 | } |
| 1128 | } | |
| 1129 | ||
| 1130 | 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
|
1131 | CxJsonValue *v = NULL; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1132 | cxMapRemoveAndGet(value->object, name, &v); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1133 | return v; |
| 440 | 1134 | } |
| 1135 | ||
| 1136 | 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
|
1137 | 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
|
1138 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1139 | 6, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1140 | false, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1141 | 4, |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1142 | false |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1143 | }; |
| 440 | 1144 | } |
| 1145 | ||
| 1146 | CxJsonWriter cxJsonWriterPretty(bool use_spaces) { | |
| 1147 | return (CxJsonWriter) { | |
| 1148 | 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
|
1149 | 6, |
| 440 | 1150 | 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
|
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 |
| 440 | 1153 | }; |
| 1154 | } | |
| 1155 | ||
| 1156 | static int cx_json_writer_indent( | |
| 1157 | void *target, | |
| 1158 | cx_write_func wfunc, | |
| 1159 | const CxJsonWriter *settings, | |
| 1160 | unsigned int depth | |
| 1161 | ) { | |
| 1162 | if (depth == 0) return 0; | |
| 1163 | ||
| 1164 | // determine the width and characters to use | |
| 1165 | const char* indent; // for 32 prepared chars | |
| 1166 | size_t width = depth; | |
| 1167 | if (settings->indent_space) { | |
| 1168 | if (settings->indent == 0) return 0; | |
| 1169 | width *= settings->indent; | |
| 1170 | indent = " "; | |
| 1171 | } else { | |
| 1172 | 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"; | |
| 1173 | } | |
| 1174 | ||
| 1175 | // calculate the number of write calls and write | |
| 1176 | size_t full = width / 32; | |
| 1177 | size_t remaining = width % 32; | |
| 1178 | for (size_t i = 0; i < full; i++) { | |
| 1179 | if (32 != wfunc(indent, 1, 32, target)) return 1; | |
| 1180 | } | |
| 1181 | if (remaining != wfunc(indent, 1, remaining, target)) return 1; | |
| 1182 | ||
| 1183 | return 0; | |
| 1184 | } | |
| 1185 | ||
| 1186 | ||
| 1187 | int cx_json_write_rec( | |
| 1188 | void *target, | |
| 1189 | const CxJsonValue *value, | |
| 1190 | cx_write_func wfunc, | |
| 1191 | const CxJsonWriter *settings, | |
| 1192 | unsigned int depth | |
| 1193 | ) { | |
| 1194 | // keep track of written items | |
| 1195 | // the idea is to reduce the number of jumps for error checking | |
| 1196 | size_t actual = 0, expected = 0; | |
| 1197 | ||
| 1198 | // 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
|
1199 | char numbuf[40]; |
| 440 | 1200 | |
| 1201 | // recursively write the values | |
| 1202 | switch (value->type) { | |
| 1203 | case CX_JSON_OBJECT: { | |
| 1204 | const char *begin_obj = "{\n"; | |
| 1205 | if (settings->pretty) { | |
| 1206 | actual += wfunc(begin_obj, 1, 2, target); | |
| 1207 | expected += 2; | |
| 1208 | } else { | |
| 1209 | actual += wfunc(begin_obj, 1, 1, target); | |
| 1210 | expected++; | |
| 1211 | } | |
| 1212 | depth++; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1213 | CxMapIterator member_iter = cxJsonObjIter(value); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1214 | cx_foreach(const CxMapEntry *, member, member_iter) { |
| 440 | 1215 | // possible indentation |
| 1216 | if (settings->pretty) { | |
| 1217 | if (cx_json_writer_indent(target, wfunc, settings, depth)) { | |
| 1218 | return 1; // LCOV_EXCL_LINE | |
| 1219 | } | |
| 1220 | } | |
| 1221 | ||
| 1222 | // the name | |
| 1223 | actual += wfunc("\"", 1, 1, target); | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1224 | cxstring key = cx_hash_key_as_string(member->key); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1225 | 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
|
1226 | actual += wfunc(name.ptr, 1, name.length, target); |
| 440 | 1227 | actual += wfunc("\"", 1, 1, target); |
| 1228 | const char *obj_name_sep = ": "; | |
| 1229 | if (settings->pretty) { | |
| 1230 | actual += wfunc(obj_name_sep, 1, 2, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1231 | expected += 4 + name.length; |
| 440 | 1232 | } else { |
| 1233 | actual += wfunc(obj_name_sep, 1, 1, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1234 | expected += 3 + name.length; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1235 | } |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1236 | if (name.ptr != key.ptr) { |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1237 | cx_strfree(&name); |
| 440 | 1238 | } |
| 1239 | ||
| 1240 | // the value | |
| 1241 | if (cx_json_write_rec(target, member->value, wfunc, settings, depth)) return 1; | |
| 1242 | ||
| 1243 | // end of object-value | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1244 | if (member_iter.index < member_iter.elem_count - 1) { |
| 440 | 1245 | const char *obj_value_sep = ",\n"; |
| 1246 | if (settings->pretty) { | |
| 1247 | actual += wfunc(obj_value_sep, 1, 2, target); | |
| 1248 | expected += 2; | |
| 1249 | } else { | |
| 1250 | actual += wfunc(obj_value_sep, 1, 1, target); | |
| 1251 | expected++; | |
| 1252 | } | |
| 1253 | } else { | |
| 1254 | if (settings->pretty) { | |
| 1255 | actual += wfunc("\n", 1, 1, target); | |
| 1256 | expected ++; | |
| 1257 | } | |
| 1258 | } | |
| 1259 | } | |
| 1260 | depth--; | |
| 1261 | if (settings->pretty) { | |
| 1262 | if (cx_json_writer_indent(target, wfunc, settings, depth)) return 1; | |
| 1263 | } | |
| 1264 | actual += wfunc("}", 1, 1, target); | |
| 1265 | expected++; | |
| 1266 | break; | |
| 1267 | } | |
| 1268 | case CX_JSON_ARRAY: { | |
| 1269 | actual += wfunc("[", 1, 1, target); | |
| 1270 | expected++; | |
| 1271 | CxIterator iter = cxJsonArrIter(value); | |
| 1272 | cx_foreach(CxJsonValue*, element, iter) { | |
| 1273 | if (cx_json_write_rec( | |
| 1274 | target, element, | |
| 1275 | wfunc, settings, depth) | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1276 | ) { |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1277 | return 1; // LCOV_EXCL_LINE |
|
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
1278 | } |
| 440 | 1279 | |
| 1280 | if (iter.index < iter.elem_count - 1) { | |
| 1281 | const char *arr_value_sep = ", "; | |
| 1282 | if (settings->pretty) { | |
| 1283 | actual += wfunc(arr_value_sep, 1, 2, target); | |
| 1284 | expected += 2; | |
| 1285 | } else { | |
| 1286 | actual += wfunc(arr_value_sep, 1, 1, target); | |
| 1287 | expected++; | |
| 1288 | } | |
| 1289 | } | |
| 1290 | } | |
| 1291 | actual += wfunc("]", 1, 1, target); | |
| 1292 | expected++; | |
| 1293 | break; | |
| 1294 | } | |
| 1295 | case CX_JSON_STRING: { | |
| 1296 | actual += wfunc("\"", 1, 1, target); | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1297 | cxmutstr str = escape_string(cx_strcast(value->string), |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1298 | 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
|
1299 | actual += wfunc(str.ptr, 1, str.length, target); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1300 | actual += wfunc("\"", 1, 1, target); |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1301 | expected += 2 + str.length; |
|
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1302 | 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
|
1303 | 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
|
1304 | } |
| 440 | 1305 | break; |
| 1306 | } | |
| 1307 | 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
|
1308 | 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
|
1309 | // 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
|
1310 | // 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
|
1311 | precision = 1 + (precision > 15 ? 30 : 2 * precision); |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1312 | 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
|
1313 | 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
|
1314 | 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
|
1315 | // 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
|
1316 | 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
|
1317 | 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
|
1318 | 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
|
1319 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1320 | 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
|
1321 | // 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
|
1322 | // 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
|
1323 | 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
|
1324 | 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
|
1325 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1326 | // 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
|
1327 | // 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
|
1328 | // 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
|
1329 | 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
|
1330 | 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
|
1331 | 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
|
1332 | 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
|
1333 | 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
|
1334 | 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
|
1335 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1336 | // 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
|
1337 | 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
|
1338 | 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
|
1339 | expected++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1340 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1341 | dot++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1342 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1343 | // 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
|
1344 | 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
|
1345 | 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
|
1346 | // 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
|
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 | 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
|
1349 | 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
|
1350 | 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
|
1351 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1352 | 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
|
1353 | 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
|
1354 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1355 | } else { |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1356 | // 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
|
1357 | // 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
|
1358 | 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
|
1359 | 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
|
1360 | 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
|
1361 | 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
|
1362 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1363 | 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
|
1364 | 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
|
1365 | } |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1366 | 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
|
1367 | expected++; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1368 | exp++; |
|
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 = 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
|
1370 | 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
|
1371 | 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
|
1372 | } |
| 440 | 1373 | break; |
| 1374 | } | |
| 1375 | case CX_JSON_INTEGER: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1376 | snprintf(numbuf, 32, "%" PRIi64, value->integer); |
| 440 | 1377 | size_t len = strlen(numbuf); |
| 1378 | actual += wfunc(numbuf, 1, len, target); | |
| 1379 | expected += len; | |
| 1380 | break; | |
| 1381 | } | |
| 1382 | case CX_JSON_LITERAL: { | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1383 | if (value->literal == CX_JSON_TRUE) { |
| 440 | 1384 | actual += wfunc("true", 1, 4, target); |
| 1385 | expected += 4; | |
|
992
f421aef8f865
remove old UCX2 properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
943
diff
changeset
|
1386 | } else if (value->literal == CX_JSON_FALSE) { |
| 440 | 1387 | actual += wfunc("false", 1, 5, target); |
| 1388 | expected += 5; | |
| 1389 | } else { | |
| 1390 | actual += wfunc("null", 1, 4, target); | |
| 1391 | expected += 4; | |
| 1392 | } | |
| 1393 | break; | |
| 1394 | } | |
| 1395 | case CX_JSON_NOTHING: { | |
| 1396 | // deliberately supported as an empty string! | |
| 1397 | // users might want to just write the result | |
| 1398 | // of a get operation without testing the value | |
| 1399 | // and therefore this should not blow up | |
| 1400 | break; | |
| 1401 | } | |
| 1402 | default: assert(false); // LCOV_EXCL_LINE | |
| 1403 | } | |
| 1404 | ||
| 1405 | return expected != actual; | |
| 1406 | } | |
| 1407 | ||
| 1408 | int cxJsonWrite( | |
| 1409 | void *target, | |
| 1410 | const CxJsonValue *value, | |
| 1411 | cx_write_func wfunc, | |
| 1412 | const CxJsonWriter *settings | |
| 1413 | ) { | |
| 1414 | assert(target != NULL); | |
| 1415 | assert(value != NULL); | |
| 1416 | assert(wfunc != NULL); | |
| 1417 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
1418 | 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
|
1419 | 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
|
1420 | 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
|
1421 | } |
| 440 | 1422 | return cx_json_write_rec(target, value, wfunc, settings, 0); |
| 1423 | } | |
| 995 | 1424 | |
| 1425 | static cxmutstr cx_json_to_string(CxJsonValue *value, const CxAllocator *allocator, CxJsonWriter *writer) { | |
| 1426 | if (allocator == NULL) allocator = cxDefaultAllocator; | |
| 1427 | CxBuffer buffer; | |
| 1016 | 1428 | if (cxBufferInit(&buffer, allocator, NULL, 128, |
| 1429 | CX_BUFFER_AUTO_EXTEND | CX_BUFFER_DO_NOT_FREE)) { | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1430 | return CX_NULLSTR; // LCOV_EXCL_LINE |
| 995 | 1431 | } |
| 1432 | if (cx_json_write_rec(&buffer, value, cxBufferWriteFunc, writer, 0) | |
| 1433 | || cxBufferTerminate(&buffer)) { | |
| 1434 | // LCOV_EXCL_START | |
| 1435 | buffer.flags &= ~CX_BUFFER_DO_NOT_FREE; | |
| 1436 | cxBufferDestroy(&buffer); | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1437 | return CX_NULLSTR; |
| 995 | 1438 | // LCOV_EXCL_STOP |
| 1439 | } else { | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1440 | cxmutstr str = cx_bstr_m(&buffer); |
| 995 | 1441 | cxBufferDestroy(&buffer); |
| 1442 | return str; | |
| 1443 | } | |
| 1444 | ||
| 1445 | } | |
| 1446 | ||
| 1016 | 1447 | cxmutstr cxJsonToString(const CxAllocator *allocator, CxJsonValue *value) { |
| 995 | 1448 | CxJsonWriter writer = cxJsonWriterCompact(); |
| 1449 | return cx_json_to_string(value, allocator, &writer); | |
| 1450 | } | |
| 1451 | ||
| 1016 | 1452 | cxmutstr cxJsonToPrettyString(const CxAllocator *allocator, CxJsonValue *value) { |
| 995 | 1453 | CxJsonWriter writer = cxJsonWriterPretty(true); |
| 1454 | return cx_json_to_string(value, allocator, &writer); | |
| 1455 | } | |
| 1016 | 1456 | |
| 1457 | int cxJsonCompare(const CxJsonValue *json, const CxJsonValue *other) { | |
| 1458 | if (json == other) return 0; | |
| 1459 | if (json == NULL || other == NULL) return -1; | |
| 1460 | if (json->type != other->type) { | |
| 1461 | if (!cxJsonIsNumber(json)) return -1; | |
| 1462 | if (!cxJsonIsNumber(other)) return -1; | |
| 1463 | } | |
| 1464 | switch (json->type) { | |
| 1465 | case CX_JSON_NOTHING: | |
| 1466 | return 0; | |
| 1467 | case CX_JSON_OBJECT: | |
| 1468 | return cxMapCompare(json->object, other->object); | |
| 1469 | case CX_JSON_ARRAY: | |
| 1470 | if (json->array.size != other->array.size) return -1; | |
| 1471 | for (size_t i = 0; i < json->array.size; i++) { | |
| 1472 | const int d = cxJsonCompare(json->array.data[i], other->array.data[i]); | |
| 1473 | if (d != 0) return d; | |
| 1474 | } | |
| 1475 | return 0; | |
| 1476 | case CX_JSON_STRING: | |
| 1477 | return cx_strcmp(json->string, other->string); | |
| 1478 | case CX_JSON_INTEGER: | |
| 1479 | if (other->type == CX_JSON_INTEGER) { | |
| 1480 | return cx_vcmp_int64(json->integer, other->integer); | |
| 1481 | } else { | |
| 1482 | return cx_vcmp_double(cxJsonAsDouble(json), other->number); | |
| 1483 | } | |
| 1484 | case CX_JSON_NUMBER: | |
| 1485 | return cx_vcmp_double(json->number, cxJsonAsDouble(other)); | |
| 1486 | case CX_JSON_LITERAL: | |
| 1487 | return json->literal == other->literal ? 0 : -1; | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1488 | default: // LCOV_EXCL_START |
| 1016 | 1489 | // unreachable |
| 1490 | assert(false); | |
| 1491 | return -1; | |
| 1492 | // LCOV_EXCL_STOP | |
| 1493 | } | |
| 1494 | } | |
| 1495 | ||
| 1496 | CxJsonValue* cxJsonClone(const CxJsonValue* value, const CxAllocator* allocator) { | |
| 1497 | return cx_json_clone_func(NULL, value, allocator, NULL); | |
| 1498 | } | |
| 1499 | ||
| 1500 | CxJsonValue* cx_json_clone_func(CxJsonValue* target, const CxJsonValue* source, | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1501 | const CxAllocator* allocator, CX_UNUSED void *data) { |
| 1016 | 1502 | if (source == NULL || source->type == CX_JSON_NOTHING) { |
| 1503 | return &cx_json_value_nothing; | |
| 1504 | } | |
| 1505 | if (allocator == NULL) allocator = cxDefaultAllocator; | |
| 1506 | ||
| 1507 | #define return_value(v) { \ | |
| 1508 | CxJsonValue *ret = v; \ | |
| 1509 | if (target == NULL) { \ | |
| 1510 | return ret; \ | |
| 1511 | } else { \ | |
| 1512 | *target = *ret; \ | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1513 | ret->type = CX_JSON_UNINITIALIZED; \ |
|
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1514 | cxJsonValueFree(ret); \ |
| 1016 | 1515 | return target; \ |
| 1516 | } \ | |
| 1517 | } | |
| 1518 | ||
| 1519 | switch (source->type) { | |
| 1520 | case CX_JSON_OBJECT: { | |
| 1521 | CxJsonValue *obj = cxJsonCreateObj(allocator); | |
| 1522 | if (obj == NULL) return NULL; // LCOV_EXCL_LINE | |
| 1523 | if (cxMapClone(obj->object, source->object, cxJsonCloneFunc, allocator, NULL)) { | |
| 1524 | // LCOV_EXCL_START | |
| 1525 | cxJsonValueFree(obj); | |
| 1526 | return NULL; | |
| 1527 | // LCOV_EXCL_STOP | |
| 1528 | } | |
| 1529 | return_value(obj); | |
| 1530 | } | |
| 1531 | case CX_JSON_ARRAY: { | |
| 1532 | const size_t elem_count = source->array.size; | |
| 1533 | CxJsonValue *arr = cxJsonCreateArr(allocator, elem_count); | |
| 1534 | if (arr == NULL) return NULL; // LCOV_EXCL_LINE | |
| 1535 | arr->array.size = elem_count; | |
| 1536 | for (size_t i = 0 ; i < elem_count ; i++) { | |
| 1537 | CxJsonValue *e = cx_json_clone_func(NULL, source->array.data[i], allocator, NULL); | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1538 | if (e == NULL) { // LCOV_EXCL_START |
| 1016 | 1539 | cxJsonValueFree(arr); |
| 1540 | return NULL; | |
| 1541 | // LCOV_EXCL_STOP | |
| 1542 | } | |
| 1543 | arr->array.data[i] = e; | |
| 1544 | } | |
| 1545 | return_value(arr); | |
| 1546 | } | |
| 1547 | case CX_JSON_STRING: | |
| 1548 | return_value(cxJsonCreateString(allocator, source->string)); | |
| 1549 | case CX_JSON_INTEGER: | |
| 1550 | return_value(cxJsonCreateInteger(allocator, source->integer)); | |
| 1551 | case CX_JSON_NUMBER: | |
| 1552 | return_value(cxJsonCreateNumber(allocator, source->number)); | |
| 1553 | case CX_JSON_LITERAL: | |
| 1554 | return_value(cxJsonCreateLiteral(allocator, source->literal)); | |
|
1040
473d8cb58a6c
update ucx to version 4.0
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1035
diff
changeset
|
1555 | default: // LCOV_EXCL_START |
| 1016 | 1556 | // unreachable |
| 1557 | assert(false); | |
| 1558 | return NULL; | |
| 1559 | // LCOV_EXCL_STOP | |
| 1560 | } | |
| 1561 | #undef return_value | |
| 1562 | } |