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.
39 #include <rte_common.h>
41 #include "rte_cfgfile.h"
43 struct rte_cfgfile_section {
44 char name[CFG_NAME_LEN];
46 int allocated_entries;
47 struct rte_cfgfile_entry *entries;
53 int allocated_sections;
54 struct rte_cfgfile_section *sections;
57 /** when we resize a file structure, how many extra entries
58 * for new sections do we add in */
59 #define CFG_ALLOC_SECTION_BATCH 8
60 /** when we resize a section structure, how many extra entries
61 * for new entries do we add in */
62 #define CFG_ALLOC_ENTRY_BATCH 16
65 * Default cfgfile load parameters.
67 static const struct rte_cfgfile_parameters default_cfgfile_params = {
68 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
72 * Defines the list of acceptable comment characters supported by this
75 static const char valid_comment_chars[] = {
84 _strip(char *str, unsigned len)
90 if (isspace(str[len-1])) {
91 /* strip trailing whitespace */
92 while (newlen > 0 && isspace(str[newlen - 1]))
96 if (isspace(str[0])) {
97 /* strip leading whitespace */
99 while (isspace(str[start]) && start < newlen)
103 for (i = 0; i < newlen; i++)
104 str[i] = str[i+start];
110 static struct rte_cfgfile_section *
111 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
115 for (i = 0; i < cfg->num_sections; i++) {
116 if (strncmp(cfg->sections[i].name, sectionname,
117 sizeof(cfg->sections[0].name)) == 0)
118 return &cfg->sections[i];
124 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
126 unsigned int valid_comment;
130 printf("Error - missing cfgfile parameters\n");
135 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
136 if (params->comment_character == valid_comment_chars[i]) {
142 if (valid_comment == 0) {
143 printf("Error - invalid comment characters %c\n",
144 params->comment_character);
152 rte_cfgfile_load(const char *filename, int flags)
154 return rte_cfgfile_load_with_params(filename, flags,
155 &default_cfgfile_params);
159 rte_cfgfile_load_with_params(const char *filename, int flags,
160 const struct rte_cfgfile_parameters *params)
162 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
163 int allocated_entries = 0;
164 int curr_section = -1;
166 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
168 struct rte_cfgfile *cfg = NULL;
170 if (rte_cfgfile_check_params(params))
173 FILE *f = fopen(filename, "r");
177 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
182 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
184 if (flags & CFG_FLAG_GLOBAL_SECTION) {
186 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
187 cfg->sections = malloc(
188 sizeof(cfg->sections[0]) +
189 sizeof(cfg->sections[0].entries) *
191 if (cfg->sections == NULL) {
192 printf("Error - no memory for global section\n");
196 snprintf(cfg->sections[curr_section].name,
197 sizeof(cfg->sections[0].name), "GLOBAL");
200 while (fgets(buffer, sizeof(buffer), f) != NULL) {
202 size_t len = strnlen(buffer, sizeof(buffer));
204 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
205 printf("Error line %d - no \\n found on string. "
206 "Check if line too long\n", lineno);
209 pos = memchr(buffer, params->comment_character, len);
215 len = _strip(buffer, len);
216 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
219 if (buffer[0] == '[') {
220 /* section heading line */
221 char *end = memchr(buffer, ']', len);
223 printf("Error line %d - no terminating '['"
224 "character found\n", lineno);
228 _strip(&buffer[1], end - &buffer[1]);
230 /* close off old section and add start new one */
231 if (curr_section >= 0)
232 cfg->sections[curr_section].num_entries =
236 /* resize overall struct if we don't have room for more
238 if (curr_section == allocated_sections) {
239 allocated_sections += CFG_ALLOC_SECTION_BATCH;
240 struct rte_cfgfile *n_cfg = realloc(cfg,
241 sizeof(*cfg) + sizeof(cfg->sections[0])
242 * allocated_sections);
245 printf("Error - no more memory\n");
251 /* allocate space for new section */
252 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
254 cfg->sections = malloc(
255 sizeof(cfg->sections[0]) +
256 sizeof(cfg->sections[0].entries) *
258 if (cfg->sections == NULL) {
259 printf("Error - no more memory\n");
263 snprintf(cfg->sections[curr_section].name,
264 sizeof(cfg->sections[0].name),
268 if (curr_section < 0) {
269 printf("Error line %d - value outside of"
270 "section\n", lineno);
274 struct rte_cfgfile_section *sect = cfg->sections;
276 char *split[2] = {NULL};
278 split[1] = memchr(buffer, '=', len);
280 /* when delimeter not found */
281 if (split[1] == NULL) {
282 printf("Error at line %d - cannot "
283 "split string\n", lineno);
286 /* when delimeter found */
290 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
291 (*split[1] == '\0')) {
292 printf("Error at line %d - cannot "
293 "split string\n", lineno);
299 if (curr_entry == allocated_entries) {
300 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
301 struct rte_cfgfile_section *n_sect = realloc(
302 sect, sizeof(*sect) +
303 sizeof(sect->entries[0]) *
305 if (n_sect == NULL) {
307 printf("Error - no more memory\n");
310 sect = cfg->sections = n_sect;
313 sect->entries = malloc(
314 sizeof(sect->entries[0]));
315 if (sect->entries == NULL) {
316 printf("Error - no more memory\n");
320 struct rte_cfgfile_entry *entry = sect->entries;
321 snprintf(entry->name, sizeof(entry->name), "%s",
323 snprintf(entry->value, sizeof(entry->value), "%s",
324 split[1] ? split[1] : "");
325 _strip(entry->name, strnlen(entry->name,
326 sizeof(entry->name)));
327 _strip(entry->value, strnlen(entry->value,
328 sizeof(entry->value)));
333 cfg->num_sections = curr_section + 1;
334 /* curr_section will still be -1 if we have an empty file */
335 if (curr_section >= 0)
336 cfg->sections[curr_section].num_entries = curr_entry + 1;
340 cfg->num_sections = curr_section + 1;
341 if (curr_section >= 0)
342 cfg->sections[curr_section].num_entries = curr_entry + 1;
343 rte_cfgfile_close(cfg);
349 int rte_cfgfile_close(struct rte_cfgfile *cfg)
356 if (cfg->sections != NULL) {
357 for (i = 0; i < cfg->allocated_sections; i++) {
358 if (cfg->sections[i].entries != NULL) {
359 free(cfg->sections[i].entries);
360 cfg->sections[i].entries = NULL;
364 cfg->sections = NULL;
373 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
377 int num_sections = 0;
378 for (i = 0; i < cfg->num_sections; i++) {
379 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
386 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
391 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
392 snprintf(sections[i], CFG_NAME_LEN, "%s",
393 cfg->sections[i].name);
399 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
401 return _get_section(cfg, sectionname) != NULL;
405 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
406 const char *sectionname)
408 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
411 return s->num_entries;
415 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
416 char *sectionname, int index)
418 if (index < 0 || index >= cfg->num_sections)
421 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
423 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
424 return sect->num_entries;
427 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
428 struct rte_cfgfile_entry *entries, int max_entries)
431 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
434 for (i = 0; i < max_entries && i < sect->num_entries; i++)
435 entries[i] = sect->entries[i];
440 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
442 struct rte_cfgfile_entry *entries, int max_entries)
445 const struct rte_cfgfile_section *sect;
447 if (index < 0 || index >= cfg->num_sections)
449 sect = &cfg->sections[index];
450 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
451 for (i = 0; i < max_entries && i < sect->num_entries; i++)
452 entries[i] = sect->entries[i];
457 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
458 const char *entryname)
461 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
464 for (i = 0; i < sect->num_entries; i++)
465 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
467 return sect->entries[i].value;
472 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
473 const char *entryname)
475 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;