4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_string_fns.h>
40 #include "rte_cfgfile.h"
42 struct rte_cfgfile_section {
43 char name[CFG_NAME_LEN];
45 struct rte_cfgfile_entry *entries[0];
51 struct rte_cfgfile_section *sections[0];
54 /** when we resize a file structure, how many extra entries
55 * for new sections do we add in */
56 #define CFG_ALLOC_SECTION_BATCH 8
57 /** when we resize a section structure, how many extra entries
58 * for new entries do we add in */
59 #define CFG_ALLOC_ENTRY_BATCH 16
62 _strip(char *str, unsigned len)
68 if (isspace(str[len-1])) {
69 /* strip trailing whitespace */
70 while (newlen > 0 && isspace(str[newlen - 1]))
74 if (isspace(str[0])) {
75 /* strip leading whitespace */
77 while (isspace(str[start]) && start < newlen)
81 for (i = 0; i < newlen; i++)
82 str[i] = str[i+start];
89 rte_cfgfile_load(const char *filename, int flags)
91 int allocated_sections = CFG_ALLOC_SECTION_BATCH;
92 int allocated_entries = 0;
93 int curr_section = -1;
97 struct rte_cfgfile *cfg = NULL;
99 FILE *f = fopen(filename, "r");
103 cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
108 memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
110 while (fgets(buffer, sizeof(buffer), f) != NULL) {
112 size_t len = strnlen(buffer, sizeof(buffer));
114 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
115 printf("Error line %d - no \\n found on string. "
116 "Check if line too long\n", lineno);
119 pos = memchr(buffer, ';', sizeof(buffer));
125 len = _strip(buffer, len);
126 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
129 if (buffer[0] == '[') {
130 /* section heading line */
131 char *end = memchr(buffer, ']', len);
133 printf("Error line %d - no terminating '['"
134 "character found\n", lineno);
138 _strip(&buffer[1], end - &buffer[1]);
140 /* close off old section and add start new one */
141 if (curr_section >= 0)
142 cfg->sections[curr_section]->num_entries =
146 /* resize overall struct if we don't have room for more
148 if (curr_section == allocated_sections) {
149 allocated_sections += CFG_ALLOC_SECTION_BATCH;
150 struct rte_cfgfile *n_cfg = realloc(cfg,
151 sizeof(*cfg) + sizeof(cfg->sections[0])
152 * allocated_sections);
154 printf("Error - no more memory\n");
160 /* allocate space for new section */
161 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
163 cfg->sections[curr_section] = malloc(
164 sizeof(*cfg->sections[0]) +
165 sizeof(cfg->sections[0]->entries[0]) *
167 if (cfg->sections[curr_section] == NULL) {
168 printf("Error - no more memory\n");
172 snprintf(cfg->sections[curr_section]->name,
173 sizeof(cfg->sections[0]->name),
177 if (curr_section < 0) {
178 printf("Error line %d - value outside of"
179 "section\n", lineno);
183 struct rte_cfgfile_section *sect =
184 cfg->sections[curr_section];
186 if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
188 printf("Error at line %d - cannot split "
194 if (curr_entry == allocated_entries) {
195 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
196 struct rte_cfgfile_section *n_sect = realloc(
197 sect, sizeof(*sect) +
198 sizeof(sect->entries[0]) *
200 if (n_sect == NULL) {
201 printf("Error - no more memory\n");
204 sect = cfg->sections[curr_section] = n_sect;
207 sect->entries[curr_entry] = malloc(
208 sizeof(*sect->entries[0]));
209 if (sect->entries[curr_entry] == NULL) {
210 printf("Error - no more memory\n");
214 struct rte_cfgfile_entry *entry = sect->entries[
216 snprintf(entry->name, sizeof(entry->name), "%s",
218 snprintf(entry->value, sizeof(entry->value), "%s",
220 _strip(entry->name, strnlen(entry->name,
221 sizeof(entry->name)));
222 _strip(entry->value, strnlen(entry->value,
223 sizeof(entry->value)));
228 cfg->sections[curr_section]->num_entries = curr_entry + 1;
229 cfg->num_sections = curr_section + 1;
233 rte_cfgfile_close(cfg);
240 int rte_cfgfile_close(struct rte_cfgfile *cfg)
247 for (i = 0; i < cfg->num_sections; i++) {
248 if (cfg->sections[i] != NULL) {
249 if (cfg->sections[i]->num_entries) {
250 for (j = 0; j < cfg->sections[i]->num_entries;
252 if (cfg->sections[i]->entries[j] !=
254 free(cfg->sections[i]->
258 free(cfg->sections[i]);
267 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
271 int num_sections = 0;
272 for (i = 0; i < cfg->num_sections; i++) {
273 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
280 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
285 for (i = 0; i < cfg->num_sections && i < max_sections; i++)
286 snprintf(sections[i], CFG_NAME_LEN, "%s",
287 cfg->sections[i]->name);
292 static const struct rte_cfgfile_section *
293 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
296 for (i = 0; i < cfg->num_sections; i++) {
297 if (strncmp(cfg->sections[i]->name, sectionname,
298 sizeof(cfg->sections[0]->name)) == 0)
299 return cfg->sections[i];
305 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
307 return (_get_section(cfg, sectionname) != NULL);
311 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
312 const char *sectionname)
314 const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
317 return s->num_entries;
322 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
323 struct rte_cfgfile_entry *entries, int max_entries)
326 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
329 for (i = 0; i < max_entries && i < sect->num_entries; i++)
330 entries[i] = *sect->entries[i];
335 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
336 const char *entryname)
339 const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
342 for (i = 0; i < sect->num_entries; i++)
343 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
345 return sect->entries[i]->value;
350 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
351 const char *entryname)
353 return (rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL);