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 | #include <unistd.h> |
| 19 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 20 | #include "common.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 21 | #include "log.h" |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame^] | 22 | #include "printer.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" |
| 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 Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 32 | * @param[in] options [Data printer flags](@ref dataprinterflags). With \p format LYD_LYB, only #LYDP_WITHSIBLINGS option is accepted. |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 33 | * @return LY_ERR value. |
| 34 | */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 35 | API ssize_t |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 36 | lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options) |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 37 | { |
| 38 | LY_ERR ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 39 | size_t printed_prev; |
| 40 | |
| 41 | LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL); |
| 42 | |
| 43 | printed_prev = out->printed; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 44 | |
| 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 Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 58 | LOGERR(out->ctx, LY_EINVAL, "Unknown output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 59 | ret = LY_EINVAL; |
| 60 | break; |
| 61 | } |
| 62 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 63 | if (ret) { |
| 64 | /* error */ |
| 65 | return (-1) * ret; |
| 66 | } else { |
| 67 | /* success */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 68 | return (ssize_t)(out->printed - printed_prev); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 69 | } |
| 70 | } |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame^] | 71 | |
| 72 | static LY_ERR |
| 73 | lyd_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 | |
| 90 | API LY_ERR |
| 91 | lyd_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 | |
| 104 | API LY_ERR |
| 105 | lyd_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 | |
| 115 | API LY_ERR |
| 116 | lyd_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 | |
| 126 | API LY_ERR |
| 127 | lyd_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 | |
| 137 | API LY_ERR |
| 138 | lyd_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 | } |