blob: 1f67b76333769acd7581dfaa89c4306b94e77111 [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
26/**
27 * @brief Common YANG data printer.
28 *
29 * @param[in] out Prepared structure defining the type and details of the printer output.
30 * @param[in] root The root element of the (sub)tree to print.
31 * @param[in] format Output format.
Radek Krejci241f6b52020-05-21 18:13:49 +020032 * @param[in] options [Data printer flags](@ref dataprinterflags). With \p format LYD_LYB, only #LYDP_WITHSIBLINGS option is accepted.
Radek Krejcie7b95092019-05-15 11:03:07 +020033 * @return LY_ERR value.
34 */
Radek Krejcia5bba312020-01-09 15:41:20 +010035API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020036lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020037{
38 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010039 size_t printed_prev;
40
41 LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL);
42
43 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020044
45 switch (format) {
46 case LYD_XML:
47 ret = xml_print_data(out, root, options);
48 break;
49#if 0
50 case LYD_JSON:
51 ret = json_print_data(out, root, options);
52 break;
53 case LYD_LYB:
54 ret = lyb_print_data(out, root, options);
55 break;
56#endif
57 default:
Radek Krejcidf3da792019-05-17 10:32:24 +020058 LOGERR(out->ctx, LY_EINVAL, "Unknown output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020059 ret = LY_EINVAL;
60 break;
61 }
62
Radek Krejcie7b95092019-05-15 11:03:07 +020063 if (ret) {
64 /* error */
65 return (-1) * ret;
66 } else {
67 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +010068 return (ssize_t)(out->printed - printed_prev);
Radek Krejcie7b95092019-05-15 11:03:07 +020069 }
70}
Radek Krejci26da5222020-06-09 17:43:17 +020071
72static LY_ERR
73lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
74{
75 ssize_t result;
76
77 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
78
79 result = lyd_print(out, root, format, options);
80
81 ly_out_free(out, NULL, 0);
82
83 if (result < 0) {
84 return (-1) * result;
85 } else {
86 return LY_SUCCESS;
87 }
88}
89
90API LY_ERR
91lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options)
92{
93 struct ly_out *out;
94
95 LY_CHECK_ARG_RET(NULL, strp, root, LY_EINVAL);
96
97 /* init */
98 *strp = NULL;
99
100 out = ly_out_new_memory(strp, 0);
101 return lyd_print_(out, root, format, options);
102}
103
104API LY_ERR
105lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options)
106{
107 struct ly_out *out;
108
109 LY_CHECK_ARG_RET(NULL, fd != -1, root, LY_EINVAL);
110
111 out = ly_out_new_fd(fd);
112 return lyd_print_(out, root, format, options);
113}
114
115API LY_ERR
116lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options)
117{
118 struct ly_out *out;
119
120 LY_CHECK_ARG_RET(NULL, f, root, LY_EINVAL);
121
122 out = ly_out_new_file(f);
123 return lyd_print_(out, root, format, options);
124}
125
126API LY_ERR
127lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options)
128{
129 struct ly_out *out;
130
131 LY_CHECK_ARG_RET(NULL, path, root, LY_EINVAL);
132
133 out = ly_out_new_filepath(path);
134 return lyd_print_(out, root, format, options);
135}
136
137API LY_ERR
138lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
139 const struct lyd_node *root, LYD_FORMAT format, int options)
140{
141 struct ly_out *out;
142
143 LY_CHECK_ARG_RET(NULL, writeclb, root, LY_EINVAL);
144
145 out = ly_out_new_clb(writeclb, arg);
146 return lyd_print_(out, root, format, options);
147}