blob: 7f6a84eadbaaa50c3d07e9df1b504a7974d115d0 [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;
Radek Krejcie7b95092019-05-15 11:03:07 +020035 case LYD_JSON:
36 ret = json_print_data(out, root, options);
37 break;
38 case LYD_LYB:
39 ret = lyb_print_data(out, root, options);
40 break;
Michal Vasko60ea6352020-06-29 13:39:39 +020041 case LYD_SCHEMA:
42 LOGERR(out->ctx, LY_EINVAL, "Invalid output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020043 ret = LY_EINVAL;
44 break;
45 }
46
Michal Vasko63f3d842020-07-08 10:10:14 +020047 return ret;
Radek Krejcie7b95092019-05-15 11:03:07 +020048}
Radek Krejci26da5222020-06-09 17:43:17 +020049
Michal Vasko3a41dff2020-07-15 14:30:28 +020050API LY_ERR
51lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejci26da5222020-06-09 17:43:17 +020052{
Michal Vasko3f5fa762020-08-11 13:44:07 +020053 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020054
Michal Vasko3a41dff2020-07-15 14:30:28 +020055 /* reset the number of printed bytes */
56 out->func_printed = 0;
Radek Krejci26da5222020-06-09 17:43:17 +020057
Michal Vasko3a41dff2020-07-15 14:30:28 +020058 /* get first top-level sibling */
59 while (root->parent) {
60 root = (struct lyd_node *)root->parent;
61 }
62 while (root->prev->next) {
63 root = root->prev;
64 }
Radek Krejci26da5222020-06-09 17:43:17 +020065
Michal Vasko3a41dff2020-07-15 14:30:28 +020066 /* print each top-level sibling */
67 LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS));
68
69 return LY_SUCCESS;
70}
71
72API LY_ERR
73lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
74{
Michal Vasko3f5fa762020-08-11 13:44:07 +020075 LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
Michal Vasko3a41dff2020-07-15 14:30:28 +020076
77 /* reset the number of printed bytes */
78 out->func_printed = 0;
79
80 /* print the subtree */
81 LY_CHECK_RET(lyd_print_(out, root, format, options));
82
83 return LY_SUCCESS;
Radek Krejci26da5222020-06-09 17:43:17 +020084}
85
86API LY_ERR
87lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options)
88{
Michal Vasko3a41dff2020-07-15 14:30:28 +020089 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +020090 struct ly_out *out;
91
Michal Vasko3f5fa762020-08-11 13:44:07 +020092 LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +020093
94 /* init */
95 *strp = NULL;
96
Radek Krejci84ce7b12020-06-11 17:28:25 +020097 LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +020098 ret = lyd_print_(out, root, format, options);
99 ly_out_free(out, NULL, 0);
100 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200101}
102
103API LY_ERR
104lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options)
105{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200106 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200107 struct ly_out *out;
108
Michal Vasko3f5fa762020-08-11 13:44:07 +0200109 LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200110
Radek Krejci84ce7b12020-06-11 17:28:25 +0200111 LY_CHECK_RET(ly_out_new_fd(fd, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200112 ret = lyd_print_(out, root, format, options);
113 ly_out_free(out, NULL, 0);
114 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200115}
116
117API LY_ERR
118lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options)
119{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200120 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200121 struct ly_out *out;
122
Michal Vasko3f5fa762020-08-11 13:44:07 +0200123 LY_CHECK_ARG_RET(NULL, f, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200124
Radek Krejci84ce7b12020-06-11 17:28:25 +0200125 LY_CHECK_RET(ly_out_new_file(f, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200126 ret = lyd_print_(out, root, format, options);
127 ly_out_free(out, NULL, 0);
128 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200129}
130
131API LY_ERR
132lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options)
133{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200134 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200135 struct ly_out *out;
136
Michal Vasko3f5fa762020-08-11 13:44:07 +0200137 LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200138
Radek Krejci84ce7b12020-06-11 17:28:25 +0200139 LY_CHECK_RET(ly_out_new_filepath(path, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200140 ret = lyd_print_(out, root, format, options);
141 ly_out_free(out, NULL, 0);
142 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200143}
144
145API LY_ERR
146lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg,
147 const struct lyd_node *root, LYD_FORMAT format, int options)
148{
Michal Vasko3a41dff2020-07-15 14:30:28 +0200149 LY_ERR ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200150 struct ly_out *out;
151
Michal Vasko3f5fa762020-08-11 13:44:07 +0200152 LY_CHECK_ARG_RET(NULL, writeclb, LY_EINVAL);
Radek Krejci26da5222020-06-09 17:43:17 +0200153
Radek Krejci84ce7b12020-06-11 17:28:25 +0200154 LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200155 ret = lyd_print_(out, root, format, options);
156 ly_out_free(out, NULL, 0);
157 return ret;
Radek Krejci26da5222020-06-09 17:43:17 +0200158}