api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_node_str.c
index d53ea39..7c37220 100644 (file)
 EC_LOG_TYPE_REGISTER(node_str);
 
 struct ec_node_str {
-       struct ec_node gen;
        char *string;
        unsigned len;
 };
 
 static int
-ec_node_str_parse(const struct ec_node *gen_node,
-               struct ec_parse *state,
+ec_node_str_parse(const struct ec_node *node,
+               struct ec_pnode *pstate,
                const struct ec_strvec *strvec)
 {
-       struct ec_node_str *node = (struct ec_node_str *)gen_node;
+       struct ec_node_str *priv = ec_node_priv(node);
        const char *str;
 
-       (void)state;
+       (void)pstate;
 
-       if (node->string == NULL) {
+       if (priv->string == NULL) {
                errno = EINVAL;
                return -1;
        }
@@ -44,27 +43,29 @@ ec_node_str_parse(const struct ec_node *gen_node,
                return EC_PARSE_NOMATCH;
 
        str = ec_strvec_val(strvec, 0);
-       if (strcmp(str, node->string) != 0)
+       if (strcmp(str, priv->string) != 0)
                return EC_PARSE_NOMATCH;
 
        return 1;
 }
 
 static int
-ec_node_str_complete(const struct ec_node *gen_node,
+ec_node_str_complete(const struct ec_node *node,
                struct ec_comp *comp,
                const struct ec_strvec *strvec)
 {
-       struct ec_node_str *node = (struct ec_node_str *)gen_node;
+       struct ec_node_str *priv = ec_node_priv(node);
+       const struct ec_comp_item *item;
        const char *str;
        size_t n = 0;
 
        if (ec_strvec_len(strvec) != 1)
                return 0;
 
+       /* XXX startswith ? */
        str = ec_strvec_val(strvec, 0);
-       for (n = 0; n < node->len; n++) {
-               if (str[n] != node->string[n])
+       for (n = 0; n < priv->len; n++) {
+               if (str[n] != priv->string[n])
                        break;
        }
 
@@ -72,25 +73,25 @@ ec_node_str_complete(const struct ec_node *gen_node,
        if (str[n] != '\0')
                return EC_PARSE_NOMATCH;
 
-       if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL,
-                                       str, node->string) < 0)
+       item = ec_comp_add_item(comp, node, EC_COMP_FULL, str, priv->string);
+       if (item == NULL)
                return -1;
 
        return 0;
 }
 
-static const char *ec_node_str_desc(const struct ec_node *gen_node)
+static char *ec_node_str_desc(const struct ec_node *node)
 {
-       struct ec_node_str *node = (struct ec_node_str *)gen_node;
+       struct ec_node_str *priv = ec_node_priv(node);
 
-       return node->string;
+       return ec_strdup(priv->string);
 }
 
-static void ec_node_str_free_priv(struct ec_node *gen_node)
+static void ec_node_str_free_priv(struct ec_node *node)
 {
-       struct ec_node_str *node = (struct ec_node_str *)gen_node;
+       struct ec_node_str *priv = ec_node_priv(node);
 
-       ec_free(node->string);
+       ec_free(priv->string);
 }
 
 static const struct ec_config_schema ec_node_str_schema[] = {
@@ -104,10 +105,10 @@ static const struct ec_config_schema ec_node_str_schema[] = {
        },
 };
 
-static int ec_node_str_set_config(struct ec_node *gen_node,
+static int ec_node_str_set_config(struct ec_node *node,
                                const struct ec_config *config)
 {
-       struct ec_node_str *node = (struct ec_node_str *)gen_node;
+       struct ec_node_str *priv = ec_node_priv(node);
        const struct ec_config *value = NULL;
        char *s = NULL;
 
@@ -121,9 +122,9 @@ static int ec_node_str_set_config(struct ec_node *gen_node,
        if (s == NULL)
                goto fail;
 
-       ec_free(node->string);
-       node->string = s;
-       node->len = strlen(node->string);
+       ec_free(priv->string);
+       priv->string = s;
+       priv->len = strlen(priv->string);
 
        return 0;
 
@@ -145,12 +146,12 @@ static struct ec_node_type ec_node_str_type = {
 
 EC_NODE_TYPE_REGISTER(ec_node_str_type);
 
-int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
+int ec_node_str_set_str(struct ec_node *node, const char *str)
 {
        struct ec_config *config = NULL;
        int ret;
 
-       if (ec_node_check_type(gen_node, &ec_node_str_type) < 0)
+       if (ec_node_check_type(node, &ec_node_str_type) < 0)
                goto fail;
 
        if (str == NULL) {
@@ -166,7 +167,7 @@ int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
        if (ret < 0)
                goto fail;
 
-       ret = ec_node_set_config(gen_node, config);
+       ret = ec_node_set_config(node, config);
        config = NULL;
        if (ret < 0)
                goto fail;
@@ -180,19 +181,19 @@ fail:
 
 struct ec_node *ec_node_str(const char *id, const char *str)
 {
-       struct ec_node *gen_node = NULL;
+       struct ec_node *node = NULL;
 
-       gen_node = ec_node_from_type(&ec_node_str_type, id);
-       if (gen_node == NULL)
+       node = ec_node_from_type(&ec_node_str_type, id);
+       if (node == NULL)
                goto fail;
 
-       if (ec_node_str_set_str(gen_node, str) < 0)
+       if (ec_node_str_set_str(node, str) < 0)
                goto fail;
 
-       return gen_node;
+       return node;
 
 fail:
-       ec_node_free(gen_node);
+       ec_node_free(node);
        return NULL;
 }
 
@@ -201,14 +202,18 @@ static int ec_node_str_testcase(void)
 {
        struct ec_node *node;
        int testres = 0;
+       char *desc = NULL;
 
        node = ec_node_str(EC_NO_ID, "foo");
        if (node == NULL) {
                EC_LOG(EC_LOG_ERR, "cannot create node\n");
                return -1;
        }
-       testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
+       desc = ec_node_desc(node);
+       testres |= EC_TEST_CHECK(!strcmp(desc, "foo"),
                "Invalid node description.");
+       ec_free(desc);
+       desc = NULL;
        testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
        testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
        testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
@@ -246,20 +251,20 @@ static int ec_node_str_testcase(void)
                return -1;
        }
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST);
+               EC_VA_END,
+               EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "f", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "f", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "foo", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "foo", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "x", EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST);
+               "x", EC_VA_END,
+               EC_VA_END);
        ec_node_free(node);
 
        return testres;