source: mainline/uspace/lib/bithenge/src/print.c@ 35b8bfe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35b8bfe was 6cd10ac, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Bithenge: hide os.h from public include/

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[11b9ad7]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.
[842ed146]35 * @todo Allow more control over the printing style, and handle printing in
36 * limited space.
[11b9ad7]37 */
38
39#include <errno.h>
[6be4142]40#include <stdarg.h>
[11b9ad7]41#include <stdio.h>
[8fc0f47c]42#include <bithenge/blob.h>
43#include <bithenge/print.h>
44#include <bithenge/tree.h>
[6cd10ac]45#include "common.h"
[11b9ad7]46
47typedef struct {
48 bithenge_print_type_t type;
49 bool first;
[842ed146]50 int depth;
[6be4142]51 char *buffer;
52 size_t buffer_size;
[842ed146]53} state_t;
[11b9ad7]54
[6be4142]55static 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
[842ed146]74static int print_node(state_t *, bithenge_node_t *);
75
76static void newline(state_t *state)
77{
[6be4142]78 state_printf(state, "\n");
[842ed146]79 for (int i = 0; i < state->depth; i++) {
[6be4142]80 state_printf(state, " ");
[842ed146]81 }
82}
83
84static void increase_depth(state_t *state)
85{
86 state->depth++;
87}
88
89static void decrease_depth(state_t *state)
90{
91 state->depth--;
92}
93
94static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
[11b9ad7]95{
[842ed146]96 state_t *state = (state_t *)data;
[04a7435f]97 int rc = EOK;
[842ed146]98 if (!state->first)
[6be4142]99 state_printf(state, ",");
[842ed146]100 newline(state);
101 state->first = false;
102 bool add_quotes = state->type == BITHENGE_PRINT_JSON
[11b9ad7]103 && bithenge_node_type(key) != BITHENGE_NODE_STRING;
104 if (add_quotes)
[6be4142]105 state_printf(state, "\"");
[842ed146]106 rc = print_node(state, key);
[11b9ad7]107 if (rc != EOK)
[04a7435f]108 goto end;
[11b9ad7]109 if (add_quotes)
[6be4142]110 state_printf(state, "\"");
111 state_printf(state, ": ");
[842ed146]112 rc = print_node(state, value);
[11b9ad7]113 if (rc != EOK)
[04a7435f]114 goto end;
115end:
116 bithenge_node_dec_ref(key);
117 bithenge_node_dec_ref(value);
118 return rc;
[11b9ad7]119}
120
[842ed146]121static int print_internal(state_t *state, bithenge_node_t *node)
[11b9ad7]122{
123 int rc;
[6be4142]124 state_printf(state, "{");
[842ed146]125 increase_depth(state);
126 state->first = true;
127 rc = bithenge_node_for_each(node, print_internal_func, state);
[11b9ad7]128 if (rc != EOK)
129 return rc;
[842ed146]130 decrease_depth(state);
131 if (!state->first)
132 newline(state);
133 state->first = false;
[6be4142]134 state_printf(state, "}");
[11b9ad7]135 return EOK;
136}
137
[842ed146]138static int print_boolean(state_t *state, bithenge_node_t *node)
[11b9ad7]139{
140 bool value = bithenge_boolean_node_value(node);
[842ed146]141 switch (state->type) {
[11b9ad7]142 case BITHENGE_PRINT_PYTHON:
[6be4142]143 state_printf(state, value ? "True" : "False");
[11b9ad7]144 break;
145 case BITHENGE_PRINT_JSON:
[6be4142]146 state_printf(state, value ? "true" : "false");
[11b9ad7]147 break;
148 }
149 return EOK;
150}
151
[842ed146]152static int print_integer(state_t *state, bithenge_node_t *node)
[11b9ad7]153{
154 bithenge_int_t value = bithenge_integer_node_value(node);
[6be4142]155 state_printf(state, "%" BITHENGE_PRId, value);
[11b9ad7]156 return EOK;
157}
158
[842ed146]159static int print_string(state_t *state, bithenge_node_t *node)
[11b9ad7]160{
161 const char *value = bithenge_string_node_value(node);
[6be4142]162 state_printf(state, "\"");
[da0fef6]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;
[11b9ad7]168 if (ch == '"' || ch == '\\') {
[6be4142]169 state_printf(state, "\\%lc", (wint_t) ch);
[11b9ad7]170 } else if (ch <= 0x1f) {
[6be4142]171 state_printf(state, "\\u%04x", (unsigned int) ch);
[11b9ad7]172 } else {
[6be4142]173 state_printf(state, "%lc", (wint_t) ch);
[11b9ad7]174 }
175 }
[6be4142]176 state_printf(state, "\"");
[11b9ad7]177 return EOK;
178}
179
[842ed146]180static int print_blob(state_t *state, bithenge_node_t *node)
[5c925ce]181{
182 bithenge_blob_t *blob = bithenge_node_as_blob(node);
183 aoff64_t pos = 0;
[2988aec7]184 uint8_t buffer[1024];
[5c925ce]185 aoff64_t size = sizeof(buffer);
186 int rc;
[6be4142]187 state_printf(state,
188 state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
[5c925ce]189 do {
[2988aec7]190 rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
[5c925ce]191 if (rc != EOK)
192 return rc;
193 for (aoff64_t i = 0; i < size; i++)
[6be4142]194 state_printf(state, "\\x%02x",
[2988aec7]195 (unsigned int)buffer[i]);
[5c925ce]196 pos += size;
197 } while (size == sizeof(buffer));
[6be4142]198 state_printf(state, "\"");
[5c925ce]199 return EOK;
200}
201
[842ed146]202static int print_node(state_t *state, bithenge_node_t *tree)
[11b9ad7]203{
204 switch (bithenge_node_type(tree)) {
205 case BITHENGE_NODE_INTERNAL:
[842ed146]206 return print_internal(state, tree);
[11b9ad7]207 case BITHENGE_NODE_BOOLEAN:
[842ed146]208 return print_boolean(state, tree);
[11b9ad7]209 case BITHENGE_NODE_INTEGER:
[842ed146]210 return print_integer(state, tree);
[11b9ad7]211 case BITHENGE_NODE_STRING:
[842ed146]212 return print_string(state, tree);
[5c925ce]213 case BITHENGE_NODE_BLOB:
[842ed146]214 return print_blob(state, tree);
[11b9ad7]215 }
216 return ENOTSUP;
217}
[8375d0eb]218
[6be4142]219/** Print a tree as text to stdout.
[842ed146]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. */
223int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
224{
[6be4142]225 state_t state = {type, true, 0, NULL, 0};
[842ed146]226 return print_node(&state, tree);
227}
228
[6be4142]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. */
237int 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
[8375d0eb]247/** @}
248 */
Note: See TracBrowser for help on using the repository browser.