From fcb7bffdda0b122c34978e3f7390b6f26689848a Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Fri, 4 Jan 2019 19:08:10 -0200 Subject: [PATCH] lib: introduce new YANG helper function One use case for the new yang_data_list_find() function is to find input parameters in RPC northbound callbacks easily, without the need to iterate over the input parameters manually. Signed-off-by: Renato Westphal --- lib/yang.c | 19 +++++++++++++++++++ lib/yang.h | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/yang.c b/lib/yang.c index 71b41c35d8..4077b29746 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -608,6 +608,25 @@ struct list *yang_data_list_new(void) return list; } +struct yang_data *yang_data_list_find(const struct list *list, + const char *xpath_fmt, ...) +{ + char xpath[XPATH_MAXLEN]; + struct yang_data *data; + struct listnode *node; + va_list ap; + + va_start(ap, xpath_fmt); + vsnprintf(xpath, sizeof(xpath), xpath_fmt, ap); + va_end(ap); + + for (ALL_LIST_ELEMENTS_RO(list, node, data)) + if (strmatch(data->xpath, xpath)) + return data; + + return NULL; +} + static void *ly_dup_cb(const void *priv) { /* Make a shallow copy of the priv pointer. */ diff --git a/lib/yang.h b/lib/yang.h index 3259189e98..09d92cafdd 100644 --- a/lib/yang.h +++ b/lib/yang.h @@ -509,6 +509,21 @@ extern void yang_data_free(struct yang_data *data); */ extern struct list *yang_data_list_new(void); +/* + * Find the yang_data structure corresponding to an XPath in a list. + * + * list + * list of yang_data structures to operate on. + * + * xpath_fmt + * XPath to search for (format string). + * + * Returns: + * Pointer to yang_data if found, NULL otherwise. + */ +extern struct yang_data *yang_data_list_find(const struct list *list, + const char *xpath_fmt, ...); + /* * Initialize the YANG subsystem. Should be called only once during the * daemon initialization process. -- 2.39.5