2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 2.17 $
8 @brief Parser for ini files.
10 /*--------------------------------------------------------------------------*/
13 $Id: iniparser.c,v 2.17 2007-05-27 13:03:43 ndevilla Exp $
15 $Date: 2007-05-27 13:03:43 $
19 /*---------------------------------------------------------------------------
21 ---------------------------------------------------------------------------*/
23 #include "iniparser.h"
26 #define ASCIILINESZ 1024
27 #define INI_INVALID_KEY ((char*)-1)
29 /*---------------------------------------------------------------------------
30 Private to this module
31 ---------------------------------------------------------------------------*/
33 /* Private: add an entry to the dictionary */
34 static void iniparser_add_entry(
40 char longkey[2*ASCIILINESZ+1];
43 /* Make a key as section:keyword */
45 snprintf(longkey, sizeof(longkey), "%s:%s", sec, key);
51 /* Add (key,val) to dictionary */
52 dictionary_set(d, l, val);
57 /*-------------------------------------------------------------------------*/
59 @brief Get number of sections in a dictionary
60 @param d Dictionary to examine
61 @return int Number of sections found in dictionary
63 This function returns the number of sections found in a dictionary.
64 The test to recognize sections is done on the string stored in the
65 dictionary: a section name is given as "section" whereas a key is
66 stored as "section:key", thus the test looks for entries that do not
69 This clearly fails in the case a section name contains a colon, but
70 this should simply be avoided.
72 This function returns -1 in case of error.
74 /*--------------------------------------------------------------------------*/
76 int iniparser_getnsec(dictionary * d)
81 if (d==NULL) return -1 ;
83 for (i=0 ; i<d->size ; i++) {
86 if (strchr(d->key[i], ':')==NULL) {
94 /*-------------------------------------------------------------------------*/
96 @brief Get name for section n in a dictionary.
97 @param d Dictionary to examine
98 @param n Section number (from 0 to nsec-1).
99 @return Pointer to char string
101 This function locates the n-th section in a dictionary and returns
102 its name as a pointer to a string statically allocated inside the
103 dictionary. Do not free or modify the returned string!
105 This function returns NULL in case of error.
107 /*--------------------------------------------------------------------------*/
109 char * iniparser_getsecname(dictionary * d, int n)
114 if (d==NULL || n<0) return NULL ;
116 for (i=0 ; i<d->size ; i++) {
119 if (strchr(d->key[i], ':')==NULL) {
132 /*-------------------------------------------------------------------------*/
134 @brief Dump a dictionary to an opened file pointer.
135 @param d Dictionary to dump.
136 @param f Opened file pointer to dump to.
139 This function prints out the contents of a dictionary, one element by
140 line, onto the provided file pointer. It is OK to specify @c stderr
141 or @c stdout as output files. This function is meant for debugging
144 /*--------------------------------------------------------------------------*/
145 void iniparser_dump(dictionary * d, FILE * f)
149 if (d==NULL || f==NULL) return ;
150 for (i=0 ; i<d->size ; i++) {
153 if (d->val[i]!=NULL) {
154 fprintf(f, "[%s]=[%s]\n", d->key[i], d->val[i]);
156 fprintf(f, "[%s]=UNDEF\n", d->key[i]);
162 /*-------------------------------------------------------------------------*/
164 @brief Save a dictionary to a loadable ini file
165 @param d Dictionary to dump
166 @param f Opened file pointer to dump to
169 This function dumps a given dictionary into a loadable ini file.
170 It is Ok to specify @c stderr or @c stdout as output files.
172 /*--------------------------------------------------------------------------*/
174 void iniparser_dump_ini(dictionary * d, FILE * f)
177 char keym[ASCIILINESZ+1];
182 if (d==NULL || f==NULL) return ;
184 nsec = iniparser_getnsec(d);
186 /* No section in file: dump all keys as they are */
187 for (i=0 ; i<d->size ; i++) {
190 fprintf(f, "%s = %s\n", d->key[i], d->val[i]);
194 for (i=0 ; i<nsec ; i++) {
195 secname = iniparser_getsecname(d, i) ;
196 seclen = (int)strlen(secname);
197 fprintf(f, "\n[%s]\n", secname);
198 sprintf(keym, "%s:", secname);
199 for (j=0 ; j<d->size ; j++) {
202 if (!strncmp(d->key[j], keym, seclen+1)) {
206 d->val[j] ? d->val[j] : "");
217 /*-------------------------------------------------------------------------*/
219 @brief Get the string associated to a key, return NULL if not found
220 @param d Dictionary to search
221 @param key Key string to look for
222 @return pointer to statically allocated character string, or NULL.
224 This function queries a dictionary for a key. A key as read from an
225 ini file is given as "section:key". If the key cannot be found,
227 The returned char pointer is pointing to a string allocated in
228 the dictionary, do not free or modify it.
230 This function is only provided for backwards compatibility with
231 previous versions of iniparser. It is recommended to use
232 iniparser_getstring() instead.
234 /*--------------------------------------------------------------------------*/
235 char * iniparser_getstr(dictionary * d, const char * key)
237 return iniparser_getstring(d, key, NULL);
241 /*-------------------------------------------------------------------------*/
243 @brief Get the string associated to a key
244 @param d Dictionary to search
245 @param key Key string to look for
246 @param def Default value to return if key not found.
247 @return pointer to statically allocated character string
249 This function queries a dictionary for a key. A key as read from an
250 ini file is given as "section:key". If the key cannot be found,
251 the pointer passed as 'def' is returned.
252 The returned char pointer is pointing to a string allocated in
253 the dictionary, do not free or modify it.
255 /*--------------------------------------------------------------------------*/
256 char * iniparser_getstring(dictionary * d, const char * key, char * def)
261 if (d==NULL || key==NULL)
264 if (!(lc_key = strdup(strlwc(key)))) {
267 sval = dictionary_get(d, lc_key, def);
274 /*-------------------------------------------------------------------------*/
276 @brief Get the string associated to a key, convert to an int
277 @param d Dictionary to search
278 @param key Key string to look for
279 @param notfound Value to return in case of error
282 This function queries a dictionary for a key. A key as read from an
283 ini file is given as "section:key". If the key cannot be found,
284 the notfound value is returned.
286 Supported values for integers include the usual C notation
287 so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
288 are supported. Examples:
291 "042" -> 34 (octal -> decimal)
292 "0x42" -> 66 (hexa -> decimal)
294 Warning: the conversion may overflow in various ways. Conversion is
295 totally outsourced to strtol(), see the associated man page for overflow
298 Credits: Thanks to A. Becker for suggesting strtol()
300 /*--------------------------------------------------------------------------*/
301 int iniparser_getint(dictionary * d, const char * key, int notfound)
305 str = iniparser_getstring(d, key, INI_INVALID_KEY);
306 if (str==INI_INVALID_KEY) return notfound ;
307 return (int)strtol(str, NULL, 0);
311 /*-------------------------------------------------------------------------*/
313 @brief Get the string associated to a key, convert to a double
314 @param d Dictionary to search
315 @param key Key string to look for
316 @param notfound Value to return in case of error
319 This function queries a dictionary for a key. A key as read from an
320 ini file is given as "section:key". If the key cannot be found,
321 the notfound value is returned.
323 /*--------------------------------------------------------------------------*/
324 double iniparser_getdouble(dictionary * d, char * key, double notfound)
328 str = iniparser_getstring(d, key, INI_INVALID_KEY);
329 if (str==INI_INVALID_KEY) return notfound ;
335 /*-------------------------------------------------------------------------*/
337 @brief Get the string associated to a key, convert to a boolean
338 @param d Dictionary to search
339 @param key Key string to look for
340 @param notfound Value to return in case of error
343 This function queries a dictionary for a key. A key as read from an
344 ini file is given as "section:key". If the key cannot be found,
345 the notfound value is returned.
347 A true boolean is found if one of the following is matched:
349 - A string starting with 'y'
350 - A string starting with 'Y'
351 - A string starting with 't'
352 - A string starting with 'T'
353 - A string starting with '1'
355 A false boolean is found if one of the following is matched:
357 - A string starting with 'n'
358 - A string starting with 'N'
359 - A string starting with 'f'
360 - A string starting with 'F'
361 - A string starting with '0'
363 The notfound value returned if no boolean is identified, does not
364 necessarily have to be 0 or 1.
366 /*--------------------------------------------------------------------------*/
367 int iniparser_getboolean(dictionary * d, const char * key, int notfound)
372 c = iniparser_getstring(d, key, INI_INVALID_KEY);
373 if (c==INI_INVALID_KEY) return notfound ;
374 if (c[0]=='y' || c[0]=='Y' || c[0]=='1' || c[0]=='t' || c[0]=='T') {
376 } else if (c[0]=='n' || c[0]=='N' || c[0]=='0' || c[0]=='f' || c[0]=='F') {
385 /*-------------------------------------------------------------------------*/
387 @brief Finds out if a given entry exists in a dictionary
388 @param ini Dictionary to search
389 @param entry Name of the entry to look for
390 @return integer 1 if entry exists, 0 otherwise
392 Finds out if a given entry exists in the dictionary. Since sections
393 are stored as keys with NULL associated values, this is the only way
394 of querying for the presence of sections in a dictionary.
396 /*--------------------------------------------------------------------------*/
398 int iniparser_find_entry(
404 if (iniparser_getstring(ini, entry, INI_INVALID_KEY)!=INI_INVALID_KEY) {
412 /*-------------------------------------------------------------------------*/
414 @brief Set an entry in a dictionary.
415 @param ini Dictionary to modify.
416 @param entry Entry to modify (entry name)
417 @param val New value to associate to the entry.
418 @return int 0 if Ok, -1 otherwise.
420 If the given entry can be found in the dictionary, it is modified to
421 contain the provided value. If it cannot be found, -1 is returned.
422 It is Ok to set val to NULL.
424 /*--------------------------------------------------------------------------*/
426 int iniparser_setstr(dictionary * ini, char * entry, char * val)
428 dictionary_set(ini, strlwc(entry), val);
432 /*-------------------------------------------------------------------------*/
434 @brief Delete an entry in a dictionary
435 @param ini Dictionary to modify
436 @param entry Entry to delete (entry name)
439 If the given entry can be found, it is deleted from the dictionary.
441 /*--------------------------------------------------------------------------*/
442 void iniparser_unset(dictionary * ini, char * entry)
444 dictionary_unset(ini, strlwc(entry));
448 /*-------------------------------------------------------------------------*/
450 @brief Parse an ini file and return an allocated dictionary object
451 @param ininame Name of the ini file to read.
452 @return Pointer to newly allocated dictionary
454 This is the parser for ini files. This function is called, providing
455 the name of the file to be read. It returns a dictionary object that
456 should not be accessed directly, but through accessor functions
459 The returned dictionary must be freed using iniparser_freedict().
461 /*--------------------------------------------------------------------------*/
463 dictionary * iniparser_load(const char * ininame)
466 char lin[ASCIILINESZ+1];
467 char sec[ASCIILINESZ+1];
468 char key[ASCIILINESZ+1];
469 char val[ASCIILINESZ+1];
474 if ((ini=fopen(ininame, "r"))==NULL) {
481 * Initialize a new dictionary entry
483 if (!(d = dictionary_new(0))) {
488 while (fgets(lin, ASCIILINESZ, ini)!=NULL) {
490 where = strskp(lin); /* Skip leading spaces */
491 if (*where==';' || *where=='#' || *where==0)
492 continue ; /* Comment lines */
494 if (sscanf(where, "[%[^]]", sec)==1) {
495 /* Valid section name */
496 strcpy(sec, strlwc(sec));
497 iniparser_add_entry(d, sec, NULL, NULL);
498 } else if (sscanf (where, "%[^=] = \"%[^\"]\"", key, val) == 2
499 || sscanf (where, "%[^=] = '%[^\']'", key, val) == 2
500 || sscanf (where, "%[^=] = %[^;#]", key, val) == 2) {
501 strcpy(key, strlwc(strcrop(key)));
503 * sscanf cannot handle "" or '' as empty value,
506 if (!strcmp(val, "\"\"") || !strcmp(val, "''")) {
509 strcpy(val, strcrop(val));
511 iniparser_add_entry(d, sec, key, val);
521 /*-------------------------------------------------------------------------*/
523 @brief Free all memory associated to an ini dictionary
524 @param d Dictionary to free
527 Free all memory associated to an ini dictionary.
528 It is mandatory to call this function before the dictionary object
529 gets out of the current context.
531 /*--------------------------------------------------------------------------*/
533 void iniparser_freedict(dictionary * d)
538 /* vim: set ts=4 et sw=4 tw=75 */