Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 1 | /** |
| 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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 15 | #include "printer_data.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 16 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 18 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 19 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 20 | #include "log.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 21 | #include "out.h" |
| 22 | #include "out_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "printer_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 24 | #include "tree_data.h" |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 25 | #include "tree_schema.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 26 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 27 | static LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 28 | lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 29 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 30 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 31 | |
| 32 | switch (format) { |
| 33 | case LYD_XML: |
| 34 | ret = xml_print_data(out, root, options); |
| 35 | break; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 36 | case LYD_JSON: |
| 37 | ret = json_print_data(out, root, options); |
| 38 | break; |
| 39 | case LYD_LYB: |
| 40 | ret = lyb_print_data(out, root, options); |
| 41 | break; |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 42 | case LYD_UNKNOWN: |
Michal Vasko | b7be7a8 | 2020-08-20 09:09:04 +0200 | [diff] [blame] | 43 | LOGINT(root ? LYD_CTX(root) : NULL); |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 44 | ret = LY_EINT; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 45 | break; |
| 46 | } |
| 47 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 48 | return ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 49 | } |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 50 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 51 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 52 | lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 53 | { |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 54 | LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 55 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 56 | /* reset the number of printed bytes */ |
| 57 | out->func_printed = 0; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 58 | |
Radek Krejci | 2e87477 | 2020-08-28 16:36:33 +0200 | [diff] [blame] | 59 | if (root) { |
| 60 | /* 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 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 67 | } |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 68 | |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 69 | /* print each top-level sibling */ |
| 70 | LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS)); |
| 71 | |
| 72 | return LY_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 76 | lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 77 | { |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 78 | LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 79 | |
| 80 | /* reset the number of printed bytes */ |
| 81 | out->func_printed = 0; |
| 82 | |
| 83 | /* print the subtree */ |
| 84 | LY_CHECK_RET(lyd_print_(out, root, format, options)); |
| 85 | |
| 86 | return LY_SUCCESS; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 90 | lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 91 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 92 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 93 | struct ly_out *out; |
| 94 | |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 95 | LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 96 | |
| 97 | /* init */ |
| 98 | *strp = NULL; |
| 99 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 100 | LY_CHECK_RET(ly_out_new_memory(strp, 0, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 101 | ret = lyd_print_(out, root, format, options); |
| 102 | ly_out_free(out, NULL, 0); |
| 103 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 107 | lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 108 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 109 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 110 | struct ly_out *out; |
| 111 | |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 112 | LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 113 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 114 | LY_CHECK_RET(ly_out_new_fd(fd, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 115 | ret = lyd_print_(out, root, format, options); |
| 116 | ly_out_free(out, NULL, 0); |
| 117 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 121 | lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 122 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 123 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 124 | struct ly_out *out; |
| 125 | |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 126 | LY_CHECK_ARG_RET(NULL, f, LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 127 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 128 | LY_CHECK_RET(ly_out_new_file(f, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 129 | ret = lyd_print_(out, root, format, options); |
| 130 | ly_out_free(out, NULL, 0); |
| 131 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 135 | lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 136 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 137 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 138 | struct ly_out *out; |
| 139 | |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 140 | LY_CHECK_ARG_RET(NULL, path, LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 141 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 142 | LY_CHECK_RET(ly_out_new_filepath(path, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 143 | ret = lyd_print_(out, root, format, options); |
| 144 | ly_out_free(out, NULL, 0); |
| 145 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | API LY_ERR |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 149 | lyd_print_clb(ly_write_clb writeclb, void *user_data, const struct lyd_node *root, LYD_FORMAT format, uint32_t options) |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 150 | { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 151 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 152 | struct ly_out *out; |
| 153 | |
Michal Vasko | 3f5fa76 | 2020-08-11 13:44:07 +0200 | [diff] [blame] | 154 | LY_CHECK_ARG_RET(NULL, writeclb, LY_EINVAL); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 155 | |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 156 | LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out)); |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 157 | ret = lyd_print_(out, root, format, options); |
| 158 | ly_out_free(out, NULL, 0); |
| 159 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 160 | } |