blob: 3ffbef17db75f3ab67c84f11e72e04e3d156e782 [file] [log] [blame]
Radek Krejcia5bba312020-01-09 15:41:20 +01001/**
2 * @file printer.h
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Generic libyang printer structures and 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#ifndef LY_PRINTER_H_
16#define LY_PRINTER_H_
17
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <stdio.h>
Radek Krejcia5bba312020-01-09 15:41:20 +010019#include <unistd.h>
20
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include "log.h"
22
Radek Krejcica376bd2020-06-11 16:04:06 +020023#ifdef __cplusplus
24extern "C" {
25#endif
26
Radek Krejcia5bba312020-01-09 15:41:20 +010027/**
28 * @brief Printer output structure specifying where the data are printed.
29 */
Radek Krejci241f6b52020-05-21 18:13:49 +020030struct ly_out;
Radek Krejcia5bba312020-01-09 15:41:20 +010031
32/**
33 * @brief Types of the printer's output
34 */
Radek Krejci241f6b52020-05-21 18:13:49 +020035typedef enum LY_OUT_TYPE {
36 LY_OUT_ERROR = -1, /**< error value to indicate failure of the functions returning LY_OUT_TYPE */
37 LY_OUT_FD, /**< file descriptor printer */
38 LY_OUT_FDSTREAM, /**< internal replacement for LY_OUT_FD in case vdprintf() is not available */
39 LY_OUT_FILE, /**< FILE stream printer */
40 LY_OUT_FILEPATH, /**< filepath printer */
41 LY_OUT_MEMORY, /**< memory printer */
42 LY_OUT_CALLBACK /**< callback printer */
43} LY_OUT_TYPE;
Radek Krejcia5bba312020-01-09 15:41:20 +010044
45/**
46 * @brief Get output type of the printer handler.
47 *
48 * @param[in] out Printer handler.
49 * @return Type of the printer's output.
50 */
Radek Krejci241f6b52020-05-21 18:13:49 +020051LY_OUT_TYPE ly_out_type(const struct ly_out *out);
Radek Krejcia5bba312020-01-09 15:41:20 +010052
53/**
54 * @brief Reset the output medium to write from its beginning, so the following printer function will rewrite the current data
55 * instead of appending.
56 *
57 * Note that in case the underlying output is not seekable (stream referring a pipe/FIFO/socket or the callback output type),
58 * nothing actually happens despite the function succeeds. Also note that the medium is not returned to the state it was when
Radek Krejcic5a12e12020-05-27 17:09:59 +020059 * the handler was created. For example, file is seeked into the offset zero and truncated, the content from the time it was opened with
60 * ly_out_new_file() is not restored.
Radek Krejcia5bba312020-01-09 15:41:20 +010061 *
62 * @param[in] out Printer handler.
63 * @return LY_SUCCESS in case of success
64 * @return LY_ESYS in case of failure
65 */
Radek Krejci241f6b52020-05-21 18:13:49 +020066LY_ERR ly_out_reset(struct ly_out *out);
Radek Krejcia5bba312020-01-09 15:41:20 +010067
68/**
Michal Vaskoce2b8742020-08-24 13:20:25 +020069 * @brief Generic write callback for data printed by libyang.
70 *
71 * @param[in] user_data Optional caller-specific argument.
72 * @param[in] buf Data to write.
73 * @param[in] count Number of bytes to write.
74 * @return Number of printed bytes.
75 * @return Negative value in case of error.
76 */
77typedef ssize_t (*ly_write_clb)(void *user_data, const void *buf, size_t count);
78
79/**
Radek Krejcia5bba312020-01-09 15:41:20 +010080 * @brief Create printer handler using callback printer function.
81 *
82 * @param[in] writeclb Pointer to the printer callback function writing the data (see write(2)).
Michal Vaskoce2b8742020-08-24 13:20:25 +020083 * @param[in] user_data Optional caller-specific argument to be passed to the @p writeclb callback.
Radek Krejci84ce7b12020-06-11 17:28:25 +020084 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
85 * @return LY_SUCCESS in case of success
86 * @return LY_EMEM in case allocating the @p out handler fails.
Radek Krejcia5bba312020-01-09 15:41:20 +010087 */
Michal Vaskoce2b8742020-08-24 13:20:25 +020088LY_ERR ly_out_new_clb(ly_write_clb writeclb, void *user_data, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +010089
90/**
91 * @brief Get or reset callback function associated with a callback printer handler.
92 *
93 * @param[in] out Printer handler.
94 * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned.
95 * @return Previous value of the file descriptor.
96 */
Michal Vaskoce2b8742020-08-24 13:20:25 +020097ly_write_clb ly_out_clb(struct ly_out *out, ly_write_clb writeclb);
Radek Krejcia5bba312020-01-09 15:41:20 +010098
99/**
100 * @brief Get or reset callback function's argument aasociated with a callback printer handler.
101 *
102 * @param[in] out Printer handler.
103 * @param[in] arg caller-specific argument to be passed to the callback function associated with the printer handler.
104 * If NULL, only the current file descriptor value is returned.
105 * @return The previous callback argument.
106 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200107void *ly_out_clb_arg(struct ly_out *out, void *arg);
Radek Krejcia5bba312020-01-09 15:41:20 +0100108
109/**
110 * @brief Create printer handler using file descriptor.
111 *
112 * @param[in] fd File descriptor to use.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200113 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
114 * @return LY_SUCCESS in case of success
115 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100116 */
Radek Krejci84ce7b12020-06-11 17:28:25 +0200117LY_ERR ly_out_new_fd(int fd, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100118
119/**
120 * @brief Get or reset file descriptor printer handler.
121 *
122 * @param[in] out Printer handler.
123 * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned.
124 * @return Previous value of the file descriptor. Note that caller is responsible for closing the returned file descriptor in case of setting new descriptor @p fd.
125 * @return -1 in case of error when setting up the new file descriptor.
126 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200127int ly_out_fd(struct ly_out *out, int fd);
Radek Krejcia5bba312020-01-09 15:41:20 +0100128
129/**
130 * @brief Create printer handler using file stream.
131 *
132 * @param[in] f File stream to use.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200133 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
134 * @return LY_SUCCESS in case of success
135 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100136 */
Radek Krejci84ce7b12020-06-11 17:28:25 +0200137LY_ERR ly_out_new_file(FILE *f, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100138
139/**
140 * @brief Get or reset file stream printer handler.
141 *
142 * @param[in] out Printer handler.
143 * @param[in] f Optional new file stream for the handler. If NULL, only the current file stream is returned.
144 * @return Previous file stream of the handler. Note that caller is responsible for closing the returned stream in case of setting new stream @p f.
145 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200146FILE *ly_out_file(struct ly_out *out, FILE *f);
Radek Krejcia5bba312020-01-09 15:41:20 +0100147
148/**
149 * @brief Create printer handler using memory to dump data.
150 *
151 * @param[in] strp Pointer to store the resulting data. If it points to a pointer to an allocated buffer and
152 * @p size of the buffer is set, the buffer is used (and extended if needed) to store the printed data.
153 * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
154 * is newly allocated even if @p strp points to a pointer to an existing buffer.
Radek Krejci84ce7b12020-06-11 17:28:25 +0200155 * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions.
156 * @return LY_SUCCESS in case of success
157 * @return LY_ERR value in case of failure.
Radek Krejcia5bba312020-01-09 15:41:20 +0100158 */
Radek Krejci84ce7b12020-06-11 17:28:25 +0200159LY_ERR ly_out_new_memory(char **strp, size_t size, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100160
161/**
162 * @brief Get or change memory where the data are dumped.
163 *
164 * @param[in] out Printer handler.
Radek Krejcibaeb8382020-05-27 16:44:53 +0200165 * @param[in] strp Optional new string pointer to store the resulting data, same rules as in ly_out_new_memory() are applied.
Radek Krejcia5bba312020-01-09 15:41:20 +0100166 * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data
Radek Krejcibaeb8382020-05-27 16:44:53 +0200167 * is newly allocated even if @p strp points to a pointer to an existing buffer. In case the @p strp is NULL, this
168 * parameter is ignored.
Radek Krejcia5bba312020-01-09 15:41:20 +0100169 * @return Previous dumped data. Note that the caller is responsible to free the data in case of changing string pointer @p strp.
170 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200171char *ly_out_memory(struct ly_out *out, char **strp, size_t size);
Radek Krejcia5bba312020-01-09 15:41:20 +0100172
173/**
174 * @brief Create printer handler file of the given filename.
175 *
176 * @param[in] filepath Path of the file where to write data.
177 * @return NULL in case of error.
178 * @return Created printer handler supposed to be passed to different ly*_print_*() functions.
179 */
Radek Krejci84ce7b12020-06-11 17:28:25 +0200180LY_ERR ly_out_new_filepath(const char *filepath, struct ly_out **out);
Radek Krejcia5bba312020-01-09 15:41:20 +0100181
182/**
183 * @brief Get or change the filepath of the file where the printer prints the data.
184 *
185 * Note that in case of changing the filepath, the current file is closed and a new one is
186 * created/opened instead of renaming the previous file. Also note that the previous filepath
187 * string is returned only in case of not changing it's value.
188 *
189 * @param[in] out Printer handler.
190 * @param[in] filepath Optional new filepath for the handler. If and only if NULL, the current filepath string is returned.
191 * @return Previous filepath string in case the @p filepath argument is NULL.
192 * @return NULL if changing filepath succeedes and ((void *)-1) otherwise.
193 */
Radek Krejci241f6b52020-05-21 18:13:49 +0200194const char *ly_out_filepath(struct ly_out *out, const char *filepath);
Radek Krejcia5bba312020-01-09 15:41:20 +0100195
196/**
197 * @brief Generic printer of the given format string into the specified output.
198 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200199 * Alternatively, ly_write() can be used.
Radek Krejcia5bba312020-01-09 15:41:20 +0100200 *
201 * @param[in] out Output specification.
Michal Vasko5233e962020-08-14 14:26:20 +0200202 * @param[in] format Format string to be printed.
203 * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed.
Radek Krejcia5bba312020-01-09 15:41:20 +0100204 */
Michal Vasko5233e962020-08-14 14:26:20 +0200205LY_ERR ly_print(struct ly_out *out, const char *format, ...);
Radek Krejcia5bba312020-01-09 15:41:20 +0100206
207/**
Radek Krejci099fd212020-05-27 18:17:35 +0200208 * @brief Flush the output from any internal buffers and clean any auxiliary data.
209 * @param[in] out Output specification.
210 */
211void ly_print_flush(struct ly_out *out);
212
213/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100214 * @brief Generic printer of the given string buffer into the specified output.
215 *
Radek Krejci241f6b52020-05-21 18:13:49 +0200216 * Alternatively, ly_print() can be used.
Radek Krejcia5bba312020-01-09 15:41:20 +0100217 *
Radek Krejcia5bba312020-01-09 15:41:20 +0100218 * @param[in] out Output specification.
219 * @param[in] buf Memory buffer with the data to print.
220 * @param[in] len Length of the data to print in the @p buf.
Michal Vasko5233e962020-08-14 14:26:20 +0200221 * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed.
Radek Krejcia5bba312020-01-09 15:41:20 +0100222 */
Michal Vasko5233e962020-08-14 14:26:20 +0200223LY_ERR ly_write(struct ly_out *out, const char *buf, size_t len);
Radek Krejcia5bba312020-01-09 15:41:20 +0100224
225/**
Michal Vasko63f3d842020-07-08 10:10:14 +0200226 * @brief Get the number of printed bytes by the last function.
227 *
228 * @param[in] out Out structure used.
229 * @return Number of printed bytes.
230 */
231size_t ly_out_printed(const struct ly_out *out);
232
233/**
Radek Krejcia5bba312020-01-09 15:41:20 +0100234 * @brief Free the printer handler.
235 * @param[in] out Printer handler to free.
Radek Krejci241f6b52020-05-21 18:13:49 +0200236 * @param[in] clb_arg_destructor Freeing function for printer callback (LY_OUT_CALLBACK) argument.
237 * @param[in] destroy Flag to free allocated buffer (for LY_OUT_MEMORY) or to
238 * close stream/file descriptor (for LY_OUT_FD, LY_OUT_FDSTREAM and LY_OUT_FILE)
Radek Krejcia5bba312020-01-09 15:41:20 +0100239 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200240void ly_out_free(struct ly_out *out, void (*clb_arg_destructor)(void *arg), uint8_t destroy);
Radek Krejcia5bba312020-01-09 15:41:20 +0100241
Radek Krejcica376bd2020-06-11 16:04:06 +0200242#ifdef __cplusplus
243}
244#endif
245
Radek Krejcia5bba312020-01-09 15:41:20 +0100246#endif /* LY_PRINTER_H_ */