1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_string_fns.h>
11 #include <rte_common.h>
13 #include "rte_cfgfile.h"
15 struct rte_cfgfile_section {
16 char name[CFG_NAME_LEN];
18 int allocated_entries;
19 struct rte_cfgfile_entry *entries;
25 int allocated_sections;
26 struct rte_cfgfile_section *sections;
29 /** when we resize a file structure, how many extra entries
30 * for new sections do we add in */
31 #define CFG_ALLOC_SECTION_BATCH 8
32 /** when we resize a section structure, how many extra entries
33 * for new entries do we add in */
34 #define CFG_ALLOC_ENTRY_BATCH 16
37 * Default cfgfile load parameters.
39 static const struct rte_cfgfile_parameters default_cfgfile_params = {
40 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
44 * Defines the list of acceptable comment characters supported by this
47 static const char valid_comment_chars[] = {
56 _strip(char *str, unsigned len)
62 if (isspace(str[len-1])) {
63 /* strip trailing whitespace */
64 while (newlen > 0 && isspace(str[newlen - 1]))
68 if (isspace(str[0])) {
69 /* strip leading whitespace */
71 while (isspace(str[start]) && start < newlen)
75 for (i = 0; i < newlen; i++)
76 str[i] = str[i+start];
82 static struct rte_cfgfile_section *
83 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
87 for (i = 0; i < cfg->num_sections; i++) {
88 if (strncmp(cfg->sections[i].name, sectionname,
89 sizeof(cfg->sections[0].name)) == 0)
90 return &cfg->sections[i];
96 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
97 const char *entryvalue)
99 /* resize entry structure if we don't have room for more entries */
100 if (section->num_entries == section->allocated_entries) {
101 struct rte_cfgfile_entry *n_entries = realloc(
103 sizeof(struct rte_cfgfile_entry) *
104 ((section->allocated_entries) +
105 CFG_ALLOC_ENTRY_BATCH));
107 if (n_entries == NULL)
110 section->entries = n_entries;
111 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
113 /* fill up entry fields with key name and value */
114 struct rte_cfgfile_entry *curr_entry =
115 §ion->entries[section->num_entries];
117 strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
118 strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
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 strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
345 sizeof(cfg->sections[0].name));
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 strlcpy(curr_section->entries[i].value, entryvalue,
396 sizeof(curr_section->entries[i].value));
399 printf("Error - entry name doesn't exist\n");
403 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
407 if ((cfg == NULL) || (filename == NULL))
410 FILE *f = fopen(filename, "w");
415 for (i = 0; i < cfg->num_sections; i++) {
416 fprintf(f, "[%s]\n", cfg->sections[i].name);
418 for (j = 0; j < cfg->sections[i].num_entries; j++) {
419 fprintf(f, "%s=%s\n",
420 cfg->sections[i].entries[j].name,
421 cfg->sections[i].entries[j].value);
427 int rte_cfgfile_close(struct rte_cfgfile *cfg)
434 if (cfg->sections != NULL) {
435 for (i = 0; i < cfg->allocated_sections; i++) {
436 if (cfg->sections[i].entries != NULL) {
437 free(cfg->sections[i].entries);
438 cfg->sections[i].entries = NULL;
442 cfg->sections = NULL;
451 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
455 int num_sections = 0;
456 for (i = 0; i < cfg->num_sections; i++) {
457 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
464 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
469 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
470 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
476 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
478 return _get_section(cfg, sectionname) != NULL;
482 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
483 const char *sectionname)
485 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
488 return s->num_entries;
492 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
493 char *sectionname, int index)
495 if (index < 0 || index >= cfg->num_sections)
498 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
500 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
501 return sect->num_entries;
504 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
505 struct rte_cfgfile_entry *entries, int max_entries)
508 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
511 for (i = 0; i < max_entries && i < sect->num_entries; i++)
512 entries[i] = sect->entries[i];
517 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
519 struct rte_cfgfile_entry *entries, int max_entries)
522 const struct rte_cfgfile_section *sect;
524 if (index < 0 || index >= cfg->num_sections)
526 sect = &cfg->sections[index];
527 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
528 for (i = 0; i < max_entries && i < sect->num_entries; i++)
529 entries[i] = sect->entries[i];
534 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
535 const char *entryname)
538 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
541 for (i = 0; i < sect->num_entries; i++)
542 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
544 return sect->entries[i].value;
549 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
550 const char *entryname)
552 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;