Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser.h |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Generic libyang parsers structures and functions |
| 5 | * |
| 6 | * Copyright (c) 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_PARSER_H_ |
| 16 | #define LY_PARSER_H_ |
| 17 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 18 | #include <stdio.h> |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 21 | #include "log.h" |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 27 | /** |
| 28 | * @brief Parser input structure specifying where the data are read. |
| 29 | */ |
| 30 | struct ly_in; |
| 31 | |
| 32 | /** |
| 33 | * @brief Types of the parser's inputs |
| 34 | */ |
| 35 | typedef enum LY_IN_TYPE { |
| 36 | LY_IN_ERROR = -1, /**< error value to indicate failure of the functions returning LY_IN_TYPE */ |
| 37 | LY_IN_FD, /**< file descriptor printer */ |
| 38 | LY_IN_FILE, /**< FILE stream parser */ |
| 39 | LY_IN_FILEPATH, /**< filepath parser */ |
| 40 | LY_IN_MEMORY /**< memory parser */ |
| 41 | } LY_IN_TYPE; |
| 42 | |
| 43 | /** |
| 44 | * @brief Get input type of the input handler. |
| 45 | * |
| 46 | * @param[in] in Input handler. |
| 47 | * @return Type of the parser's input. |
| 48 | */ |
| 49 | LY_IN_TYPE ly_in_type(const struct ly_in *in); |
| 50 | |
| 51 | /** |
| 52 | * @brief Reset the input medium to read from its beginning, so the following parser function will read from the object's beginning. |
| 53 | * |
| 54 | * Note that in case the underlying output is not seekable (stream referring a pipe/FIFO/socket or the callback output type), |
| 55 | * nothing actually happens despite the function succeeds. Also note that the medium is not returned to the state it was when |
| 56 | * the handler was created. For example, file is seeked into the offset zero, not to the offset where it was opened when |
| 57 | * ly_in_new_file() was called. |
| 58 | * |
| 59 | * @param[in] in Input handler. |
| 60 | * @return LY_SUCCESS in case of success |
| 61 | * @return LY_ESYS in case of failure |
| 62 | */ |
| 63 | LY_ERR ly_in_reset(struct ly_in *in); |
| 64 | |
| 65 | /** |
| 66 | * @brief Create input handler using file descriptor. |
| 67 | * |
| 68 | * @param[in] fd File descriptor to use. |
| 69 | * @param[out] in Created input handler supposed to be passed to different ly*_parse() functions. |
| 70 | * @return LY_SUCCESS in case of success |
| 71 | * @return LY_ERR value in case of failure. |
| 72 | */ |
| 73 | LY_ERR ly_in_new_fd(int fd, struct ly_in **in); |
| 74 | |
| 75 | /** |
| 76 | * @brief Get or reset file descriptor input handler. |
| 77 | * |
| 78 | * @param[in] in Input handler. |
| 79 | * @param[in] fd Optional value of a new file descriptor for the handler. If -1, only the current file descriptor value is returned. |
| 80 | * @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. |
| 81 | * @return -1 in case of error when setting up the new file descriptor. |
| 82 | */ |
| 83 | int ly_in_fd(struct ly_in *in, int fd); |
| 84 | |
| 85 | /** |
| 86 | * @brief Create input handler using file stream. |
| 87 | * |
| 88 | * @param[in] f File stream to use. |
| 89 | * @param[out] in Created input handler supposed to be passed to different ly*_parse() functions. |
| 90 | * @return LY_SUCCESS in case of success |
| 91 | * @return LY_ERR value in case of failure. |
| 92 | */ |
| 93 | LY_ERR ly_in_new_file(FILE *f, struct ly_in **in); |
| 94 | |
| 95 | /** |
| 96 | * @brief Get or reset file stream input handler. |
| 97 | * |
| 98 | * @param[in] in Input handler. |
| 99 | * @param[in] f Optional new file stream for the handler. If NULL, only the current file stream is returned. |
| 100 | * @return NULL in case of invalid argument or an error when setting up the new input file, original input handler @p in is untouched in this case. |
| 101 | * @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. |
| 102 | */ |
| 103 | FILE *ly_in_file(struct ly_in *in, FILE *f); |
| 104 | |
| 105 | /** |
| 106 | * @brief Create input handler using memory to read data. |
| 107 | * |
| 108 | * @param[in] str Pointer where to start reading data. The input data are expected to be NULL-terminated. |
| 109 | * Note that in case the destroy argument of ly_in_free() is used, the input string is passed to free(), |
| 110 | * so if it is really a static string, do not use the destroy argument! |
| 111 | * @param[out] in Created input handler supposed to be passed to different ly*_parse() functions. |
| 112 | * @return LY_SUCCESS in case of success |
| 113 | * @return LY_ERR value in case of failure. |
| 114 | */ |
| 115 | LY_ERR ly_in_new_memory(const char *str, struct ly_in **in); |
| 116 | |
| 117 | /** |
| 118 | * @brief Get or change memory where the data are read from. |
| 119 | * |
| 120 | * @param[in] in Input handler. |
| 121 | * @param[in] str String containing the data to read. The input data are expected to be NULL-terminated. |
| 122 | * Note that in case the destroy argument of ly_in_free() is used, the input string is passed to free(), |
| 123 | * so if it is really a static string, do not use the destroy argument! |
| 124 | * @return Previous starting address to read data from. Note that the caller is responsible to free |
| 125 | * the data in case of changing string pointer @p str. |
| 126 | */ |
| 127 | const char *ly_in_memory(struct ly_in *in, const char *str); |
| 128 | |
| 129 | /** |
| 130 | * @brief Create input handler file of the given filename. |
| 131 | * |
| 132 | * @param[in] filepath Path of the file where to read data. |
| 133 | * @param[in] len Optional number of bytes to use from @p filepath. If 0, the @p filepath is considered to be NULL-terminated and |
| 134 | * the whole string is taken into account. |
| 135 | * @param[out] in Created input handler supposed to be passed to different ly*_parse() functions. |
| 136 | * @return LY_SUCCESS in case of success |
| 137 | * @return LY_ERR value in case of failure. |
| 138 | */ |
| 139 | LY_ERR ly_in_new_filepath(const char *filepath, size_t len, struct ly_in **in); |
| 140 | |
| 141 | /** |
| 142 | * @brief Get or change the filepath of the file where the parser reads the data. |
| 143 | * |
| 144 | * Note that in case of changing the filepath, the current file is closed and a new one is |
| 145 | * created/opened instead of renaming the previous file. Also note that the previous filepath |
| 146 | * string is returned only in case of not changing it's value. |
| 147 | * |
| 148 | * @param[in] in Input handler. |
| 149 | * @param[in] filepath Optional new filepath for the handler. If and only if NULL, the current filepath string is returned. |
| 150 | * @param[in] len Optional number of bytes to use from @p filepath. If 0, the @p filepath is considered to be NULL-terminated and |
| 151 | * the whole string is taken into account. |
| 152 | * @return Previous filepath string in case the @p filepath argument is NULL. |
| 153 | * @return NULL if changing filepath succeedes and ((void *)-1) otherwise. |
| 154 | */ |
| 155 | const char *ly_in_filepath(struct ly_in *in, const char *filepath, size_t len); |
| 156 | |
| 157 | /** |
| 158 | * @brief Free the input handler. |
| 159 | * @param[in] in Input handler to free. |
| 160 | * @param[in] destroy Flag to free the input data buffer (for LY_IN_MEMORY) or to |
| 161 | * close stream/file descriptor (for LY_IN_FD and LY_IN_FILE) |
| 162 | */ |
| 163 | void ly_in_free(struct ly_in *in, int destroy); |
| 164 | |
Radek Krejci | ca376bd | 2020-06-11 16:04:06 +0200 | [diff] [blame] | 165 | #ifdef __cplusplus |
| 166 | } |
| 167 | #endif |
| 168 | |
Radek Krejci | f0e1ba5 | 2020-05-22 15:14:35 +0200 | [diff] [blame] | 169 | #endif /* LY_PARSER_H_ */ |