blob: ea330c73949b99e3659f991530b6748b7c93fef4 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
2 * @file printer_data.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic data printers functions.
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
Radek Krejci535ea9f2020-05-29 16:01:05 +020015#include "printer_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020020#include "log.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020021#include "out.h"
22#include "out_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include "printer_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "tree_data.h"
25
Michal Vasko3a41dff2020-07-15 14:30:28 +020026static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020027lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +020028{
Michal Vasko3a41dff2020-07-15 14:30:28 +020029 LY_ERR ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +020030
31 switch (format) {
32 case LYD_XML:
33 ret = xml_print_data(out, root, options);
34 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020035 case LYD_JSON:
36 ret = json_print_data(out, root, options);
37 break;
38 case LYD_LYB:
39 ret = lyb_print_data(out, root, options);
40 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020041 case LYD_UNKNOWN:
Michal Vaskob7be7a82020-08-20 09:09:04 +020042 LOGINT(root ? LYD_CTX(root) : NULL);
Michal Vaskoc8a230d2020-08-14 12:17:10 +020043 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +020044 break;
45 }
46
Michal Vasko63f3d842020-07-08 10:10:14 +020047 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020048}
Radek Krejci26da5222020-06-09 17:43:17 +020049
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010050LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020051lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +020052{
Michal Vasko3f5fa762020-08-11 13:44:07 +020053 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020054
Michal Vasko3a41dff2020-07-15 14:30:28 +020055 /* reset the number of printed bytes */
56 out->func_printed = 0;
Radek Krejci26da5222020-06-09 17:43:17 +020057
Radek Krejci2e874772020-08-28 16:36:33 +020058 if (root) {
59 /* get first top-level sibling */
60 while (root->parent) {
Michal Vasko9e685082021-01-29 14:49:09 +010061 root = lyd_parent(root);
Radek Krejci2e874772020-08-28 16:36:33 +020062 }
63 while (root->prev->next) {
64 root = root->prev;
65 }
Michal Vasko3a41dff2020-07-15 14:30:28 +020066 }
Radek Krejci26da5222020-06-09 17:43:17 +020067
Michal Vasko3a41dff2020-07-15 14:30:28 +020068 /* print each top-level sibling */
69 LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS));
70
71 return LY_SUCCESS;
72}
73
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010074LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020075lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +020076{
Michal Vasko3f5fa762020-08-11 13:44:07 +020077 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +020078
79 /* reset the number of printed bytes */
80 out->func_printed = 0;
81
82 /* print the subtree */
83 LY_CHECK_RET(lyd_print_(out, root, format, options));
84
85 return LY_SUCCESS;
Radek Krejci26da5222020-06-09 17:43:17 +020086}
87
Jan Kundrátc53a7ec2021-12-09 16:01:19 +010088LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020089lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +020090{
Michal Vasko3a41dff2020-07-15 14:30:28 +020091 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020092 struct ly_out *out;
93
Michal Vasko3f5fa762020-08-11 13:44:07 +020094 LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020095
96 /* init */
97 *strp = NULL;
98
Radek Krejci84ce7b12020-06-11 17:28:25 +020099 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200100 ret = lyd_print_(out, root, format, options);
101 ly_out_free(out, NULL, 0);
102 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200103}
104
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100105LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200106lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200107{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200108 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200109 struct ly_out *out;
110
Michal Vasko3f5fa762020-08-11 13:44:07 +0200111 LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200112
Radek Krejci84ce7b12020-06-11 17:28:25 +0200113 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200114 ret = lyd_print_(out, root, format, options);
115 ly_out_free(out, NULL, 0);
116 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200117}
118
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100119LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200120lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200121{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200122 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200123 struct ly_out *out;
124
Michal Vasko3f5fa762020-08-11 13:44:07 +0200125 LY_CHECK_ARG_RET(NULL, f, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200126
Radek Krejci84ce7b12020-06-11 17:28:25 +0200127 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200128 ret = lyd_print_(out, root, format, options);
129 ly_out_free(out, NULL, 0);
130 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200131}
132
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100133LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200134lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200135{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200136 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200137 struct ly_out *out;
138
Michal Vasko3f5fa762020-08-11 13:44:07 +0200139 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200140
Radek Krejci84ce7b12020-06-11 17:28:25 +0200141 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200142 ret = lyd_print_(out, root, format, options);
143 ly_out_free(out, NULL, 0);
144 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200145}
146
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100147LIBYANG_API_DEF LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200148lyd_print_clb(ly_write_clb writeclb, void *user_data, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200149{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200150 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200151 struct ly_out *out;
152
Michal Vasko3f5fa762020-08-11 13:44:07 +0200153 LY_CHECK_ARG_RET(NULL, writeclb, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200154
Michal Vaskoce2b8742020-08-24 13:20:25 +0200155 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200156 ret = lyd_print_(out, root, format, options);
157 ly_out_free(out, NULL, 0);
158 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200159}