blob: 3d78d3b684cabf854d25ea1451ca8c2011f4d5cc [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"
25
Michal Vasko3a41dff2020-07-15 14:30:28 +020026static LY_ERR
27lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int 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;
35#if 0
36 case LYD_JSON:
37 ret = json_print_data(out, root, options);
38 break;
Michal Vasko60ea6352020-06-29 13:39:39 +020039#endif
Radek Krejcie7b95092019-05-15 11:03:07 +020040 case LYD_LYB:
41 ret = lyb_print_data(out, root, options);
42 break;
Michal Vasko60ea6352020-06-29 13:39:39 +020043 case LYD_SCHEMA:
44 LOGERR(out->ctx, LY_EINVAL, "Invalid output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020045 ret = LY_EINVAL;
46 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
53lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejci26da5222020-06-09 17:43:17 +020054{
Michal Vasko3a41dff2020-07-15 14:30:28 +020055 LY_CHECK_ARG_RET(NULL, out, root, !(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
Michal Vasko3a41dff2020-07-15 14:30:28 +020060 /* get first top-level sibling */
61 while (root->parent) {
62 root = (struct lyd_node *)root->parent;
63 }
64 while (root->prev->next) {
65 root = root->prev;
66 }
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
74API LY_ERR
75lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
76{
77 LY_CHECK_ARG_RET(NULL, out, root, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
78
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
88API LY_ERR
89lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options)
90{
Michal Vasko3a41dff2020-07-15 14:30:28 +020091 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020092 struct ly_out *out;
93
94 LY_CHECK_ARG_RET(NULL, strp, root, LY_EINVAL);
95
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
105API LY_ERR
106lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options)
107{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200108 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200109 struct ly_out *out;
110
111 LY_CHECK_ARG_RET(NULL, fd != -1, root, LY_EINVAL);
112
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
119API LY_ERR
120lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options)
121{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200122 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200123 struct ly_out *out;
124
125 LY_CHECK_ARG_RET(NULL, f, root, LY_EINVAL);
126
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
133API LY_ERR
134lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options)
135{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200136 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200137 struct ly_out *out;
138
139 LY_CHECK_ARG_RET(NULL, path, root, LY_EINVAL);
140
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
147API LY_ERR
148lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
149 const struct lyd_node *root, LYD_FORMAT format, int options)
150{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200151 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200152 struct ly_out *out;
153
154 LY_CHECK_ARG_RET(NULL, writeclb, root, LY_EINVAL);
155
Radek Krejci84ce7b12020-06-11 17:28:25 +0200156 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200157 ret = lyd_print_(out, root, format, options);
158 ly_out_free(out, NULL, 0);
159 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200160}