blob: 02f6ede4dfc10c5cde42b19a0b6f9113b543befc [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
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 Krejcia5bba312020-01-09 15:41:20 +010024#include "printer_data.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020025#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 Krejci241f6b52020-05-21 18:13:49 +020034 * @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 +020035 * @return LY_ERR value.
36 */
Radek Krejcia5bba312020-01-09 15:41:20 +010037API ssize_t
Radek Krejci241f6b52020-05-21 18:13:49 +020038lyd_print(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, int options)
Radek Krejcie7b95092019-05-15 11:03:07 +020039{
40 LY_ERR ret;
Radek Krejcia5bba312020-01-09 15:41:20 +010041 size_t printed_prev;
42
43 LY_CHECK_ARG_RET(NULL, out, root, -LY_EINVAL);
44
45 printed_prev = out->printed;
Radek Krejcie7b95092019-05-15 11:03:07 +020046
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 Krejcidf3da792019-05-17 10:32:24 +020060 LOGERR(out->ctx, LY_EINVAL, "Unknown output format.");
Radek Krejcie7b95092019-05-15 11:03:07 +020061 ret = LY_EINVAL;
62 break;
63 }
64
Radek Krejcie7b95092019-05-15 11:03:07 +020065 if (ret) {
66 /* error */
67 return (-1) * ret;
68 } else {
69 /* success */
Radek Krejcia5bba312020-01-09 15:41:20 +010070 return (ssize_t)(out->printed - printed_prev);
Radek Krejcie7b95092019-05-15 11:03:07 +020071 }
72}