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