save
[protos/libecoli.git] / lib / ecoli_node_subset.c
index 6b5fee0..1803b4d 100644 (file)
@@ -44,6 +44,8 @@
 #include <ecoli_node_or.h>
 #include <ecoli_test.h>
 
+EC_LOG_TYPE_REGISTER(node_subset);
+
 struct ec_node_subset {
        struct ec_node gen;
        struct ec_node **table;
@@ -173,14 +175,13 @@ ec_node_subset_parse(const struct ec_node *gen_node,
        return ret;
 }
 
-static struct ec_completed *
+static int
 __ec_node_subset_complete(struct ec_node **table, size_t table_len,
-                       struct ec_parsed *state, const struct ec_strvec *strvec)
+                       struct ec_completed *completed,
+                       struct ec_parsed *parsed,
+                       const struct ec_strvec *strvec)
 {
-       struct ec_completed *completed = NULL;
-       struct ec_completed *child_completed = NULL;
        struct ec_strvec *childvec = NULL;
-       struct ec_parsed *parsed = NULL;
        struct ec_node *save;
        size_t i, len;
        int ret;
@@ -194,23 +195,15 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len,
         *   + __subset_complete([a, b], childvec) if c matches
         */
 
-       completed = ec_completed();
-       if (completed == NULL)
-               goto fail;
-
        /* first, try to complete with each node of the table */
        for (i = 0; i < table_len; i++) {
                if (table[i] == NULL)
                        continue;
 
-               child_completed = ec_node_complete_child(table[i],
-                       state, strvec);
-
-               if (child_completed == NULL)
+               ret = ec_node_complete_child(table[i],
+                                       completed, parsed, strvec);
+               if (ret < 0)
                        goto fail;
-
-               ec_completed_merge(completed, child_completed);
-               child_completed = NULL;
        }
 
        /* then, if a node matches, advance in strvec and try to complete with
@@ -219,7 +212,7 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len,
                if (table[i] == NULL)
                        continue;
 
-               ret = ec_node_parse_child(table[i], state, strvec);
+               ret = ec_node_parse_child(table[i], parsed, strvec);
                if (ret == EC_PARSED_NOMATCH)
                        continue;
                else if (ret < 0)
@@ -229,46 +222,39 @@ __ec_node_subset_complete(struct ec_node **table, size_t table_len,
                childvec = ec_strvec_ndup(strvec, len,
                                        ec_strvec_len(strvec) - len);
                if (childvec == NULL) {
-                       ec_parsed_del_last_child(state);
+                       ec_parsed_del_last_child(parsed);
                        goto fail;
                }
 
                save = table[i];
                table[i] = NULL;
-               child_completed = __ec_node_subset_complete(table, table_len,
-                                                       state, childvec);
+               ret = __ec_node_subset_complete(table, table_len,
+                                               completed, parsed, childvec);
                table[i] = save;
                ec_strvec_free(childvec);
                childvec = NULL;
-               ec_parsed_del_last_child(state);
+               ec_parsed_del_last_child(parsed);
 
-               if (child_completed == NULL)
+               if (ret < 0)
                        goto fail;
-
-               ec_completed_merge(completed, child_completed);
-               child_completed = NULL;
-
-               ec_parsed_free(parsed);
-               parsed = NULL;
        }
 
-       return completed;
-fail:
-       ec_parsed_free(parsed);
-       ec_completed_free(child_completed);
-       ec_completed_free(completed);
+       return 0;
 
-       return NULL;
+fail:
+       return -1;
 }
 
-static struct ec_completed *
+static int
 ec_node_subset_complete(const struct ec_node *gen_node,
-                       struct ec_parsed *state,
+                       struct ec_completed *completed,
+                       struct ec_parsed *parsed,
                        const struct ec_strvec *strvec)
 {
        struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
 
-       return __ec_node_subset_complete(node->table, node->len, state, strvec);
+       return __ec_node_subset_complete(node->table, node->len, completed,
+                                       parsed, strvec);
 }
 
 static void ec_node_subset_free_priv(struct ec_node *gen_node)
@@ -358,6 +344,7 @@ fail:
        return NULL;
 }
 
+/* LCOV_EXCL_START */
 static int ec_node_subset_testcase(void)
 {
        struct ec_node *node;
@@ -371,7 +358,7 @@ static int ec_node_subset_testcase(void)
                ec_node_str(NULL, "toto")
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
        ret |= EC_TEST_CHECK_PARSE(node, 0);
@@ -395,49 +382,44 @@ static int ec_node_subset_testcase(void)
                ec_node_str(NULL, "titi")
        );
        if (node == NULL) {
-               ec_log(EC_LOG_ERR, "cannot create node\n");
+               EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "", EC_NODE_ENDLIST,
-               "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST,
-               "");
+               "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
+       ret |= EC_TEST_CHECK_COMPLETE(node,
+               "", EC_NODE_ENDLIST,
+               "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "bar", "bar2", "", EC_NODE_ENDLIST,
-               "foo", "toto", "titi", EC_NODE_ENDLIST,
-               "");
+               "foo", "toto", "titi", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "f", EC_NODE_ENDLIST,
-               "oo", EC_NODE_ENDLIST,
-               "oo");
+               "foo", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "b", EC_NODE_ENDLIST,
-               "ar", "ar2", EC_NODE_ENDLIST,
-               "ar");
+               "bar", "bar2", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "bar", EC_NODE_ENDLIST,
-               "", "2", EC_NODE_ENDLIST,
-               "");
+               "bar", "bar2", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "bar", "b", EC_NODE_ENDLIST,
-               "ar2", EC_NODE_ENDLIST,
-               "ar2");
+               "bar2", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "t", EC_NODE_ENDLIST,
-               "oto", "iti", EC_NODE_ENDLIST,
-               "");
+               "toto", "titi", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "to", EC_NODE_ENDLIST,
-               "to", EC_NODE_ENDLIST,
-               "to");
+               "toto", EC_NODE_ENDLIST);
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "x", EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST,
-               "");
+               EC_NODE_ENDLIST);
        ec_node_free(node);
 
        return ret;
 }
+/* LCOV_EXCL_STOP */
 
 static struct ec_test ec_node_subset_test = {
        .name = "node_subset",