rename __ec_node
[protos/libecoli.git] / lib / ecoli_node_seq.c
index be03f31..fa9eaac 100644 (file)
@@ -18,6 +18,7 @@
 #include <ecoli_config.h>
 #include <ecoli_parse.h>
 #include <ecoli_complete.h>
+#include <ecoli_node_helper.h>
 #include <ecoli_node_str.h>
 #include <ecoli_node_option.h>
 #include <ecoli_node_or.h>
@@ -186,43 +187,25 @@ static int ec_node_seq_set_config(struct ec_node *gen_node,
                                const struct ec_config *config)
 {
        struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
-       const struct ec_config *children = NULL, *child;
        struct ec_node **table = NULL;
-       size_t n, i;
+       size_t i, len = 0;
 
-       children = ec_config_dict_get(config, "children");
-       if (children == NULL) {
-               errno = EINVAL;
-               goto fail;
-       }
-
-       n = 0;
-       TAILQ_FOREACH(child, &children->list, next)
-               n++;
-
-       table = ec_malloc(n * sizeof(*table));
+       table = ec_node_config_node_list_to_table(
+               ec_config_dict_get(config, "children"), &len);
        if (table == NULL)
                goto fail;
 
-       n = 0;
-       TAILQ_FOREACH(child, &children->list, next) {
-               table[n] = ec_node_clone(child->node);
-               n++;
-       }
-
        for (i = 0; i < node->len; i++)
                ec_node_free(node->table[i]);
        ec_free(node->table);
        node->table = table;
-       node->len = n;
+       node->len = len;
 
        return 0;
 
 fail:
-       if (table != NULL) {
-               for (i = 0; i < n; i++)
-                       ec_node_free(table[i]);
-       }
+       for (i = 0; i < len; i++)
+               ec_node_free(table[i]);
        ec_free(table);
        return -1;
 }
@@ -234,26 +217,20 @@ ec_node_seq_get_children_count(const struct ec_node *gen_node)
        return node->len;
 }
 
-static struct ec_node *
-ec_node_seq_get_child(const struct ec_node *gen_node, size_t i)
+static int
+ec_node_seq_get_child(const struct ec_node *gen_node, size_t i,
+               struct ec_node **child, unsigned int *refs)
 {
        struct ec_node_seq *node = (struct ec_node_seq *)gen_node;
 
        if (i >= node->len)
-               return NULL;
-
-       return node->table[i];
-}
-
-static unsigned int
-ec_node_seq_get_child_refs(const struct ec_node *gen_node, size_t i)
-{
-       (void)gen_node;
-       (void)i;
+               return -1;
 
+       *child = node->table[i];
        /* each child node is referenced twice: once in the config and
         * once in the node->table[] */
-       return 2;
+       *refs = 2;
+       return 0;
 }
 
 static struct ec_node_type ec_node_seq_type = {
@@ -267,7 +244,6 @@ static struct ec_node_type ec_node_seq_type = {
        .free_priv = ec_node_seq_free_priv,
        .get_children_count = ec_node_seq_get_children_count,
        .get_child = ec_node_seq_get_child,
-       .get_child_refs = ec_node_seq_get_child_refs,
 };
 
 EC_NODE_TYPE_REGISTER(ec_node_seq_type);
@@ -333,7 +309,7 @@ struct ec_node *__ec_node_seq(const char *id, ...)
        va_start(ap, id);
        child = va_arg(ap, struct ec_node *);
 
-       gen_node = __ec_node(&ec_node_seq_type, id);
+       gen_node = ec_node_from_type(&ec_node_seq_type, id);
        if (gen_node == NULL)
                goto fail_free_children;