libsrx: Add lyx versions of lyd_find_xpath

- Helper to get a set of nodes, using a formatted path
- Helper to get a single node, using a formatted path
This commit is contained in:
Tobias Waldekranz
2025-01-08 09:57:55 +01:00
parent f041d009b5
commit b425edffce
2 changed files with 50 additions and 0 deletions
+44
View File
@@ -147,6 +147,50 @@ struct lyd_node *lydx_find_by_name(struct lyd_node *from, const char *by, const
return NULL;
}
struct ly_set *lydx_find_vxpathf(struct lyd_node *ctx, const char *xpfmt, va_list ap)
{
struct ly_set *set;
char *xpath;
int err;
vasprintf(&xpath, xpfmt, ap);
err = lyd_find_xpath(ctx, xpath, &set);
free(xpath);
return err ? NULL : set;
}
struct ly_set *lydx_find_xpathf(struct lyd_node *ctx, const char *xpfmt, ...)
{
struct ly_set *set;
va_list ap;
va_start(ap, xpfmt);
set = lydx_find_vxpathf(ctx, xpfmt, ap);
va_end(ap);
return set;
}
struct lyd_node *lydx_get_xpathf(struct lyd_node *ctx, const char *xpfmt, ...)
{
struct lyd_node *node = NULL;
struct ly_set *set;
va_list ap;
va_start(ap, xpfmt);
set = lydx_find_vxpathf(ctx, xpfmt, ap);
va_end(ap);
if (!set)
return NULL;
if (set->count == 1)
node = *set->dnodes;
ly_set_free(set, NULL);
return node;
}
const char *lydx_get_mattr(struct lyd_node *node, const char *name)
{
struct lyd_meta *meta;
+6
View File
@@ -39,6 +39,12 @@ struct lyd_node *lydx_vdescend(struct lyd_node *from, va_list ap);
struct lyd_node *lydx_get_descendant(struct lyd_node *from, ...);
struct lyd_node *lydx_find_by_name(struct lyd_node *from, const char *by, const char *name);
struct ly_set *lydx_find_vxpathf(struct lyd_node *ctx, const char *xpfmt, va_list ap);
struct ly_set *lydx_find_xpathf(struct lyd_node *ctx, const char *xpfmt, ...)
__attribute__ ((format (printf, 2, 3)));
struct lyd_node *lydx_get_xpathf(struct lyd_node *ctx, const char *xpfmt, ...)
__attribute__ ((format (printf, 2, 3)));
const char *lydx_get_mattr(struct lyd_node *node, const char *name);
const char *lydx_get_attr(struct lyd_node *sibling, const char *name);
const char *lydx_get_cattr(struct lyd_node *parent, const char *name);