a267ed0b63905bd08bc9dfe11c40b9a8cfd88e14
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef __INCLUDE_RTE_CFGFILE_H__
35 #define __INCLUDE_RTE_CFGFILE_H__
36
37 #include <stddef.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @file
45 * RTE Configuration File
46 *
47 * This library allows reading application defined parameters from standard
48 * format configuration file.
49 *
50 ***/
51
52 #ifndef CFG_NAME_LEN
53 #define CFG_NAME_LEN 64
54 #endif
55
56 #ifndef CFG_VALUE_LEN
57 #define CFG_VALUE_LEN 256
58 #endif
59
60 /** Configuration file */
61 struct rte_cfgfile;
62
63 /** Configuration file entry */
64 struct rte_cfgfile_entry {
65         char name[CFG_NAME_LEN]; /**< Name */
66         char value[CFG_VALUE_LEN]; /**< Value */
67 };
68
69 /** Configuration file operation optional arguments */
70 struct rte_cfgfile_parameters {
71         /** Config file comment character; one of '!', '#', '%', ';', '@' */
72         char comment_character;
73 };
74
75 /**@{ cfgfile load operation flags */
76 enum {
77         /**
78          * Indicates that the file supports key value entries before the first
79          * defined section.  These entries can be accessed in the "GLOBAL"
80          * section.
81          */
82         CFG_FLAG_GLOBAL_SECTION = 1,
83 };
84 /**@} */
85
86 /** Defines the default comment character used for parsing config files. */
87 #define CFG_DEFAULT_COMMENT_CHARACTER ';'
88
89 /**
90 * Open config file
91 *
92 * @param filename
93 *   Config file name
94 * @param flags
95 *   Config file flags
96 * @return
97 *   Handle to configuration file on success, NULL otherwise
98 */
99 struct rte_cfgfile *rte_cfgfile_load(const char *filename, int flags);
100
101 /**
102  * Open config file with specified optional parameters.
103  *
104  * @param filename
105  *   Config file name
106  * @param flags
107  *   Config file flags
108  * @param params
109  *   Additional configuration attributes.  Must be configured with desired
110  *   values prior to invoking this API.
111  * @return
112  *   Handle to configuration file on success, NULL otherwise
113  */
114 struct rte_cfgfile *rte_cfgfile_load_with_params(const char *filename,
115         int flags, const struct rte_cfgfile_parameters *params);
116
117 /**
118 * Get number of sections in config file
119 *
120 * @param cfg
121 *   Config file
122 * @param sec_name
123 *   Section name
124 * @param length
125 *   Maximum section name length
126 * @return
127 *   Number of sections
128 */
129 int rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sec_name,
130         size_t length);
131
132 /**
133 * Get name of all config file sections.
134 *
135 * Fills in the array sections with the name of all the sections in the file
136 * (up to the number of max_sections sections).
137 *
138 * @param cfg
139 *   Config file
140 * @param sections
141 *   Array containing section names after successful invocation. Each element
142 *   of this array should be preallocated by the user with at least
143 *   CFG_NAME_LEN characters.
144 * @param max_sections
145 *   Maximum number of section names to be stored in sections array
146 * @return
147 *   Number of populated sections names
148 */
149 int rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
150         int max_sections);
151
152 /**
153 * Check if given section exists in config file
154 *
155 * @param cfg
156 *   Config file
157 * @param sectionname
158 *   Section name
159 * @return
160 *   TRUE (value different than 0) if section exists, FALSE (value 0) otherwise
161 */
162 int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname);
163
164 /**
165 * Get number of entries in given config file section
166 *
167 * If multiple sections have the given name this function operates on the
168 * first one.
169 *
170 * @param cfg
171 *   Config file
172 * @param sectionname
173 *   Section name
174 * @return
175 *   Number of entries in section on success, -1 otherwise
176 */
177 int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
178         const char *sectionname);
179
180 /**
181 * Get section entries as key-value pairs
182 *
183 * If multiple sections have the given name this function operates on the
184 * first one.
185 *
186 * @param cfg
187 *   Config file
188 * @param sectionname
189 *   Section name
190 * @param entries
191 *   Pre-allocated array of at least max_entries entries where the section
192 *   entries are stored as key-value pair after successful invocation
193 * @param max_entries
194 *   Maximum number of section entries to be stored in entries array
195 * @return
196 *   Number of entries populated on success, -1 otherwise
197 */
198 int rte_cfgfile_section_entries(struct rte_cfgfile *cfg,
199         const char *sectionname,
200         struct rte_cfgfile_entry *entries,
201         int max_entries);
202
203 /**
204 * Get section entries as key-value pairs
205 *
206 * The index of a section is the same as the index of its name in the
207 * result of rte_cfgfile_sections. This API can be used when there are
208 * multiple sections with the same name.
209 *
210 * @param cfg
211 *   Config file
212 * @param index
213 *   Section index
214 * @param sectionname
215 *   Pre-allocated string of at least CFG_NAME_LEN characters where the
216 *   section name is stored after successful invocation.
217 * @param entries
218 *   Pre-allocated array of at least max_entries entries where the section
219 *   entries are stored as key-value pair after successful invocation
220 * @param max_entries
221 *   Maximum number of section entries to be stored in entries array
222 * @return
223 *   Number of entries populated on success, -1 otherwise
224 */
225 int rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg,
226         int index,
227         char *sectionname,
228         struct rte_cfgfile_entry *entries,
229         int max_entries);
230
231 /**
232 * Get value of the named entry in named config file section
233 *
234 * If multiple sections have the given name this function operates on the
235 * first one.
236 *
237 * @param cfg
238 *   Config file
239 * @param sectionname
240 *   Section name
241 * @param entryname
242 *   Entry name
243 * @return
244 *   Entry value on success, NULL otherwise
245 */
246 const char *rte_cfgfile_get_entry(struct rte_cfgfile *cfg,
247         const char *sectionname,
248         const char *entryname);
249
250 /**
251 * Check if given entry exists in named config file section
252 *
253 * If multiple sections have the given name this function operates on the
254 * first one.
255 *
256 * @param cfg
257 *   Config file
258 * @param sectionname
259 *   Section name
260 * @param entryname
261 *   Entry name
262 * @return
263 *   TRUE (value different than 0) if entry exists, FALSE (value 0) otherwise
264 */
265 int rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
266         const char *entryname);
267
268 /**
269 * Close config file
270 *
271 * @param cfg
272 *   Config file
273 * @return
274 *   0 on success, -1 otherwise
275 */
276 int rte_cfgfile_close(struct rte_cfgfile *cfg);
277
278 #ifdef __cplusplus
279 }
280 #endif
281
282 #endif