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