2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <ecoli_malloc.h>
36 #include <ecoli_string.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_keyval.h>
39 #include <ecoli_log.h>
40 #include <ecoli_node.h>
42 EC_LOG_TYPE_REGISTER(node);
44 static struct ec_node_type_list node_type_list =
45 TAILQ_HEAD_INITIALIZER(node_type_list);
47 struct ec_node_type *ec_node_type_lookup(const char *name)
49 struct ec_node_type *type;
51 TAILQ_FOREACH(type, &node_type_list, next) {
52 if (!strcmp(name, type->name))
59 int ec_node_type_register(struct ec_node_type *type)
61 if (ec_node_type_lookup(type->name) != NULL)
63 if (type->size < sizeof(struct ec_node))
66 TAILQ_INSERT_TAIL(&node_type_list, type, next);
71 void ec_node_type_dump(FILE *out)
73 struct ec_node_type *type;
75 TAILQ_FOREACH(type, &node_type_list, next)
76 fprintf(out, "%s\n", type->name);
79 struct ec_node *__ec_node(const struct ec_node_type *type, const char *id)
81 struct ec_node *node = NULL;
83 EC_LOG(EC_LOG_DEBUG, "create node type=%s id=%s\n",
90 node = ec_calloc(1, type->size);
97 node->id = ec_strdup(id);
101 if (ec_asprintf(&node->desc, "<%s>", type->name) < 0)
104 node->attrs = ec_keyval();
105 if (node->attrs == NULL)
108 if (type->init_priv != NULL) {
109 if (type->init_priv(node) < 0)
117 ec_keyval_free(node->attrs);
126 struct ec_node *ec_node(const char *typename, const char *id)
128 struct ec_node_type *type;
130 type = ec_node_type_lookup(typename);
132 EC_LOG(EC_LOG_ERR, "type=%s does not exist\n",
137 return __ec_node(type, id);
140 void ec_node_free(struct ec_node *node)
145 assert(node->refcnt > 0);
147 if (--node->refcnt > 0)
150 if (node->type != NULL && node->type->free_priv != NULL)
151 node->type->free_priv(node);
152 ec_free(node->children);
155 ec_free(node->attrs);
159 struct ec_node *ec_node_clone(struct ec_node *node)
166 size_t ec_node_get_children_count(const struct ec_node *node)
168 return node->n_children;
172 ec_node_get_child(const struct ec_node *node, size_t i)
174 if (i >= ec_node_get_children_count(node))
176 return node->children[i];
179 int ec_node_add_child(struct ec_node *node, struct ec_node *child)
181 struct ec_node **children = NULL;
184 if (node == NULL || child == NULL) {
189 n = node->n_children;
190 children = ec_realloc(node->children,
191 (n + 1) * sizeof(child));
192 if (children == NULL)
196 node->children = children;
197 node->n_children = n + 1;
206 int ec_node_del_child(struct ec_node *node, struct ec_node *child)
210 if (node == NULL || child == NULL)
213 n = node->n_children;
214 for (i = 0; i < n; i++) {
215 if (node->children[i] != child)
217 memcpy(&node->children[i], &node->children[i+1],
218 (n - i - 1) * sizeof(child));
227 struct ec_node *ec_node_find(struct ec_node *node, const char *id)
229 struct ec_node *child, *ret;
230 const char *node_id = ec_node_id(node);
233 if (id != NULL && node_id != NULL && !strcmp(node_id, id))
236 n = node->n_children;
237 for (i = 0; i < n; i++) {
238 child = node->children[i];
239 ret = ec_node_find(child, id);
247 struct ec_keyval *ec_node_attrs(const struct ec_node *node)
252 const char *ec_node_id(const struct ec_node *node)
254 if (node->id == NULL)
259 static void __ec_node_dump(FILE *out,
260 const struct ec_node *node, size_t indent)
262 const char *id, *typename;
263 struct ec_node *child;
266 id = ec_node_id(node);
267 typename = node->type->name;
269 fprintf(out, "%*s" "type=%s id=%s %p\n",
270 (int)indent * 4, "", typename, id, node);
271 n = node->n_children;
272 for (i = 0; i < n; i++) {
273 child = node->children[i];
274 __ec_node_dump(out, child, indent + 1);
278 void ec_node_dump(FILE *out, const struct ec_node *node)
280 fprintf(out, "------------------- node dump:\n");
283 fprintf(out, "node is NULL\n");
287 __ec_node_dump(out, node, 0);
290 const char *ec_node_desc(const struct ec_node *node)
292 if (node->type->desc != NULL)
293 return node->type->desc(node);
298 int ec_node_check_type(const struct ec_node *node,
299 const struct ec_node_type *type)
301 if (strcmp(node->type->name, type->name)) {