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