[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 |
|
---|
| 48 | typedef 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] | 56 | static 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 |
|
---|
[842ed146] | 75 | static int print_node(state_t *, bithenge_node_t *);
|
---|
| 76 |
|
---|
| 77 | static 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 |
|
---|
| 85 | static void increase_depth(state_t *state)
|
---|
| 86 | {
|
---|
| 87 | state->depth++;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | static void decrease_depth(state_t *state)
|
---|
| 91 | {
|
---|
| 92 | state->depth--;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data)
|
---|
[11b9ad7] | 96 | {
|
---|
[842ed146] | 97 | state_t *state = (state_t *)data;
|
---|
[04a7435f] | 98 | int 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;
|
---|
| 116 | end:
|
---|
| 117 | bithenge_node_dec_ref(key);
|
---|
| 118 | bithenge_node_dec_ref(value);
|
---|
| 119 | return rc;
|
---|
[11b9ad7] | 120 | }
|
---|
| 121 |
|
---|
[842ed146] | 122 | static int print_internal(state_t *state, bithenge_node_t *node)
|
---|
[11b9ad7] | 123 | {
|
---|
| 124 | int 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 |
|
---|
[842ed146] | 139 | static int 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 |
|
---|
[842ed146] | 153 | static int 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 |
|
---|
[842ed146] | 160 | static int 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, "\"");
|
---|
[da0fef6] | 164 | for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
|
---|
| 165 | wchar_t ch;
|
---|
| 166 | int rc = string_iterator_next(&i, &ch);
|
---|
| 167 | if (rc != EOK)
|
---|
| 168 | return rc;
|
---|
[11b9ad7] | 169 | if (ch == '"' || ch == '\\') {
|
---|
[6be4142] | 170 | state_printf(state, "\\%lc", (wint_t) ch);
|
---|
[11b9ad7] | 171 | } else if (ch <= 0x1f) {
|
---|
[6be4142] | 172 | state_printf(state, "\\u%04x", (unsigned int) ch);
|
---|
[11b9ad7] | 173 | } else {
|
---|
[6be4142] | 174 | state_printf(state, "%lc", (wint_t) ch);
|
---|
[11b9ad7] | 175 | }
|
---|
| 176 | }
|
---|
[6be4142] | 177 | state_printf(state, "\"");
|
---|
[11b9ad7] | 178 | return EOK;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[842ed146] | 181 | static int print_blob(state_t *state, bithenge_node_t *node)
|
---|
[5c925ce] | 182 | {
|
---|
| 183 | bithenge_blob_t *blob = bithenge_node_as_blob(node);
|
---|
| 184 | aoff64_t pos = 0;
|
---|
[2988aec7] | 185 | uint8_t buffer[1024];
|
---|
[5c925ce] | 186 | aoff64_t size = sizeof(buffer);
|
---|
| 187 | int rc;
|
---|
[6be4142] | 188 | state_printf(state,
|
---|
| 189 | state->type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
|
---|
[5c925ce] | 190 | do {
|
---|
[2988aec7] | 191 | rc = bithenge_blob_read(blob, pos, (char *)buffer, &size);
|
---|
[5c925ce] | 192 | if (rc != EOK)
|
---|
| 193 | return rc;
|
---|
| 194 | for (aoff64_t i = 0; i < size; i++)
|
---|
[6be4142] | 195 | state_printf(state, "\\x%02x",
|
---|
[2988aec7] | 196 | (unsigned int)buffer[i]);
|
---|
[5c925ce] | 197 | pos += size;
|
---|
| 198 | } while (size == sizeof(buffer));
|
---|
[6be4142] | 199 | state_printf(state, "\"");
|
---|
[5c925ce] | 200 | return EOK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[842ed146] | 203 | static int print_node(state_t *state, bithenge_node_t *tree)
|
---|
[11b9ad7] | 204 | {
|
---|
| 205 | switch (bithenge_node_type(tree)) {
|
---|
| 206 | case BITHENGE_NODE_INTERNAL:
|
---|
[842ed146] | 207 | return print_internal(state, tree);
|
---|
[11b9ad7] | 208 | case BITHENGE_NODE_BOOLEAN:
|
---|
[842ed146] | 209 | return print_boolean(state, tree);
|
---|
[11b9ad7] | 210 | case BITHENGE_NODE_INTEGER:
|
---|
[842ed146] | 211 | return print_integer(state, tree);
|
---|
[11b9ad7] | 212 | case BITHENGE_NODE_STRING:
|
---|
[842ed146] | 213 | return print_string(state, tree);
|
---|
[5c925ce] | 214 | case BITHENGE_NODE_BLOB:
|
---|
[842ed146] | 215 | return print_blob(state, tree);
|
---|
[11b9ad7] | 216 | }
|
---|
| 217 | return ENOTSUP;
|
---|
| 218 | }
|
---|
[8375d0eb] | 219 |
|
---|
[6be4142] | 220 | /** Print a tree as text to stdout.
|
---|
[842ed146] | 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. */
|
---|
| 224 | int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
| 225 | {
|
---|
[6be4142] | 226 | state_t state = {type, true, 0, NULL, 0};
|
---|
[842ed146] | 227 | return print_node(&state, tree);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[6be4142] | 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. */
|
---|
| 238 | int 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 | int rc = print_node(&state, tree);
|
---|
| 243 | *str = state.buffer;
|
---|
| 244 | *size = state.buffer_size;
|
---|
| 245 | return rc;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[8375d0eb] | 248 | /** @}
|
---|
| 249 | */
|
---|