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
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>
[16bfcd3]42#include <wchar.h>
[8fc0f47c]43#include <bithenge/blob.h>
44#include <bithenge/print.h>
45#include <bithenge/tree.h>
[6cd10ac]46#include "common.h"
[11b9ad7]47
48typedef struct {
49 bithenge_print_type_t type;
50 bool first;
[842ed146]51 int depth;
[6be4142]52 char *buffer;
53 size_t buffer_size;
[842ed146]54} state_t;
[11b9ad7]55
[6be4142]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
[b7fd2a0]75static errno_t print_node(state_t *, bithenge_node_t *);
[842ed146]76
77static void newline(state_t *state)
78{
[6be4142]79 state_printf(state, "\n");
[842ed146]80 for (int i = 0; i < state->depth; i++) {
[6be4142]81 state_printf(state, " ");
[842ed146]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
[b7fd2a0]95static errno_t print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
[11b9ad7]96{
[842ed146]97 state_t *state = (state_t *)data;
[b7fd2a0]98 errno_t rc = EOK;
[842ed146]99 if (!state->first)
[6be4142]100 state_printf(state, ",");
[842ed146]101 newline(state);
102 state->first = false;
103 bool add_quotes = state->type == BITHENGE_PRINT_JSON
[11b9ad7]104 && bithenge_node_type(key) != BITHENGE_NODE_STRING;
105 if (add_quotes)
[6be4142]106 state_printf(state, "\"");
[842ed146]107 rc = print_node(state, key);
[11b9ad7]108 if (rc != EOK)
[04a7435f]109 goto end;
[11b9ad7]110 if (add_quotes)
[6be4142]111 state_printf(state, "\"");
112 state_printf(state, ": ");
[842ed146]113 rc = print_node(state, value);
[11b9ad7]114 if (rc != EOK)
[04a7435f]115 goto end;
116end:
117 bithenge_node_dec_ref(key);
118 bithenge_node_dec_ref(value);
119 return rc;
[11b9ad7]120}
121
[b7fd2a0]122static errno_t print_internal(state_t *state, bithenge_node_t *node)
[11b9ad7]123{
[b7fd2a0]124 errno_t rc;
[6be4142]125 state_printf(state, "{");
[842ed146]126 increase_depth(state);
127 state->first = true;
128 rc = bithenge_node_for_each(node, print_internal_func, state);
[11b9ad7]129 if (rc != EOK)
130 return rc;
[842ed146]131 decrease_depth(state);
132 if (!state->first)
133 newline(state);
134 state->first = false;
[6be4142]135 state_printf(state, "}");
[11b9ad7]136 return EOK;
137}
138
[b7fd2a0]139static errno_t print_boolean(state_t *state, bithenge_node_t *node)
[11b9ad7]140{
141 bool value = bithenge_boolean_node_value(node);
[842ed146]142 switch (state->type) {
[11b9ad7]143 case BITHENGE_PRINT_PYTHON:
[6be4142]144 state_printf(state, value ? "True" : "False");
[11b9ad7]145 break;
146 case BITHENGE_PRINT_JSON:
[6be4142]147 state_printf(state, value ? "true" : "false");
[11b9ad7]148 break;
149 }
150 return EOK;
151}
152
[b7fd2a0]153static errno_t print_integer(state_t *state, bithenge_node_t *node)
[11b9ad7]154{
155 bithenge_int_t value = bithenge_integer_node_value(node);
[6be4142]156 state_printf(state, "%" BITHENGE_PRId, value);
[11b9ad7]157 return EOK;
158}
159
[b7fd2a0]160static errno_t print_string(state_t *state, bithenge_node_t *node)
[11b9ad7]161{
162 const char *value = bithenge_string_node_value(node);
[6be4142]163 state_printf(state, "\"");
[84239b1]164 string_iterator_t i = string_iterator(value);
165 while (!string_iterator_done(&i)) {
[da0fef6]166 wchar_t ch;
[b7fd2a0]167 errno_t rc = string_iterator_next(&i, &ch);
[da0fef6]168 if (rc != EOK)
169 return rc;
[11b9ad7]170 if (ch == '"' || ch == '\\') {
[6be4142]171 state_printf(state, "\\%lc", (wint_t) ch);
[11b9ad7]172 } else if (ch <= 0x1f) {
[6be4142]173 state_printf(state, "\\u%04x", (unsigned int) ch);
[11b9ad7]174 } else {
[6be4142]175 state_printf(state, "%lc", (wint_t) ch);
[11b9ad7]176 }
177 }
[6be4142]178 state_printf(state, "\"");
[11b9ad7]179 return EOK;
180}
181
[b7fd2a0]182static errno_t print_blob(state_t *state, bithenge_node_t *node)
[5c925ce]183{
184 bithenge_blob_t *blob = bithenge_node_as_blob(node);
185 aoff64_t pos = 0;
[2988aec7]186 uint8_t buffer[1024];
[5c925ce]187 aoff64_t size = sizeof(buffer);
[b7fd2a0]188 errno_t rc;
[6be4142]189 state_printf(state,
190 state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
[5c925ce]191 do {
[2988aec7]192 rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
[5c925ce]193 if (rc != EOK)
194 return rc;
195 for (aoff64_t i = 0; i < size; i++)
[6be4142]196 state_printf(state, "\\x%02x",
[2988aec7]197 (unsigned int)buffer[i]);
[5c925ce]198 pos += size;
199 } while (size == sizeof(buffer));
[6be4142]200 state_printf(state, "\"");
[5c925ce]201 return EOK;
202}
203
[b7fd2a0]204static errno_t print_node(state_t *state, bithenge_node_t *tree)
[11b9ad7]205{
206 switch (bithenge_node_type(tree)) {
207 case BITHENGE_NODE_INTERNAL:
[842ed146]208 return print_internal(state, tree);
[11b9ad7]209 case BITHENGE_NODE_BOOLEAN:
[842ed146]210 return print_boolean(state, tree);
[11b9ad7]211 case BITHENGE_NODE_INTEGER:
[842ed146]212 return print_integer(state, tree);
[11b9ad7]213 case BITHENGE_NODE_STRING:
[842ed146]214 return print_string(state, tree);
[5c925ce]215 case BITHENGE_NODE_BLOB:
[842ed146]216 return print_blob(state, tree);
[11b9ad7]217 }
218 return ENOTSUP;
219}
[8375d0eb]220
[6be4142]221/** Print a tree as text to stdout.
[842ed146]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. */
[b7fd2a0]225errno_t bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
[842ed146]226{
[6be4142]227 state_t state = {type, true, 0, NULL, 0};
[842ed146]228 return print_node(&state, tree);
229}
230
[6be4142]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. */
[b7fd2a0]239errno_t bithenge_print_node_to_string(char **str, size_t *size,
[6be4142]240 bithenge_print_type_t type, bithenge_node_t *tree)
241{
242 state_t state = {type, true, 0, *str, *size};
[b7fd2a0]243 errno_t rc = print_node(&state, tree);
[6be4142]244 *str = state.buffer;
245 *size = state.buffer_size;
246 return rc;
247}
248
[8375d0eb]249/** @}
250 */
Note: See TracBrowser for help on using the repository browser.