source: mainline/uspace/lib/bithenge/src/print.c@ 850fd32

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 850fd32 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • 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 for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
165 wchar_t ch;
166 errno_t rc = string_iterator_next(&i, &ch);
167 if (rc != EOK)
168 return rc;
169 if (ch == '"' || ch == '\\') {
170 state_printf(state, "\\%lc", (wint_t) ch);
171 } else if (ch <= 0x1f) {
172 state_printf(state, "\\u%04x", (unsigned int) ch);
173 } else {
174 state_printf(state, "%lc", (wint_t) ch);
175 }
176 }
177 state_printf(state, "\"");
178 return EOK;
179}
180
181static errno_t print_blob(state_t *state, bithenge_node_t *node)
182{
183 bithenge_blob_t *blob = bithenge_node_as_blob(node);
184 aoff64_t pos = 0;
185 uint8_t buffer[1024];
186 aoff64_t size = sizeof(buffer);
187 errno_t rc;
188 state_printf(state,
189 state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
190 do {
191 rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
192 if (rc != EOK)
193 return rc;
194 for (aoff64_t i = 0; i < size; i++)
195 state_printf(state, "\\x%02x",
196 (unsigned int)buffer[i]);
197 pos += size;
198 } while (size == sizeof(buffer));
199 state_printf(state, "\"");
200 return EOK;
201}
202
203static errno_t print_node(state_t *state, bithenge_node_t *tree)
204{
205 switch (bithenge_node_type(tree)) {
206 case BITHENGE_NODE_INTERNAL:
207 return print_internal(state, tree);
208 case BITHENGE_NODE_BOOLEAN:
209 return print_boolean(state, tree);
210 case BITHENGE_NODE_INTEGER:
211 return print_integer(state, tree);
212 case BITHENGE_NODE_STRING:
213 return print_string(state, tree);
214 case BITHENGE_NODE_BLOB:
215 return print_blob(state, tree);
216 }
217 return ENOTSUP;
218}
219
220/** Print a tree as text to stdout.
221 * @param type The format to use.
222 * @param tree The root node of the tree to print.
223 * @return EOK on success or an error code from errno.h. */
224errno_t bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
225{
226 state_t state = {type, true, 0, NULL, 0};
227 return print_node(&state, tree);
228}
229
230/** Print a tree as text into a buffer.
231 * @param[in,out] str Holds a pointer to the buffer; changed to point to the
232 * null character.
233 * @param[in,out] size Holds the size of the buffer; changed to hold the
234 * remaining size.
235 * @param type The format to use.
236 * @param tree The root node of the tree to print.
237 * @return EOK on success or an error code from errno.h. */
238errno_t bithenge_print_node_to_string(char **str, size_t *size,
239 bithenge_print_type_t type, bithenge_node_t *tree)
240{
241 state_t state = {type, true, 0, *str, *size};
242 errno_t rc = print_node(&state, tree);
243 *str = state.buffer;
244 *size = state.buffer_size;
245 return rc;
246}
247
248/** @}
249 */
Note: See TracBrowser for help on using the repository browser.