source: mainline/uspace/lib/bithenge/src/print.c@ 84239b1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 84239b1 was 84239b1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

And there was much fixing.

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