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>
14 #include "rte_cfgfile.h"
16 struct rte_cfgfile_section {
17 char name[CFG_NAME_LEN];
19 int allocated_entries;
20 struct rte_cfgfile_entry *entries;
26 int allocated_sections;
27 struct rte_cfgfile_section *sections;
30 RTE_LOG_REGISTER(cfgfile_logtype, lib.cfgfile, INFO);
32 #define CFG_LOG(level, fmt, args...) \
33 rte_log(RTE_LOG_ ## level, cfgfile_logtype, "%s(): " fmt "\n", \
36 /** when we resize a file structure, how many extra entries
37 * for new sections do we add in */
38 #define CFG_ALLOC_SECTION_BATCH 8
39 /** when we resize a section structure, how many extra entries
40 * for new entries do we add in */
41 #define CFG_ALLOC_ENTRY_BATCH 16
44 * Default cfgfile load parameters.
46 static const struct rte_cfgfile_parameters default_cfgfile_params = {
47 .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
51 * Defines the list of acceptable comment characters supported by this
54 static const char valid_comment_chars[] = {
63 _strip(char *str, unsigned len)
69 if (isspace(str[len-1])) {
70 /* strip trailing whitespace */
71 while (newlen > 0 && isspace(str[newlen - 1]))
75 if (isspace(str[0])) {
76 /* strip leading whitespace */
78 while (isspace(str[start]) && start < newlen)
82 for (i = 0; i < newlen; i++)
83 str[i] = str[i+start];
89 static struct rte_cfgfile_section *
90 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
94 for (i = 0; i < cfg->num_sections; i++) {
95 if (strncmp(cfg->sections[i].name, sectionname,
96 sizeof(cfg->sections[0].name)) == 0)
97 return &cfg->sections[i];
103 _add_entry(struct rte_cfgfile_section *section, const char *entryname,
104 const char *entryvalue)
106 /* resize entry structure if we don't have room for more entries */
107 if (section->num_entries == section->allocated_entries) {
108 struct rte_cfgfile_entry *n_entries = realloc(
110 sizeof(struct rte_cfgfile_entry) *
111 ((section->allocated_entries) +
112 CFG_ALLOC_ENTRY_BATCH));
114 if (n_entries == NULL)
117 section->entries = n_entries;
118 section->allocated_entries += CFG_ALLOC_ENTRY_BATCH;
120 /* fill up entry fields with key name and value */
121 struct rte_cfgfile_entry *curr_entry =
122 §ion->entries[section->num_entries];
124 strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
125 strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
126 section->num_entries++;
132 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
134 unsigned int valid_comment;
138 CFG_LOG(ERR, "missing cfgfile parameters\n");
143 for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
144 if (params->comment_character == valid_comment_chars[i]) {
150 if (valid_comment == 0) {
151 CFG_LOG(ERR, "invalid comment characters %c\n",
152 params->comment_character);
160 rte_cfgfile_load(const char *filename, int flags)
162 return rte_cfgfile_load_with_params(filename, flags,
163 &default_cfgfile_params);
167 rte_cfgfile_load_with_params(const char *filename, int flags,
168 const struct rte_cfgfile_parameters *params)
170 char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4];
172 struct rte_cfgfile *cfg;
174 if (rte_cfgfile_check_params(params))
177 FILE *f = fopen(filename, "r");
181 cfg = rte_cfgfile_create(flags);
183 while (fgets(buffer, sizeof(buffer), f) != NULL) {
185 size_t len = strnlen(buffer, sizeof(buffer));
187 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
188 CFG_LOG(ERR, " line %d - no \\n found on string. "
189 "Check if line too long\n", lineno);
192 /* skip parsing if comment character found */
193 pos = memchr(buffer, params->comment_character, len);
195 (pos == buffer || *(pos-1) != '\\')) {
200 len = _strip(buffer, len);
201 /* skip lines without useful content */
202 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
205 if (buffer[0] == '[') {
206 /* section heading line */
207 char *end = memchr(buffer, ']', len);
210 "line %d - no terminating ']' character found\n",
215 _strip(&buffer[1], end - &buffer[1]);
217 rte_cfgfile_add_section(cfg, &buffer[1]);
219 /* key and value line */
220 char *split[2] = {NULL};
223 split[1] = memchr(buffer, '=', len);
224 if (split[1] == NULL) {
226 "line %d - no '=' character found\n",
233 _strip(split[0], strlen(split[0]));
234 _strip(split[1], strlen(split[1]));
235 char *end = memchr(split[1], '\\', strlen(split[1]));
237 size_t split_len = strlen(split[1]) + 1;
238 while (end != NULL) {
239 if (*(end+1) == params->comment_character) {
241 strlcat(split[1], end+1, split_len);
244 end = memchr(end, '\\', strlen(end));
247 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
248 (*split[1] == '\0')) {
250 "line %d - cannot use empty values\n",
255 if (cfg->num_sections == 0)
258 _add_entry(&cfg->sections[cfg->num_sections - 1],
265 rte_cfgfile_close(cfg);
271 rte_cfgfile_create(int flags)
274 struct rte_cfgfile *cfg;
276 /* future proof flags usage */
277 if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
280 cfg = malloc(sizeof(*cfg));
286 cfg->num_sections = 0;
288 /* allocate first batch of sections and entries */
289 cfg->sections = calloc(CFG_ALLOC_SECTION_BATCH,
290 sizeof(struct rte_cfgfile_section));
291 if (cfg->sections == NULL)
294 cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
296 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
297 cfg->sections[i].entries = calloc(CFG_ALLOC_ENTRY_BATCH,
298 sizeof(struct rte_cfgfile_entry));
300 if (cfg->sections[i].entries == NULL)
303 cfg->sections[i].num_entries = 0;
304 cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
307 if (flags & CFG_FLAG_GLOBAL_SECTION)
308 rte_cfgfile_add_section(cfg, "GLOBAL");
312 if (cfg->sections != NULL) {
313 for (i = 0; i < cfg->allocated_sections; i++) {
314 if (cfg->sections[i].entries != NULL) {
315 free(cfg->sections[i].entries);
316 cfg->sections[i].entries = NULL;
320 cfg->sections = NULL;
327 rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
334 if (sectionname == NULL)
337 /* resize overall struct if we don't have room for more sections */
338 if (cfg->num_sections == cfg->allocated_sections) {
340 struct rte_cfgfile_section *n_sections =
341 realloc(cfg->sections,
342 sizeof(struct rte_cfgfile_section) *
343 ((cfg->allocated_sections) +
344 CFG_ALLOC_SECTION_BATCH));
346 if (n_sections == NULL)
349 for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
350 n_sections[i + cfg->allocated_sections].num_entries = 0;
352 cfg->allocated_sections].allocated_entries = 0;
353 n_sections[i + cfg->allocated_sections].entries = NULL;
355 cfg->sections = n_sections;
356 cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
359 strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
360 sizeof(cfg->sections[0].name));
361 cfg->sections[cfg->num_sections].num_entries = 0;
367 int rte_cfgfile_add_entry(struct rte_cfgfile *cfg,
368 const char *sectionname, const char *entryname,
369 const char *entryvalue)
373 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL)
374 || (entryvalue == NULL))
377 if (rte_cfgfile_has_entry(cfg, sectionname, entryname) != 0)
380 /* search for section pointer by sectionname */
381 struct rte_cfgfile_section *curr_section = _get_section(cfg,
383 if (curr_section == NULL)
386 ret = _add_entry(curr_section, entryname, entryvalue);
391 int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
392 const char *entryname, const char *entryvalue)
396 if ((cfg == NULL) || (sectionname == NULL) || (entryname == NULL))
399 /* search for section pointer by sectionname */
400 struct rte_cfgfile_section *curr_section = _get_section(cfg,
402 if (curr_section == NULL)
405 if (entryvalue == NULL)
408 for (i = 0; i < curr_section->num_entries; i++)
409 if (!strcmp(curr_section->entries[i].name, entryname)) {
410 strlcpy(curr_section->entries[i].value, entryvalue,
411 sizeof(curr_section->entries[i].value));
415 CFG_LOG(ERR, "entry name doesn't exist\n");
419 int rte_cfgfile_save(struct rte_cfgfile *cfg, const char *filename)
423 if ((cfg == NULL) || (filename == NULL))
426 FILE *f = fopen(filename, "w");
431 for (i = 0; i < cfg->num_sections; i++) {
432 fprintf(f, "[%s]\n", cfg->sections[i].name);
434 for (j = 0; j < cfg->sections[i].num_entries; j++) {
435 fprintf(f, "%s=%s\n",
436 cfg->sections[i].entries[j].name,
437 cfg->sections[i].entries[j].value);
443 int rte_cfgfile_close(struct rte_cfgfile *cfg)
450 if (cfg->sections != NULL) {
451 for (i = 0; i < cfg->allocated_sections; i++) {
452 if (cfg->sections[i].entries != NULL) {
453 free(cfg->sections[i].entries);
454 cfg->sections[i].entries = NULL;
458 cfg->sections = NULL;
467 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
471 int num_sections = 0;
472 for (i = 0; i < cfg->num_sections; i++) {
473 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
480 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
485 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
486 strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
492 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
494 return _get_section(cfg, sectionname) != NULL;
498 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
499 const char *sectionname)
501 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
504 return s->num_entries;
508 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
509 char *sectionname, int index)
511 if (index < 0 || index >= cfg->num_sections)
514 const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
516 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
517 return sect->num_entries;
520 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
521 struct rte_cfgfile_entry *entries, int max_entries)
524 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
527 for (i = 0; i < max_entries && i < sect->num_entries; i++)
528 entries[i] = sect->entries[i];
533 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
535 struct rte_cfgfile_entry *entries, int max_entries)
538 const struct rte_cfgfile_section *sect;
540 if (index < 0 || index >= cfg->num_sections)
542 sect = &cfg->sections[index];
543 strlcpy(sectionname, sect->name, CFG_NAME_LEN);
544 for (i = 0; i < max_entries && i < sect->num_entries; i++)
545 entries[i] = sect->entries[i];
550 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
551 const char *entryname)
554 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
557 for (i = 0; i < sect->num_entries; i++)
558 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
560 return sect->entries[i].value;
565 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
566 const char *entryname)
568 return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;