if (ret == EC_PARSE_NOMATCH) {
if (!is_root) {
- ec_pnode_unlink_child(pstate, child);
+ ec_pnode_unlink_child(child);
ec_pnode_free(child);
}
return ret;
fail:
if (!is_root) {
- ec_pnode_unlink_child(pstate, child);
+ ec_pnode_unlink_child(child);
ec_pnode_free(child);
}
return -1;
const struct ec_pnode *root;
struct ec_pnode *dup_root, *dup = NULL;
- root = ec_pnode_get_root(pnode);
+ root = EC_PNODE_GET_ROOT(pnode);
dup_root = __ec_pnode_dup(root, pnode, &dup);
if (dup_root == NULL)
return NULL;
__ec_pnode_dump(out, child, indent + 1);
}
+// XXX dump in other formats? yaml? json?
void ec_pnode_dump(FILE *out, const struct ec_pnode *pnode)
{
fprintf(out, "------------------- parse dump:\n");
child->parent = pnode;
}
-void ec_pnode_unlink_child(struct ec_pnode *pnode,
- struct ec_pnode *child)
+void ec_pnode_unlink_child(struct ec_pnode *child)
{
- TAILQ_REMOVE(&pnode->children, child, next);
- child->parent = NULL;
+ struct ec_pnode *parent = child->parent;
+
+ if (parent != NULL) {
+ TAILQ_REMOVE(&parent->children, child, next);
+ child->parent = NULL;
+ }
}
struct ec_pnode *
return TAILQ_NEXT(pnode, next);
}
-bool ec_pnode_has_child(const struct ec_pnode *pnode)
-{
- return !TAILQ_EMPTY(&pnode->children);
-}
-
const struct ec_node *ec_pnode_get_node(const struct ec_pnode *pnode)
{
if (pnode == NULL)
struct ec_pnode *child;
child = ec_pnode_get_last_child(pnode);
- ec_pnode_unlink_child(pnode, child);
- ec_pnode_free(child);
+ if (child != NULL) {
+ ec_pnode_unlink_child(child);
+ ec_pnode_free(child);
+ }
}
-struct ec_pnode *__ec_pnode_get_root(struct ec_pnode *pnode)
+struct ec_pnode *ec_pnode_get_root(struct ec_pnode *pnode)
{
if (pnode == NULL)
return NULL;
}
struct ec_pnode *
-ec_pnode_find_next(struct ec_pnode *root, struct ec_pnode *start,
+ec_pnode_find_next(struct ec_pnode *root, struct ec_pnode *prev,
const char *id, bool iter_children)
{
struct ec_pnode *iter;
if (root == NULL)
return NULL;
- if (start == NULL)
- start = root;
+ if (prev == NULL)
+ prev = root;
else
- start = EC_PNODE_ITER_NEXT(root, start, iter_children);
+ prev = EC_PNODE_ITER_NEXT(root, prev, iter_children);
- for (iter = start; iter != NULL;
+ for (iter = prev; iter != NULL;
iter = EC_PNODE_ITER_NEXT(root, iter, 1)) {
if (iter->node != NULL &&
!strcmp(ec_node_id(iter->node), id))
return NULL;
}
-struct ec_pnode *ec_pnode_find(struct ec_pnode *pnode,
- const char *id)
+struct ec_pnode *ec_pnode_find(struct ec_pnode *root, const char *id)
{
- return ec_pnode_find_next(pnode, NULL, id, 1);
+ return ec_pnode_find_next(root, NULL, id, 1);
}
struct ec_dict *