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 | #include "common.h"
|
---|
46 |
|
---|
47 | typedef struct {
|
---|
48 | bithenge_print_type_t type;
|
---|
49 | bool first;
|
---|
50 | int depth;
|
---|
51 | char *buffer;
|
---|
52 | size_t buffer_size;
|
---|
53 | } state_t;
|
---|
54 |
|
---|
55 | static void state_printf(state_t *state, const char *format, ...)
|
---|
56 | {
|
---|
57 | va_list ap;
|
---|
58 | va_start(ap, format);
|
---|
59 | if (state->buffer) {
|
---|
60 | int rc = vsnprintf(state->buffer, state->buffer_size, format,
|
---|
61 | ap);
|
---|
62 | if (rc > 0 && (size_t)rc >= state->buffer_size)
|
---|
63 | rc = state->buffer_size - 1;
|
---|
64 | if (rc > 0) {
|
---|
65 | state->buffer += rc;
|
---|
66 | state->buffer_size -= rc;
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | vprintf(format, ap);
|
---|
70 | }
|
---|
71 | va_end(ap);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static int print_node(state_t *, bithenge_node_t *);
|
---|
75 |
|
---|
76 | static void newline(state_t *state)
|
---|
77 | {
|
---|
78 | state_printf(state, "\n");
|
---|
79 | for (int i = 0; i < state->depth; i++) {
|
---|
80 | state_printf(state, " ");
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void increase_depth(state_t *state)
|
---|
85 | {
|
---|
86 | state->depth++;
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void decrease_depth(state_t *state)
|
---|
90 | {
|
---|
91 | state->depth--;
|
---|
92 | }
|
---|
93 |
|
---|
94 | static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
|
---|
95 | {
|
---|
96 | state_t *state = (state_t *)data;
|
---|
97 | int rc = EOK;
|
---|
98 | if (!state->first)
|
---|
99 | state_printf(state, ",");
|
---|
100 | newline(state);
|
---|
101 | state->first = false;
|
---|
102 | bool add_quotes = state->type == BITHENGE_PRINT_JSON
|
---|
103 | && bithenge_node_type(key) != BITHENGE_NODE_STRING;
|
---|
104 | if (add_quotes)
|
---|
105 | state_printf(state, "\"");
|
---|
106 | rc = print_node(state, key);
|
---|
107 | if (rc != EOK)
|
---|
108 | goto end;
|
---|
109 | if (add_quotes)
|
---|
110 | state_printf(state, "\"");
|
---|
111 | state_printf(state, ": ");
|
---|
112 | rc = print_node(state, value);
|
---|
113 | if (rc != EOK)
|
---|
114 | goto end;
|
---|
115 | end:
|
---|
116 | bithenge_node_dec_ref(key);
|
---|
117 | bithenge_node_dec_ref(value);
|
---|
118 | return rc;
|
---|
119 | }
|
---|
120 |
|
---|
121 | static int print_internal(state_t *state, bithenge_node_t *node)
|
---|
122 | {
|
---|
123 | int rc;
|
---|
124 | state_printf(state, "{");
|
---|
125 | increase_depth(state);
|
---|
126 | state->first = true;
|
---|
127 | rc = bithenge_node_for_each(node, print_internal_func, state);
|
---|
128 | if (rc != EOK)
|
---|
129 | return rc;
|
---|
130 | decrease_depth(state);
|
---|
131 | if (!state->first)
|
---|
132 | newline(state);
|
---|
133 | state->first = false;
|
---|
134 | state_printf(state, "}");
|
---|
135 | return EOK;
|
---|
136 | }
|
---|
137 |
|
---|
138 | static int print_boolean(state_t *state, bithenge_node_t *node)
|
---|
139 | {
|
---|
140 | bool value = bithenge_boolean_node_value(node);
|
---|
141 | switch (state->type) {
|
---|
142 | case BITHENGE_PRINT_PYTHON:
|
---|
143 | state_printf(state, value ? "True" : "False");
|
---|
144 | break;
|
---|
145 | case BITHENGE_PRINT_JSON:
|
---|
146 | state_printf(state, value ? "true" : "false");
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | return EOK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | static int print_integer(state_t *state, bithenge_node_t *node)
|
---|
153 | {
|
---|
154 | bithenge_int_t value = bithenge_integer_node_value(node);
|
---|
155 | state_printf(state, "%" BITHENGE_PRId, value);
|
---|
156 | return EOK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | static int print_string(state_t *state, bithenge_node_t *node)
|
---|
160 | {
|
---|
161 | const char *value = bithenge_string_node_value(node);
|
---|
162 | state_printf(state, "\"");
|
---|
163 | for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
|
---|
164 | wchar_t ch;
|
---|
165 | int rc = string_iterator_next(&i, &ch);
|
---|
166 | if (rc != EOK)
|
---|
167 | return rc;
|
---|
168 | if (ch == '"' || ch == '\\') {
|
---|
169 | state_printf(state, "\\%lc", (wint_t) ch);
|
---|
170 | } else if (ch <= 0x1f) {
|
---|
171 | state_printf(state, "\\u%04x", (unsigned int) ch);
|
---|
172 | } else {
|
---|
173 | state_printf(state, "%lc", (wint_t) ch);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | state_printf(state, "\"");
|
---|
177 | return EOK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | static int print_blob(state_t *state, bithenge_node_t *node)
|
---|
181 | {
|
---|
182 | bithenge_blob_t *blob = bithenge_node_as_blob(node);
|
---|
183 | aoff64_t pos = 0;
|
---|
184 | uint8_t buffer[1024];
|
---|
185 | aoff64_t size = sizeof(buffer);
|
---|
186 | int rc;
|
---|
187 | state_printf(state,
|
---|
188 | state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
|
---|
189 | do {
|
---|
190 | rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
|
---|
191 | if (rc != EOK)
|
---|
192 | return rc;
|
---|
193 | for (aoff64_t i = 0; i < size; i++)
|
---|
194 | state_printf(state, "\\x%02x",
|
---|
195 | (unsigned int)buffer[i]);
|
---|
196 | pos += size;
|
---|
197 | } while (size == sizeof(buffer));
|
---|
198 | state_printf(state, "\"");
|
---|
199 | return EOK;
|
---|
200 | }
|
---|
201 |
|
---|
202 | static int print_node(state_t *state, bithenge_node_t *tree)
|
---|
203 | {
|
---|
204 | switch (bithenge_node_type(tree)) {
|
---|
205 | case BITHENGE_NODE_INTERNAL:
|
---|
206 | return print_internal(state, tree);
|
---|
207 | case BITHENGE_NODE_BOOLEAN:
|
---|
208 | return print_boolean(state, tree);
|
---|
209 | case BITHENGE_NODE_INTEGER:
|
---|
210 | return print_integer(state, tree);
|
---|
211 | case BITHENGE_NODE_STRING:
|
---|
212 | return print_string(state, tree);
|
---|
213 | case BITHENGE_NODE_BLOB:
|
---|
214 | return print_blob(state, tree);
|
---|
215 | }
|
---|
216 | return ENOTSUP;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /** Print a tree as text to stdout.
|
---|
220 | * @param type The format to use.
|
---|
221 | * @param tree The root node of the tree to print.
|
---|
222 | * @return EOK on success or an error code from errno.h. */
|
---|
223 | int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
224 | {
|
---|
225 | state_t state = {type, true, 0, NULL, 0};
|
---|
226 | return print_node(&state, tree);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Print a tree as text into a buffer.
|
---|
230 | * @param[in,out] str Holds a pointer to the buffer; changed to point to the
|
---|
231 | * null character.
|
---|
232 | * @param[in,out] size Holds the size of the buffer; changed to hold the
|
---|
233 | * remaining size.
|
---|
234 | * @param type The format to use.
|
---|
235 | * @param tree The root node of the tree to print.
|
---|
236 | * @return EOK on success or an error code from errno.h. */
|
---|
237 | int bithenge_print_node_to_string(char **str, size_t *size,
|
---|
238 | bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
239 | {
|
---|
240 | state_t state = {type, true, 0, *str, *size};
|
---|
241 | int rc = print_node(&state, tree);
|
---|
242 | *str = state.buffer;
|
---|
243 | *size = state.buffer_size;
|
---|
244 | return rc;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** @}
|
---|
248 | */
|
---|