source: mainline/uspace/app/bithenge/tree.c@ 04a7435f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 04a7435f was 04a7435f, checked in by Sean Bartell <wingedtachikoma@…>, 13 years ago

Bithenge: add the struct transform

  • Property mode set to 100644
File size: 8.0 KB
Line 
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 * Trees and nodes.
35 */
36
37#include <errno.h>
38#include <stdlib.h>
39#include "blob.h"
40#include "os.h"
41#include "tree.h"
42
43static int blob_destroy(bithenge_node_t *base)
44{
45 bithenge_blob_t *blob = bithenge_node_as_blob(base);
46 assert(blob->base.blob_ops);
47 return blob->base.blob_ops->destroy(blob);
48}
49
50static int node_destroy(bithenge_node_t *node)
51{
52 switch (bithenge_node_type(node)) {
53 case BITHENGE_NODE_BLOB:
54 return blob_destroy(node);
55 case BITHENGE_NODE_STRING:
56 if (node->string_value.needs_free)
57 free((void *)node->string_value.ptr);
58 break;
59 case BITHENGE_NODE_INTERNAL:
60 return node->internal_ops->destroy(node);
61 case BITHENGE_NODE_BOOLEAN:
62 return EOK; // the boolean nodes are allocated statically below
63 case BITHENGE_NODE_INTEGER: /* pass-through */
64 break;
65 }
66 free(node);
67 return EOK;
68}
69
70/** Decrement a node's reference count and free it if appropriate.
71 * @memberof bithenge_node_t
72 * @param node The node to dereference, or NULL.
73 * @return EOK on success or an error code from errno.h. */
74int bithenge_node_dec_ref(bithenge_node_t *node)
75{
76 if (!node)
77 return EOK;
78 if (--node->refs == 0)
79 return node_destroy(node);
80 return EOK;
81}
82
83typedef struct
84{
85 bithenge_node_t base;
86 bithenge_node_t **nodes;
87 bithenge_int_t len;
88 bool needs_free;
89} simple_internal_node_t;
90
91static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
92{
93 return (simple_internal_node_t *)node;
94}
95
96static bithenge_node_t *simple_as_node(simple_internal_node_t *node)
97{
98 return &node->base;
99}
100
101static int simple_internal_node_for_each(bithenge_node_t *base,
102 bithenge_for_each_func_t func, void *data)
103{
104 int rc;
105 simple_internal_node_t *node = node_as_simple(base);
106 for (bithenge_int_t i = 0; i < node->len; i++) {
107 bithenge_node_inc_ref(node->nodes[2*i+0]);
108 bithenge_node_inc_ref(node->nodes[2*i+1]);
109 rc = func(node->nodes[2*i+0], node->nodes[2*i+1], data);
110 if (rc != EOK)
111 return rc;
112 }
113 return EOK;
114}
115
116static int simple_internal_node_destroy(bithenge_node_t *base)
117{
118 int rc;
119 simple_internal_node_t *node = node_as_simple(base);
120 for (bithenge_int_t i = 0; i < 2 * node->len; i++) {
121 rc = bithenge_node_dec_ref(node->nodes[i]);
122 if (rc != EOK)
123 return rc;
124 }
125 if (node->needs_free)
126 free(node->nodes);
127 free(node);
128 return EOK;
129}
130
131static bithenge_internal_node_ops_t simple_internal_node_ops = {
132 .for_each = simple_internal_node_for_each,
133 .destroy = simple_internal_node_destroy,
134};
135
136/** Initialize an internal node.
137 * @memberof bithenge_node_t
138 * @param[out] node The node.
139 * @param[in] ops The operations provided.
140 * @return EOK on success or an error code from errno.h. */
141int bithenge_init_internal_node(bithenge_node_t *node,
142 const bithenge_internal_node_ops_t *ops)
143{
144 node->type = BITHENGE_NODE_INTERNAL;
145 node->refs = 1;
146 node->internal_ops = ops;
147 return EOK;
148}
149
150/** Create an internal node from a set of keys and values. This function takes
151 * ownership of a reference to the key and value nodes, and optionally the
152 * array @a nodes.
153 * @memberof bithenge_node_t
154 * @param[out] out Stores the created internal node.
155 * @param nodes The array of key-value pairs. Keys are stored at even indices
156 * and values are stored at odd indices.
157 * @param len The number of key-value pairs in the node array.
158 * @param needs_free If true, when the internal node is destroyed it will free
159 * the nodes array rather than just dereferencing each node inside it.
160 * @return EOK on success or an error code from errno.h. */
161int bithenge_new_simple_internal_node(bithenge_node_t **out,
162 bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
163{
164 int rc;
165 assert(out);
166 simple_internal_node_t *node = malloc(sizeof(*node));
167 if (!node) {
168 rc = ENOMEM;
169 goto error;
170 }
171 rc = bithenge_init_internal_node(simple_as_node(node),
172 &simple_internal_node_ops);
173 if (rc != EOK)
174 goto error;
175 node->nodes = nodes;
176 node->len = len;
177 node->needs_free = needs_free;
178 *out = simple_as_node(node);
179 return EOK;
180error:
181 for (bithenge_int_t i = 0; i < 2 * len; i++)
182 bithenge_node_dec_ref(nodes[i]);
183 if (needs_free)
184 free(nodes);
185 free(node);
186 return rc;
187}
188
189static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = false };
190static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, 1, .boolean_value = true };
191
192/** Create a boolean node.
193 * @memberof bithenge_node_t
194 * @param[out] out Stores the created boolean node.
195 * @param value The value for the node to hold.
196 * @return EOK on success or an error code from errno.h. */
197int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
198{
199 assert(out);
200 *out = value ? &true_node : &false_node;
201 (*out)->refs++;
202 return EOK;
203}
204
205/** Create an integer node.
206 * @memberof bithenge_node_t
207 * @param[out] out Stores the created integer node.
208 * @param value The value for the node to hold.
209 * @return EOK on success or an error code from errno.h. */
210int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
211{
212 assert(out);
213 bithenge_node_t *node = malloc(sizeof(*node));
214 if (!node)
215 return ENOMEM;
216 node->type = BITHENGE_NODE_INTEGER;
217 node->refs = 1;
218 node->integer_value = value;
219 *out = node;
220 return EOK;
221}
222
223/** Create a string node.
224 * @memberof bithenge_node_t
225 * @param[out] out Stores the created string node. On error, this is unchanged.
226 * @param value The value for the node to hold.
227 * @param needs_free Whether the string should be freed when the node is
228 * destroyed.
229 * @return EOK on success or an error code from errno.h. */
230int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
231{
232 assert(out);
233 bithenge_node_t *node = malloc(sizeof(*node));
234 if (!node)
235 return ENOMEM;
236 node->type = BITHENGE_NODE_STRING;
237 node->refs = 1;
238 node->string_value.ptr = value;
239 node->string_value.needs_free = needs_free;
240 *out = node;
241 return EOK;
242}
243
244/** Check whether the contents of two nodes are equal. Does not yet work for
245 * internal nodes.
246 * @memberof bithenge_node_t
247 * @param a, b Nodes to compare.
248 * @return Whether the nodes are equal. If an error occurs, returns false.
249 * @todo Add support for internal nodes.
250 */
251bool bithenge_node_equal(bithenge_node_t *a, bithenge_node_t *b)
252{
253 if (a->type != b->type)
254 return false;
255 switch (a->type) {
256 case BITHENGE_NODE_INTERNAL:
257 return false;
258 case BITHENGE_NODE_BOOLEAN:
259 return a->boolean_value == b->boolean_value;
260 case BITHENGE_NODE_INTEGER:
261 return a->integer_value == b->integer_value;
262 case BITHENGE_NODE_STRING:
263 return !str_cmp(a->string_value.ptr, b->string_value.ptr);
264 case BITHENGE_NODE_BLOB:
265 return bithenge_blob_equal(bithenge_node_as_blob(a),
266 bithenge_node_as_blob(b));
267 }
268 return false;
269}
270
271/** @}
272 */
Note: See TracBrowser for help on using the repository browser.