From b425edffce74f1c7f9ff51b111f831a6231edaae Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 20 Dec 2024 16:34:57 +0100 Subject: [PATCH] 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 --- src/libsrx/src/lyx.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/libsrx/src/lyx.h | 6 ++++++ 2 files changed, 50 insertions(+) diff --git a/src/libsrx/src/lyx.c b/src/libsrx/src/lyx.c index b669c2ad..e7ca2833 100644 --- a/src/libsrx/src/lyx.c +++ b/src/libsrx/src/lyx.c @@ -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; diff --git a/src/libsrx/src/lyx.h b/src/libsrx/src/lyx.h index e4ea6edc..da0f3f7d 100644 --- a/src/libsrx/src/lyx.h +++ b/src/libsrx/src/lyx.h @@ -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);