EC_LOG_TYPE_REGISTER(node_subset);
struct ec_node_subset {
- struct ec_node gen;
struct ec_node **table;
unsigned int len;
};
* updated accordingly. */
static int
__ec_node_subset_parse(struct parse_result *out, struct ec_node **table,
- size_t table_len, struct ec_parse *state,
+ size_t table_len, struct ec_pnode *pstate,
const struct ec_strvec *strvec)
{
struct ec_node **child_table;
struct ec_strvec *childvec = NULL;
size_t i, j, len = 0;
struct parse_result best_result, result;
- struct ec_parse *best_parse = NULL;
+ struct ec_pnode *best_parse = NULL;
int ret;
if (table_len == 0)
for (i = 0; i < table_len; i++) {
/* try to parse elt i */
- ret = ec_node_parse_child(table[i], state, strvec);
+ ret = ec_parse_child(table[i], pstate, strvec);
if (ret < 0)
goto fail;
memset(&result, 0, sizeof(result));
ret = __ec_node_subset_parse(&result, child_table,
- table_len - 1, state, childvec);
+ table_len - 1, pstate, childvec);
ec_strvec_free(childvec);
childvec = NULL;
if (ret < 0)
/* if result is not the best, ignore */
if (result.parse_len < best_result.parse_len) {
memset(&result, 0, sizeof(result));
- ec_parse_del_last_child(state);
+ ec_pnode_del_last_child(pstate);
continue;
}
/* replace the previous best result */
- ec_parse_free(best_parse);
- best_parse = ec_parse_get_last_child(state);
- ec_parse_unlink_child(state, best_parse);
+ ec_pnode_free(best_parse);
+ best_parse = ec_pnode_get_last_child(pstate);
+ ec_pnode_unlink_child(best_parse);
best_result.parse_len = result.parse_len + 1;
best_result.len = len + result.len;
*out = best_result;
ec_free(child_table);
if (best_parse != NULL)
- ec_parse_link_child(state, best_parse);
+ ec_pnode_link_child(pstate, best_parse);
return 0;
fail:
- ec_parse_free(best_parse);
+ ec_pnode_free(best_parse);
ec_strvec_free(childvec);
ec_free(child_table);
return -1;
}
static int
-ec_node_subset_parse(const struct ec_node *gen_node,
- struct ec_parse *state,
+ec_node_subset_parse(const struct ec_node *node,
+ struct ec_pnode *pstate,
const struct ec_strvec *strvec)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
- struct ec_parse *parse = NULL;
+ struct ec_node_subset *priv = ec_node_priv(node);
+ struct ec_pnode *parse = NULL;
struct parse_result result;
int ret;
memset(&result, 0, sizeof(result));
- ret = __ec_node_subset_parse(&result, node->table,
- node->len, state, strvec);
+ ret = __ec_node_subset_parse(&result, priv->table,
+ priv->len, pstate, strvec);
if (ret < 0)
goto fail;
return result.len;
fail:
- ec_parse_free(parse);
+ ec_pnode_free(parse);
return ret;
}
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;
struct ec_node *save;
size_t i, len;
if (table[i] == NULL)
continue;
- ret = ec_node_complete_child(table[i],
+ ret = ec_complete_child(table[i],
comp, strvec);
if (ret < 0)
goto fail;
if (table[i] == NULL)
continue;
- ret = ec_node_parse_child(table[i], parse, strvec);
+ ret = ec_parse_child(table[i], parse, strvec);
if (ret < 0)
goto fail;
childvec = ec_strvec_ndup(strvec, len,
ec_strvec_len(strvec) - len);
if (childvec == NULL) {
- ec_parse_del_last_child(parse);
+ ec_pnode_del_last_child(parse);
goto fail;
}
table[i] = save;
ec_strvec_free(childvec);
childvec = NULL;
- ec_parse_del_last_child(parse);
+ ec_pnode_del_last_child(parse);
if (ret < 0)
goto fail;
}
static int
-ec_node_subset_complete(const struct ec_node *gen_node,
+ec_node_subset_complete(const struct ec_node *node,
struct ec_comp *comp,
const struct ec_strvec *strvec)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
+ struct ec_node_subset *priv = ec_node_priv(node);
- return __ec_node_subset_complete(node->table, node->len, comp,
+ return __ec_node_subset_complete(priv->table, priv->len, comp,
strvec);
}
-static void ec_node_subset_free_priv(struct ec_node *gen_node)
+static void ec_node_subset_free_priv(struct ec_node *node)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
+ struct ec_node_subset *priv = ec_node_priv(node);
size_t i;
- for (i = 0; i < node->len; i++)
- ec_node_free(node->table[i]);
- ec_free(node->table);
+ for (i = 0; i < priv->len; i++)
+ ec_node_free(priv->table[i]);
+ ec_free(priv->table);
}
static size_t
-ec_node_subset_get_children_count(const struct ec_node *gen_node)
+ec_node_subset_get_children_count(const struct ec_node *node)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
- return node->len;
+ struct ec_node_subset *priv = ec_node_priv(node);
+ return priv->len;
}
static int
-ec_node_subset_get_child(const struct ec_node *gen_node, size_t i,
+ec_node_subset_get_child(const struct ec_node *node, size_t i,
struct ec_node **child, unsigned int *refs)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
+ struct ec_node_subset *priv = ec_node_priv(node);
- if (i >= node->len)
+ if (i >= priv->len)
return -1;
- *child = node->table[i];
+ *child = priv->table[i];
*refs = 1;
return 0;
}
EC_NODE_TYPE_REGISTER(ec_node_subset_type);
-int ec_node_subset_add(struct ec_node *gen_node, struct ec_node *child)
+int ec_node_subset_add(struct ec_node *node, struct ec_node *child)
{
- struct ec_node_subset *node = (struct ec_node_subset *)gen_node;
+ struct ec_node_subset *priv = ec_node_priv(node);
struct ec_node **table;
assert(node != NULL); // XXX specific assert for it, like in libyang
goto fail;
}
- if (ec_node_check_type(gen_node, &ec_node_subset_type) < 0)
+ if (ec_node_check_type(node, &ec_node_subset_type) < 0)
goto fail;
- table = ec_realloc(node->table, (node->len + 1) * sizeof(*node->table));
+ table = ec_realloc(priv->table, (priv->len + 1) * sizeof(*priv->table));
if (table == NULL) {
ec_node_free(child);
return -1;
}
- node->table = table;
- table[node->len] = child;
- node->len++;
+ priv->table = table;
+ table[priv->len] = child;
+ priv->len++;
return 0;
struct ec_node *__ec_node_subset(const char *id, ...)
{
- struct ec_node *gen_node = NULL;
- struct ec_node_subset *node = NULL;
+ struct ec_node *node = NULL;
struct ec_node *child;
va_list ap;
int fail = 0;
va_start(ap, id);
- gen_node = ec_node_from_type(&ec_node_subset_type, id);
- node = (struct ec_node_subset *)gen_node;
+ node = ec_node_from_type(&ec_node_subset_type, id);
if (node == NULL)
fail = 1;;
for (child = va_arg(ap, struct ec_node *);
- child != EC_NODE_ENDLIST;
+ child != EC_VA_END;
child = va_arg(ap, struct ec_node *)) {
/* on error, don't quit the loop to avoid leaks */
if (fail == 1 || child == NULL ||
- ec_node_subset_add(gen_node, child) < 0) {
+ ec_node_subset_add(node, child) < 0) {
fail = 1;
ec_node_free(child);
}
goto fail;
va_end(ap);
- return gen_node;
+ return node;
fail:
- ec_node_free(gen_node); /* will also free children */
+ ec_node_free(node); /* will also free children */
va_end(ap);
return NULL;
}
return -1;
}
testres |= EC_TEST_CHECK_COMPLETE(node,
- "", EC_NODE_ENDLIST,
- "foo", "bar", "bar2", "toto", "titi", EC_NODE_ENDLIST);
+ "", EC_VA_END,
+ "foo", "bar", "bar2", "toto", "titi", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "", EC_NODE_ENDLIST,
- "bar2", "bar", "foo", "toto", "titi", EC_NODE_ENDLIST);
+ "", EC_VA_END,
+ "bar2", "bar", "foo", "toto", "titi", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "bar", "bar2", "", EC_NODE_ENDLIST,
- "foo", "toto", "titi", EC_NODE_ENDLIST);
+ "bar", "bar2", "", EC_VA_END,
+ "foo", "toto", "titi", 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,
- "b", EC_NODE_ENDLIST,
- "bar", "bar2", EC_NODE_ENDLIST);
+ "b", EC_VA_END,
+ "bar", "bar2", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "bar", EC_NODE_ENDLIST,
- "bar", "bar2", EC_NODE_ENDLIST);
+ "bar", EC_VA_END,
+ "bar", "bar2", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "bar", "b", EC_NODE_ENDLIST,
- "bar2", EC_NODE_ENDLIST);
+ "bar", "b", EC_VA_END,
+ "bar2", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "t", EC_NODE_ENDLIST,
- "toto", "titi", EC_NODE_ENDLIST);
+ "t", EC_VA_END,
+ "toto", "titi", EC_VA_END);
testres |= EC_TEST_CHECK_COMPLETE(node,
- "to", EC_NODE_ENDLIST,
- "toto", EC_NODE_ENDLIST);
+ "to", EC_VA_END,
+ "toto", 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;