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