blob: 93ba171d0c8d866baa0b1556a3484ee81fb9d8d2 [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"
aPiecek704f8e92021-08-25 13:35:05 +020019
20struct lyd_node;
Michal Vaskoafac7822020-10-20 14:22:26 +020021
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/**
Michal Vaskoafac7822020-10-20 14:22:26 +020060 * @brief Generic printer of the given format string into the specified output.
61 *
62 * Does not reset printed bytes. Adds to printed bytes.
63 *
64 * @param[in] out Output specification.
65 * @param[in] format Format string to be printed.
66 * @return LY_ERR value.
67 */
68LY_ERR ly_print_(struct ly_out *out, const char *format, ...);
69
70/**
71 * @brief Generic printer of the given string buffer into the specified output.
72 *
73 * Does not reset printed bytes. Adds to printed bytes.
74 *
75 * @param[in] out Output specification.
76 * @param[in] buf Memory buffer with the data to print.
77 * @param[in] len Length of the data to print in the @p buf.
78 * @return LY_ERR value.
79 */
80LY_ERR ly_write_(struct ly_out *out, const char *buf, size_t len);
81
82/**
83 * @brief Create a hole in the output data that will be filled later.
84 *
85 * Adds printed bytes.
86 *
87 * @param[in] out Output specification.
88 * @param[in] len Length of the created hole.
89 * @param[out] position Position of the hole, value must be later provided to the ::ly_write_skipped() call.
90 * @return LY_ERR value.
91 */
92LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position);
93
94/**
95 * @brief Write data into the hole at given position.
96 *
97 * Does not change printed bytes.
98 *
99 * @param[in] out Output specification.
100 * @param[in] position Position of the hole to fill, the value was provided by ::ly_write_skip().
101 * @param[in] buf Memory buffer with the data to print.
102 * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond
103 * to the len value specified in the corresponding ::ly_write_skip() call.
104 * @return LY_ERR value.
105 */
106LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len);
107
108#endif /* LY_OUT_INTERNAL_H_ */