From 54200eaba993d6709d1c996fab4b98ae68bd2044 Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 21 Apr 2023 16:02:00 +0200 Subject: [PATCH] confd: Add wrapper for sr_set_item This allows the xpath to be built using format string. --- src/confd/src/srx_val.c | 22 ++++++++++++++++++++++ src/confd/src/srx_val.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/confd/src/srx_val.c b/src/confd/src/srx_val.c index a1556753..7099cb7a 100644 --- a/src/confd/src/srx_val.c +++ b/src/confd/src/srx_val.c @@ -3,6 +3,28 @@ #include #include "core.h" +int srx_set_item(sr_session_ctx_t *session, const sr_val_t *val, sr_edit_options_t opts, + const char *fmt, ...) +{ + char *xpath; + va_list ap; + size_t len; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap) + 1; + va_end(ap); + + xpath = alloca(len); + if (!val) + return -1; + + va_start(ap, fmt); + vsnprintf(xpath, len, fmt, ap); + va_end(ap); + + return sr_set_item(session, xpath, val, opts); +} + int lydx_new_path(const struct ly_ctx *ctx, struct lyd_node **parent, int *first, char *xpath_base, char *node, const char *fmt, ...) { diff --git a/src/confd/src/srx_val.h b/src/confd/src/srx_val.h index 4c8941b5..aad2183a 100644 --- a/src/confd/src/srx_val.h +++ b/src/confd/src/srx_val.h @@ -8,6 +8,8 @@ #define SRX_GET_UINT8(s,v,fmt,...) srx_get_int(s, &v, SR_UINT8_T, fmt, ##__VA_ARGS__) #define SRX_GET_UINT32(s,v,fmt,...) srx_get_int(s, &v, SR_UINT32_T, fmt, ##__VA_ARGS__) +int srx_set_item(sr_session_ctx_t *session, const sr_val_t *val, sr_edit_options_t opts, + const char *fmt, ...) __attribute__ ((format (printf, 4, 5))); int lydx_new_path(const struct ly_ctx *ctx, struct lyd_node **parent, int *first, char *xpath_base, char *node, const char *fmt, ...);