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 snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
118 snprintf(curr_entry->value,
119 sizeof(curr_entry->value), "%s", entryvalue);
120 section->num_entries++;
126 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
128 unsigned int valid_comment;
132 printf("Error - missing cfgfile parameters\n");
137 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
138 if (params->comment_character == valid_comment_chars[i]) {
144 if (valid_comment == 0) {
145 printf("Error - invalid comment characters %c\n",
146 params->comment_character);
154 rte_cfgfile_load(const char *filename, int flags)
156 return rte_cfgfile_load_with_params(filename, flags,
157 &default_cfgfile_params);
161 rte_cfgfile_load_with_params(const char *filename, int flags,
162 const struct rte_cfgfile_parameters *params)
164 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
166 struct rte_cfgfile *cfg = NULL;
168 if (rte_cfgfile_check_params(params))
171 FILE *f = fopen(filename, "r");
175 cfg = rte_cfgfile_create(flags);
177 while (fgets(buffer, sizeof(buffer), f) != NULL) {
179 size_t len = strnlen(buffer, sizeof(buffer));
181 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
182 printf("Error line %d - no \\n found on string. "
183 "Check if line too long\n", lineno);
186 /* skip parsing if comment character found */
187 pos = memchr(buffer, params->comment_character, len);
188 if (pos != NULL && (*(pos-1) != '\\')) {
193 len = _strip(buffer, len);
194 /* skip lines without useful content */
195 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
198 if (buffer[0] == '[') {
199 /* section heading line */
200 char *end = memchr(buffer, ']', len);
202 printf("Error line %d - no terminating ']'"
203 "character found\n", lineno);
207 _strip(&buffer[1], end - &buffer[1]);
209 rte_cfgfile_add_section(cfg, &buffer[1]);
211 /* key and value line */
212 char *split[2] = {NULL};
215 split[1] = memchr(buffer, '=', len);
216 if (split[1] == NULL) {
217 printf("Error line %d - no '='"
218 "character found\n", lineno);
224 _strip(split[0], strlen(split[0]));
225 _strip(split[1], strlen(split[1]));
226 char *end = memchr(split[1], '\\', strlen(split[1]));
228 size_t split_len = strlen(split[1]) + 1;
229 while (end != NULL) {
230 if (*(end+1) == params->comment_character) {
232 strlcat(split[1], end+1, split_len);
235 end = memchr(end, '\\', strlen(end));
238 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
239 (*split[1] == '\0')) {
240 printf("Error at line %d - cannot use empty "
245 if (cfg->num_sections == 0)
248 _add_entry(&cfg->sections[cfg->num_sections - 1],
255 rte_cfgfile_close(cfg);
261 rte_cfgfile_create(int flags)
264 struct rte_cfgfile *cfg = NULL;
266 cfg = malloc(sizeof(*cfg));
272 cfg->num_sections = 0;
274 /* allocate first batch of sections and entries */
275 cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
276 CFG_ALLOC_SECTION_BATCH);
278 if (cfg->sections == NULL)
281 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
283 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
284 cfg->sections[i].entries = malloc(sizeof(
285 struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
287 if (cfg->sections[i].entries == NULL)
290 cfg->sections[i].num_entries = 0;
291 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
294 if (flags & CFG_FLAG_GLOBAL_SECTION)
295 rte_cfgfile_add_section(cfg, "GLOBAL");
299 if (cfg->sections != NULL) {
300 for (i = 0; i < cfg->allocated_sections; i++) {
301 if (cfg->sections[i].entries != NULL) {
302 free(cfg->sections[i].entries);
303 cfg->sections[i].entries = NULL;
307 cfg->sections = NULL;
314 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
321 if (sectionname == NULL)
324 /* resize overall struct if we don't have room for more sections */
325 if (cfg->num_sections == cfg->allocated_sections) {
327 struct rte_cfgfile_section *n_sections =
328 realloc(cfg->sections,
329 sizeof(struct rte_cfgfile_section) *
330 ((cfg->allocated_sections) +
331 CFG_ALLOC_SECTION_BATCH));
333 if (n_sections == NULL)
336 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
337 n_sections[i + cfg->allocated_sections].num_entries = 0;
339 cfg->allocated_sections].allocated_entries = 0;
340 n_sections[i + cfg->allocated_sections].entries = NULL;
342 cfg->sections = n_sections;
343 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
346 snprintf(cfg->sections[cfg->num_sections].name,
347 sizeof(cfg->sections[0].name), "%s", sectionname);
348 cfg->sections[cfg->num_sections].num_entries = 0;
354 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
355 const char *sectionname, const char *entryname,
356 const char *entryvalue)
360 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
361 || (entryvalue == NULL))
364 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
367 /* search for section pointer by sectionname */
368 struct rte_cfgfile_section *curr_section = _get_section(cfg,
370 if (curr_section == NULL)
373 ret = _add_entry(curr_section, entryname, entryvalue);
378 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
379 const char *entryname, const char *entryvalue)
383 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
386 /* search for section pointer by sectionname */
387 struct rte_cfgfile_section *curr_section = _get_section(cfg,
389 if (curr_section == NULL)
392 if (entryvalue == NULL)
395 for (i = 0; i < curr_section->num_entries; i++)
396 if (!strcmp(curr_section->entries[i].name, entryname)) {
397 snprintf(curr_section->entries[i].value,
398 sizeof(curr_section->entries[i].value),
402 printf("Error - entry name doesn't exist\n");
406 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
410 if ((cfg == NULL) || (filename == NULL))
413 FILE *f = fopen(filename, "w");
418 for (i = 0; i < cfg->num_sections; i++) {
419 fprintf(f, "[%s]\n", cfg->sections[i].name);
421 for (j = 0; j < cfg->sections[i].num_entries; j++) {
422 fprintf(f, "%s=%s\n",
423 cfg->sections[i].entries[j].name,
424 cfg->sections[i].entries[j].value);
430 int rte_cfgfile_close(struct rte_cfgfile *cfg)
437 if (cfg->sections != NULL) {
438 for (i = 0; i < cfg->allocated_sections; i++) {
439 if (cfg->sections[i].entries != NULL) {
440 free(cfg->sections[i].entries);
441 cfg->sections[i].entries = NULL;
445 cfg->sections = NULL;
454 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
458 int num_sections = 0;
459 for (i = 0; i < cfg->num_sections; i++) {
460 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
467 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
472 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
473 snprintf(sections[i], CFG_NAME_LEN, "%s",
474 cfg->sections[i].name);
480 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
482 return _get_section(cfg, sectionname) != NULL;
486 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
487 const char *sectionname)
489 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
492 return s->num_entries;
496 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
497 char *sectionname, int index)
499 if (index < 0 || index >= cfg->num_sections)
502 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
504 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
505 return sect->num_entries;
508 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
509 struct rte_cfgfile_entry *entries, int max_entries)
512 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
515 for (i = 0; i < max_entries && i < sect->num_entries; i++)
516 entries[i] = sect->entries[i];
521 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
523 struct rte_cfgfile_entry *entries, int max_entries)
526 const struct rte_cfgfile_section *sect;
528 if (index < 0 || index >= cfg->num_sections)
530 sect = &cfg->sections[index];
531 snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
532 for (i = 0; i < max_entries && i < sect->num_entries; i++)
533 entries[i] = sect->entries[i];
538 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
539 const char *entryname)
542 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
545 for (i = 0; i < sect->num_entries; i++)
546 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
548 return sect->entries[i].value;
553 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
554 const char *entryname)
556 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;