From e39b60bac1e8f99ad9053e7d7d7cb73c2f1cc414 Mon Sep 17 00:00:00 2001 From: Igor Ryzhov Date: Fri, 26 Jan 2024 16:54:36 +0200 Subject: [PATCH] lib: add yang function for counting data nodes Signed-off-by: Igor Ryzhov --- lib/yang.c | 24 ++++++++++++++++++++++++ lib/yang.h | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/yang.c b/lib/yang.c index 7d35fb0d3d..ed855c8498 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -508,6 +508,30 @@ void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg, ly_set_free(set, NULL); } +uint32_t yang_dnode_count(const struct lyd_node *dnode, const char *xpath_fmt, + ...) +{ + va_list ap; + char xpath[XPATH_MAXLEN]; + struct ly_set *set; + uint32_t count; + + va_start(ap, xpath_fmt); + vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); + va_end(ap); + + if (lyd_find_xpath(dnode, xpath, &set)) { + assert(0); + return 0; + } + + count = set->count; + + ly_set_free(set, NULL); + + return count; +} + bool yang_dnode_is_default(const struct lyd_node *dnode, const char *xpath) { const struct lysc_node *snode; diff --git a/lib/yang.h b/lib/yang.h index dbb7f7163b..1235125f26 100644 --- a/lib/yang.h +++ b/lib/yang.h @@ -420,6 +420,21 @@ void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg, const struct lyd_node *dnode, const char *xpath_fmt, ...) PRINTFRR(4, 5); +/* + * Count the number of data nodes that satisfy an XPath query. + * + * dnode + * Base libyang data node to operate on. + * + * xpath_fmt + * XPath expression (absolute or relative). + * + * ... + * any parameters for xpath_fmt. + */ +uint32_t yang_dnode_count(const struct lyd_node *dnode, const char *xpath_fmt, + ...) PRINTFRR(2, 3); + /* * Check if the libyang data node contains a default value. Non-presence * containers are assumed to always contain a default value. -- 2.39.5