blob: ffa0462d25a9c41f473f6f505e8c97d28e1a98c8 [file] [log] [blame]
Radek Krejcie7b95092019-05-15 11:03:07 +02001/**
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 Krejci535ea9f2020-05-29 16:01:05 +020015#include "printer_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020016
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020018#include <unistd.h>
19
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "common.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020021#include "log.h"
22#include "printer_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020023#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 Krejci241f6b52020-05-21 18:13:49 +020031 * @param[in] options [Data printer flags](@ref dataprinterflags). With \p format LYD_LYB, only #LYDP_WITHSIBLINGS option is accepted.
Radek Krejcie7b95092019-05-15 11:03:07 +020032 * @return LY_ERR value.
33 */
Radek Krejcia5bba312020-01-09 15:41:20 +010034API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020035lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020036{
37 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010038 size_t printed_prev;
39
40 LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL);
41
42 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020043
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 Krejcidf3da792019-05-17 10:32:24 +020057 LOGERR(out->ctx, LY_EINVAL, "Unknown output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020058 ret = LY_EINVAL;
59 break;
60 }
61
Radek Krejcie7b95092019-05-15 11:03:07 +020062 if (ret) {
63 /* error */
64 return (-1) * ret;
65 } else {
66 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +010067 return (ssize_t)(out->printed - printed_prev);
Radek Krejcie7b95092019-05-15 11:03:07 +020068 }
69}