blob: 31cfc718ee7349d9a61cb60f7d83c20cb9746495 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
Michal Vasko52927e22020-03-16 17:26:14 +01002 * @file printer_data.h
Radek Krejcie7b95092019-05-15 11:03:07 +02003 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko52927e22020-03-16 17:26:14 +01004 * @brief Data printers for libyang
Radek Krejcie7b95092019-05-15 11:03:07 +02005 *
6 * Copyright (c) 2015-2019 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#ifndef LY_PRINTER_DATA_H_
16#define LY_PRINTER_DATA_H_
17
Radek Krejci26da5222020-06-09 17:43:17 +020018#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <unistd.h>
20
Radek Krejci26da5222020-06-09 17:43:17 +020021#include "log.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020023
Radek Krejcica376bd2020-06-11 16:04:06 +020024#ifdef __cplusplus
25extern "C" {
26#endif
27
Radek Krejci535ea9f2020-05-29 16:01:05 +020028struct ly_out;
Radek Krejcie7b95092019-05-15 11:03:07 +020029
30/**
31 * @defgroup dataprinterflags Data printer flags
32 * @ingroup datatree
33 *
34 * Validity flags for data nodes.
35 *
36 * @{
37 */
38#define LYDP_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. */
39#define LYDP_FORMAT 0x02 /**< Flag for formatted output. */
40#define LYDP_KEEPEMPTYCONT 0x04 /**< Preserve empty non-presence containers */
41#define LYDP_WD_MASK 0xF0 /**< Mask for with-defaults modes */
42#define LYDP_WD_EXPLICIT 0x00 /**< Explicit mode - print only data explicitly being present in the data tree.
43 Note that this is the default value when no WD option is specified. */
44#define LYDP_WD_TRIM 0x10 /**< Do not print the nodes with the value equal to their default value */
45#define LYDP_WD_ALL 0x20 /**< Include implicit default nodes */
Radek Krejci241f6b52020-05-21 18:13:49 +020046#define LYDP_WD_ALL_TAG 0x40 /**< Same as #LYDP_WD_ALL but also adds attribute 'default' with value 'true' to
Radek Krejcie7b95092019-05-15 11:03:07 +020047 all nodes that has its default value. The 'default' attribute has namespace:
48 urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are
49 printed only when the ietf-netconf-with-defaults module is present in libyang
50 context (but in that case this namespace is always printed). */
Radek Krejci241f6b52020-05-21 18:13:49 +020051#define LYDP_WD_IMPL_TAG 0x80 /**< Same as LYDP_WD_ALL_TAG but the attributes are added only to the nodes that
Radek Krejcie7b95092019-05-15 11:03:07 +020052 are not explicitly present in the original data tree despite their
53 value is equal to their default value. There is the same limitation regarding
54 the presence of ietf-netconf-with-defaults module in libyang context. */
Radek Krejcie7b95092019-05-15 11:03:07 +020055/**
56 * @}
57 */
58
59/**
Radek Krejcia5bba312020-01-09 15:41:20 +010060 * @brief Common YANG data printer.
Radek Krejcie7b95092019-05-15 11:03:07 +020061 *
Radek Krejci241f6b52020-05-21 18:13:49 +020062 * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler.
Radek Krejcia5bba312020-01-09 15:41:20 +010063 * @param[in] root The root element of the (sub)tree to print.
64 * @param[in] format Output format.
Radek Krejci241f6b52020-05-21 18:13:49 +020065 * @param[in] options [Data printer flags](@ref dataprinterflags). With \p format LYD_LYB, only #LYDP_WITHSIBLINGS option is accepted.
Radek Krejci26a5dfb2019-07-26 14:51:06 +020066 * @return Number of printed characters (excluding the null byte used to end the string) in case of success.
Radek Krejcie7b95092019-05-15 11:03:07 +020067 * @return Negative value failure (absolute value corresponds to LY_ERR values).
68 */
Radek Krejci241f6b52020-05-21 18:13:49 +020069ssize_t lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options);
Radek Krejcie7b95092019-05-15 11:03:07 +020070
Radek Krejci26da5222020-06-09 17:43:17 +020071/**
72* @brief Print data tree in the specified format.
73*
74* @param[out] strp Pointer to store the resulting dump.
75 * @param[in] root The root element of the (sub)tree to print.
76 * @param[in] format Output format.
77* @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
78* @return LY_ERR value.
79*/
80LY_ERR lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options);
81
82/**
83 * @brief Print data tree in the specified format.
84 *
85 * @param[in] fd File descriptor where to print the data.
86 * @param[in] root The root element of the (sub)tree to print.
87 * @param[in] format Output format.
88 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
89 * @return LY_ERR value.
90 */
91LY_ERR lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options);
92
93/**
94 * @brief Print data tree in the specified format.
95 *
96 * @param[in] f File stream where to print the data.
97 * @param[in] root The root element of the (sub)tree to print.
98 * @param[in] format Output format.
99 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
100 * @return LY_ERR value.
101 */
102LY_ERR lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options);
103
104/**
105 * @brief Print data tree in the specified format.
106 *
107 * @param[in] path File path where to print the data.
108 * @param[in] root The root element of the (sub)tree to print.
109 * @param[in] format Output format.
110 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
111 * @return LY_ERR value.
112 */
113LY_ERR lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options);
114
115/**
116 * @brief Print data tree in the specified format.
117 *
118 * @param[in] writeclb Callback function to write the data (see write(1)).
119 * @param[in] arg Optional caller-specific argument to be passed to the \p writeclb callback.
120 * @param[in] root The root element of the (sub)tree to print.
121 * @param[in] format Output format.
122 * @param[in] options [printer flags](@ref printerflags). \p format LYD_LYB accepts only #LYP_WITHSIBLINGS option.
123 * @return LY_ERR value.
124 */
125LY_ERR lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
126 const struct lyd_node *root, LYD_FORMAT format, int options);
127
128#ifdef __cplusplus
129}
130#endif
131
Radek Krejcie7b95092019-05-15 11:03:07 +0200132#endif /* LY_PRINTER_DATA_H_ */