Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 1 | /** |
| 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" |
aPiecek | 704f8e9 | 2021-08-25 13:35:05 +0200 | [diff] [blame] | 19 | |
| 20 | struct lyd_node; |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * @brief Printer output structure specifying where the data are printed. |
| 24 | */ |
| 25 | struct ly_out { |
| 26 | LY_OUT_TYPE type; /**< type of the output to select the output method */ |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame^] | 27 | |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 28 | union { |
| 29 | int fd; /**< file descriptor for LY_OUT_FD type */ |
| 30 | FILE *f; /**< file structure for LY_OUT_FILE, LY_OUT_FDSTREAM and LY_OUT_FILEPATH types */ |
Michal Vasko | 26bbb27 | 2022-08-02 14:54:33 +0200 | [diff] [blame^] | 31 | |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 32 | struct { |
| 33 | FILE *f; /**< file stream from the original file descriptor, variable is mapped to the LY_OUT_FILE's f */ |
| 34 | int fd; /**< original file descriptor, which was not used directly because of missing vdprintf() */ |
| 35 | } fdstream; /**< structure for LY_OUT_FDSTREAM type, which is LY_OUT_FD when vdprintf() is missing */ |
| 36 | struct { |
| 37 | FILE *f; /**< file structure for LY_OUT_FILEPATH, variable is mapped to the LY_OUT_FILE's f */ |
| 38 | char *filepath; /**< stored original filepath */ |
| 39 | } fpath; /**< filepath structure for LY_OUT_FILEPATH */ |
| 40 | struct { |
| 41 | char **buf; /**< storage for the pointer to the memory buffer to store the output */ |
| 42 | size_t len; /**< number of used bytes in the buffer */ |
| 43 | size_t size; /**< allocated size of the buffer */ |
| 44 | } mem; /**< memory buffer information for LY_OUT_MEMORY type */ |
| 45 | struct { |
| 46 | ssize_t (*func)(void *arg, const void *buf, size_t count); /**< callback function */ |
| 47 | void *arg; /**< optional argument for the callback function */ |
| 48 | } clb; /**< printer callback for LY_OUT_CALLBACK type */ |
| 49 | } method; /**< type-specific information about the output */ |
| 50 | |
| 51 | /* LYB only */ |
| 52 | char *buffered; /**< additional buffer for holes */ |
| 53 | size_t buf_len; /**< number of used bytes in the additional buffer for holes */ |
| 54 | size_t buf_size; /**< allocated size of the buffer for holes */ |
| 55 | size_t hole_count; /**< hole counter */ |
| 56 | |
| 57 | size_t printed; /**< Total number of printed bytes */ |
| 58 | size_t func_printed; /**< Number of bytes printed by the last function */ |
| 59 | }; |
| 60 | |
| 61 | /** |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 62 | * @brief Generic printer of the given format string into the specified output. |
| 63 | * |
| 64 | * Does not reset printed bytes. Adds to printed bytes. |
| 65 | * |
| 66 | * @param[in] out Output specification. |
| 67 | * @param[in] format Format string to be printed. |
| 68 | * @return LY_ERR value. |
| 69 | */ |
| 70 | LY_ERR ly_print_(struct ly_out *out, const char *format, ...); |
| 71 | |
| 72 | /** |
| 73 | * @brief Generic printer of the given string buffer into the specified output. |
| 74 | * |
| 75 | * Does not reset printed bytes. Adds to printed bytes. |
| 76 | * |
| 77 | * @param[in] out Output specification. |
| 78 | * @param[in] buf Memory buffer with the data to print. |
| 79 | * @param[in] len Length of the data to print in the @p buf. |
| 80 | * @return LY_ERR value. |
| 81 | */ |
| 82 | LY_ERR ly_write_(struct ly_out *out, const char *buf, size_t len); |
| 83 | |
| 84 | /** |
| 85 | * @brief Create a hole in the output data that will be filled later. |
| 86 | * |
| 87 | * Adds printed bytes. |
| 88 | * |
| 89 | * @param[in] out Output specification. |
| 90 | * @param[in] len Length of the created hole. |
| 91 | * @param[out] position Position of the hole, value must be later provided to the ::ly_write_skipped() call. |
| 92 | * @return LY_ERR value. |
| 93 | */ |
| 94 | LY_ERR ly_write_skip(struct ly_out *out, size_t len, size_t *position); |
| 95 | |
| 96 | /** |
| 97 | * @brief Write data into the hole at given position. |
| 98 | * |
| 99 | * Does not change printed bytes. |
| 100 | * |
| 101 | * @param[in] out Output specification. |
| 102 | * @param[in] position Position of the hole to fill, the value was provided by ::ly_write_skip(). |
| 103 | * @param[in] buf Memory buffer with the data to print. |
| 104 | * @param[in] len Length of the data to print in the @p buf. Not that the length must correspond |
| 105 | * to the len value specified in the corresponding ::ly_write_skip() call. |
| 106 | * @return LY_ERR value. |
| 107 | */ |
| 108 | LY_ERR ly_write_skipped(struct ly_out *out, size_t position, const char *buf, size_t len); |
| 109 | |
| 110 | #endif /* LY_OUT_INTERNAL_H_ */ |