1 /* Copyright (c) 2000-2007 by Nicolas Devillard.
2 * Copyright (x) 2009 by Tim Post <tinkertim@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /** @addtogroup ciniparser
29 * @author N. Devillard
32 * @brief Parser for ini files.
36 #include <ccan/ciniparser/ciniparser.h>
38 #define ASCIILINESZ (1024)
39 #define INI_INVALID_KEY ((char*) NULL)
42 * This enum stores the status for each parsed line (internal use only).
44 typedef enum _line_status_ {
55 * @brief Convert a string to lowercase.
56 * @param s String to convert.
57 * @return ptr to statically allocated string.
59 * This function returns a pointer to a statically allocated string
60 * containing a lowercased version of the input string. Do not free
61 * or modify the returned string! Since the returned string is statically
62 * allocated, it will be modified at each function call (not re-entrant).
64 static char *strlwc(const char *s)
66 static char l[ASCIILINESZ+1];
72 for (i = 0; s[i] && i < ASCIILINESZ; i++)
79 * @brief Remove blanks at the beginning and the end of a string.
80 * @param s String to parse.
81 * @return ptr to statically allocated string.
83 * This function returns a pointer to a statically allocated string,
84 * which is identical to the input string, except that all blank
85 * characters at the end and the beg. of the string have been removed.
86 * Do not free or modify the returned string! Since the returned string
87 * is statically allocated, it will be modified at each function call
90 static char *strstrip(const char *s)
92 static char l[ASCIILINESZ+1];
93 unsigned int i, numspc;
101 for (i = numspc = 0; s[i] && i < ASCIILINESZ; i++) {
108 l[i - numspc] = '\0';
113 * @brief Load a single line from an INI file
114 * @param input_line Input line, may be concatenated multi-line input
115 * @param section Output space to store section
116 * @param key Output space to store key
117 * @param value Output space to store value
118 * @return line_status value
121 line_status ciniparser_line(char *input_line, char *section,
122 char *key, char *value)
125 char line[ASCIILINESZ+1];
128 strcpy(line, strstrip(input_line));
129 len = (int) strlen(line);
131 sta = LINE_UNPROCESSED;
135 } else if (line[0] == '#') {
138 } else if (line[0] == '[' && line[len-1] == ']') {
140 sscanf(line, "[%[^]]", section);
141 strcpy(section, strstrip(section));
142 strcpy(section, strlwc(section));
144 } else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2
145 || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
146 || sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
147 /* Usual key=value, with or without comments */
148 strcpy(key, strstrip(key));
149 strcpy(key, strlwc(key));
150 strcpy(value, strstrip(value));
152 * sscanf cannot handle '' or "" as empty values
155 if (!strcmp(value, "\"\"") || (!strcmp(value, "''"))) {
159 } else if (sscanf(line, "%[^=] = %[;#]", key, value) == 2
160 || sscanf(line, "%[^=] %[=]", key, value) == 2) {
167 strcpy(key, strstrip(key));
168 strcpy(key, strlwc(key));
172 /* Generate syntax error */
178 /* The remaining public functions are documented in ciniparser.h */
180 int ciniparser_getnsec(dictionary *d)
189 for (i = 0; i < d->size; i++) {
190 if (d->key[i] == NULL)
192 if (strchr(d->key[i], ':') == NULL) {
200 char *ciniparser_getsecname(dictionary *d, int n)
205 if (d == NULL || n < 0)
213 for (i = 0; i < d->size; i++) {
214 if (d->key[i] == NULL)
216 if (! strchr(d->key[i], ':')) {
227 return (char *) NULL;
230 void ciniparser_dump(dictionary *d, FILE *f)
234 if (d == NULL || f == NULL)
237 for (i = 0; i < d->size; i++) {
238 if (d->key[i] == NULL)
240 if (d->val[i] != NULL) {
241 fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
243 fprintf(f, "[%s]=UNDEF\n", d->key[i]);
250 void ciniparser_dump_ini(dictionary *d, FILE *f)
253 char keym[ASCIILINESZ+1];
258 if (d == NULL || f == NULL)
261 memset(keym, 0, ASCIILINESZ + 1);
263 nsec = ciniparser_getnsec(d);
265 /* No section in file: dump all keys as they are */
266 for (i = 0; i < d->size; i++) {
267 if (d->key[i] == NULL)
269 fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
274 for (i = 0; i < nsec; i++) {
275 secname = ciniparser_getsecname(d, i);
276 seclen = (int)strlen(secname);
277 fprintf(f, "\n[%s]\n", secname);
278 snprintf(keym, ASCIILINESZ + 1, "%s:", secname);
279 for (j = 0; j < d->size; j++) {
280 if (d->key[j] == NULL)
282 if (!strncmp(d->key[j], keym, seclen+1)) {
283 fprintf(f, "%-30s = %s\n",
285 d->val[j] ? d->val[j] : "");
294 char *ciniparser_getstring(dictionary *d, const char *key, char *def)
299 if (d == NULL || key == NULL)
302 lc_key = strlwc(key);
303 sval = dictionary_get(d, lc_key, def);
308 int ciniparser_getint(dictionary *d, const char *key, int notfound)
312 str = ciniparser_getstring(d, key, INI_INVALID_KEY);
314 if (str == INI_INVALID_KEY)
317 return (int) strtol(str, NULL, 10);
320 double ciniparser_getdouble(dictionary *d, char *key, double notfound)
324 str = ciniparser_getstring(d, key, INI_INVALID_KEY);
326 if (str == INI_INVALID_KEY)
332 int ciniparser_getboolean(dictionary *d, const char *key, int notfound)
337 c = ciniparser_getstring(d, key, INI_INVALID_KEY);
338 if (c == INI_INVALID_KEY)
342 case 'y': case 'Y': case '1': case 't': case 'T':
345 case 'n': case 'N': case '0': case 'f': case 'F':
356 int ciniparser_find_entry(dictionary *ini, char *entry)
360 if (ciniparser_getstring(ini, entry, INI_INVALID_KEY) != INI_INVALID_KEY) {
367 int ciniparser_set(dictionary *d, char *entry, char *val)
369 return dictionary_set(d, strlwc(entry), val);
372 void ciniparser_unset(dictionary *ini, char *entry)
374 dictionary_unset(ini, strlwc(entry));
377 dictionary *ciniparser_load(const char *ininame)
380 char line[ASCIILINESZ+1];
381 char section[ASCIILINESZ+1];
382 char key[ASCIILINESZ+1];
383 char tmp[ASCIILINESZ+1];
384 char val[ASCIILINESZ+1];
385 int last = 0, len, lineno = 0, errs = 0;
388 if ((in = fopen(ininame, "r")) == NULL) {
389 fprintf(stderr, "ciniparser: cannot open %s\n", ininame);
393 dict = dictionary_new(0);
399 memset(line, 0, ASCIILINESZ + 1);
400 memset(section, 0, ASCIILINESZ + 1);
401 memset(key, 0, ASCIILINESZ + 1);
402 memset(val, 0, ASCIILINESZ + 1);
405 while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
407 len = (int) strlen(line)-1;
408 /* Safety check against buffer overflows */
409 if (line[len] != '\n') {
411 "ciniparser: input line too long in %s (%d)\n",
414 dictionary_del(dict);
419 /* Get rid of \n and spaces at end of line */
421 ((line[len] == '\n') || (isspace(line[len])))) {
426 /* Detect multi-line */
427 if (line[len] == '\\') {
428 /* Multi-line value */
435 switch (ciniparser_line(line, section, key, val)) {
441 errs = dictionary_set(dict, section, NULL);
445 snprintf(tmp, ASCIILINESZ + 1, "%s:%s", section, key);
446 errs = dictionary_set(dict, tmp, val);
450 fprintf(stderr, "ciniparser: syntax error in %s (%d):\n",
452 fprintf(stderr, "-> %s\n", line);
459 memset(line, 0, ASCIILINESZ);
462 fprintf(stderr, "ciniparser: memory allocation failure\n");
468 dictionary_del(dict);
477 void ciniparser_freedict(dictionary *d)