get node children and refs in the same function
[protos/libecoli.git] / lib / ecoli_node_or.c
index b1af790..aeac206 100644 (file)
@@ -13,7 +13,7 @@
 #include <ecoli_log.h>
 #include <ecoli_strvec.h>
 #include <ecoli_node.h>
-#include <ecoli_parsed.h>
+#include <ecoli_parse.h>
 #include <ecoli_complete.h>
 #include <ecoli_node_or.h>
 #include <ecoli_node_str.h>
@@ -29,7 +29,7 @@ struct ec_node_or {
 
 static int
 ec_node_or_parse(const struct ec_node *gen_node,
-               struct ec_parsed *state,
+               struct ec_parse *state,
                const struct ec_strvec *strvec)
 {
        struct ec_node_or *node = (struct ec_node_or *)gen_node;
@@ -38,12 +38,12 @@ ec_node_or_parse(const struct ec_node *gen_node,
 
        for (i = 0; i < node->len; i++) {
                ret = ec_node_parse_child(node->table[i], state, strvec);
-               if (ret == EC_PARSED_NOMATCH)
+               if (ret == EC_PARSE_NOMATCH)
                        continue;
                return ret;
        }
 
-       return EC_PARSED_NOMATCH;
+       return EC_PARSE_NOMATCH;
 }
 
 static int
@@ -68,19 +68,42 @@ ec_node_or_complete(const struct ec_node *gen_node,
 static void ec_node_or_free_priv(struct ec_node *gen_node)
 {
        struct ec_node_or *node = (struct ec_node_or *)gen_node;
-       unsigned int i;
+       size_t i;
 
        for (i = 0; i < node->len; i++)
                ec_node_free(node->table[i]);
        ec_free(node->table);
 }
 
+static size_t
+ec_node_or_get_children_count(const struct ec_node *gen_node)
+{
+       struct ec_node_or *node = (struct ec_node_or *)gen_node;
+       return node->len;
+}
+
+static int
+ec_node_or_get_child(const struct ec_node *gen_node, size_t i,
+               struct ec_node **child, unsigned int *refs)
+{
+       struct ec_node_or *node = (struct ec_node_or *)gen_node;
+
+       if (i >= node->len)
+               return -1;
+
+       *child = node->table[i];
+       *refs = 1;
+       return 0;
+}
+
 static struct ec_node_type ec_node_or_type = {
        .name = "or",
        .parse = ec_node_or_parse,
        .complete = ec_node_or_complete,
        .size = sizeof(struct ec_node_or),
        .free_priv = ec_node_or_free_priv,
+       .get_children_count = ec_node_or_get_children_count,
+       .get_child = ec_node_or_get_child,
 };
 
 EC_NODE_TYPE_REGISTER(ec_node_or_type);
@@ -107,10 +130,6 @@ int ec_node_or_add(struct ec_node *gen_node, struct ec_node *child)
                goto fail;
 
        node->table = table;
-
-       if (ec_node_add_child(gen_node, child) < 0)
-               goto fail;
-
        table[node->len] = child;
        node->len++;