1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_common.h>
12 #include "rte_cfgfile.h"
14 struct rte_cfgfile_section {
15 char name[CFG_NAME_LEN];
17 int allocated_entries;
18 struct rte_cfgfile_entry *entries;
24 int allocated_sections;
25 struct rte_cfgfile_section *sections;
28 /** when we resize a file structure, how many extra entries
29 * for new sections do we add in */
30 #define CFG_ALLOC_SECTION_BATCH 8
31 /** when we resize a section structure, how many extra entries
32 * for new entries do we add in */
33 #define CFG_ALLOC_ENTRY_BATCH 16
36 * Default cfgfile load parameters.
38 static const struct rte_cfgfile_parameters default_cfgfile_params = {
39 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
43 * Defines the list of acceptable comment characters supported by this
46 static const char valid_comment_chars[] = {
55 _strip(char *str, unsigned len)
61 if (isspace(str[len-1])) {
62 /* strip trailing whitespace */
63 while (newlen > 0 && isspace(str[newlen - 1]))
67 if (isspace(str[0])) {
68 /* strip leading whitespace */
70 while (isspace(str[start]) && start < newlen)
74 for (i = 0; i < newlen; i++)
75 str[i] = str[i+start];
81 static struct rte_cfgfile_section *
82 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
86 for (i = 0; i < cfg->num_sections; i++) {
87 if (strncmp(cfg->sections[i].name, sectionname,
88 sizeof(cfg->sections[0].name)) == 0)
89 return &cfg->sections[i];
95 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
96 const char *entryvalue)
98 /* resize entry structure if we don't have room for more entries */
99 if (section->num_entries == section->allocated_entries) {
100 struct rte_cfgfile_entry *n_entries = realloc(
102 sizeof(struct rte_cfgfile_entry) *
103 ((section->allocated_entries) +
104 CFG_ALLOC_ENTRY_BATCH));
106 if (n_entries == NULL)
109 section->entries = n_entries;
110 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
112 /* fill up entry fields with key name and value */
113 struct rte_cfgfile_entry *curr_entry =
114 §ion->entries[section->num_entries];
116 snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
117 snprintf(curr_entry->value,
118 sizeof(curr_entry->value), "%s", entryvalue);
119 section->num_entries++;
125 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
127 unsigned int valid_comment;
131 printf("Error - missing cfgfile parameters\n");
136 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
137 if (params->comment_character == valid_comment_chars[i]) {
143 if (valid_comment == 0) {
144 printf("Error - invalid comment characters %c\n",
145 params->comment_character);
153 rte_cfgfile_load(const char *filename, int flags)
155 return rte_cfgfile_load_with_params(filename, flags,
156 &default_cfgfile_params);
160 rte_cfgfile_load_with_params(const char *filename, int flags,
161 const struct rte_cfgfile_parameters *params)
163 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
165 struct rte_cfgfile *cfg = NULL;
167 if (rte_cfgfile_check_params(params))
170 FILE *f = fopen(filename, "r");
174 cfg = rte_cfgfile_create(flags);
176 while (fgets(buffer, sizeof(buffer), f) != NULL) {
178 size_t len = strnlen(buffer, sizeof(buffer));
180 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
181 printf("Error line %d - no \\n found on string. "
182 "Check if line too long\n", lineno);
185 /* skip parsing if comment character found */
186 pos = memchr(buffer, params->comment_character, len);
187 if (pos != NULL && (*(pos-1) != '\\')) {
192 len = _strip(buffer, len);
193 /* skip lines without useful content */
194 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
197 if (buffer[0] == '[') {
198 /* section heading line */
199 char *end = memchr(buffer, ']', len);
201 printf("Error line %d - no terminating ']'"
202 "character found\n", lineno);
206 _strip(&buffer[1], end - &buffer[1]);
208 rte_cfgfile_add_section(cfg, &buffer[1]);
210 /* key and value line */
211 char *split[2] = {NULL};
214 split[1] = memchr(buffer, '=', len);
215 if (split[1] == NULL) {
216 printf("Error line %d - no '='"
217 "character found\n", lineno);
223 _strip(split[0], strlen(split[0]));
224 _strip(split[1], strlen(split[1]));
225 char *end = memchr(split[1], '\\', strlen(split[1]));
227 while (end != NULL) {
228 if (*(end+1) == params->comment_character) {
230 strcat(split[1], end+1);
233 end = memchr(end, '\\', strlen(end));
236 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
237 (*split[1] == '\0')) {
238 printf("Error at line %d - cannot use empty "
243 if (cfg->num_sections == 0)
246 _add_entry(&cfg->sections[cfg->num_sections - 1],
253 rte_cfgfile_close(cfg);
259 rte_cfgfile_create(int flags)
262 struct rte_cfgfile *cfg = NULL;
264 cfg = malloc(sizeof(*cfg));
270 cfg->num_sections = 0;
272 /* allocate first batch of sections and entries */
273 cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
274 CFG_ALLOC_SECTION_BATCH);
276 if (cfg->sections == NULL)
279 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
281 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
282 cfg->sections[i].entries = malloc(sizeof(
283 struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
285 if (cfg->sections[i].entries == NULL)
288 cfg->sections[i].num_entries = 0;
289 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
292 if (flags & CFG_FLAG_GLOBAL_SECTION)
293 rte_cfgfile_add_section(cfg, "GLOBAL");
297 if (cfg->sections != NULL) {
298 for (i = 0; i < cfg->allocated_sections; i++) {
299 if (cfg->sections[i].entries != NULL) {
300 free(cfg->sections[i].entries);
301 cfg->sections[i].entries = NULL;
305 cfg->sections = NULL;
312 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
319 if (sectionname == NULL)
322 /* resize overall struct if we don't have room for more sections */
323 if (cfg->num_sections == cfg->allocated_sections) {
325 struct rte_cfgfile_section *n_sections =
326 realloc(cfg->sections,
327 sizeof(struct rte_cfgfile_section) *
328 ((cfg->allocated_sections) +
329 CFG_ALLOC_SECTION_BATCH));
331 if (n_sections == NULL)
334 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
335 n_sections[i + cfg->allocated_sections].num_entries = 0;
337 cfg->allocated_sections].allocated_entries = 0;
338 n_sections[i + cfg->allocated_sections].entries = NULL;
340 cfg->sections = n_sections;
341 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
344 snprintf(cfg->sections[cfg->num_sections].name,
345 sizeof(cfg->sections[0].name), "%s", sectionname);
346 cfg->sections[cfg->num_sections].num_entries = 0;
352 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
353 const char *sectionname, const char *entryname,
354 const char *entryvalue)
358 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
359 || (entryvalue == NULL))
362 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
365 /* search for section pointer by sectionname */
366 struct rte_cfgfile_section *curr_section = _get_section(cfg,
368 if (curr_section == NULL)
371 ret = _add_entry(curr_section, entryname, entryvalue);
376 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
377 const char *entryname, const char *entryvalue)
381 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
384 /* search for section pointer by sectionname */
385 struct rte_cfgfile_section *curr_section = _get_section(cfg,
387 if (curr_section == NULL)
390 if (entryvalue == NULL)
393 for (i = 0; i < curr_section->num_entries; i++)
394 if (!strcmp(curr_section->entries[i].name, entryname)) {
395 snprintf(curr_section->entries[i].value,
396 sizeof(curr_section->entries[i].value),
400 printf("Error - entry name doesn't exist\n");
404 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
408 if ((cfg == NULL) || (filename == NULL))
411 FILE *f = fopen(filename, "w");
416 for (i = 0; i < cfg->num_sections; i++) {
417 fprintf(f, "[%s]\n", cfg->sections[i].name);
419 for (j = 0; j < cfg->sections[i].num_entries; j++) {
420 fprintf(f, "%s=%s\n",
421 cfg->sections[i].entries[j].name,
422 cfg->sections[i].entries[j].value);
428 int rte_cfgfile_close(struct rte_cfgfile *cfg)
435 if (cfg->sections != NULL) {
436 for (i = 0; i < cfg->allocated_sections; i++) {
437 if (cfg->sections[i].entries != NULL) {
438 free(cfg->sections[i].entries);
439 cfg->sections[i].entries = NULL;
443 cfg->sections = NULL;
452 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
456 int num_sections = 0;
457 for (i = 0; i < cfg->num_sections; i++) {
458 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
465 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
470 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
471 snprintf(sections[i], CFG_NAME_LEN, "%s",
472 cfg->sections[i].name);
478 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
480 return _get_section(cfg, sectionname) != NULL;
484 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
485 const char *sectionname)
487 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
490 return s->num_entries;
494 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
495 char *sectionname, int index)
497 if (index < 0 || index >= cfg->num_sections)
500 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
502 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
503 return sect->num_entries;
506 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
507 struct rte_cfgfile_entry *entries, int max_entries)
510 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
513 for (i = 0; i < max_entries && i < sect->num_entries; i++)
514 entries[i] = sect->entries[i];
519 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
521 struct rte_cfgfile_entry *entries, int max_entries)
524 const struct rte_cfgfile_section *sect;
526 if (index < 0 || index >= cfg->num_sections)
528 sect = &cfg->sections[index];
529 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
530 for (i = 0; i < max_entries && i < sect->num_entries; i++)
531 entries[i] = sect->entries[i];
536 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
537 const char *entryname)
540 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
543 for (i = 0; i < sect->num_entries; i++)
544 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
546 return sect->entries[i].value;
551 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
552 const char *entryname)
554 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;