Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 1 | /** |
| 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 Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Radek Krejci | 535ea9f | 2020-05-29 16:01:05 +0200 | [diff] [blame] | 21 | #include "log.h" |
| 22 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 27 | /** |
| 28 | * @brief Printer output structure specifying where the data are printed. |
| 29 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 30 | struct ly_out; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 31 | |
| 32 | /** |
Radek Krejci | 52f6555 | 2020-09-01 17:03:35 +0200 | [diff] [blame] | 33 | * @brief Common value for data as well as schema printers to avoid formatting indentations and new lines |
| 34 | */ |
| 35 | #define LY_PRINT_SHRINK 0x02 |
| 36 | |
| 37 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 38 | * @brief Types of the printer's output |
| 39 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 40 | typedef enum LY_OUT_TYPE { |
| 41 | LY_OUT_ERROR = -1, /**< error value to indicate failure of the functions returning LY_OUT_TYPE */ |
| 42 | LY_OUT_FD, /**< file descriptor printer */ |
| 43 | LY_OUT_FDSTREAM, /**< internal replacement for LY_OUT_FD in case vdprintf() is not available */ |
| 44 | LY_OUT_FILE, /**< FILE stream printer */ |
| 45 | LY_OUT_FILEPATH, /**< filepath printer */ |
| 46 | LY_OUT_MEMORY, /**< memory printer */ |
| 47 | LY_OUT_CALLBACK /**< callback printer */ |
| 48 | } LY_OUT_TYPE; |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * @brief Get output type of the printer handler. |
| 52 | * |
| 53 | * @param[in] out Printer handler. |
| 54 | * @return Type of the printer's output. |
| 55 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 56 | LY_OUT_TYPE ly_out_type(const struct ly_out *out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @brief Reset the output medium to write from its beginning, so the following printer function will rewrite the current data |
| 60 | * instead of appending. |
| 61 | * |
| 62 | * Note that in case the underlying output is not seekable (stream referring a pipe/FIFO/socket or the callback output type), |
| 63 | * nothing actually happens despite the function succeeds. Also note that the medium is not returned to the state it was when |
Radek Krejci | c5a12e1 | 2020-05-27 17:09:59 +0200 | [diff] [blame] | 64 | * the handler was created. For example, file is seeked into the offset zero and truncated, the content from the time it was opened with |
| 65 | * ly_out_new_file() is not restored. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 66 | * |
| 67 | * @param[in] out Printer handler. |
| 68 | * @return LY_SUCCESS in case of success |
| 69 | * @return LY_ESYS in case of failure |
| 70 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 71 | LY_ERR ly_out_reset(struct ly_out *out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 72 | |
| 73 | /** |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 74 | * @brief Generic write callback for data printed by libyang. |
| 75 | * |
| 76 | * @param[in] user_data Optional caller-specific argument. |
| 77 | * @param[in] buf Data to write. |
| 78 | * @param[in] count Number of bytes to write. |
| 79 | * @return Number of printed bytes. |
| 80 | * @return Negative value in case of error. |
| 81 | */ |
| 82 | typedef ssize_t (*ly_write_clb)(void *user_data, const void *buf, size_t count); |
| 83 | |
| 84 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 85 | * @brief Create printer handler using callback printer function. |
| 86 | * |
| 87 | * @param[in] writeclb Pointer to the printer callback function writing the data (see write(2)). |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 88 | * @param[in] user_data Optional caller-specific argument to be passed to the @p writeclb callback. |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 89 | * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions. |
| 90 | * @return LY_SUCCESS in case of success |
| 91 | * @return LY_EMEM in case allocating the @p out handler fails. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 92 | */ |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 93 | LY_ERR ly_out_new_clb(ly_write_clb writeclb, void *user_data, struct ly_out **out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * @brief Get or reset callback function associated with a callback printer handler. |
| 97 | * |
| 98 | * @param[in] out Printer handler. |
| 99 | * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned. |
| 100 | * @return Previous value of the file descriptor. |
| 101 | */ |
Michal Vasko | ce2b874 | 2020-08-24 13:20:25 +0200 | [diff] [blame] | 102 | ly_write_clb ly_out_clb(struct ly_out *out, ly_write_clb writeclb); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * @brief Get or reset callback function's argument aasociated with a callback printer handler. |
| 106 | * |
| 107 | * @param[in] out Printer handler. |
| 108 | * @param[in] arg caller-specific argument to be passed to the callback function associated with the printer handler. |
| 109 | * If NULL, only the current file descriptor value is returned. |
| 110 | * @return The previous callback argument. |
| 111 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 112 | void *ly_out_clb_arg(struct ly_out *out, void *arg); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * @brief Create printer handler using file descriptor. |
| 116 | * |
| 117 | * @param[in] fd File descriptor to use. |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 118 | * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions. |
| 119 | * @return LY_SUCCESS in case of success |
| 120 | * @return LY_ERR value in case of failure. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 121 | */ |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 122 | LY_ERR ly_out_new_fd(int fd, struct ly_out **out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * @brief Get or reset file descriptor printer handler. |
| 126 | * |
| 127 | * @param[in] out Printer handler. |
| 128 | * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned. |
| 129 | * @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. |
| 130 | * @return -1 in case of error when setting up the new file descriptor. |
| 131 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 132 | int ly_out_fd(struct ly_out *out, int fd); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * @brief Create printer handler using file stream. |
| 136 | * |
| 137 | * @param[in] f File stream to use. |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 138 | * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions. |
| 139 | * @return LY_SUCCESS in case of success |
| 140 | * @return LY_ERR value in case of failure. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 141 | */ |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 142 | LY_ERR ly_out_new_file(FILE *f, struct ly_out **out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * @brief Get or reset file stream printer handler. |
| 146 | * |
| 147 | * @param[in] out Printer handler. |
| 148 | * @param[in] f Optional new file stream for the handler. If NULL, only the current file stream is returned. |
| 149 | * @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. |
| 150 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 151 | FILE *ly_out_file(struct ly_out *out, FILE *f); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * @brief Create printer handler using memory to dump data. |
| 155 | * |
| 156 | * @param[in] strp Pointer to store the resulting data. If it points to a pointer to an allocated buffer and |
| 157 | * @p size of the buffer is set, the buffer is used (and extended if needed) to store the printed data. |
| 158 | * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data |
| 159 | * is newly allocated even if @p strp points to a pointer to an existing buffer. |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 160 | * @param[out] out Created printer handler supposed to be passed to different ly*_print() functions. |
| 161 | * @return LY_SUCCESS in case of success |
| 162 | * @return LY_ERR value in case of failure. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 163 | */ |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 164 | LY_ERR ly_out_new_memory(char **strp, size_t size, struct ly_out **out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * @brief Get or change memory where the data are dumped. |
| 168 | * |
| 169 | * @param[in] out Printer handler. |
Radek Krejci | baeb838 | 2020-05-27 16:44:53 +0200 | [diff] [blame] | 170 | * @param[in] strp Optional new string pointer to store the resulting data, same rules as in ly_out_new_memory() are applied. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 171 | * @param[in] size Size of the buffer provided via @p strp. In case it is 0, the buffer for the printed data |
Radek Krejci | baeb838 | 2020-05-27 16:44:53 +0200 | [diff] [blame] | 172 | * is newly allocated even if @p strp points to a pointer to an existing buffer. In case the @p strp is NULL, this |
| 173 | * parameter is ignored. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 174 | * @return Previous dumped data. Note that the caller is responsible to free the data in case of changing string pointer @p strp. |
| 175 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 176 | char *ly_out_memory(struct ly_out *out, char **strp, size_t size); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * @brief Create printer handler file of the given filename. |
| 180 | * |
| 181 | * @param[in] filepath Path of the file where to write data. |
| 182 | * @return NULL in case of error. |
| 183 | * @return Created printer handler supposed to be passed to different ly*_print_*() functions. |
| 184 | */ |
Radek Krejci | 84ce7b1 | 2020-06-11 17:28:25 +0200 | [diff] [blame] | 185 | LY_ERR ly_out_new_filepath(const char *filepath, struct ly_out **out); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * @brief Get or change the filepath of the file where the printer prints the data. |
| 189 | * |
| 190 | * Note that in case of changing the filepath, the current file is closed and a new one is |
| 191 | * created/opened instead of renaming the previous file. Also note that the previous filepath |
| 192 | * string is returned only in case of not changing it's value. |
| 193 | * |
| 194 | * @param[in] out Printer handler. |
| 195 | * @param[in] filepath Optional new filepath for the handler. If and only if NULL, the current filepath string is returned. |
| 196 | * @return Previous filepath string in case the @p filepath argument is NULL. |
| 197 | * @return NULL if changing filepath succeedes and ((void *)-1) otherwise. |
| 198 | */ |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 199 | const char *ly_out_filepath(struct ly_out *out, const char *filepath); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * @brief Generic printer of the given format string into the specified output. |
| 203 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 204 | * Alternatively, ly_write() can be used. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 205 | * |
| 206 | * @param[in] out Output specification. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 207 | * @param[in] format Format string to be printed. |
| 208 | * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 209 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 210 | LY_ERR ly_print(struct ly_out *out, const char *format, ...); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 211 | |
| 212 | /** |
Radek Krejci | 099fd21 | 2020-05-27 18:17:35 +0200 | [diff] [blame] | 213 | * @brief Flush the output from any internal buffers and clean any auxiliary data. |
| 214 | * @param[in] out Output specification. |
| 215 | */ |
| 216 | void ly_print_flush(struct ly_out *out); |
| 217 | |
| 218 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 219 | * @brief Generic printer of the given string buffer into the specified output. |
| 220 | * |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 221 | * Alternatively, ly_print() can be used. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 222 | * |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 223 | * @param[in] out Output specification. |
| 224 | * @param[in] buf Memory buffer with the data to print. |
| 225 | * @param[in] len Length of the data to print in the @p buf. |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 226 | * @return LY_ERR value, get number of the printed bytes using ::ly_out_printed. |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 227 | */ |
Michal Vasko | 5233e96 | 2020-08-14 14:26:20 +0200 | [diff] [blame] | 228 | LY_ERR ly_write(struct ly_out *out, const char *buf, size_t len); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 229 | |
| 230 | /** |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 231 | * @brief Get the number of printed bytes by the last function. |
| 232 | * |
| 233 | * @param[in] out Out structure used. |
| 234 | * @return Number of printed bytes. |
| 235 | */ |
| 236 | size_t ly_out_printed(const struct ly_out *out); |
| 237 | |
| 238 | /** |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 239 | * @brief Free the printer handler. |
| 240 | * @param[in] out Printer handler to free. |
Radek Krejci | 241f6b5 | 2020-05-21 18:13:49 +0200 | [diff] [blame] | 241 | * @param[in] clb_arg_destructor Freeing function for printer callback (LY_OUT_CALLBACK) argument. |
| 242 | * @param[in] destroy Flag to free allocated buffer (for LY_OUT_MEMORY) or to |
| 243 | * close stream/file descriptor (for LY_OUT_FD, LY_OUT_FDSTREAM and LY_OUT_FILE) |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 244 | */ |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 245 | void ly_out_free(struct ly_out *out, void (*clb_arg_destructor)(void *arg), ly_bool destroy); |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 246 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 247 | #ifdef __cplusplus |
| 248 | } |
| 249 | #endif |
| 250 | |
Radek Krejci | a5bba31 | 2020-01-09 15:41:20 +0100 | [diff] [blame] | 251 | #endif /* LY_PRINTER_H_ */ |