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 size_t split_len = strlen(split[1]) + 1;
228 while (end != NULL) {
229 if (*(end+1) == params->comment_character) {
231 strlcat(split[1], end+1, split_len);
234 end = memchr(end, '\\', strlen(end));
237 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
238 (*split[1] == '\0')) {
239 printf("Error at line %d - cannot use empty "
244 if (cfg->num_sections == 0)
247 _add_entry(&cfg->sections[cfg->num_sections - 1],
254 rte_cfgfile_close(cfg);
260 rte_cfgfile_create(int flags)
263 struct rte_cfgfile *cfg = NULL;
265 cfg = malloc(sizeof(*cfg));
271 cfg->num_sections = 0;
273 /* allocate first batch of sections and entries */
274 cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
275 CFG_ALLOC_SECTION_BATCH);
277 if (cfg->sections == NULL)
280 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
282 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
283 cfg->sections[i].entries = malloc(sizeof(
284 struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
286 if (cfg->sections[i].entries == NULL)
289 cfg->sections[i].num_entries = 0;
290 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
293 if (flags & CFG_FLAG_GLOBAL_SECTION)
294 rte_cfgfile_add_section(cfg, "GLOBAL");
298 if (cfg->sections != NULL) {
299 for (i = 0; i < cfg->allocated_sections; i++) {
300 if (cfg->sections[i].entries != NULL) {
301 free(cfg->sections[i].entries);
302 cfg->sections[i].entries = NULL;
306 cfg->sections = NULL;
313 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
320 if (sectionname == NULL)
323 /* resize overall struct if we don't have room for more sections */
324 if (cfg->num_sections == cfg->allocated_sections) {
326 struct rte_cfgfile_section *n_sections =
327 realloc(cfg->sections,
328 sizeof(struct rte_cfgfile_section) *
329 ((cfg->allocated_sections) +
330 CFG_ALLOC_SECTION_BATCH));
332 if (n_sections == NULL)
335 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
336 n_sections[i + cfg->allocated_sections].num_entries = 0;
338 cfg->allocated_sections].allocated_entries = 0;
339 n_sections[i + cfg->allocated_sections].entries = NULL;
341 cfg->sections = n_sections;
342 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
345 strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
346 sizeof(cfg->sections[0].name));
347 cfg->sections[cfg->num_sections].num_entries = 0;
353 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
354 const char *sectionname, const char *entryname,
355 const char *entryvalue)
359 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
360 || (entryvalue == NULL))
363 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
366 /* search for section pointer by sectionname */
367 struct rte_cfgfile_section *curr_section = _get_section(cfg,
369 if (curr_section == NULL)
372 ret = _add_entry(curr_section, entryname, entryvalue);
377 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
378 const char *entryname, const char *entryvalue)
382 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
385 /* search for section pointer by sectionname */
386 struct rte_cfgfile_section *curr_section = _get_section(cfg,
388 if (curr_section == NULL)
391 if (entryvalue == NULL)
394 for (i = 0; i < curr_section->num_entries; i++)
395 if (!strcmp(curr_section->entries[i].name, entryname)) {
396 strlcpy(curr_section->entries[i].value, entryvalue,
397 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 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
477 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
479 return _get_section(cfg, sectionname) != NULL;
483 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
484 const char *sectionname)
486 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
489 return s->num_entries;
493 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
494 char *sectionname, int index)
496 if (index < 0 || index >= cfg->num_sections)
499 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
501 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
502 return sect->num_entries;
505 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
506 struct rte_cfgfile_entry *entries, int max_entries)
509 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
512 for (i = 0; i < max_entries && i < sect->num_entries; i++)
513 entries[i] = sect->entries[i];
518 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
520 struct rte_cfgfile_entry *entries, int max_entries)
523 const struct rte_cfgfile_section *sect;
525 if (index < 0 || index >= cfg->num_sections)
527 sect = &cfg->sections[index];
528 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
529 for (i = 0; i < max_entries && i < sect->num_entries; i++)
530 entries[i] = sect->entries[i];
535 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
536 const char *entryname)
539 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
542 for (i = 0; i < sect->num_entries; i++)
543 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
545 return sect->entries[i].value;
550 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
551 const char *entryname)
553 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;