blob: ffe90820d712ab22635e2cda471ac2428f6e324e [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"
Michal Vaskoafac7822020-10-20 14:22:26 +020022#include "out.h"
23#include "out_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020024#include "printer_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "tree_data.h"
Michal Vaskoc8a230d2020-08-14 12:17:10 +020026#include "tree_schema.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020027
Michal Vasko3a41dff2020-07-15 14:30:28 +020028static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020029lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejcie7b95092019-05-15 11:03:07 +020030{
Michal Vasko3a41dff2020-07-15 14:30:28 +020031 LY_ERR ret = LY_SUCCESS;
Radek Krejcie7b95092019-05-15 11:03:07 +020032
33 switch (format) {
34 case LYD_XML:
35 ret = xml_print_data(out, root, options);
36 break;
Radek Krejcie7b95092019-05-15 11:03:07 +020037 case LYD_JSON:
38 ret = json_print_data(out, root, options);
39 break;
40 case LYD_LYB:
41 ret = lyb_print_data(out, root, options);
42 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +020043 case LYD_UNKNOWN:
Michal Vaskob7be7a82020-08-20 09:09:04 +020044 LOGINT(root ? LYD_CTX(root) : NULL);
Michal Vaskoc8a230d2020-08-14 12:17:10 +020045 ret = LY_EINT;
Radek Krejcie7b95092019-05-15 11:03:07 +020046 break;
47 }
48
Michal Vasko63f3d842020-07-08 10:10:14 +020049 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020050}
Radek Krejci26da5222020-06-09 17:43:17 +020051
Michal Vasko3a41dff2020-07-15 14:30:28 +020052API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020053lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +020054{
Michal Vasko3f5fa762020-08-11 13:44:07 +020055 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020056
Michal Vasko3a41dff2020-07-15 14:30:28 +020057 /* reset the number of printed bytes */
58 out->func_printed = 0;
Radek Krejci26da5222020-06-09 17:43:17 +020059
Radek Krejci2e874772020-08-28 16:36:33 +020060 if (root) {
61 /* get first top-level sibling */
62 while (root->parent) {
63 root = (struct lyd_node *)root->parent;
64 }
65 while (root->prev->next) {
66 root = root->prev;
67 }
Michal Vasko3a41dff2020-07-15 14:30:28 +020068 }
Radek Krejci26da5222020-06-09 17:43:17 +020069
Michal Vasko3a41dff2020-07-15 14:30:28 +020070 /* print each top-level sibling */
71 LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS));
72
73 return LY_SUCCESS;
74}
75
76API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020077lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Michal Vasko3a41dff2020-07-15 14:30:28 +020078{
Michal Vasko3f5fa762020-08-11 13:44:07 +020079 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +020080
81 /* reset the number of printed bytes */
82 out->func_printed = 0;
83
84 /* print the subtree */
85 LY_CHECK_RET(lyd_print_(out, root, format, options));
86
87 return LY_SUCCESS;
Radek Krejci26da5222020-06-09 17:43:17 +020088}
89
90API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +020091lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +020092{
Michal Vasko3a41dff2020-07-15 14:30:28 +020093 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020094 struct ly_out *out;
95
Michal Vasko3f5fa762020-08-11 13:44:07 +020096 LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020097
98 /* init */
99 *strp = NULL;
100
Radek Krejci84ce7b12020-06-11 17:28:25 +0200101 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200102 ret = lyd_print_(out, root, format, options);
103 ly_out_free(out, NULL, 0);
104 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200105}
106
107API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200108lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200109{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200110 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200111 struct ly_out *out;
112
Michal Vasko3f5fa762020-08-11 13:44:07 +0200113 LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200114
Radek Krejci84ce7b12020-06-11 17:28:25 +0200115 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200116 ret = lyd_print_(out, root, format, options);
117 ly_out_free(out, NULL, 0);
118 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200119}
120
121API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200122lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200123{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200124 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200125 struct ly_out *out;
126
Michal Vasko3f5fa762020-08-11 13:44:07 +0200127 LY_CHECK_ARG_RET(NULL, f, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200128
Radek Krejci84ce7b12020-06-11 17:28:25 +0200129 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200130 ret = lyd_print_(out, root, format, options);
131 ly_out_free(out, NULL, 0);
132 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200133}
134
135API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200136lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
Radek Krejci26da5222020-06-09 17:43:17 +0200137{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200138 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200139 struct ly_out *out;
140
Michal Vasko3f5fa762020-08-11 13:44:07 +0200141 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200142
Radek Krejci84ce7b12020-06-11 17:28:25 +0200143 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200144 ret = lyd_print_(out, root, format, options);
145 ly_out_free(out, NULL, 0);
146 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200147}
148
149API LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200150lyd_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 +0200151{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200152 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200153 struct ly_out *out;
154
Michal Vasko3f5fa762020-08-11 13:44:07 +0200155 LY_CHECK_ARG_RET(NULL, writeclb, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200156
Michal Vaskoce2b8742020-08-24 13:20:25 +0200157 LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200158 ret = lyd_print_(out, root, format, options);
159 ly_out_free(out, NULL, 0);
160 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200161}