libsrx: new helper function srx_isset()

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-06-25 17:22:36 +02:00
parent 2c81c7cdb6
commit 170392cdb1
2 changed files with 33 additions and 0 deletions
+31
View File
@@ -25,6 +25,37 @@ sr_error_t srx_get_diff(sr_session_ctx_t *session, struct lyd_node **treep)
return err;
}
/* check if string at xpath (fmt) is set (non-zero length) */
bool srx_isset(sr_session_ctx_t *session, const char *fmt, ...)
{
sr_val_t *value = NULL;
bool isset = false;
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 (!xpath)
return false;
va_start(ap, fmt);
vsnprintf(xpath, len, fmt, ap);
va_end(ap);
if (sr_get_item(session, xpath, 0, &value))
return false;
if (value->data.string_val[0] != 0)
isset = true;
sr_free_val(value);
return isset;
}
int srx_set_item(sr_session_ctx_t *session, const sr_val_t *val, sr_edit_options_t opts,
const char *fmt, ...)
{
+2
View File
@@ -3,12 +3,14 @@
#ifndef CONFD_SRX_VAL_H_
#define CONFD_SRX_VAL_H_
#include <stdbool.h>
#include "common.h"
#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__)
sr_error_t srx_get_diff(sr_session_ctx_t *session, struct lyd_node **treep);
bool srx_isset(sr_session_ctx_t *session, const char *fmt, ...);
int srx_set_item(sr_session_ctx_t *, const sr_val_t *, sr_edit_options_t, const char *, ...)
__attribute__ ((format (printf, 4, 5)));