source: mainline/uspace/app/bithenge/tree.c@ 5f679702

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

Bithenge: simplification

  • Property mode set to 100644
File size: 4.1 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 "tree.h"
40
41int bithenge_node_destroy(bithenge_node_t *node)
42{
43 switch (bithenge_node_type(node)) {
44 case BITHENGE_NODE_STRING:
45 if (node->string_value.needs_free)
46 free(node->string_value.ptr);
47 break;
48 case BITHENGE_NODE_INTERNAL:
49 /* TODO */
50 break;
51 case BITHENGE_NODE_BOOLEAN:
52 return EOK; // the boolean nodes are allocated statically below
53 case BITHENGE_NODE_INTEGER: /* pass-through */
54 case BITHENGE_NODE_NONE:
55 break;
56 }
57 free(node);
58 return EOK;
59}
60
61typedef struct
62{
63 bithenge_node_t base;
64 bithenge_node_t **nodes;
65 bithenge_int_t len;
66 bool needs_free;
67} simple_internal_node_t;
68
69static simple_internal_node_t *node_as_simple(bithenge_node_t *node)
70{
71 return (simple_internal_node_t *)node;
72}
73
74static int simple_internal_node_for_each(bithenge_node_t *base, bithenge_for_each_func_t func, void *data)
75{
76 int rc;
77 simple_internal_node_t *node = node_as_simple(base);
78 for (bithenge_int_t i = 0; i < node->len; i++) {
79 rc = func(node->nodes[2*i+0], node->nodes[2*i+1], data);
80 if (rc != EOK)
81 return rc;
82 }
83 return EOK;
84}
85
86static bithenge_internal_node_ops_t simple_internal_node_ops = {
87 .for_each = simple_internal_node_for_each
88};
89
90static bithenge_node_t *simple_internal_as_node(simple_internal_node_t *node)
91{
92 return &node->base;
93}
94
95int bithenge_new_simple_internal_node(bithenge_node_t **out, bithenge_node_t **nodes, bithenge_int_t len, bool needs_free)
96{
97 assert(out);
98 simple_internal_node_t *node = malloc(sizeof(*node));
99 if (!node)
100 return ENOMEM;
101 node->base.type = BITHENGE_NODE_INTERNAL;
102 node->base.internal_ops = &simple_internal_node_ops;
103 node->nodes = nodes;
104 node->len = len;
105 node->needs_free = needs_free;
106 *out = simple_internal_as_node(node);
107 return EOK;
108}
109
110static bithenge_node_t false_node = { BITHENGE_NODE_BOOLEAN, .boolean_value = false };
111static bithenge_node_t true_node = { BITHENGE_NODE_BOOLEAN, .boolean_value = true };
112
113int bithenge_new_boolean_node(bithenge_node_t **out, bool value)
114{
115 assert(out);
116 *out = value ? &true_node : &false_node;
117 return EOK;
118}
119
120int bithenge_new_integer_node(bithenge_node_t **out, bithenge_int_t value)
121{
122 assert(out);
123 bithenge_node_t *node = malloc(sizeof(*node));
124 if (!node)
125 return ENOMEM;
126 node->type = BITHENGE_NODE_INTEGER;
127 node->integer_value = value;
128 *out = node;
129 return EOK;
130}
131
132int bithenge_new_string_node(bithenge_node_t **out, const char *value, bool needs_free)
133{
134 assert(out);
135 bithenge_node_t *node = malloc(sizeof(*node));
136 if (!node)
137 return ENOMEM;
138 node->type = BITHENGE_NODE_STRING;
139 node->string_value.ptr = value;
140 node->string_value.needs_free = needs_free;
141 *out = node;
142 return EOK;
143}
Note: See TracBrowser for help on using the repository browser.