cfgfile: library to interpret config files
[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 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42 * @file
43 * RTE Configuration File
44 *
45 * This library allows reading application defined parameters from standard
46 * format configuration file.
47 *
48 ***/
49
50 #define CFG_NAME_LEN 32
51 #define CFG_VALUE_LEN 64
52
53 /** Configuration file */
54 struct rte_cfgfile;
55
56 /** Configuration file entry */
57 struct rte_cfgfile_entry {
58         char name[CFG_NAME_LEN]; /**< Name */
59         char value[CFG_VALUE_LEN]; /**< Value */
60 };
61
62 /**
63 * Open config file
64 *
65 * @param filename
66 *   Config file name
67 * @param flags
68 *   Config file flags, Reserved for future use. Must be set to 0.
69 * @return
70 *   Handle to configuration file
71 */
72 struct rte_cfgfile *rte_cfgfile_load(const char *filename, int flags);
73
74 /**
75 * Get number of sections in config file
76 *
77 * @param cfg
78 *   Config file
79 * @param sec_name
80 *   Section name
81 * @param length
82 *   Maximum section name length
83 * @return
84 *   0 on success, error code otherwise
85 */
86 int rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sec_name,
87         size_t length);
88
89 /**
90 * Get name of all config file sections.
91 *
92 * Fills in the array sections with the name of all the sections in the file
93 * (up to the number of max_sections sections).
94 *
95 * @param cfg
96 *   Config file
97 * @param sections
98 *   Array containing section names after successful invocation. Each elemen
99 *   of this array should be preallocated by the user with at least
100 *   CFG_NAME_LEN characters.
101 * @param max_sections
102 *   Maximum number of section names to be stored in sections array
103 * @return
104 *   0 on success, error code otherwise
105 */
106 int rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
107         int max_sections);
108
109 /**
110 * Check if given section exists in config file
111 *
112 * @param cfg
113 *   Config file
114 * @param sectionname
115 *   Section name
116 * @return
117 *   TRUE (value different than 0) if section exists, FALSE (value 0) otherwise
118 */
119 int rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname);
120
121 /**
122 * Get number of entries in given config file section
123 *
124 * @param cfg
125 *   Config file
126 * @param sectionname
127 *   Section name
128 * @return
129 *   Number of entries in section
130 */
131 int rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
132         const char *sectionname);
133
134 /** Get section entries as key-value pairs
135 *
136 * @param cfg
137 *   Config file
138 * @param sectionname
139 *   Section name
140 * @param entries
141 *   Pre-allocated array of at least max_entries entries where the section
142 *   entries are stored as key-value pair after successful invocation
143 * @param max_entries
144 *   Maximum number of section entries to be stored in entries array
145 * @return
146 *   0 on success, error code otherwise
147 */
148 int rte_cfgfile_section_entries(struct rte_cfgfile *cfg,
149         const char *sectionname,
150         struct rte_cfgfile_entry *entries,
151         int max_entries);
152
153 /** Get value of the named entry in named config file section
154 *
155 * @param cfg
156 *   Config file
157 * @param sectionname
158 *   Section name
159 * @param entryname
160 *   Entry name
161 * @return
162 *   Entry value
163 */
164 const char *rte_cfgfile_get_entry(struct rte_cfgfile *cfg,
165         const char *sectionname,
166         const char *entryname);
167
168 /** Check if given entry exists in named config file section
169 *
170 * @param cfg
171 *   Config file
172 * @param sectionname
173 *   Section name
174 * @param entryname
175 *   Entry name
176 * @return
177 *   TRUE (value different than 0) if entry exists, FALSE (value 0) otherwise
178 */
179 int rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
180         const char *entryname);
181
182 /** Close config file
183 *
184 * @param cfg
185 *   Config file
186 * @return
187 *   0 on success, error code otherwise
188 */
189 int rte_cfgfile_close(struct rte_cfgfile *cfg);
190
191 #ifdef __cplusplus
192 }
193 #endif
194
195 #endif