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 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
125 const char *entryvalue)
127 /* resize entry structure if we don't have room for more entries */
128 if (section->num_entries == section->allocated_entries) {
129 struct rte_cfgfile_entry *n_entries = realloc(
131 sizeof(struct rte_cfgfile_entry) *
132 ((section->allocated_entries) +
133 CFG_ALLOC_ENTRY_BATCH));
135 if (n_entries == NULL)
138 section->entries = n_entries;
139 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
141 /* fill up entry fields with key name and value */
142 struct rte_cfgfile_entry *curr_entry =
143 §ion->entries[section->num_entries];
145 snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
146 snprintf(curr_entry->value,
147 sizeof(curr_entry->value), "%s", entryvalue);
148 section->num_entries++;
154 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
156 unsigned int valid_comment;
160 printf("Error - missing cfgfile parameters\n");
165 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
166 if (params->comment_character == valid_comment_chars[i]) {
172 if (valid_comment == 0) {
173 printf("Error - invalid comment characters %c\n",
174 params->comment_character);
182 rte_cfgfile_load(const char *filename, int flags)
184 return rte_cfgfile_load_with_params(filename, flags,
185 &default_cfgfile_params);
189 rte_cfgfile_load_with_params(const char *filename, int flags,
190 const struct rte_cfgfile_parameters *params)
192 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
194 struct rte_cfgfile *cfg = NULL;
196 if (rte_cfgfile_check_params(params))
199 FILE *f = fopen(filename, "r");
203 cfg = rte_cfgfile_create(flags);
205 while (fgets(buffer, sizeof(buffer), f) != NULL) {
207 size_t len = strnlen(buffer, sizeof(buffer));
209 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
210 printf("Error line %d - no \\n found on string. "
211 "Check if line too long\n", lineno);
214 /* skip parsing if comment character found */
215 pos = memchr(buffer, params->comment_character, len);
216 if (pos != NULL && (*(pos-1) != '\\')) {
221 len = _strip(buffer, len);
222 /* skip lines without useful content */
223 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
226 if (buffer[0] == '[') {
227 /* section heading line */
228 char *end = memchr(buffer, ']', len);
230 printf("Error line %d - no terminating ']'"
231 "character found\n", lineno);
235 _strip(&buffer[1], end - &buffer[1]);
237 rte_cfgfile_add_section(cfg, &buffer[1]);
239 /* key and value line */
240 char *split[2] = {NULL};
243 split[1] = memchr(buffer, '=', len);
244 if (split[1] == NULL) {
245 printf("Error line %d - no '='"
246 "character found\n", lineno);
252 _strip(split[0], strlen(split[0]));
253 _strip(split[1], strlen(split[1]));
254 char *end = memchr(split[1], '\\', strlen(split[1]));
256 while (end != NULL) {
257 if (*(end+1) == params->comment_character) {
259 strcat(split[1], end+1);
262 end = memchr(end, '\\', strlen(end));
265 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
266 (*split[1] == '\0')) {
267 printf("Error at line %d - cannot use empty "
272 if (cfg->num_sections == 0)
275 _add_entry(&cfg->sections[cfg->num_sections - 1],
282 rte_cfgfile_close(cfg);
288 rte_cfgfile_create(int flags)
291 struct rte_cfgfile *cfg = NULL;
293 cfg = malloc(sizeof(*cfg));
299 cfg->num_sections = 0;
301 /* allocate first batch of sections and entries */
302 cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
303 CFG_ALLOC_SECTION_BATCH);
305 if (cfg->sections == NULL)
308 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
310 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
311 cfg->sections[i].entries = malloc(sizeof(
312 struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
314 if (cfg->sections[i].entries == NULL)
317 cfg->sections[i].num_entries = 0;
318 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
321 if (flags & CFG_FLAG_GLOBAL_SECTION)
322 rte_cfgfile_add_section(cfg, "GLOBAL");
326 if (cfg->sections != NULL) {
327 for (i = 0; i < cfg->allocated_sections; i++) {
328 if (cfg->sections[i].entries != NULL) {
329 free(cfg->sections[i].entries);
330 cfg->sections[i].entries = NULL;
334 cfg->sections = NULL;
341 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
348 if (sectionname == NULL)
351 /* resize overall struct if we don't have room for more sections */
352 if (cfg->num_sections == cfg->allocated_sections) {
354 struct rte_cfgfile_section *n_sections =
355 realloc(cfg->sections,
356 sizeof(struct rte_cfgfile_section) *
357 ((cfg->allocated_sections) +
358 CFG_ALLOC_SECTION_BATCH));
360 if (n_sections == NULL)
363 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
364 n_sections[i + cfg->allocated_sections].num_entries = 0;
366 cfg->allocated_sections].allocated_entries = 0;
367 n_sections[i + cfg->allocated_sections].entries = NULL;
369 cfg->sections = n_sections;
370 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
373 snprintf(cfg->sections[cfg->num_sections].name,
374 sizeof(cfg->sections[0].name), "%s", sectionname);
375 cfg->sections[cfg->num_sections].num_entries = 0;
381 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
382 const char *sectionname, const char *entryname,
383 const char *entryvalue)
387 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
388 || (entryvalue == NULL))
391 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
394 /* search for section pointer by sectionname */
395 struct rte_cfgfile_section *curr_section = _get_section(cfg,
397 if (curr_section == NULL)
400 ret = _add_entry(curr_section, entryname, entryvalue);
405 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
406 const char *entryname, const char *entryvalue)
410 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
413 /* search for section pointer by sectionname */
414 struct rte_cfgfile_section *curr_section = _get_section(cfg,
416 if (curr_section == NULL)
419 if (entryvalue == NULL)
422 for (i = 0; i < curr_section->num_entries; i++)
423 if (!strcmp(curr_section->entries[i].name, entryname)) {
424 snprintf(curr_section->entries[i].value,
425 sizeof(curr_section->entries[i].value),
429 printf("Error - entry name doesn't exist\n");
433 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
437 if ((cfg == NULL) || (filename == NULL))
440 FILE *f = fopen(filename, "w");
445 for (i = 0; i < cfg->num_sections; i++) {
446 fprintf(f, "[%s]\n", cfg->sections[i].name);
448 for (j = 0; j < cfg->sections[i].num_entries; j++) {
449 fprintf(f, "%s=%s\n",
450 cfg->sections[i].entries[j].name,
451 cfg->sections[i].entries[j].value);
457 int rte_cfgfile_close(struct rte_cfgfile *cfg)
464 if (cfg->sections != NULL) {
465 for (i = 0; i < cfg->allocated_sections; i++) {
466 if (cfg->sections[i].entries != NULL) {
467 free(cfg->sections[i].entries);
468 cfg->sections[i].entries = NULL;
472 cfg->sections = NULL;
481 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
485 int num_sections = 0;
486 for (i = 0; i < cfg->num_sections; i++) {
487 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
494 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
499 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
500 snprintf(sections[i], CFG_NAME_LEN, "%s",
501 cfg->sections[i].name);
507 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
509 return _get_section(cfg, sectionname) != NULL;
513 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
514 const char *sectionname)
516 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
519 return s->num_entries;
523 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
524 char *sectionname, int index)
526 if (index < 0 || index >= cfg->num_sections)
529 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
531 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
532 return sect->num_entries;
535 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
536 struct rte_cfgfile_entry *entries, int max_entries)
539 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
542 for (i = 0; i < max_entries && i < sect->num_entries; i++)
543 entries[i] = sect->entries[i];
548 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
550 struct rte_cfgfile_entry *entries, int max_entries)
553 const struct rte_cfgfile_section *sect;
555 if (index < 0 || index >= cfg->num_sections)
557 sect = &cfg->sections[index];
558 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
559 for (i = 0; i < max_entries && i < sect->num_entries; i++)
560 entries[i] = sect->entries[i];
565 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
566 const char *entryname)
569 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
572 for (i = 0; i < sect->num_entries; i++)
573 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
575 return sect->entries[i].value;
580 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
581 const char *entryname)
583 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;