1 | /*
|
---|
2 | * Copyright (c) 2012 Sean Bartell
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup bithenge
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * Write a tree as JSON or other text formats.
|
---|
35 | * @todo Allow more control over the printing style, and handle printing in
|
---|
36 | * limited space.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <errno.h>
|
---|
40 | #include <stdarg.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <bithenge/blob.h>
|
---|
43 | #include <bithenge/print.h>
|
---|
44 | #include <bithenge/tree.h>
|
---|
45 |
|
---|
46 | typedef struct {
|
---|
47 | bithenge_print_type_t type;
|
---|
48 | bool first;
|
---|
49 | int depth;
|
---|
50 | char *buffer;
|
---|
51 | size_t buffer_size;
|
---|
52 | } state_t;
|
---|
53 |
|
---|
54 | static void state_printf(state_t *state, const char *format, ...)
|
---|
55 | {
|
---|
56 | va_list ap;
|
---|
57 | va_start(ap, format);
|
---|
58 | if (state->buffer) {
|
---|
59 | int rc = vsnprintf(state->buffer, state->buffer_size, format,
|
---|
60 | ap);
|
---|
61 | if (rc > 0 && (size_t)rc >= state->buffer_size)
|
---|
62 | rc = state->buffer_size - 1;
|
---|
63 | if (rc > 0) {
|
---|
64 | state->buffer += rc;
|
---|
65 | state->buffer_size -= rc;
|
---|
66 | }
|
---|
67 | } else {
|
---|
68 | vprintf(format, ap);
|
---|
69 | }
|
---|
70 | va_end(ap);
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int print_node(state_t *, bithenge_node_t *);
|
---|
74 |
|
---|
75 | static void newline(state_t *state)
|
---|
76 | {
|
---|
77 | state_printf(state, "\n");
|
---|
78 | for (int i = 0; i < state->depth; i++) {
|
---|
79 | state_printf(state, " ");
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void increase_depth(state_t *state)
|
---|
84 | {
|
---|
85 | state->depth++;
|
---|
86 | }
|
---|
87 |
|
---|
88 | static void decrease_depth(state_t *state)
|
---|
89 | {
|
---|
90 | state->depth--;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
|
---|
94 | {
|
---|
95 | state_t *state = (state_t *)data;
|
---|
96 | int rc = EOK;
|
---|
97 | if (!state->first)
|
---|
98 | state_printf(state, ",");
|
---|
99 | newline(state);
|
---|
100 | state->first = false;
|
---|
101 | bool add_quotes = state->type == BITHENGE_PRINT_JSON
|
---|
102 | && bithenge_node_type(key) != BITHENGE_NODE_STRING;
|
---|
103 | if (add_quotes)
|
---|
104 | state_printf(state, "\"");
|
---|
105 | rc = print_node(state, key);
|
---|
106 | if (rc != EOK)
|
---|
107 | goto end;
|
---|
108 | if (add_quotes)
|
---|
109 | state_printf(state, "\"");
|
---|
110 | state_printf(state, ": ");
|
---|
111 | rc = print_node(state, value);
|
---|
112 | if (rc != EOK)
|
---|
113 | goto end;
|
---|
114 | end:
|
---|
115 | bithenge_node_dec_ref(key);
|
---|
116 | bithenge_node_dec_ref(value);
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static int print_internal(state_t *state, bithenge_node_t *node)
|
---|
121 | {
|
---|
122 | int rc;
|
---|
123 | state_printf(state, "{");
|
---|
124 | increase_depth(state);
|
---|
125 | state->first = true;
|
---|
126 | rc = bithenge_node_for_each(node, print_internal_func, state);
|
---|
127 | if (rc != EOK)
|
---|
128 | return rc;
|
---|
129 | decrease_depth(state);
|
---|
130 | if (!state->first)
|
---|
131 | newline(state);
|
---|
132 | state->first = false;
|
---|
133 | state_printf(state, "}");
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int print_boolean(state_t *state, bithenge_node_t *node)
|
---|
138 | {
|
---|
139 | bool value = bithenge_boolean_node_value(node);
|
---|
140 | switch (state->type) {
|
---|
141 | case BITHENGE_PRINT_PYTHON:
|
---|
142 | state_printf(state, value ? "True" : "False");
|
---|
143 | break;
|
---|
144 | case BITHENGE_PRINT_JSON:
|
---|
145 | state_printf(state, value ? "true" : "false");
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | return EOK;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static int print_integer(state_t *state, bithenge_node_t *node)
|
---|
152 | {
|
---|
153 | bithenge_int_t value = bithenge_integer_node_value(node);
|
---|
154 | state_printf(state, "%" BITHENGE_PRId, value);
|
---|
155 | return EOK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | static int print_string(state_t *state, bithenge_node_t *node)
|
---|
159 | {
|
---|
160 | const char *value = bithenge_string_node_value(node);
|
---|
161 | state_printf(state, "\"");
|
---|
162 | for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
|
---|
163 | wchar_t ch;
|
---|
164 | int rc = string_iterator_next(&i, &ch);
|
---|
165 | if (rc != EOK)
|
---|
166 | return rc;
|
---|
167 | if (ch == '"' || ch == '\\') {
|
---|
168 | state_printf(state, "\\%lc", (wint_t) ch);
|
---|
169 | } else if (ch <= 0x1f) {
|
---|
170 | state_printf(state, "\\u%04x", (unsigned int) ch);
|
---|
171 | } else {
|
---|
172 | state_printf(state, "%lc", (wint_t) ch);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | state_printf(state, "\"");
|
---|
176 | return EOK;
|
---|
177 | }
|
---|
178 |
|
---|
179 | static int print_blob(state_t *state, bithenge_node_t *node)
|
---|
180 | {
|
---|
181 | bithenge_blob_t *blob = bithenge_node_as_blob(node);
|
---|
182 | aoff64_t pos = 0;
|
---|
183 | uint8_t buffer[1024];
|
---|
184 | aoff64_t size = sizeof(buffer);
|
---|
185 | int rc;
|
---|
186 | state_printf(state,
|
---|
187 | state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
|
---|
188 | do {
|
---|
189 | rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
|
---|
190 | if (rc != EOK)
|
---|
191 | return rc;
|
---|
192 | for (aoff64_t i = 0; i < size; i++)
|
---|
193 | state_printf(state, "\\x%02x",
|
---|
194 | (unsigned int)buffer[i]);
|
---|
195 | pos += size;
|
---|
196 | } while (size == sizeof(buffer));
|
---|
197 | state_printf(state, "\"");
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static int print_node(state_t *state, bithenge_node_t *tree)
|
---|
202 | {
|
---|
203 | switch (bithenge_node_type(tree)) {
|
---|
204 | case BITHENGE_NODE_INTERNAL:
|
---|
205 | return print_internal(state, tree);
|
---|
206 | case BITHENGE_NODE_BOOLEAN:
|
---|
207 | return print_boolean(state, tree);
|
---|
208 | case BITHENGE_NODE_INTEGER:
|
---|
209 | return print_integer(state, tree);
|
---|
210 | case BITHENGE_NODE_STRING:
|
---|
211 | return print_string(state, tree);
|
---|
212 | case BITHENGE_NODE_BLOB:
|
---|
213 | return print_blob(state, tree);
|
---|
214 | }
|
---|
215 | return ENOTSUP;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /** Print a tree as text to stdout.
|
---|
219 | * @param type The format to use.
|
---|
220 | * @param tree The root node of the tree to print.
|
---|
221 | * @return EOK on success or an error code from errno.h. */
|
---|
222 | int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
223 | {
|
---|
224 | state_t state = {type, true, 0, NULL, 0};
|
---|
225 | return print_node(&state, tree);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Print a tree as text into a buffer.
|
---|
229 | * @param[in,out] str Holds a pointer to the buffer; changed to point to the
|
---|
230 | * null character.
|
---|
231 | * @param[in,out] size Holds the size of the buffer; changed to hold the
|
---|
232 | * remaining size.
|
---|
233 | * @param type The format to use.
|
---|
234 | * @param tree The root node of the tree to print.
|
---|
235 | * @return EOK on success or an error code from errno.h. */
|
---|
236 | int bithenge_print_node_to_string(char **str, size_t *size,
|
---|
237 | bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
238 | {
|
---|
239 | state_t state = {type, true, 0, *str, *size};
|
---|
240 | int rc = print_node(&state, tree);
|
---|
241 | *str = state.buffer;
|
---|
242 | *size = state.buffer_size;
|
---|
243 | return rc;
|
---|
244 | }
|
---|
245 |
|
---|
246 | /** @}
|
---|
247 | */
|
---|