Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lyd_print_mem_len API to return printed size #2103

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/printer_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint3
return ret;
}

LIBYANG_API_DEF LY_ERR
lyd_print_mem_len(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options, size_t *len)
{
LY_ERR ret;
struct ly_out *out;

LY_CHECK_ARG_RET(NULL, strp && len, LY_EINVAL);

/* init */
*strp = NULL;

LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
ret = lyd_print_(out, root, format, options);
*len = out->printed;
ly_out_free(out, NULL, 0);
return ret;
}

LIBYANG_API_DEF LY_ERR
lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
{
Expand Down
5 changes: 5 additions & 0 deletions src/printer_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ LIBYANG_API_DECL LY_ERR lyd_print_tree(struct ly_out *out, const struct lyd_node
*/
LIBYANG_API_DECL LY_ERR lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options);

/**
* @brief Same as lyd_print_mem but also return the length of buffer written
*/
LIBYANG_API_DEF LY_ERR lyd_print_mem_len(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options, size_t *len);

/**
* @brief Print data tree in the specified format.
*
Expand Down