4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <rte_common.h>
39 #include <rte_string_fns.h>
41 #include "rte_cfgfile.h"
43 struct rte_cfgfile_section {
44 char name[CFG_NAME_LEN];
46 struct rte_cfgfile_entry *entries[0];
52 struct rte_cfgfile_section *sections[0];
55 /** when we resize a file structure, how many extra entries
56 * for new sections do we add in */
57 #define CFG_ALLOC_SECTION_BATCH 8
58 /** when we resize a section structure, how many extra entries
59 * for new entries do we add in */
60 #define CFG_ALLOC_ENTRY_BATCH 16
63 * Default cfgfile load parameters.
65 static const struct rte_cfgfile_parameters default_cfgfile_params = {
66 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
70 * Defines the list of acceptable comment characters supported by this
73 static const char valid_comment_chars[] = {
82 _strip(char *str, unsigned len)
88 if (isspace(str[len-1])) {
89 /* strip trailing whitespace */
90 while (newlen > 0 && isspace(str[newlen - 1]))
94 if (isspace(str[0])) {
95 /* strip leading whitespace */
97 while (isspace(str[start]) && start < newlen)
101 for (i = 0; i < newlen; i++)
102 str[i] = str[i+start];
109 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
111 unsigned int valid_comment;
115 printf("Error - missing cfgfile parameters\n");
120 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
121 if (params->comment_character == valid_comment_chars[i]) {
127 if (valid_comment == 0) {
128 printf("Error - invalid comment characters %c\n",
129 params->comment_character);
137 rte_cfgfile_load(const char *filename, int flags)
139 return rte_cfgfile_load_with_params(filename, flags,
140 &default_cfgfile_params);
144 rte_cfgfile_load_with_params(const char *filename, int flags,
145 const struct rte_cfgfile_parameters *params)
147 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
148 int allocated_entries = 0;
149 int curr_section = -1;
151 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
153 struct rte_cfgfile *cfg = NULL;
155 if (rte_cfgfile_check_params(params))
158 FILE *f = fopen(filename, "r");
162 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
167 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
169 if (flags & CFG_FLAG_GLOBAL_SECTION) {
171 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
172 cfg->sections[curr_section] = malloc(
173 sizeof(*cfg->sections[0]) +
174 sizeof(cfg->sections[0]->entries[0]) *
176 if (cfg->sections[curr_section] == NULL) {
177 printf("Error - no memory for global section\n");
181 snprintf(cfg->sections[curr_section]->name,
182 sizeof(cfg->sections[0]->name), "GLOBAL");
185 while (fgets(buffer, sizeof(buffer), f) != NULL) {
187 size_t len = strnlen(buffer, sizeof(buffer));
189 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
190 printf("Error line %d - no \\n found on string. "
191 "Check if line too long\n", lineno);
194 pos = memchr(buffer, params->comment_character, len);
200 len = _strip(buffer, len);
201 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
204 if (buffer[0] == '[') {
205 /* section heading line */
206 char *end = memchr(buffer, ']', len);
208 printf("Error line %d - no terminating '['"
209 "character found\n", lineno);
213 _strip(&buffer[1], end - &buffer[1]);
215 /* close off old section and add start new one */
216 if (curr_section >= 0)
217 cfg->sections[curr_section]->num_entries =
221 /* resize overall struct if we don't have room for more
223 if (curr_section == allocated_sections) {
224 allocated_sections += CFG_ALLOC_SECTION_BATCH;
225 struct rte_cfgfile *n_cfg = realloc(cfg,
226 sizeof(*cfg) + sizeof(cfg->sections[0])
227 * allocated_sections);
230 printf("Error - no more memory\n");
236 /* allocate space for new section */
237 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
239 cfg->sections[curr_section] = malloc(
240 sizeof(*cfg->sections[0]) +
241 sizeof(cfg->sections[0]->entries[0]) *
243 if (cfg->sections[curr_section] == NULL) {
244 printf("Error - no more memory\n");
248 snprintf(cfg->sections[curr_section]->name,
249 sizeof(cfg->sections[0]->name),
253 if (curr_section < 0) {
254 printf("Error line %d - value outside of"
255 "section\n", lineno);
259 struct rte_cfgfile_section *sect =
260 cfg->sections[curr_section];
262 if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
264 printf("Error at line %d - cannot split "
270 if (curr_entry == allocated_entries) {
271 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
272 struct rte_cfgfile_section *n_sect = realloc(
273 sect, sizeof(*sect) +
274 sizeof(sect->entries[0]) *
276 if (n_sect == NULL) {
278 printf("Error - no more memory\n");
281 sect = cfg->sections[curr_section] = n_sect;
284 sect->entries[curr_entry] = malloc(
285 sizeof(*sect->entries[0]));
286 if (sect->entries[curr_entry] == NULL) {
287 printf("Error - no more memory\n");
291 struct rte_cfgfile_entry *entry = sect->entries[
293 snprintf(entry->name, sizeof(entry->name), "%s",
295 snprintf(entry->value, sizeof(entry->value), "%s",
297 _strip(entry->name, strnlen(entry->name,
298 sizeof(entry->name)));
299 _strip(entry->value, strnlen(entry->value,
300 sizeof(entry->value)));
305 cfg->num_sections = curr_section + 1;
306 /* curr_section will still be -1 if we have an empty file */
307 if (curr_section >= 0)
308 cfg->sections[curr_section]->num_entries = curr_entry + 1;
312 cfg->num_sections = curr_section + 1;
313 if (curr_section >= 0)
314 cfg->sections[curr_section]->num_entries = curr_entry + 1;
315 rte_cfgfile_close(cfg);
322 int rte_cfgfile_close(struct rte_cfgfile *cfg)
329 for (i = 0; i < cfg->num_sections; i++) {
330 if (cfg->sections[i] != NULL) {
331 if (cfg->sections[i]->num_entries) {
332 for (j = 0; j < cfg->sections[i]->num_entries;
334 if (cfg->sections[i]->entries[j] !=
336 free(cfg->sections[i]->
340 free(cfg->sections[i]);
349 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
353 int num_sections = 0;
354 for (i = 0; i < cfg->num_sections; i++) {
355 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
362 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
367 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
368 snprintf(sections[i], CFG_NAME_LEN, "%s",
369 cfg->sections[i]->name);
374 static const struct rte_cfgfile_section *
375 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
378 for (i = 0; i < cfg->num_sections; i++) {
379 if (strncmp(cfg->sections[i]->name, sectionname,
380 sizeof(cfg->sections[0]->name)) == 0)
381 return cfg->sections[i];
387 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
389 return _get_section(cfg, sectionname) != NULL;
393 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
394 const char *sectionname)
396 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
399 return s->num_entries;
404 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
405 struct rte_cfgfile_entry *entries, int max_entries)
408 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
411 for (i = 0; i < max_entries && i < sect->num_entries; i++)
412 entries[i] = *sect->entries[i];
417 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
419 struct rte_cfgfile_entry *entries, int max_entries)
422 const struct rte_cfgfile_section *sect;
424 if (index < 0 || index >= cfg->num_sections)
427 sect = cfg->sections[index];
428 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
429 for (i = 0; i < max_entries && i < sect->num_entries; i++)
430 entries[i] = *sect->entries[i];
435 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
436 const char *entryname)
439 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
442 for (i = 0; i < sect->num_entries; i++)
443 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
445 return sect->entries[i]->value;
450 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
451 const char *entryname)
453 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;