cfgfile: support global properties section
[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 /**@{ cfgfile load operation flags */
70 enum {
71         /**
72          * Indicates that the file supports key value entries before the first
73          * defined section.  These entries can be accessed in the "GLOBAL"
74          * section.
75          */
76         CFG_FLAG_GLOBAL_SECTION = 1,
77 };
78 /**@} */
79
80 /**
81 * Open config file
82 *
83 * @param filename
84 *   Config file name
85 * @param flags
86 *   Config file flags
87 * @return
88 *   Handle to configuration file on success, NULL otherwise
89 */
90 struct rte_cfgfile *rte_cfgfile_load(const char *filename, int flags);
91
92 /**
93 * Get number of sections in config file
94 *
95 * @param cfg
96 *   Config file
97 * @param sec_name
98 *   Section name
99 * @param length
100 *   Maximum section name length
101 * @return
102 *   Number of sections
103 */
104 int rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sec_name,
105         size_t length);
106
107 /**
108 * Get name of all config file sections.
109 *
110 * Fills in the array sections with the name of all the sections in the file
111 * (up to the number of max_sections sections).
112 *
113 * @param cfg
114 *   Config file
115 * @param sections
116 *   Array containing section names after successful invocation. Each element
117 *   of this array should be preallocated by the user with at least
118 *   CFG_NAME_LEN characters.
119 * @param max_sections
120 *   Maximum number of section names to be stored in sections array
121 * @return
122 *   Number of populated sections names
123 */
124 int rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
125         int max_sections);
126
127 /**
128 * Check if given section exists in config file
129 *
130 * @param cfg
131 *   Config file
132 * @param sectionname
133 *   Section name
134 * @return
135 *   TRUE (value different than 0) if section exists, FALSE (value 0) otherwise
136 */
137 int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname);
138
139 /**
140 * Get number of entries in given config file section
141 *
142 * If multiple sections have the given name this function operates on the
143 * first one.
144 *
145 * @param cfg
146 *   Config file
147 * @param sectionname
148 *   Section name
149 * @return
150 *   Number of entries in section on success, -1 otherwise
151 */
152 int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
153         const char *sectionname);
154
155 /**
156 * Get section entries as key-value pairs
157 *
158 * If multiple sections have the given name this function operates on the
159 * first one.
160 *
161 * @param cfg
162 *   Config file
163 * @param sectionname
164 *   Section name
165 * @param entries
166 *   Pre-allocated array of at least max_entries entries where the section
167 *   entries are stored as key-value pair after successful invocation
168 * @param max_entries
169 *   Maximum number of section entries to be stored in entries array
170 * @return
171 *   Number of entries populated on success, -1 otherwise
172 */
173 int rte_cfgfile_section_entries(struct rte_cfgfile *cfg,
174         const char *sectionname,
175         struct rte_cfgfile_entry *entries,
176         int max_entries);
177
178 /**
179 * Get section entries as key-value pairs
180 *
181 * The index of a section is the same as the index of its name in the
182 * result of rte_cfgfile_sections. This API can be used when there are
183 * multiple sections with the same name.
184 *
185 * @param cfg
186 *   Config file
187 * @param index
188 *   Section index
189 * @param sectionname
190 *   Pre-allocated string of at least CFG_NAME_LEN characters where the
191 *   section name is stored after successful invocation.
192 * @param entries
193 *   Pre-allocated array of at least max_entries entries where the section
194 *   entries are stored as key-value pair after successful invocation
195 * @param max_entries
196 *   Maximum number of section entries to be stored in entries array
197 * @return
198 *   Number of entries populated on success, -1 otherwise
199 */
200 int rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg,
201         int index,
202         char *sectionname,
203         struct rte_cfgfile_entry *entries,
204         int max_entries);
205
206 /**
207 * Get value of the named entry in named config file section
208 *
209 * If multiple sections have the given name this function operates on the
210 * first one.
211 *
212 * @param cfg
213 *   Config file
214 * @param sectionname
215 *   Section name
216 * @param entryname
217 *   Entry name
218 * @return
219 *   Entry value on success, NULL otherwise
220 */
221 const char *rte_cfgfile_get_entry(struct rte_cfgfile *cfg,
222         const char *sectionname,
223         const char *entryname);
224
225 /**
226 * Check if given entry exists in named config file section
227 *
228 * If multiple sections have the given name this function operates on the
229 * first one.
230 *
231 * @param cfg
232 *   Config file
233 * @param sectionname
234 *   Section name
235 * @param entryname
236 *   Entry name
237 * @return
238 *   TRUE (value different than 0) if entry exists, FALSE (value 0) otherwise
239 */
240 int rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
241         const char *entryname);
242
243 /**
244 * Close config file
245 *
246 * @param cfg
247 *   Config file
248 * @return
249 *   0 on success, -1 otherwise
250 */
251 int rte_cfgfile_close(struct rte_cfgfile *cfg);
252
253 #ifdef __cplusplus
254 }
255 #endif
256
257 #endif