blob: a554e01d5ea24f3ce931e18eb46b7aff6c99f585 [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
Radek Krejcia5bba312020-01-09 15:41:20 +010026API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020027lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020028{
29 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010030 size_t printed_prev;
31
32 LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL);
33
34 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020035
36 switch (format) {
37 case LYD_XML:
38 ret = xml_print_data(out, root, options);
39 break;
40#if 0
41 case LYD_JSON:
42 ret = json_print_data(out, root, options);
43 break;
44 case LYD_LYB:
45 ret = lyb_print_data(out, root, options);
46 break;
47#endif
48 default:
Radek Krejcidf3da792019-05-17 10:32:24 +020049 LOGERR(out->ctx, LY_EINVAL, "Unknown output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020050 ret = LY_EINVAL;
51 break;
52 }
53
Radek Krejcie7b95092019-05-15 11:03:07 +020054 if (ret) {
55 /* error */
Michal Vaskob130f332020-06-29 11:48:35 +020056 return (-1) * (signed)ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020057 } else {
58 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +010059 return (ssize_t)(out->printed - printed_prev);
Radek Krejcie7b95092019-05-15 11:03:07 +020060 }
61}
Radek Krejci26da5222020-06-09 17:43:17 +020062
63static LY_ERR
64lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
65{
66 ssize_t result;
67
68 LY_CHECK_ARG_RET(NULL, out, LY_EINVAL);
69
70 result = lyd_print(out, root, format, options);
71
72 ly_out_free(out, NULL, 0);
73
74 if (result < 0) {
75 return (-1) * result;
76 } else {
77 return LY_SUCCESS;
78 }
79}
80
81API LY_ERR
82lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options)
83{
84 struct ly_out *out;
85
86 LY_CHECK_ARG_RET(NULL, strp, root, LY_EINVAL);
87
88 /* init */
89 *strp = NULL;
90
Radek Krejci84ce7b12020-06-11 17:28:25 +020091 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Radek Krejci26da5222020-06-09 17:43:17 +020092 return lyd_print_(out, root, format, options);
93}
94
95API LY_ERR
96lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options)
97{
98 struct ly_out *out;
99
100 LY_CHECK_ARG_RET(NULL, fd != -1, root, LY_EINVAL);
101
Radek Krejci84ce7b12020-06-11 17:28:25 +0200102 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200103 return lyd_print_(out, root, format, options);
104}
105
106API LY_ERR
107lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options)
108{
109 struct ly_out *out;
110
111 LY_CHECK_ARG_RET(NULL, f, root, LY_EINVAL);
112
Radek Krejci84ce7b12020-06-11 17:28:25 +0200113 LY_CHECK_RET(ly_out_new_file(f, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200114 return lyd_print_(out, root, format, options);
115}
116
117API LY_ERR
118lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options)
119{
120 struct ly_out *out;
121
122 LY_CHECK_ARG_RET(NULL, path, root, LY_EINVAL);
123
Radek Krejci84ce7b12020-06-11 17:28:25 +0200124 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200125 return lyd_print_(out, root, format, options);
126}
127
128API LY_ERR
129lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
130 const struct lyd_node *root, LYD_FORMAT format, int options)
131{
132 struct ly_out *out;
133
134 LY_CHECK_ARG_RET(NULL, writeclb, root, LY_EINVAL);
135
Radek Krejci84ce7b12020-06-11 17:28:25 +0200136 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Radek Krejci26da5222020-06-09 17:43:17 +0200137 return lyd_print_(out, root, format, options);
138}