source: mainline/uspace/app/bithenge/tree.c@ 11b9ad7

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

Bithenge: tree API and JSON/Python printing

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