lib: yang: add tree "printing" utility functions

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2023-07-09 05:51:20 -04:00
parent 8790457c46
commit 80cac370d0
2 changed files with 65 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include <zebra.h>
#include "darr.h"
#include "log.h"
#include "lib_errors.h"
#include "yang.h"
@ -673,6 +674,37 @@ static void ly_log_cb(LY_LOG_LEVEL level, const char *msg, const char *path)
zlog(priority, "libyang: %s", msg);
}
static ssize_t yang_print_darr(void *arg, const void *buf, size_t count)
{
uint8_t *dst = darr_append_n(*(uint8_t **)arg, count);
memcpy(dst, buf, count);
return count;
}
LY_ERR yang_print_tree_append(uint8_t **darr, const struct lyd_node *root,
LYD_FORMAT format, uint32_t options)
{
LY_ERR err;
err = lyd_print_clb(yang_print_darr, darr, root, format, options);
if (err)
zlog_err("Failed to save yang tree: %s", ly_last_errmsg());
else if (format != LYD_LYB)
*darr_append(*darr) = 0;
return err;
}
uint8_t *yang_print_tree(const struct lyd_node *root, LYD_FORMAT format,
uint32_t options)
{
uint8_t *darr = NULL;
if (yang_print_tree_append(&darr, root, format, options))
return NULL;
return darr;
}
const char *yang_print_errors(struct ly_ctx *ly_ctx, char *buf, size_t buf_len)
{
struct ly_err_item *ei;

View File

@ -600,6 +600,39 @@ extern struct ly_ctx *yang_ctx_new_setup(bool embedded_modules,
*/
extern void yang_debugging_set(bool enable);
/*
* "Print" the yang tree in `root` into dynamic sized array.
*
* Args:
* root: root of the subtree to "print" along with siblings.
* format: LYD_FORMAT of output (see lyd_print_mem)
* options: printing options (see lyd_print_mem)
*
* Return:
* A darr dynamic array with the "printed" output or NULL on failure.
*/
extern uint8_t *yang_print_tree(const struct lyd_node *root, LYD_FORMAT format,
uint32_t options);
/*
* "Print" the yang tree in `root` into an existing dynamic sized array.
*
* This function does not initialize or free the dynamic array, the array can
* already existing data, the tree will be appended to this data.
*
* Args:
* darr: existing `uint8_t *`, dynamic array.
* root: root of the subtree to "print" along with siblings.
* format: LYD_FORMAT of output (see lyd_print_mem)
* options: printing options (see lyd_print_mem)
*
* Return:
* LY_ERR from underlying calls.
*/
extern LY_ERR yang_print_tree_append(uint8_t **darr, const struct lyd_node *root,
LYD_FORMAT format, uint32_t options);
/*
* Print libyang error messages into the provided buffer.
*