save
[protos/libecoli.git] / lib / ecoli_node_str.c
index 4a9ee82..483f63c 100644 (file)
@@ -39,6 +39,8 @@
 #include <ecoli_completed.h>
 #include <ecoli_node_str.h>
 
+EC_LOG_TYPE_REGISTER(node_str);
+
 struct ec_node_str {
        struct ec_node gen;
        char *string;
@@ -65,24 +67,22 @@ ec_node_str_parse(const struct ec_node *gen_node,
        return 1;
 }
 
-static struct ec_completed *
+static int
 ec_node_str_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_completed_item *item = NULL;
        struct ec_node_str *node = (struct ec_node_str *)gen_node;
-       struct ec_completed *completed;
        const char *str;
        size_t n = 0;
+       int ret;
 
-       (void)state;
-
-       completed = ec_completed();
-       if (completed == NULL)
-               return NULL;
+       (void)parsed;
 
        if (ec_strvec_len(strvec) != 1)
-               return completed;
+               return 0;
 
        str = ec_strvec_val(strvec, 0);
        for (n = 0; n < node->len; n++) {
@@ -90,20 +90,25 @@ ec_node_str_complete(const struct ec_node *gen_node,
                        break;
        }
 
-       if (str[n] != '\0') {
-               if (ec_completed_add_no_match(completed, state, gen_node) < 0) {
-                       ec_completed_free(completed);
-                       return NULL;
-               }
-       } else {
-               if (ec_completed_add_match(completed, state, gen_node,
-                                               node->string + n) < 0) {
-                       ec_completed_free(completed);
-                       return NULL;
-               }
+       /* no completion */
+       if (str[n] != '\0')
+               return 0; // XXX add a no_match instead?
+
+       item = ec_completed_item(parsed, gen_node);
+       if (item == NULL)
+               return -ENOMEM;
+       ret = ec_completed_item_set(item, EC_MATCH, node->string);
+       if (ret < 0) {
+               ec_completed_item_free(item);
+               return ret;
+       }
+       ret = ec_completed_item_add(completed, item);
+       if (ret < 0) {
+               ec_completed_item_free(item);
+               return ret;
        }
 
-       return completed;
+       return 0;
 }
 
 static const char *ec_node_str_desc(const struct ec_node *gen_node)
@@ -183,7 +188,7 @@ static int ec_node_str_testcase(void)
        /* XXX use EC_NO_ID instead of NULL */
        node = ec_node_str(NULL, "foo");
        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, 1, "foo");
@@ -195,7 +200,7 @@ static int ec_node_str_testcase(void)
 
        node = ec_node_str(NULL, "Здравствуйте");
        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, 1, "Здравствуйте");
@@ -208,7 +213,7 @@ static int ec_node_str_testcase(void)
        /* an empty string node always matches */
        node = ec_node_str(NULL, "");
        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, 1, "");
@@ -219,7 +224,7 @@ static int ec_node_str_testcase(void)
        /* test completion */
        node = ec_node_str(NULL, "foo");
        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,
@@ -232,12 +237,12 @@ static int ec_node_str_testcase(void)
                "foo");
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "f", EC_NODE_ENDLIST,
-               "oo", EC_NODE_ENDLIST,
-               "oo");
+               "foo", EC_NODE_ENDLIST,
+               "foo");
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "foo", EC_NODE_ENDLIST,
-               "", EC_NODE_ENDLIST,
-               "");
+               "foo", EC_NODE_ENDLIST,
+               "foo");
        ret |= EC_TEST_CHECK_COMPLETE(node,
                "x", EC_NODE_ENDLIST,
                EC_NODE_ENDLIST,