api documentation for ec_parse
[protos/libecoli.git] / src / ecoli_node_many.c
index dfdd866..5bfb92c 100644 (file)
 EC_LOG_TYPE_REGISTER(node_many);
 
 struct ec_node_many {
-       struct ec_node gen;
        unsigned int min;
        unsigned int max;
        struct ec_node *child;
 };
 
-static int ec_node_many_parse(const struct ec_node *gen_node,
-                       struct ec_parse *state,
+static int ec_node_many_parse(const struct ec_node *node,
+                       struct ec_pnode *pstate,
                        const struct ec_strvec *strvec)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
-       struct ec_parse *child_parse;
+       struct ec_node_many *priv = ec_node_priv(node);
+       struct ec_pnode *child_parse;
        struct ec_strvec *childvec = NULL;
        size_t off = 0, count;
        int ret;
 
-       for (count = 0; node->max == 0 || count < node->max; count++) {
+       for (count = 0; priv->max == 0 || count < priv->max; count++) {
                childvec = ec_strvec_ndup(strvec, off,
                        ec_strvec_len(strvec) - off);
                if (childvec == NULL)
                        goto fail;
 
-               ret = ec_node_parse_child(node->child, state, childvec);
+               ret = ec_parse_child(priv->child, pstate, childvec);
                if (ret < 0)
                        goto fail;
 
@@ -58,17 +57,17 @@ static int ec_node_many_parse(const struct ec_node *gen_node,
 
                /* it matches an empty strvec, no need to continue */
                if (ret == 0) {
-                       child_parse = ec_parse_get_last_child(state);
-                       ec_parse_unlink_child(state, child_parse);
-                       ec_parse_free(child_parse);
+                       child_parse = ec_pnode_get_last_child(pstate);
+                       ec_pnode_unlink_child(child_parse);
+                       ec_pnode_free(child_parse);
                        break;
                }
 
                off += ret;
        }
 
-       if (count < node->min) {
-               ec_parse_free_children(state);
+       if (count < priv->min) {
+               ec_pnode_free_children(pstate);
                return EC_PARSE_NOMATCH;
        }
 
@@ -80,17 +79,17 @@ fail:
 }
 
 static int
-__ec_node_many_complete(struct ec_node_many *node, unsigned int max,
+__ec_node_many_complete(struct ec_node_many *priv, unsigned int max,
                        struct ec_comp *comp,
                        const struct ec_strvec *strvec)
 {
-       struct ec_parse *parse = ec_comp_get_state(comp);
+       struct ec_pnode *parse = ec_comp_get_cur_pstate(comp);
        struct ec_strvec *childvec = NULL;
        unsigned int i;
        int ret;
 
        /* first, try to complete with the child node */
-       ret = ec_node_complete_child(node->child, comp, strvec);
+       ret = ec_complete_child(priv->child, comp, strvec);
        if (ret < 0)
                goto fail;
 
@@ -109,7 +108,7 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
                if (childvec == NULL)
                        goto fail;
 
-               ret = ec_node_parse_child(node->child, parse, childvec);
+               ret = ec_parse_child(priv->child, parse, childvec);
                if (ret < 0)
                        goto fail;
 
@@ -118,18 +117,18 @@ __ec_node_many_complete(struct ec_node_many *node, unsigned int max,
 
                if ((unsigned int)ret != i) {
                        if (ret != EC_PARSE_NOMATCH)
-                               ec_parse_del_last_child(parse);
+                               ec_pnode_del_last_child(parse);
                        continue;
                }
 
                childvec = ec_strvec_ndup(strvec, i, ec_strvec_len(strvec) - i);
                if (childvec == NULL) {
-                       ec_parse_del_last_child(parse);
+                       ec_pnode_del_last_child(parse);
                        goto fail;
                }
 
-               ret = __ec_node_many_complete(node, max, comp, childvec);
-               ec_parse_del_last_child(parse);
+               ret = __ec_node_many_complete(priv, max, comp, childvec);
+               ec_pnode_del_last_child(parse);
                ec_strvec_free(childvec);
                childvec = NULL;
 
@@ -145,43 +144,43 @@ fail:
 }
 
 static int
-ec_node_many_complete(const struct ec_node *gen_node,
+ec_node_many_complete(const struct ec_node *node,
                struct ec_comp *comp,
                const struct ec_strvec *strvec)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
+       struct ec_node_many *priv = ec_node_priv(node);
 
-       return __ec_node_many_complete(node, node->max, comp,
+       return __ec_node_many_complete(priv, priv->max, comp,
                                strvec);
 }
 
-static void ec_node_many_free_priv(struct ec_node *gen_node)
+static void ec_node_many_free_priv(struct ec_node *node)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
+       struct ec_node_many *priv = ec_node_priv(node);
 
-       ec_node_free(node->child);
+       ec_node_free(priv->child);
 }
 
 static size_t
-ec_node_many_get_children_count(const struct ec_node *gen_node)
+ec_node_many_get_children_count(const struct ec_node *node)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
+       struct ec_node_many *priv = ec_node_priv(node);
 
-       if (node->child)
+       if (priv->child)
                return 1;
        return 0;
 }
 
 static int
-ec_node_many_get_child(const struct ec_node *gen_node, size_t i,
+ec_node_many_get_child(const struct ec_node *node, size_t i,
                struct ec_node **child, unsigned int *refs)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
+       struct ec_node_many *priv = ec_node_priv(node);
 
        if (i >= 1)
                return -1;
 
-       *child = node->child;
+       *child = priv->child;
        *refs = 2;
        return 0;
 }
@@ -208,10 +207,10 @@ static const struct ec_config_schema ec_node_many_schema[] = {
        },
 };
 
-static int ec_node_many_set_config(struct ec_node *gen_node,
+static int ec_node_many_set_config(struct ec_node *node,
                                const struct ec_config *config)
 {
-       struct ec_node_many *node = (struct ec_node_many *)gen_node;
+       struct ec_node_many *priv = ec_node_priv(node);
        const struct ec_config *child, *min, *max;
 
        child = ec_config_dict_get(config, "child");
@@ -234,17 +233,17 @@ static int ec_node_many_set_config(struct ec_node *gen_node,
                goto fail;
        }
 
-       if (node->child != NULL)
-               ec_node_free(node->child);
-       node->child = ec_node_clone(child->node);
+       if (priv->child != NULL)
+               ec_node_free(priv->child);
+       priv->child = ec_node_clone(child->node);
        if (min == NULL)
-               node->min = 0;
+               priv->min = 0;
        else
-               node->min = min->u64;
+               priv->min = min->u64;
        if (max == NULL)
-               node->max = 0;
+               priv->max = 0;
        else
-               node->max = max->u64;
+               priv->max = max->u64;
 
        return 0;
 
@@ -267,17 +266,17 @@ static struct ec_node_type ec_node_many_type = {
 EC_NODE_TYPE_REGISTER(ec_node_many_type);
 
 int
-ec_node_many_set_params(struct ec_node *gen_node, struct ec_node *child,
+ec_node_many_set_params(struct ec_node *node, struct ec_node *child,
        unsigned int min, unsigned int max)
 {
        const struct ec_config *cur_config = NULL;
        struct ec_config *config = NULL;
        int ret;
 
-       if (ec_node_check_type(gen_node, &ec_node_many_type) < 0)
+       if (ec_node_check_type(node, &ec_node_many_type) < 0)
                goto fail;
 
-       cur_config = ec_node_get_config(gen_node);
+       cur_config = ec_node_get_config(node);
        if (cur_config == NULL)
                config = ec_config_dict();
        else
@@ -296,7 +295,7 @@ ec_node_many_set_params(struct ec_node *gen_node, struct ec_node *child,
        if (ec_config_dict_set(config, "max", ec_config_u64(max)) < 0)
                goto fail;
 
-       ret = ec_node_set_config(gen_node, config);
+       ret = ec_node_set_config(node, config);
        config = NULL; /* freed */
        if (ret < 0)
                goto fail;
@@ -312,24 +311,25 @@ fail:
 struct ec_node *ec_node_many(const char *id, struct ec_node *child,
        unsigned int min, unsigned int max)
 {
-       struct ec_node *gen_node = NULL;
+       struct ec_node *node = NULL;
 
        if (child == NULL)
                return NULL;
 
-       gen_node = ec_node_from_type(&ec_node_many_type, id);
-       if (gen_node == NULL)
+       node = ec_node_from_type(&ec_node_many_type, id);
+       if (node == NULL)
                goto fail;
 
-       if (ec_node_many_set_params(gen_node, child, min, max) < 0) {
+       if (ec_node_many_set_params(node, child, min, max) < 0) {
                child = NULL;
                goto fail;
        }
        child = NULL;
 
-       return gen_node;
+       return node;
 
 fail:
+       ec_node_free(node);
        ec_node_free(child);
        return NULL;
 }
@@ -382,26 +382,26 @@ static int ec_node_many_testcase(void)
                return -1;
        }
        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,
-               "foo", "", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "foo", "", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "foo", "foo", "", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "foo", "foo", "", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "foo", "foo", "foo", "", EC_NODE_ENDLIST,
-               "foo", EC_NODE_ENDLIST);
+               "foo", "foo", "foo", "", EC_VA_END,
+               "foo", EC_VA_END);
        testres |= EC_TEST_CHECK_COMPLETE(node,
-               "foo", "foo", "foo", "foo", "", EC_NODE_ENDLIST,
-               EC_NODE_ENDLIST);
+               "foo", "foo", "foo", "foo", "", EC_VA_END,
+               EC_VA_END);
        ec_node_free(node);
 
        return testres;