source: mainline/uspace/app/bithenge/tree.c@ 600f5d1

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

Bithenge: add composition, ascii, and zero_terminated

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