cfgfile: support global properties section
[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 <rte_string_fns.h>
39
40 #include "rte_cfgfile.h"
41
42 struct rte_cfgfile_section {
43         char name[CFG_NAME_LEN];
44         int num_entries;
45         struct rte_cfgfile_entry *entries[0];
46 };
47
48 struct rte_cfgfile {
49         int flags;
50         int num_sections;
51         struct rte_cfgfile_section *sections[0];
52 };
53
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
60
61 static unsigned
62 _strip(char *str, unsigned len)
63 {
64         int newlen = len;
65         if (len == 0)
66                 return 0;
67
68         if (isspace(str[len-1])) {
69                 /* strip trailing whitespace */
70                 while (newlen > 0 && isspace(str[newlen - 1]))
71                         str[--newlen] = '\0';
72         }
73
74         if (isspace(str[0])) {
75                 /* strip leading whitespace */
76                 int i, start = 1;
77                 while (isspace(str[start]) && start < newlen)
78                         start++
79                         ; /* do nothing */
80                 newlen -= start;
81                 for (i = 0; i < newlen; i++)
82                         str[i] = str[i+start];
83                 str[i] = '\0';
84         }
85         return newlen;
86 }
87
88 struct rte_cfgfile *
89 rte_cfgfile_load(const char *filename, int flags)
90 {
91         int allocated_sections = CFG_ALLOC_SECTION_BATCH;
92         int allocated_entries = 0;
93         int curr_section = -1;
94         int curr_entry = -1;
95         char buffer[256] = {0};
96         int lineno = 0;
97         struct rte_cfgfile *cfg = NULL;
98
99         FILE *f = fopen(filename, "r");
100         if (f == NULL)
101                 return NULL;
102
103         cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
104                 allocated_sections);
105         if (cfg == NULL)
106                 goto error2;
107
108         memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
109
110         if (flags & CFG_FLAG_GLOBAL_SECTION) {
111                 curr_section = 0;
112                 allocated_entries = CFG_ALLOC_ENTRY_BATCH;
113                 cfg->sections[curr_section] = malloc(
114                         sizeof(*cfg->sections[0]) +
115                         sizeof(cfg->sections[0]->entries[0]) *
116                         allocated_entries);
117                 if (cfg->sections[curr_section] == NULL) {
118                         printf("Error - no memory for global section\n");
119                         goto error1;
120                 }
121
122                 snprintf(cfg->sections[curr_section]->name,
123                                  sizeof(cfg->sections[0]->name), "GLOBAL");
124         }
125
126         while (fgets(buffer, sizeof(buffer), f) != NULL) {
127                 char *pos = NULL;
128                 size_t len = strnlen(buffer, sizeof(buffer));
129                 lineno++;
130                 if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
131                         printf("Error line %d - no \\n found on string. "
132                                         "Check if line too long\n", lineno);
133                         goto error1;
134                 }
135                 pos = memchr(buffer, ';', sizeof(buffer));
136                 if (pos != NULL) {
137                         *pos = '\0';
138                         len = pos -  buffer;
139                 }
140
141                 len = _strip(buffer, len);
142                 if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
143                         continue;
144
145                 if (buffer[0] == '[') {
146                         /* section heading line */
147                         char *end = memchr(buffer, ']', len);
148                         if (end == NULL) {
149                                 printf("Error line %d - no terminating '['"
150                                         "character found\n", lineno);
151                                 goto error1;
152                         }
153                         *end = '\0';
154                         _strip(&buffer[1], end - &buffer[1]);
155
156                         /* close off old section and add start new one */
157                         if (curr_section >= 0)
158                                 cfg->sections[curr_section]->num_entries =
159                                         curr_entry + 1;
160                         curr_section++;
161
162                         /* resize overall struct if we don't have room for more
163                         sections */
164                         if (curr_section == allocated_sections) {
165                                 allocated_sections += CFG_ALLOC_SECTION_BATCH;
166                                 struct rte_cfgfile *n_cfg = realloc(cfg,
167                                         sizeof(*cfg) + sizeof(cfg->sections[0])
168                                         * allocated_sections);
169                                 if (n_cfg == NULL) {
170                                         curr_section--;
171                                         printf("Error - no more memory\n");
172                                         goto error1;
173                                 }
174                                 cfg = n_cfg;
175                         }
176
177                         /* allocate space for new section */
178                         allocated_entries = CFG_ALLOC_ENTRY_BATCH;
179                         curr_entry = -1;
180                         cfg->sections[curr_section] = malloc(
181                                 sizeof(*cfg->sections[0]) +
182                                 sizeof(cfg->sections[0]->entries[0]) *
183                                 allocated_entries);
184                         if (cfg->sections[curr_section] == NULL) {
185                                 printf("Error - no more memory\n");
186                                 goto error1;
187                         }
188
189                         snprintf(cfg->sections[curr_section]->name,
190                                         sizeof(cfg->sections[0]->name),
191                                         "%s", &buffer[1]);
192                 } else {
193                         /* value line */
194                         if (curr_section < 0) {
195                                 printf("Error line %d - value outside of"
196                                         "section\n", lineno);
197                                 goto error1;
198                         }
199
200                         struct rte_cfgfile_section *sect =
201                                 cfg->sections[curr_section];
202                         char *split[2];
203                         if (rte_strsplit(buffer, sizeof(buffer), split, 2, '=')
204                                 != 2) {
205                                 printf("Error at line %d - cannot split "
206                                         "string\n", lineno);
207                                 goto error1;
208                         }
209
210                         curr_entry++;
211                         if (curr_entry == allocated_entries) {
212                                 allocated_entries += CFG_ALLOC_ENTRY_BATCH;
213                                 struct rte_cfgfile_section *n_sect = realloc(
214                                         sect, sizeof(*sect) +
215                                         sizeof(sect->entries[0]) *
216                                         allocated_entries);
217                                 if (n_sect == NULL) {
218                                         curr_entry--;
219                                         printf("Error - no more memory\n");
220                                         goto error1;
221                                 }
222                                 sect = cfg->sections[curr_section] = n_sect;
223                         }
224
225                         sect->entries[curr_entry] = malloc(
226                                 sizeof(*sect->entries[0]));
227                         if (sect->entries[curr_entry] == NULL) {
228                                 printf("Error - no more memory\n");
229                                 goto error1;
230                         }
231
232                         struct rte_cfgfile_entry *entry = sect->entries[
233                                 curr_entry];
234                         snprintf(entry->name, sizeof(entry->name), "%s",
235                                 split[0]);
236                         snprintf(entry->value, sizeof(entry->value), "%s",
237                                 split[1]);
238                         _strip(entry->name, strnlen(entry->name,
239                                 sizeof(entry->name)));
240                         _strip(entry->value, strnlen(entry->value,
241                                 sizeof(entry->value)));
242                 }
243         }
244         fclose(f);
245         cfg->flags = flags;
246         cfg->num_sections = curr_section + 1;
247         /* curr_section will still be -1 if we have an empty file */
248         if (curr_section >= 0)
249                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
250         return cfg;
251
252 error1:
253         cfg->num_sections = curr_section + 1;
254         if (curr_section >= 0)
255                 cfg->sections[curr_section]->num_entries = curr_entry + 1;
256         rte_cfgfile_close(cfg);
257 error2:
258         fclose(f);
259         return NULL;
260 }
261
262
263 int rte_cfgfile_close(struct rte_cfgfile *cfg)
264 {
265         int i, j;
266
267         if (cfg == NULL)
268                 return -1;
269
270         for (i = 0; i < cfg->num_sections; i++) {
271                 if (cfg->sections[i] != NULL) {
272                         if (cfg->sections[i]->num_entries) {
273                                 for (j = 0; j < cfg->sections[i]->num_entries;
274                                         j++) {
275                                         if (cfg->sections[i]->entries[j] !=
276                                                 NULL)
277                                                 free(cfg->sections[i]->
278                                                         entries[j]);
279                                 }
280                         }
281                         free(cfg->sections[i]);
282                 }
283         }
284         free(cfg);
285
286         return 0;
287 }
288
289 int
290 rte_cfgfile_num_sections(struct rte_cfgfile *cfg, const char *sectionname,
291 size_t length)
292 {
293         int i;
294         int num_sections = 0;
295         for (i = 0; i < cfg->num_sections; i++) {
296                 if (strncmp(cfg->sections[i]->name, sectionname, length) == 0)
297                         num_sections++;
298         }
299         return num_sections;
300 }
301
302 int
303 rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
304         int max_sections)
305 {
306         int i;
307
308         for (i = 0; i < cfg->num_sections && i < max_sections; i++)
309                 snprintf(sections[i], CFG_NAME_LEN, "%s",
310                 cfg->sections[i]->name);
311
312         return i;
313 }
314
315 static const struct rte_cfgfile_section *
316 _get_section(struct rte_cfgfile *cfg, const char *sectionname)
317 {
318         int i;
319         for (i = 0; i < cfg->num_sections; i++) {
320                 if (strncmp(cfg->sections[i]->name, sectionname,
321                                 sizeof(cfg->sections[0]->name)) == 0)
322                         return cfg->sections[i];
323         }
324         return NULL;
325 }
326
327 int
328 rte_cfgfile_has_section(struct rte_cfgfile *cfg, const char *sectionname)
329 {
330         return _get_section(cfg, sectionname) != NULL;
331 }
332
333 int
334 rte_cfgfile_section_num_entries(struct rte_cfgfile *cfg,
335         const char *sectionname)
336 {
337         const struct rte_cfgfile_section *s = _get_section(cfg, sectionname);
338         if (s == NULL)
339                 return -1;
340         return s->num_entries;
341 }
342
343
344 int
345 rte_cfgfile_section_entries(struct rte_cfgfile *cfg, const char *sectionname,
346                 struct rte_cfgfile_entry *entries, int max_entries)
347 {
348         int i;
349         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
350         if (sect == NULL)
351                 return -1;
352         for (i = 0; i < max_entries && i < sect->num_entries; i++)
353                 entries[i] = *sect->entries[i];
354         return i;
355 }
356
357 int
358 rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
359                 char *sectionname,
360                 struct rte_cfgfile_entry *entries, int max_entries)
361 {
362         int i;
363         const struct rte_cfgfile_section *sect;
364
365         if (index < 0 || index >= cfg->num_sections)
366                 return -1;
367
368         sect = cfg->sections[index];
369         snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
370         for (i = 0; i < max_entries && i < sect->num_entries; i++)
371                 entries[i] = *sect->entries[i];
372         return i;
373 }
374
375 const char *
376 rte_cfgfile_get_entry(struct rte_cfgfile *cfg, const char *sectionname,
377                 const char *entryname)
378 {
379         int i;
380         const struct rte_cfgfile_section *sect = _get_section(cfg, sectionname);
381         if (sect == NULL)
382                 return NULL;
383         for (i = 0; i < sect->num_entries; i++)
384                 if (strncmp(sect->entries[i]->name, entryname, CFG_NAME_LEN)
385                         == 0)
386                         return sect->entries[i]->value;
387         return NULL;
388 }
389
390 int
391 rte_cfgfile_has_entry(struct rte_cfgfile *cfg, const char *sectionname,
392                 const char *entryname)
393 {
394         return rte_cfgfile_get_entry(cfg, sectionname, entryname) != NULL;
395 }