cfgfile: rework to flat arrays
[dpdk.git] / lib / librte_cfgfile / rte_cfgfile.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <rte_common.h>
40
41 #include "rte_cfgfile.h"
42
43 struct rte_cfgfile_section {
44         char name[CFG_NAME_LEN];
45         int num_entries;
46         int allocated_entries;
47         struct rte_cfgfile_entry *entries;
48 };
49
50 struct rte_cfgfile {
51         int flags;
52         int num_sections;
53         int allocated_sections;
54         struct rte_cfgfile_section *sections;
55 };
56
57 /** when we resize a file structure, how many extra entries
58  * for new sections do we add in */
59 #define CFG_ALLOC_SECTION_BATCH 8
60 /** when we resize a section structure, how many extra entries
61  * for new entries do we add in */
62 #define CFG_ALLOC_ENTRY_BATCH 16
63
64 /**
65  * Default cfgfile load parameters.
66  */
67 static const struct rte_cfgfile_parameters default_cfgfile_params = {
68         .comment_character = CFG_DEFAULT_COMMENT_CHARACTER,
69 };
70
71 /**
72  * Defines the list of acceptable comment characters supported by this
73  * library.
74  */
75 static const char valid_comment_chars[] = {
76         '!',
77         '#',
78         '%',
79         ';',
80         '@'
81 };
82
83 static unsigned
84 _strip(char *str, unsigned len)
85 {
86         int newlen = len;
87         if (len == 0)
88                 return 0;
89
90         if (isspace(str[len-1])) {
91                 /* strip trailing whitespace */
92                 while (newlen > 0 && isspace(str[newlen - 1]))
93                         str[--newlen] = '\0';
94         }
95
96         if (isspace(str[0])) {
97                 /* strip leading whitespace */
98                 int i, start = 1;
99                 while (isspace(str[start]) && start < newlen)
100                         start++
101                         ; /* do nothing */
102                 newlen -= start;
103                 for (i = 0; i < newlen; i++)
104                         str[i] = str[i+start];
105                 str[i] = '\0';
106         }
107         return newlen;
108 }
109
110 static struct rte_cfgfile_section *
111 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
112 {
113         int i;
114
115         for (i = 0; i < cfg->num_sections; i++) {
116                 if (strncmp(cfg->sections[i].name, sectionname,
117                                 sizeof(cfg->sections[0].name)) == 0)
118                         return &cfg->sections[i];
119         }
120         return NULL;
121 }
122
123 static int
124 rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
125 {
126         unsigned int valid_comment;
127         unsigned int i;
128
129         if (!params) {
130                 printf("Error - missing cfgfile parameters\n");
131                 return -EINVAL;
132         }
133
134         valid_comment = 0;
135         for (i = 0; i < RTE_DIM(valid_comment_chars); i++) {
136                 if (params->comment_character == valid_comment_chars[i]) {
137                         valid_comment = 1;
138                         break;
139                 }
140         }
141
142         if (valid_comment == 0) {
143                 printf("Error - invalid comment characters %c\n",
144                        params->comment_character);
145                 return -ENOTSUP;
146         }
147
148         return 0;
149 }
150
151 struct rte_cfgfile *
152 rte_cfgfile_load(const char *filename, int flags)
153 {
154         return rte_cfgfile_load_with_params(filename, flags,
155                                             &default_cfgfile_params);
156 }
157
158 struct rte_cfgfile *
159 rte_cfgfile_load_with_params(const char *filename, int flags,
160                              const struct rte_cfgfile_parameters *params)
161 {
162         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
163         int allocated_entries = 0;
164         int curr_section = -1;
165         int curr_entry = -1;
166         char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
167         int lineno = 0;
168         struct rte_cfgfile *cfg = NULL;
169
170         if (rte_cfgfile_check_params(params))
171                 return NULL;
172
173         FILE *f = fopen(filename, "r");
174         if (f == NULL)
175                 return NULL;
176
177         cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
178                 allocated_sections);
179         if (cfg == NULL)
180                 goto error2;
181
182         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
183
184         if (flags & CFG_FLAG_GLOBAL_SECTION) {
185                 curr_section = 0;
186                 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
187                 cfg->sections = malloc(
188                         sizeof(cfg->sections[0]) +
189                         sizeof(cfg->sections[0].entries) *
190                         allocated_entries);
191                 if (cfg->sections == NULL) {
192                         printf("Error - no memory for global section\n");
193                         goto error1;
194                 }
195
196                 snprintf(cfg->sections[curr_section].name,
197                                  sizeof(cfg->sections[0].name), "GLOBAL");
198         }
199
200         while (fgets(buffer, sizeof(buffer), f) != NULL) {
201                 char *pos = NULL;
202                 size_t len = strnlen(buffer, sizeof(buffer));
203                 lineno++;
204                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
205                         printf("Error line %d - no \\n found on string. "
206                                         "Check if line too long\n", lineno);
207                         goto error1;
208                 }
209                 pos = memchr(buffer, params->comment_character, len);
210                 if (pos != NULL) {
211                         *pos = '\0';
212                         len = pos -  buffer;
213                 }
214
215                 len = _strip(buffer, len);
216                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
217                         continue;
218
219                 if (buffer[0] == '[') {
220                         /* section heading line */
221                         char *end = memchr(buffer, ']', len);
222                         if (end == NULL) {
223                                 printf("Error line %d - no terminating '['"
224                                         "character found\n", lineno);
225                                 goto error1;
226                         }
227                         *end = '\0';
228                         _strip(&buffer[1], end - &buffer[1]);
229
230                         /* close off old section and add start new one */
231                         if (curr_section >= 0)
232                                 cfg->sections[curr_section].num_entries =
233                                         curr_entry + 1;
234                         curr_section++;
235
236                         /* resize overall struct if we don't have room for more
237                         sections */
238                         if (curr_section == allocated_sections) {
239                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
240                                 struct rte_cfgfile *n_cfg = realloc(cfg,
241                                         sizeof(*cfg) + sizeof(cfg->sections[0])
242                                         * allocated_sections);
243                                 if (n_cfg == NULL) {
244                                         curr_section--;
245                                         printf("Error - no more memory\n");
246                                         goto error1;
247                                 }
248                                 cfg = n_cfg;
249                         }
250
251                         /* allocate space for new section */
252                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
253                         curr_entry = -1;
254                         cfg->sections = malloc(
255                                 sizeof(cfg->sections[0]) +
256                                 sizeof(cfg->sections[0].entries) *
257                                 allocated_entries);
258                         if (cfg->sections == NULL) {
259                                 printf("Error - no more memory\n");
260                                 goto error1;
261                         }
262
263                         snprintf(cfg->sections[curr_section].name,
264                                         sizeof(cfg->sections[0].name),
265                                         "%s", &buffer[1]);
266                 } else {
267                         /* value line */
268                         if (curr_section < 0) {
269                                 printf("Error line %d - value outside of"
270                                         "section\n", lineno);
271                                 goto error1;
272                         }
273
274                         struct rte_cfgfile_section *sect = cfg->sections;
275
276                         char *split[2] = {NULL};
277                         split[0] = buffer;
278                         split[1] = memchr(buffer, '=', len);
279
280                         /* when delimeter not found */
281                         if (split[1] == NULL) {
282                                 printf("Error at line %d - cannot "
283                                         "split string\n", lineno);
284                                 goto error1;
285                         } else {
286                                 /* when delimeter found */
287                                 *split[1] = '\0';
288                                 split[1]++;
289
290                                 if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
291                                                 (*split[1] == '\0')) {
292                                         printf("Error at line %d - cannot "
293                                                 "split string\n", lineno);
294                                         goto error1;
295                                 }
296                         }
297
298                         curr_entry++;
299                         if (curr_entry == allocated_entries) {
300                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
301                                 struct rte_cfgfile_section *n_sect = realloc(
302                                         sect, sizeof(*sect) +
303                                         sizeof(sect->entries[0]) *
304                                         allocated_entries);
305                                 if (n_sect == NULL) {
306                                         curr_entry--;
307                                         printf("Error - no more memory\n");
308                                         goto error1;
309                                 }
310                                 sect = cfg->sections = n_sect;
311                         }
312
313                         sect->entries = malloc(
314                                 sizeof(sect->entries[0]));
315                         if (sect->entries == NULL) {
316                                 printf("Error - no more memory\n");
317                                 goto error1;
318                         }
319
320                         struct rte_cfgfile_entry *entry = sect->entries;
321                         snprintf(entry->name, sizeof(entry->name), "%s",
322                                 split[0]);
323                         snprintf(entry->value, sizeof(entry->value), "%s",
324                                  split[1] ? split[1] : "");
325                         _strip(entry->name, strnlen(entry->name,
326                                 sizeof(entry->name)));
327                         _strip(entry->value, strnlen(entry->value,
328                                 sizeof(entry->value)));
329                 }
330         }
331         fclose(f);
332         cfg->flags = flags;
333         cfg->num_sections = curr_section + 1;
334         /* curr_section will still be -1 if we have an empty file */
335         if (curr_section >= 0)
336                 cfg->sections[curr_section].num_entries = curr_entry + 1;
337         return cfg;
338
339 error1:
340         cfg->num_sections = curr_section + 1;
341         if (curr_section >= 0)
342                 cfg->sections[curr_section].num_entries = curr_entry + 1;
343         rte_cfgfile_close(cfg);
344 error2:
345         fclose(f);
346         return NULL;
347 }
348
349 int rte_cfgfile_close(struct rte_cfgfile *cfg)
350 {
351         int i;
352
353         if (cfg == NULL)
354                 return -1;
355
356         if (cfg->sections != NULL) {
357                 for (i = 0; i < cfg->allocated_sections; i++) {
358                         if (cfg->sections[i].entries != NULL) {
359                                 free(cfg->sections[i].entries);
360                                 cfg->sections[i].entries = NULL;
361                         }
362                 }
363                 free(cfg->sections);
364                 cfg->sections = NULL;
365         }
366         free(cfg);
367         cfg = NULL;
368
369         return 0;
370 }
371
372 int
373 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
374 size_t length)
375 {
376         int i;
377         int num_sections = 0;
378         for (i = 0; i < cfg->num_sections; i++) {
379                 if (strncmp(cfg->sections[i].name, sectionname, length) == 0)
380                         num_sections++;
381         }
382         return num_sections;
383 }
384
385 int
386 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
387         int max_sections)
388 {
389         int i;
390
391         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
392                 snprintf(sections[i], CFG_NAME_LEN, "%s",
393                 cfg->sections[i].name);
394
395         return i;
396 }
397
398 int
399 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
400 {
401         return _get_section(cfg, sectionname) != NULL;
402 }
403
404 int
405 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
406         const char *sectionname)
407 {
408         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
409         if (s == NULL)
410                 return -1;
411         return s->num_entries;
412 }
413
414 int
415 rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
416         char *sectionname, int index)
417 {
418         if (index < 0 || index >= cfg->num_sections)
419                 return -1;
420
421         const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
422
423         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
424         return sect->num_entries;
425 }
426 int
427 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
428                 struct rte_cfgfile_entry *entries, int max_entries)
429 {
430         int i;
431         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
432         if (sect == NULL)
433                 return -1;
434         for (i = 0; i < max_entries && i < sect->num_entries; i++)
435                 entries[i] = sect->entries[i];
436         return i;
437 }
438
439 int
440 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
441                 char *sectionname,
442                 struct rte_cfgfile_entry *entries, int max_entries)
443 {
444         int i;
445         const struct rte_cfgfile_section *sect;
446
447         if (index < 0 || index >= cfg->num_sections)
448                 return -1;
449         sect = &cfg->sections[index];
450         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
451         for (i = 0; i < max_entries && i < sect->num_entries; i++)
452                 entries[i] = sect->entries[i];
453         return i;
454 }
455
456 const char *
457 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
458                 const char *entryname)
459 {
460         int i;
461         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
462         if (sect == NULL)
463                 return NULL;
464         for (i = 0; i < sect->num_entries; i++)
465                 if (strncmp(sect->entries[i].name, entryname, CFG_NAME_LEN)
466                                                                         == 0)
467                         return sect->entries[i].value;
468         return NULL;
469 }
470
471 int
472 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
473                 const char *entryname)
474 {
475         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
476 }