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);
134 } else if (line[0] == '#') {
137 } else if (line[0] == '[' && line[len-1] == ']') {
139 sscanf(line, "[%[^]]", section);
140 strcpy(section, strstrip(section));
141 strcpy(section, strlwc(section));
143 } else if (sscanf (line, "%[^=] = \"%[^\"]\"", key, value) == 2
144 || sscanf (line, "%[^=] = '%[^\']'", key, value) == 2
145 || sscanf (line, "%[^=] = %[^;#]", key, value) == 2) {
146 /* Usual key=value, with or without comments */
147 strcpy(key, strstrip(key));
148 strcpy(key, strlwc(key));
149 strcpy(value, strstrip(value));
151 * sscanf cannot handle '' or "" as empty values
154 if (!strcmp(value, "\"\"") || (!strcmp(value, "''"))) {
158 } else if (sscanf(line, "%[^=] = %[;#]", key, value) == 2
159 || sscanf(line, "%[^=] %[=]", key, value) == 2) {
166 strcpy(key, strstrip(key));
167 strcpy(key, strlwc(key));
171 /* Generate syntax error */
177 /* The remaining public functions are documented in ciniparser.h */
179 int ciniparser_getnsec(dictionary *d)
188 for (i = 0; i < d->size; i++) {
189 if (d->key[i] == NULL)
191 if (strchr(d->key[i], ':') == NULL) {
199 char *ciniparser_getsecname(dictionary *d, int n)
204 if (d == NULL || n < 0)
212 for (i = 0; i < d->size; i++) {
213 if (d->key[i] == NULL)
215 if (! strchr(d->key[i], ':')) {
226 return (char *) NULL;
229 void ciniparser_dump(dictionary *d, FILE *f)
233 if (d == NULL || f == NULL)
236 for (i = 0; i < d->size; i++) {
237 if (d->key[i] == NULL)
239 if (d->val[i] != NULL) {
240 fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
242 fprintf(f, "[%s]=UNDEF\n", d->key[i]);
249 void ciniparser_dump_ini(dictionary *d, FILE *f)
252 char keym[ASCIILINESZ+1];
257 if (d == NULL || f == NULL)
260 memset(keym, 0, ASCIILINESZ + 1);
262 nsec = ciniparser_getnsec(d);
264 /* No section in file: dump all keys as they are */
265 for (i = 0; i < d->size; i++) {
266 if (d->key[i] == NULL)
268 fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
273 for (i = 0; i < nsec; i++) {
274 secname = ciniparser_getsecname(d, i);
275 seclen = (int)strlen(secname);
276 fprintf(f, "\n[%s]\n", secname);
277 snprintf(keym, ASCIILINESZ + 1, "%s:", secname);
278 for (j = 0; j < d->size; j++) {
279 if (d->key[j] == NULL)
281 if (!strncmp(d->key[j], keym, seclen+1)) {
282 fprintf(f, "%-30s = %s\n",
284 d->val[j] ? d->val[j] : "");
293 char *ciniparser_getstring(dictionary *d, const char *key, char *def)
298 if (d == NULL || key == NULL)
301 lc_key = strlwc(key);
302 sval = dictionary_get(d, lc_key, def);
307 int ciniparser_getint(dictionary *d, const char *key, int notfound)
311 str = ciniparser_getstring(d, key, INI_INVALID_KEY);
313 if (str == INI_INVALID_KEY)
316 return (int) strtol(str, NULL, 10);
319 double ciniparser_getdouble(dictionary *d, char *key, double notfound)
323 str = ciniparser_getstring(d, key, INI_INVALID_KEY);
325 if (str == INI_INVALID_KEY)
331 int ciniparser_getboolean(dictionary *d, const char *key, int notfound)
336 c = ciniparser_getstring(d, key, INI_INVALID_KEY);
337 if (c == INI_INVALID_KEY)
341 case 'y': case 'Y': case '1': case 't': case 'T':
344 case 'n': case 'N': case '0': case 'f': case 'F':
355 int ciniparser_find_entry(dictionary *ini, char *entry)
359 if (ciniparser_getstring(ini, entry, INI_INVALID_KEY) != INI_INVALID_KEY) {
366 int ciniparser_set(dictionary *d, char *entry, char *val)
368 return dictionary_set(d, strlwc(entry), val);
371 void ciniparser_unset(dictionary *ini, char *entry)
373 dictionary_unset(ini, strlwc(entry));
376 dictionary *ciniparser_load(const char *ininame)
379 char line[ASCIILINESZ+1];
380 char section[ASCIILINESZ+1];
381 char key[ASCIILINESZ+1];
382 char tmp[ASCIILINESZ+1];
383 char val[ASCIILINESZ+1];
384 int last = 0, len, lineno = 0, errs = 0;
387 if ((in = fopen(ininame, "r")) == NULL) {
388 fprintf(stderr, "ciniparser: cannot open %s\n", ininame);
392 dict = dictionary_new(0);
398 memset(line, 0, ASCIILINESZ + 1);
399 memset(section, 0, ASCIILINESZ + 1);
400 memset(key, 0, ASCIILINESZ + 1);
401 memset(val, 0, ASCIILINESZ + 1);
404 while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
406 len = (int) strlen(line)-1;
407 /* Safety check against buffer overflows */
408 if (line[len] != '\n') {
410 "ciniparser: input line too long in %s (%d)\n",
413 dictionary_del(dict);
418 /* Get rid of \n and spaces at end of line */
420 ((line[len] == '\n') || (isspace(line[len])))) {
425 /* Detect multi-line */
426 if (len >= 0 && line[len] == '\\') {
427 /* Multi-line value */
432 switch (ciniparser_line(line, section, key, val)) {
438 errs = dictionary_set(dict, section, NULL);
442 snprintf(tmp, ASCIILINESZ + 1, "%s:%s", section, key);
443 errs = dictionary_set(dict, tmp, val);
447 fprintf(stderr, "ciniparser: syntax error in %s (%d):\n",
449 fprintf(stderr, "-> %s\n", line);
456 memset(line, 0, ASCIILINESZ);
459 fprintf(stderr, "ciniparser: memory allocation failure\n");
465 dictionary_del(dict);
474 void ciniparser_freedict(dictionary *d)