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" |
| 22 | #include "printer_internal.h" |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 23 | #include "tree_data.h" |
| 24 | |
| 25 | /** |
| 26 | * @brief Common YANG data printer. |
| 27 | * |
| 28 | * @param[in] out Prepared structure defining the type and details of the printer output. |
| 29 | * @param[in] root The root element of the (sub)tree to print. |
| 30 | * @param[in] format Output format. |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 31 | * @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] | 32 | * @return LY_ERR value. |
| 33 | */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 34 | API ssize_t |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 35 | 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] | 36 | { |
| 37 | LY_ERR ret; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 38 | size_t printed_prev; |
| 39 | |
| 40 | LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL); |
| 41 | |
| 42 | printed_prev = out->printed; |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 43 | |
| 44 | switch (format) { |
| 45 | case LYD_XML: |
| 46 | ret = xml_print_data(out, root, options); |
| 47 | break; |
| 48 | #if 0 |
| 49 | case LYD_JSON: |
| 50 | ret = json_print_data(out, root, options); |
| 51 | break; |
| 52 | case LYD_LYB: |
| 53 | ret = lyb_print_data(out, root, options); |
| 54 | break; |
| 55 | #endif |
| 56 | default: |
Radek Krejci | df3da79 | 2019-05-17 10:32:24 +0200 | [diff] [blame] | 57 | LOGERR(out->ctx, LY_EINVAL, "Unknown output format."); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 58 | ret = LY_EINVAL; |
| 59 | break; |
| 60 | } |
| 61 | |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 62 | if (ret) { |
| 63 | /* error */ |
| 64 | return (-1) * ret; |
| 65 | } else { |
| 66 | /* success */ |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 67 | return (ssize_t)(out->printed - printed_prev); |
Radek Krejci | e7b9509 | 2019-05-15 11:03:07 +0200 | [diff] [blame] | 68 | } |
| 69 | } |