source: mainline/uspace/lib/bithenge/src/print.c@ 8fc0f47c

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

Clean Bithenge headers namespace

Move headers into <bithenge/>, put sources into src/.

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