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 | size_t off = 0;
|
---|
107 | const char *value = bithenge_string_node_value(node);
|
---|
108 | wchar_t ch;
|
---|
109 | printf("\"");
|
---|
110 | while ((ch = str_decode(value, &off, STR_NO_LIMIT)) != 0) {
|
---|
111 | if (ch == '"' || ch == '\\') {
|
---|
112 | printf("\\%lc", (wint_t) ch);
|
---|
113 | } else if (ch <= 0x1f) {
|
---|
114 | printf("\\u%04x", (unsigned int) ch);
|
---|
115 | } else {
|
---|
116 | printf("%lc", (wint_t) ch);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | printf("\"");
|
---|
120 | return EOK;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static int print_blob(bithenge_print_type_t type, bithenge_node_t *node)
|
---|
124 | {
|
---|
125 | bithenge_blob_t *blob = bithenge_node_as_blob(node);
|
---|
126 | aoff64_t pos = 0;
|
---|
127 | char buffer[1024];
|
---|
128 | aoff64_t size = sizeof(buffer);
|
---|
129 | int rc;
|
---|
130 | printf(type == BITHENGE_PRINT_PYTHON ? "b\"" : "\"");
|
---|
131 | do {
|
---|
132 | rc = bithenge_blob_read(blob, pos, buffer, &size);
|
---|
133 | if (rc != EOK)
|
---|
134 | return rc;
|
---|
135 | for (aoff64_t i = 0; i < size; i++)
|
---|
136 | printf("\\x%02x", buffer[i]);
|
---|
137 | pos += size;
|
---|
138 | } while (size == sizeof(buffer));
|
---|
139 | printf("\"");
|
---|
140 | return EOK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /** Print a tree as text.
|
---|
144 | * @param type The format to use.
|
---|
145 | * @param tree The root node of the tree to print.
|
---|
146 | * @return EOK on success or an error code from errno.h. */
|
---|
147 | int bithenge_print_node(bithenge_print_type_t type, bithenge_node_t *tree)
|
---|
148 | {
|
---|
149 | switch (bithenge_node_type(tree)) {
|
---|
150 | case BITHENGE_NODE_INTERNAL:
|
---|
151 | return print_internal(type, tree);
|
---|
152 | case BITHENGE_NODE_BOOLEAN:
|
---|
153 | return print_boolean(type, tree);
|
---|
154 | case BITHENGE_NODE_INTEGER:
|
---|
155 | return print_integer(type, tree);
|
---|
156 | case BITHENGE_NODE_STRING:
|
---|
157 | return print_string(type, tree);
|
---|
158 | case BITHENGE_NODE_BLOB:
|
---|
159 | return print_blob(type, tree);
|
---|
160 | }
|
---|
161 | return ENOTSUP;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /** @}
|
---|
165 | */
|
---|