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>
40 #include "rte_cfgfile.h"
42 struct rte_cfgfile_section {
43 char name[CFG_NAME_LEN];
45 struct rte_cfgfile_entry *entries[0];
51 struct rte_cfgfile_section *sections[0];
54 /** when we resize a file structure, how many extra entries
55 * for new sections do we add in */
56 #define CFG_ALLOC_SECTION_BATCH 8
57 /** when we resize a section structure, how many extra entries
58 * for new entries do we add in */
59 #define CFG_ALLOC_ENTRY_BATCH 16
62 * Default cfgfile load parameters.
64 static const struct rte_cfgfile_parameters default_cfgfile_params = {
65 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
69 * Defines the list of acceptable comment characters supported by this
72 static const char valid_comment_chars[] = {
81 _strip(char *str, unsigned len)
87 if (isspace(str[len-1])) {
88 /* strip trailing whitespace */
89 while (newlen > 0 && isspace(str[newlen - 1]))
93 if (isspace(str[0])) {
94 /* strip leading whitespace */
96 while (isspace(str[start]) && start < newlen)
100 for (i = 0; i < newlen; i++)
101 str[i] = str[i+start];
108 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
110 unsigned int valid_comment;
114 printf("Error - missing cfgfile parameters\n");
119 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
120 if (params->comment_character == valid_comment_chars[i]) {
126 if (valid_comment == 0) {
127 printf("Error - invalid comment characters %c\n",
128 params->comment_character);
136 rte_cfgfile_load(const char *filename, int flags)
138 return rte_cfgfile_load_with_params(filename, flags,
139 &default_cfgfile_params);
143 rte_cfgfile_load_with_params(const char *filename, int flags,
144 const struct rte_cfgfile_parameters *params)
146 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
147 int allocated_entries = 0;
148 int curr_section = -1;
150 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
152 struct rte_cfgfile *cfg = NULL;
154 if (rte_cfgfile_check_params(params))
157 FILE *f = fopen(filename, "r");
161 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
166 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
168 if (flags & CFG_FLAG_GLOBAL_SECTION) {
170 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
171 cfg->sections[curr_section] = malloc(
172 sizeof(*cfg->sections[0]) +
173 sizeof(cfg->sections[0]->entries[0]) *
175 if (cfg->sections[curr_section] == NULL) {
176 printf("Error - no memory for global section\n");
180 snprintf(cfg->sections[curr_section]->name,
181 sizeof(cfg->sections[0]->name), "GLOBAL");
184 while (fgets(buffer, sizeof(buffer), f) != NULL) {
186 size_t len = strnlen(buffer, sizeof(buffer));
188 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
189 printf("Error line %d - no \\n found on string. "
190 "Check if line too long\n", lineno);
193 pos = memchr(buffer, params->comment_character, len);
199 len = _strip(buffer, len);
200 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
203 if (buffer[0] == '[') {
204 /* section heading line */
205 char *end = memchr(buffer, ']', len);
207 printf("Error line %d - no terminating '['"
208 "character found\n", lineno);
212 _strip(&buffer[1], end - &buffer[1]);
214 /* close off old section and add start new one */
215 if (curr_section >= 0)
216 cfg->sections[curr_section]->num_entries =
220 /* resize overall struct if we don't have room for more
222 if (curr_section == allocated_sections) {
223 allocated_sections += CFG_ALLOC_SECTION_BATCH;
224 struct rte_cfgfile *n_cfg = realloc(cfg,
225 sizeof(*cfg) + sizeof(cfg->sections[0])
226 * allocated_sections);
229 printf("Error - no more memory\n");
235 /* allocate space for new section */
236 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
238 cfg->sections[curr_section] = malloc(
239 sizeof(*cfg->sections[0]) +
240 sizeof(cfg->sections[0]->entries[0]) *
242 if (cfg->sections[curr_section] == NULL) {
243 printf("Error - no more memory\n");
247 snprintf(cfg->sections[curr_section]->name,
248 sizeof(cfg->sections[0]->name),
252 if (curr_section < 0) {
253 printf("Error line %d - value outside of"
254 "section\n", lineno);
258 struct rte_cfgfile_section *sect =
259 cfg->sections[curr_section];
261 char *split[2] = {NULL};
263 split[1] = memchr(buffer, '=', len);
265 /* when delimeter not found */
266 if (split[1] == NULL) {
267 printf("Error at line %d - cannot "
268 "split string\n", lineno);
271 /* when delimeter found */
275 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
276 (*split[1] == '\0')) {
277 printf("Error at line %d - cannot "
278 "split string\n", lineno);
284 if (curr_entry == allocated_entries) {
285 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
286 struct rte_cfgfile_section *n_sect = realloc(
287 sect, sizeof(*sect) +
288 sizeof(sect->entries[0]) *
290 if (n_sect == NULL) {
292 printf("Error - no more memory\n");
295 sect = cfg->sections[curr_section] = n_sect;
298 sect->entries[curr_entry] = malloc(
299 sizeof(*sect->entries[0]));
300 if (sect->entries[curr_entry] == NULL) {
301 printf("Error - no more memory\n");
305 struct rte_cfgfile_entry *entry = sect->entries[
307 snprintf(entry->name, sizeof(entry->name), "%s",
309 snprintf(entry->value, sizeof(entry->value), "%s",
310 split[1] ? split[1] : "");
311 _strip(entry->name, strnlen(entry->name,
312 sizeof(entry->name)));
313 _strip(entry->value, strnlen(entry->value,
314 sizeof(entry->value)));
319 cfg->num_sections = curr_section + 1;
320 /* curr_section will still be -1 if we have an empty file */
321 if (curr_section >= 0)
322 cfg->sections[curr_section]->num_entries = curr_entry + 1;
326 cfg->num_sections = curr_section + 1;
327 if (curr_section >= 0)
328 cfg->sections[curr_section]->num_entries = curr_entry + 1;
329 rte_cfgfile_close(cfg);
336 int rte_cfgfile_close(struct rte_cfgfile *cfg)
343 for (i = 0; i < cfg->num_sections; i++) {
344 if (cfg->sections[i] != NULL) {
345 if (cfg->sections[i]->num_entries) {
346 for (j = 0; j < cfg->sections[i]->num_entries;
348 if (cfg->sections[i]->entries[j] !=
350 free(cfg->sections[i]->
354 free(cfg->sections[i]);
363 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
367 int num_sections = 0;
368 for (i = 0; i < cfg->num_sections; i++) {
369 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
376 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
381 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
382 snprintf(sections[i], CFG_NAME_LEN, "%s",
383 cfg->sections[i]->name);
388 static const struct rte_cfgfile_section *
389 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
392 for (i = 0; i < cfg->num_sections; i++) {
393 if (strncmp(cfg->sections[i]->name, sectionname,
394 sizeof(cfg->sections[0]->name)) == 0)
395 return cfg->sections[i];
401 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
403 return _get_section(cfg, sectionname) != NULL;
407 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
408 const char *sectionname)
410 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
413 return s->num_entries;
417 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
418 char *sectionname, int index)
420 const struct rte_cfgfile_section *sect;
422 if (index < 0 || index >= cfg->num_sections)
425 sect = cfg->sections[index];
426 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
428 return sect->num_entries;
431 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
432 struct rte_cfgfile_entry *entries, int max_entries)
435 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
438 for (i = 0; i < max_entries && i < sect->num_entries; i++)
439 entries[i] = *sect->entries[i];
444 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
446 struct rte_cfgfile_entry *entries, int max_entries)
449 const struct rte_cfgfile_section *sect;
451 if (index < 0 || index >= cfg->num_sections)
454 sect = cfg->sections[index];
455 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
456 for (i = 0; i < max_entries && i < sect->num_entries; i++)
457 entries[i] = *sect->entries[i];
462 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
463 const char *entryname)
466 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
469 for (i = 0; i < sect->num_entries; i++)
470 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
472 return sect->entries[i]->value;
477 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
478 const char *entryname)
480 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;