| 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 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include "blob.h"
|
|---|
| 40 | #include "print.h"
|
|---|
| 41 | #include "tree.h"
|
|---|
| 42 |
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | bithenge_print_type_t type;
|
|---|
| 45 | bool first;
|
|---|
| 46 | } print_internal_data_t;
|
|---|
| 47 |
|
|---|
| 48 | static int print_internal_func(bithenge_node_t *key, bithenge_node_t *value, void *data_)
|
|---|
| 49 | {
|
|---|
| 50 | print_internal_data_t *data = (print_internal_data_t *)data_;
|
|---|
| 51 | int rc;
|
|---|
| 52 | if (!data->first)
|
|---|
| 53 | printf(", ");
|
|---|
| 54 | data->first = false;
|
|---|
| 55 | bool add_quotes = data->type == BITHENGE_PRINT_JSON
|
|---|
| 56 | && bithenge_node_type(key) != BITHENGE_NODE_STRING;
|
|---|
| 57 | if (add_quotes)
|
|---|
| 58 | printf("\"");
|
|---|
| 59 | rc = bithenge_print_node(data->type, key);
|
|---|
| 60 | if (rc != EOK)
|
|---|
| 61 | return rc;
|
|---|
| 62 | if (add_quotes)
|
|---|
| 63 | printf("\"");
|
|---|
| 64 | printf(": ");
|
|---|
| 65 | rc = bithenge_print_node(data->type, value);
|
|---|
| 66 | if (rc != EOK)
|
|---|
| 67 | return rc;
|
|---|
| 68 | return EOK;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static int print_internal(bithenge_print_type_t type, bithenge_node_t *node)
|
|---|
| 72 | {
|
|---|
| 73 | int rc;
|
|---|
| 74 | print_internal_data_t data = { type, true };
|
|---|
| 75 | printf("{");
|
|---|
| 76 | rc = bithenge_node_for_each(node, print_internal_func, &data);
|
|---|
| 77 | if (rc != EOK)
|
|---|
| 78 | return rc;
|
|---|
| 79 | printf("}");
|
|---|
| 80 | return EOK;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static int print_boolean(bithenge_print_type_t type, bithenge_node_t *node)
|
|---|
| 84 | {
|
|---|
| 85 | bool value = bithenge_boolean_node_value(node);
|
|---|
| 86 | switch (type) {
|
|---|
| 87 | case BITHENGE_PRINT_PYTHON:
|
|---|
| 88 | printf(value ? "True" : "False");
|
|---|
| 89 | break;
|
|---|
| 90 | case BITHENGE_PRINT_JSON:
|
|---|
| 91 | printf(value ? "true" : "false");
|
|---|
| 92 | break;
|
|---|
| 93 | }
|
|---|
| 94 | return EOK;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | static int print_integer(bithenge_print_type_t type, bithenge_node_t *node)
|
|---|
| 98 | {
|
|---|
| 99 | bithenge_int_t value = bithenge_integer_node_value(node);
|
|---|
| 100 | printf("%" BITHENGE_PRId, value);
|
|---|
| 101 | return EOK;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | static int print_string(bithenge_print_type_t type, bithenge_node_t *node)
|
|---|
| 105 | {
|
|---|
| 106 | const char *value = bithenge_string_node_value(node);
|
|---|
| 107 | printf("\"");
|
|---|
| 108 | for (string_iterator_t i = string_iterator(value); !string_iterator_done(&i); ) {
|
|---|
| 109 | wchar_t ch;
|
|---|
| 110 | int rc = string_iterator_next(&i, &ch);
|
|---|
| 111 | if (rc != EOK)
|
|---|
| 112 | return rc;
|
|---|
| 113 | if (ch == '"' || ch == '\\') {
|
|---|
| 114 | printf("\\%lc", (wint_t) ch);
|
|---|
| 115 | } else if (ch <= 0x1f) {
|
|---|
| 116 | printf("\\u%04x", (unsigned int) ch);
|
|---|
| 117 | } else {
|
|---|
| 118 | printf("%lc", (wint_t) ch);
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | printf("\"");
|
|---|
| 122 | return EOK;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | static int print_blob(bithenge_print_type_t type, bithenge_node_t *node)
|
|---|
| 126 | {
|
|---|
| 127 | bithenge_blob_t *blob = bithenge_node_as_blob(node);
|
|---|
| 128 | aoff64_t pos = 0;
|
|---|
| 129 | char buffer[1024];
|
|---|
| 130 | aoff64_t size = sizeof(buffer);
|
|---|
| 131 | int rc;
|
|---|
| 132 | printf(type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
|
|---|
| 133 | do {
|
|---|
| 134 | rc = bithenge_blob_read(blob, pos, buffer, &size);
|
|---|
| 135 | if (rc != EOK)
|
|---|
| 136 | return rc;
|
|---|
| 137 | for (aoff64_t i = 0; i < size; i++)
|
|---|
| 138 | printf("\\x%02x", (unsigned int)(uint8_t)buffer[i]);
|
|---|
| 139 | pos += size;
|
|---|
| 140 | } while (size == sizeof(buffer));
|
|---|
| 141 | printf("\"");
|
|---|
| 142 | return EOK;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Print a tree as text.
|
|---|
| 146 | * @param type The format to use.
|
|---|
| 147 | * @param tree The root node of the tree to print.
|
|---|
| 148 | * @return EOK on success or an error code from errno.h. */
|
|---|
| 149 | int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
|
|---|
| 150 | {
|
|---|
| 151 | switch (bithenge_node_type(tree)) {
|
|---|
| 152 | case BITHENGE_NODE_INTERNAL:
|
|---|
| 153 | return print_internal(type, tree);
|
|---|
| 154 | case BITHENGE_NODE_BOOLEAN:
|
|---|
| 155 | return print_boolean(type, tree);
|
|---|
| 156 | case BITHENGE_NODE_INTEGER:
|
|---|
| 157 | return print_integer(type, tree);
|
|---|
| 158 | case BITHENGE_NODE_STRING:
|
|---|
| 159 | return print_string(type, tree);
|
|---|
| 160 | case BITHENGE_NODE_BLOB:
|
|---|
| 161 | return print_blob(type, tree);
|
|---|
| 162 | }
|
|---|
| 163 | return ENOTSUP;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** @}
|
|---|
| 167 | */
|
|---|