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 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 26 | API LY_ERR |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 27 | 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] | 28 | { |
Radek Krejci | b124784 | 2020-07-02 16:22:38 +0200 | [diff] [blame] | 29 | LY_ERR ret = LY_EINVAL; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 30 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 31 | LY_CHECK_ARG_RET(NULL, out, root, LY_EINVAL); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 32 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 33 | /* reset number of printed bytes */ |
| 34 | out->func_printed = 0; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 35 | |
| 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; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 44 | #endif |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 45 | case LYD_LYB: |
| 46 | ret = lyb_print_data(out, root, options); |
| 47 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 48 | case LYD_SCHEMA: |
| 49 | LOGERR(out->ctx, LY_EINVAL, "Invalid output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 50 | ret = LY_EINVAL; |
| 51 | break; |
| 52 | } |
| 53 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 54 | return ret; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 55 | } |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 56 | |
| 57 | static LY_ERR |
| 58 | lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options) |
| 59 | { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 60 | LY_ERR ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 61 | |
| 62 | LY_CHECK_ARG_RET(NULL, out, LY_EINVAL); |
| 63 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 64 | ret = lyd_print(out, root, format, options); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 65 | |
| 66 | ly_out_free(out, NULL, 0); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 67 | return ret; |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | API LY_ERR |
| 71 | lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, int options) |
| 72 | { |
| 73 | struct ly_out *out; |
| 74 | |
| 75 | LY_CHECK_ARG_RET(NULL, strp, root, LY_EINVAL); |
| 76 | |
| 77 | /* init */ |
| 78 | *strp = NULL; |
| 79 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 80 | LY_CHECK_RET(ly_out_new_memory(strp, 0, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 81 | return lyd_print_(out, root, format, options); |
| 82 | } |
| 83 | |
| 84 | API LY_ERR |
| 85 | lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, int options) |
| 86 | { |
| 87 | struct ly_out *out; |
| 88 | |
| 89 | LY_CHECK_ARG_RET(NULL, fd != -1, root, LY_EINVAL); |
| 90 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 91 | LY_CHECK_RET(ly_out_new_fd(fd, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 92 | return lyd_print_(out, root, format, options); |
| 93 | } |
| 94 | |
| 95 | API LY_ERR |
| 96 | lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, int options) |
| 97 | { |
| 98 | struct ly_out *out; |
| 99 | |
| 100 | LY_CHECK_ARG_RET(NULL, f, root, LY_EINVAL); |
| 101 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 102 | LY_CHECK_RET(ly_out_new_file(f, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 103 | return lyd_print_(out, root, format, options); |
| 104 | } |
| 105 | |
| 106 | API LY_ERR |
| 107 | lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, int options) |
| 108 | { |
| 109 | struct ly_out *out; |
| 110 | |
| 111 | LY_CHECK_ARG_RET(NULL, path, root, LY_EINVAL); |
| 112 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 113 | LY_CHECK_RET(ly_out_new_filepath(path, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 114 | return lyd_print_(out, root, format, options); |
| 115 | } |
| 116 | |
| 117 | API LY_ERR |
| 118 | lyd_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, |
| 119 | const struct lyd_node *root, LYD_FORMAT format, int options) |
| 120 | { |
| 121 | struct ly_out *out; |
| 122 | |
| 123 | LY_CHECK_ARG_RET(NULL, writeclb, root, LY_EINVAL); |
| 124 | |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 125 | LY_CHECK_RET(ly_out_new_clb(writeclb, arg, &out)); |
Radek Krejci | 26da522 | 2020-06-09 17:43:17 +0200 | [diff] [blame] | 126 | return lyd_print_(out, root, format, options); |
| 127 | } |