Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 2 | * @file printer_data.h |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 52927e2 | 2020-03-16 17:26:14 +0100 | [diff] [blame] | 4 | * @brief Data printers for libyang |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 5 | * |
| 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 Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 18 | #include <stdint.h> |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 21 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 22 | #include "out.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 24 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 29 | struct ly_out; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 30 | |
| 31 | /** |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 32 | * @page howtoDataPrinters Printing Data |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | * |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 34 | * Data printers allows to serialize internal representation of a data tree in a specific format. libyang |
| 35 | * supports the following data formats for printing: |
| 36 | * |
| 37 | * - XML |
| 38 | * |
| 39 | * Basic format as specified in rules of mapping YANG modeled data to XML in |
| 40 | * [RFC 6020](http://tools.ietf.org/html/rfc6020). |
| 41 | * |
| 42 | * - JSON |
| 43 | * |
| 44 | * The alternative data format available in RESTCONF protocol. Specification of JSON encoding of data modeled by YANG |
| 45 | * can be found in [RFC 7951](https://tools.ietf.org/html/rfc7951). |
| 46 | * |
| 47 | * By default, both formats are printed with indentation (formatting), which can be avoided by ::LYD_PRINT_SHRINK |
| 48 | * [printer option](@ref dataprinterflags)). Other options adjust e.g. [with-defaults mode](@ref howtoDataWD). |
| 49 | * |
| 50 | * Besides the legacy functions from libyang 1.x (::lyd_print_clb(), ::lyd_print_fd(), ::lyd_print_file(), ::lyd_print_mem() |
| 51 | * and ::lyd_print_path()) printing data into the specified output, there are also new functions using |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 52 | * [output handler](@ref howtoOutput) introduced in libyang 2.0. In contrast to |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 53 | * [schema printers](@ref howtoSchemaPrinters), there is no limit of the functionality and the functions can be used |
| 54 | * interchangeable. The only think to note is that the new functions ::lyd_print_all() and ::lyd_print_tree() does not |
| 55 | * accept ::LYD_PRINT_WITHSIBLINGS [printer option](@ref dataprinterflags)) since this flag differentiate the functions |
| 56 | * themselves. |
| 57 | * |
| 58 | * Functions List |
| 59 | * -------------- |
| 60 | * - ::lyd_print_all() |
| 61 | * - ::lyd_print_tree() |
| 62 | * - ::lyd_print_mem() |
| 63 | * - ::lyd_print_fd() |
| 64 | * - ::lyd_print_file() |
| 65 | * - ::lyd_print_path() |
| 66 | * - ::lyd_print_clb() |
| 67 | */ |
| 68 | |
| 69 | /** |
| 70 | * @ingroup datatree |
| 71 | * @defgroup dataprinterflags Data printer flags |
| 72 | * |
| 73 | * Options to change default behavior of the data printers. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 74 | * |
| 75 | * @{ |
| 76 | */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 77 | #define LYD_PRINT_WITHSIBLINGS 0x01 /**< Flag for printing also the (following) sibling nodes of the data node. |
| 78 | The flag is not allowed for ::lyd_print_all() and ::lyd_print_tree(). */ |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 79 | #define LYD_PRINT_SHRINK LY_PRINT_SHRINK /**< Flag for output without indentation and formatting new lines. */ |
| 80 | #define LYD_PRINT_KEEPEMPTYCONT 0x04 /**< Preserve empty non-presence containers */ |
| 81 | #define LYD_PRINT_WD_MASK 0xF0 /**< Mask for with-defaults modes */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 82 | #define LYD_PRINT_WD_EXPLICIT 0x00 /**< Explicit with-defaults mode. Only the data explicitly being present in |
| 83 | the data tree are printed, so the implicitly added default nodes are |
| 84 | not printed. Note that this is the default value when no WD option is |
| 85 | specified. */ |
| 86 | #define LYD_PRINT_WD_TRIM 0x10 /**< Trim mode avoids printing the nodes with the value equal to their |
| 87 | default value */ |
| 88 | #define LYD_PRINT_WD_ALL 0x20 /**< With this option, theInclude implicit default nodes */ |
| 89 | #define LYD_PRINT_WD_ALL_TAG 0x40 /**< Same as ::LYD_PRINT_WD_ALL but also adds attribute 'default' with value 'true' to |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 90 | all nodes that has its default value. The 'default' attribute has namespace: |
| 91 | urn:ietf:params:xml:ns:netconf:default:1.0 and thus the attributes are |
| 92 | printed only when the ietf-netconf-with-defaults module is present in libyang |
| 93 | context (but in that case this namespace is always printed). */ |
Radek Krejci | 8678fa4 | 2020-08-18 16:07:28 +0200 | [diff] [blame] | 94 | #define LYD_PRINT_WD_IMPL_TAG 0x80 /**< Same as ::LYD_PRINT_WD_ALL_TAG but the attributes are added only to the nodes that |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 95 | are not explicitly present in the original data tree despite their |
| 96 | value is equal to their default value. There is the same limitation regarding |
| 97 | the presence of ietf-netconf-with-defaults module in libyang context. */ |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 98 | /** |
| 99 | * @} |
| 100 | */ |
| 101 | |
| 102 | /** |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 103 | * @brief Print the whole data tree of the root, including all the siblings. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 104 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 105 | * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 106 | * @param[in] root The root element of the tree to print, can be any sibling. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 107 | * @param[in] format Output format. |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 108 | * @param[in] options [Data printer flags](@ref dataprinterflags) except ::LYD_PRINT_WITHSIBLINGS. |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 109 | * @return LY_ERR value. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 110 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 111 | LY_ERR lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @brief Print the selected data subtree. |
| 115 | * |
| 116 | * @param[in] out Printer handler for a specific output. Use ly_out_*() functions to create and free the handler. |
| 117 | * @param[in] root The root element of the subtree to print. |
| 118 | * @param[in] format Output format. |
| 119 | * @param[in] options [Data printer flags](@ref dataprinterflags) except ::LYD_PRINT_WITHSIBLINGS. |
| 120 | * @return LY_ERR value. |
| 121 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 122 | LY_ERR lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 123 | |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 124 | /** |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 125 | * @brief Print data tree in the specified format. |
| 126 | * |
| 127 | * @param[out] strp Pointer to store the resulting dump. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 128 | * @param[in] root The root element of the (sub)tree to print. |
| 129 | * @param[in] format Output format. |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 130 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
| 131 | * @return LY_ERR value. |
| 132 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 133 | LY_ERR lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * @brief Print data tree in the specified format. |
| 137 | * |
| 138 | * @param[in] fd File descriptor where to print the data. |
| 139 | * @param[in] root The root element of the (sub)tree to print. |
| 140 | * @param[in] format Output format. |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 141 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 142 | * @return LY_ERR value. |
| 143 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 144 | LY_ERR lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief Print data tree in the specified format. |
| 148 | * |
| 149 | * @param[in] f File stream where to print the data. |
| 150 | * @param[in] root The root element of the (sub)tree to print. |
| 151 | * @param[in] format Output format. |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 152 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 153 | * @return LY_ERR value. |
| 154 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 155 | LY_ERR lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * @brief Print data tree in the specified format. |
| 159 | * |
| 160 | * @param[in] path File path where to print the data. |
| 161 | * @param[in] root The root element of the (sub)tree to print. |
| 162 | * @param[in] format Output format. |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 163 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 164 | * @return LY_ERR value. |
| 165 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 166 | LY_ERR lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * @brief Print data tree in the specified format. |
| 170 | * |
| 171 | * @param[in] writeclb Callback function to write the data (see write(1)). |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 172 | * @param[in] user_data Optional caller-specific argument to be passed to the \p writeclb callback. |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 173 | * @param[in] root The root element of the (sub)tree to print. |
| 174 | * @param[in] format Output format. |
Michal Vasko | dcd356f | 2020-07-14 12:26:51 +0200 | [diff] [blame] | 175 | * @param[in] options [Data printer flags](@ref dataprinterflags). |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 176 | * @return LY_ERR value. |
| 177 | */ |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 178 | LY_ERR lyd_print_clb(ly_write_clb writeclb, void *user_data, const struct lyd_node *root, LYD_FORMAT format, uint32_t options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 179 | |
| 180 | #ifdef __cplusplus |
| 181 | } |
| 182 | #endif |
| 183 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 184 | #endif /* LY_PRINTER_DATA_H_ */ |