blob: e1577346f4275515575a1078fd21d3a8b85b7226 [file] [log] [blame]
Michal Vaskoafac7822020-10-20 14:22:26 +02001/**
2 * @file out_internal.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Internal structures and functions for libyang
5 *
6 * Copyright (c) 2015-2020 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#ifndef LY_OUT_INTERNAL_H_
16#define LY_OUT_INTERNAL_H_
17
18#include "out.h"
19#include "printer_data.h"
20#include "printer_schema.h"
21
22/**
23 * @brief Printer output structure specifying where the data are printed.
24 */
25struct ly_out {
26 LY_OUT_TYPE type; /**< type of the output to select the output method */
27 union {
28 int fd; /**< file descriptor for LY_OUT_FD type */
29 FILE *f; /**< file structure for LY_OUT_FILE, LY_OUT_FDSTREAM and LY_OUT_FILEPATH types */
30 struct {
31 FILE *f; /**< file stream from the original file descriptor, variable is mapped to the LY_OUT_FILE's f */
32 int fd; /**< original file descriptor, which was not used directly because of missing vdprintf() */
33 } fdstream; /**< structure for LY_OUT_FDSTREAM type, which is LY_OUT_FD when vdprintf() is missing */
34 struct {
35 FILE *f; /**< file structure for LY_OUT_FILEPATH, variable is mapped to the LY_OUT_FILE's f */
36 char *filepath; /**< stored original filepath */
37 } fpath; /**< filepath structure for LY_OUT_FILEPATH */
38 struct {
39 char **buf; /**< storage for the pointer to the memory buffer to store the output */
40 size_t len; /**< number of used bytes in the buffer */
41 size_t size; /**< allocated size of the buffer */
42 } mem; /**< memory buffer information for LY_OUT_MEMORY type */
43 struct {
44 ssize_t (*func)(void *arg, const void *buf, size_t count); /**< callback function */
45 void *arg; /**< optional argument for the callback function */
46 } clb; /**< printer callback for LY_OUT_CALLBACK type */
47 } method; /**< type-specific information about the output */
48
49 /* LYB only */
50 char *buffered; /**< additional buffer for holes */
51 size_t buf_len; /**< number of used bytes in the additional buffer for holes */
52 size_t buf_size; /**< allocated size of the buffer for holes */
53 size_t hole_count; /**< hole counter */
54
55 size_t printed; /**< Total number of printed bytes */
56 size_t func_printed; /**< Number of bytes printed by the last function */
57};
58
59/**
60 * @brief Check whether the node should even be printed.
61 *
62 * @param[in] node Node to check.
63 * @param[in] options Printer options.
64 * @return false (no, it should not be printed) or true (yes, it is supposed to be printed)
65 */
66ly_bool ly_should_print(const struct lyd_node *node, uint32_t options);
67
68/**
69 * @brief Generic printer of the given format string into the specified output.
70 *
71 * Does not reset printed bytes. Adds to printed bytes.
72 *
73 * @param[in] out Output specification.
74 * @param[in] format Format string to be printed.
75 * @return LY_ERR value.
76 */
77LY_ERR ly_print_(struct ly_out *out, const char *format, ...);
78
79/**
80 * @brief Generic printer of the given string buffer into the specified output.
81 *
82 * Does not reset printed bytes. Adds to printed bytes.
83 *
84 * @param[in] out Output specification.
85 * @param[in] buf Memory buffer with the data to print.
86 * @param[in] len Length of the data to print in the @p buf.
87 * @return LY_ERR value.
88 */
89LY_ERR ly_write_(struct ly_out *out, const char *buf, size_t len);
90
91/**
92 * @brief Create a hole in the output data that will be filled later.
93 *
94 * Adds printed bytes.
95 *
96 * @param[in] out Output specification.
97 * @param[in] len Length of the created hole.
98 * @param[out] position Position of the hole, value must be later provided to the ::ly_write_skipped() call.
99 * @return LY_ERR value.
100 */
101LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position);
102
103/**
104 * @brief Write data into the hole at given position.
105 *
106 * Does not change printed bytes.
107 *
108 * @param[in] out Output specification.
109 * @param[in] position Position of the hole to fill, the value was provided by ::ly_write_skip().
110 * @param[in] buf Memory buffer with the data to print.
111 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
112 * to the len value specified in the corresponding ::ly_write_skip() call.
113 * @return LY_ERR value.
114 */
115LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
116
117#endif /* LY_OUT_INTERNAL_H_ */