Coverage Report

Created: 2026-01-25 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libyang/src/printer_data.c
Line
Count
Source
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 "printer_data.h"
16
17
#include <stdio.h>
18
19
#include "common.h"
20
#include "log.h"
21
#include "out.h"
22
#include "out_internal.h"
23
#include "printer_internal.h"
24
#include "tree_data.h"
25
26
static LY_ERR
27
lyd_print_(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
28
0
{
29
0
    LY_ERR ret = LY_SUCCESS;
30
31
0
    switch (format) {
32
0
    case LYD_XML:
33
0
        ret = xml_print_data(out, root, options);
34
0
        break;
35
0
    case LYD_JSON:
36
0
        ret = json_print_data(out, root, options);
37
0
        break;
38
0
    case LYD_LYB:
39
0
        ret = lyb_print_data(out, root, options);
40
0
        break;
41
0
    case LYD_UNKNOWN:
42
0
        LOGINT(root ? LYD_CTX(root) : NULL);
43
0
        ret = LY_EINT;
44
0
        break;
45
0
    }
46
47
0
    return ret;
48
0
}
49
50
API LY_ERR
51
lyd_print_all(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
52
0
{
53
0
    LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
54
55
    /* reset the number of printed bytes */
56
0
    out->func_printed = 0;
57
58
0
    if (root) {
59
        /* get first top-level sibling */
60
0
        while (root->parent) {
61
0
            root = lyd_parent(root);
62
0
        }
63
0
        while (root->prev->next) {
64
0
            root = root->prev;
65
0
        }
66
0
    }
67
68
    /* print each top-level sibling */
69
0
    LY_CHECK_RET(lyd_print_(out, root, format, options | LYD_PRINT_WITHSIBLINGS));
70
71
0
    return LY_SUCCESS;
72
0
}
73
74
API LY_ERR
75
lyd_print_tree(struct ly_out *out, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
76
0
{
77
0
    LY_CHECK_ARG_RET(NULL, out, !(options & LYD_PRINT_WITHSIBLINGS), LY_EINVAL);
78
79
    /* reset the number of printed bytes */
80
0
    out->func_printed = 0;
81
82
    /* print the subtree */
83
0
    LY_CHECK_RET(lyd_print_(out, root, format, options));
84
85
0
    return LY_SUCCESS;
86
0
}
87
88
API LY_ERR
89
lyd_print_mem(char **strp, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
90
0
{
91
0
    LY_ERR ret;
92
0
    struct ly_out *out;
93
94
0
    LY_CHECK_ARG_RET(NULL, strp, LY_EINVAL);
95
96
    /* init */
97
0
    *strp = NULL;
98
99
0
    LY_CHECK_RET(ly_out_new_memory(strp, 0, &out));
100
0
    ret = lyd_print_(out, root, format, options);
101
0
    ly_out_free(out, NULL, 0);
102
0
    return ret;
103
0
}
104
105
API LY_ERR
106
lyd_print_fd(int fd, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
107
0
{
108
0
    LY_ERR ret;
109
0
    struct ly_out *out;
110
111
0
    LY_CHECK_ARG_RET(NULL, fd != -1, LY_EINVAL);
112
113
0
    LY_CHECK_RET(ly_out_new_fd(fd, &out));
114
0
    ret = lyd_print_(out, root, format, options);
115
0
    ly_out_free(out, NULL, 0);
116
0
    return ret;
117
0
}
118
119
API LY_ERR
120
lyd_print_file(FILE *f, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
121
0
{
122
0
    LY_ERR ret;
123
0
    struct ly_out *out;
124
125
0
    LY_CHECK_ARG_RET(NULL, f, LY_EINVAL);
126
127
0
    LY_CHECK_RET(ly_out_new_file(f, &out));
128
0
    ret = lyd_print_(out, root, format, options);
129
0
    ly_out_free(out, NULL, 0);
130
0
    return ret;
131
0
}
132
133
API LY_ERR
134
lyd_print_path(const char *path, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
135
0
{
136
0
    LY_ERR ret;
137
0
    struct ly_out *out;
138
139
0
    LY_CHECK_ARG_RET(NULL, path, LY_EINVAL);
140
141
0
    LY_CHECK_RET(ly_out_new_filepath(path, &out));
142
0
    ret = lyd_print_(out, root, format, options);
143
0
    ly_out_free(out, NULL, 0);
144
0
    return ret;
145
0
}
146
147
API LY_ERR
148
lyd_print_clb(ly_write_clb writeclb, void *user_data, const struct lyd_node *root, LYD_FORMAT format, uint32_t options)
149
0
{
150
0
    LY_ERR ret;
151
0
    struct ly_out *out;
152
153
0
    LY_CHECK_ARG_RET(NULL, writeclb, LY_EINVAL);
154
155
0
    LY_CHECK_RET(ly_out_new_clb(writeclb, user_data, &out));
156
0
    ret = lyd_print_(out, root, format, options);
157
    ly_out_free(out, NULL, 0);
158
0
    return ret;
159
0
}