blob: 29dcc09778fc5d6a6f3aa861d3719f493e41dacb [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#include <unistd.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "log.h"
Radek Krejci26da5222020-06-09 17:43:17 +020022#include "printer.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"
Michal Vaskoc8a230d2020-08-14 12:17:10 +020025#include "tree_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020026
Michal Vasko3a41dff2020-07-15 14:30:28 +020027static LY_ERR
28lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020029{
Michal Vasko3a41dff2020-07-15 14:30:28 +020030 LY_ERR ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +020031
32 switch (format) {
33 case LYD_XML:
34 ret = xml_print_data(out, root, options);
35 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020036 case LYD_JSON:
37 ret = json_print_data(out, root, options);
38 break;
39 case LYD_LYB:
40 ret = lyb_print_data(out, root, options);
41 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020042 case LYD_UNKNOWN:
43 LOGINT(root ? LYD_NODE_CTX(root) : NULL);
44 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +020045 break;
46 }
47
Michal Vasko63f3d842020-07-08 10:10:14 +020048 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020049}
Radek Krejci26da5222020-06-09 17:43:17 +020050
Michal Vasko3a41dff2020-07-15 14:30:28 +020051API LY_ERR
52lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejci26da5222020-06-09 17:43:17 +020053{
Michal Vasko3f5fa762020-08-11 13:44:07 +020054 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020055
Michal Vasko3a41dff2020-07-15 14:30:28 +020056 /* reset the number of printed bytes */
57 out->func_printed = 0;
Radek Krejci26da5222020-06-09 17:43:17 +020058
Michal Vasko3a41dff2020-07-15 14:30:28 +020059 /* get first top-level sibling */
60 while (root->parent) {
61 root = (struct lyd_node *)root->parent;
62 }
63 while (root->prev->next) {
64 root = root->prev;
65 }
Radek Krejci26da5222020-06-09 17:43:17 +020066
Michal Vasko3a41dff2020-07-15 14:30:28 +020067 /* print each top-level sibling */
68 LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS));
69
70 return LY_SUCCESS;
71}
72
73API LY_ERR
74lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
75{
Michal Vasko3f5fa762020-08-11 13:44:07 +020076 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +020077
78 /* reset the number of printed bytes */
79 out->func_printed = 0;
80
81 /* print the subtree */
82 LY_CHECK_RET(lyd_print_(out, root, format, options));
83
84 return LY_SUCCESS;
Radek Krejci26da5222020-06-09 17:43:17 +020085}
86
87API LY_ERR
88lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options)
89{
Michal Vasko3a41dff2020-07-15 14:30:28 +020090 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020091 struct ly_out *out;
92
Michal Vasko3f5fa762020-08-11 13:44:07 +020093 LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020094
95 /* init */
96 *strp = NULL;
97
Radek Krejci84ce7b12020-06-11 17:28:25 +020098 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +020099 ret = lyd_print_(out, root, format, options);
100 ly_out_free(out, NULL, 0);
101 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200102}
103
104API LY_ERR
105lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options)
106{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200107 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200108 struct ly_out *out;
109
Michal Vasko3f5fa762020-08-11 13:44:07 +0200110 LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200111
Radek Krejci84ce7b12020-06-11 17:28:25 +0200112 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200113 ret = lyd_print_(out, root, format, options);
114 ly_out_free(out, NULL, 0);
115 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200116}
117
118API LY_ERR
119lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options)
120{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200121 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200122 struct ly_out *out;
123
Michal Vasko3f5fa762020-08-11 13:44:07 +0200124 LY_CHECK_ARG_RET(NULL, f, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200125
Radek Krejci84ce7b12020-06-11 17:28:25 +0200126 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200127 ret = lyd_print_(out, root, format, options);
128 ly_out_free(out, NULL, 0);
129 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200130}
131
132API LY_ERR
133lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options)
134{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200135 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200136 struct ly_out *out;
137
Michal Vasko3f5fa762020-08-11 13:44:07 +0200138 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200139
Radek Krejci84ce7b12020-06-11 17:28:25 +0200140 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200141 ret = lyd_print_(out, root, format, options);
142 ly_out_free(out, NULL, 0);
143 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200144}
145
146API LY_ERR
147lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
148 const struct lyd_node *root, LYD_FORMAT format, int options)
149{
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
Radek Krejci84ce7b12020-06-11 17:28:25 +0200155 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &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}