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];
42 /* Make a key as section:keyword */
44 sprintf(longkey
, "%s:%s", sec
, key
);
49 /* Add (key,val) to dictionary */
50 dictionary_set(d
, longkey
, val
);
55 /*-------------------------------------------------------------------------*/
57 @brief Get number of sections in a dictionary
58 @param d Dictionary to examine
59 @return int Number of sections found in dictionary
61 This function returns the number of sections found in a dictionary.
62 The test to recognize sections is done on the string stored in the
63 dictionary: a section name is given as "section" whereas a key is
64 stored as "section:key", thus the test looks for entries that do not
67 This clearly fails in the case a section name contains a colon, but
68 this should simply be avoided.
70 This function returns -1 in case of error.
72 /*--------------------------------------------------------------------------*/
74 int iniparser_getnsec(dictionary
* d
)
79 if (d
==NULL
) return -1 ;
81 for (i
=0 ; i
<d
->size
; i
++) {
84 if (strchr(d
->key
[i
], ':')==NULL
) {
92 /*-------------------------------------------------------------------------*/
94 @brief Get name for section n in a dictionary.
95 @param d Dictionary to examine
96 @param n Section number (from 0 to nsec-1).
97 @return Pointer to char string
99 This function locates the n-th section in a dictionary and returns
100 its name as a pointer to a string statically allocated inside the
101 dictionary. Do not free or modify the returned string!
103 This function returns NULL in case of error.
105 /*--------------------------------------------------------------------------*/
107 char * iniparser_getsecname(dictionary
* d
, int n
)
112 if (d
==NULL
|| n
<0) return NULL
;
114 for (i
=0 ; i
<d
->size
; i
++) {
117 if (strchr(d
->key
[i
], ':')==NULL
) {
130 /*-------------------------------------------------------------------------*/
132 @brief Dump a dictionary to an opened file pointer.
133 @param d Dictionary to dump.
134 @param f Opened file pointer to dump to.
137 This function prints out the contents of a dictionary, one element by
138 line, onto the provided file pointer. It is OK to specify @c stderr
139 or @c stdout as output files. This function is meant for debugging
142 /*--------------------------------------------------------------------------*/
143 void iniparser_dump(dictionary
* d
, FILE * f
)
147 if (d
==NULL
|| f
==NULL
) return ;
148 for (i
=0 ; i
<d
->size
; i
++) {
151 if (d
->val
[i
]!=NULL
) {
152 fprintf(f
, "[%s]=[%s]\n", d
->key
[i
], d
->val
[i
]);
154 fprintf(f
, "[%s]=UNDEF\n", d
->key
[i
]);
160 /*-------------------------------------------------------------------------*/
162 @brief Save a dictionary to a loadable ini file
163 @param d Dictionary to dump
164 @param f Opened file pointer to dump to
167 This function dumps a given dictionary into a loadable ini file.
168 It is Ok to specify @c stderr or @c stdout as output files.
170 /*--------------------------------------------------------------------------*/
172 void iniparser_dump_ini(dictionary
* d
, FILE * f
)
175 char keym
[ASCIILINESZ
+1];
180 if (d
==NULL
|| f
==NULL
) return ;
182 nsec
= iniparser_getnsec(d
);
184 /* No section in file: dump all keys as they are */
185 for (i
=0 ; i
<d
->size
; i
++) {
188 fprintf(f
, "%s = %s\n", d
->key
[i
], d
->val
[i
]);
192 for (i
=0 ; i
<nsec
; i
++) {
193 secname
= iniparser_getsecname(d
, i
) ;
194 seclen
= (int)strlen(secname
);
195 fprintf(f
, "\n[%s]\n", secname
);
196 sprintf(keym
, "%s:", secname
);
197 for (j
=0 ; j
<d
->size
; j
++) {
200 if (!strncmp(d
->key
[j
], keym
, seclen
+1)) {
204 d
->val
[j
] ? d
->val
[j
] : "");
215 /*-------------------------------------------------------------------------*/
217 @brief Get the string associated to a key, return NULL if not found
218 @param d Dictionary to search
219 @param key Key string to look for
220 @return pointer to statically allocated character string, or NULL.
222 This function queries a dictionary for a key. A key as read from an
223 ini file is given as "section:key". If the key cannot be found,
225 The returned char pointer is pointing to a string allocated in
226 the dictionary, do not free or modify it.
228 This function is only provided for backwards compatibility with
229 previous versions of iniparser. It is recommended to use
230 iniparser_getstring() instead.
232 /*--------------------------------------------------------------------------*/
233 char * iniparser_getstr(dictionary
* d
, const char * key
)
235 return iniparser_getstring(d
, key
, NULL
);
239 /*-------------------------------------------------------------------------*/
241 @brief Get the string associated to a key
242 @param d Dictionary to search
243 @param key Key string to look for
244 @param def Default value to return if key not found.
245 @return pointer to statically allocated character string
247 This function queries a dictionary for a key. A key as read from an
248 ini file is given as "section:key". If the key cannot be found,
249 the pointer passed as 'def' is returned.
250 The returned char pointer is pointing to a string allocated in
251 the dictionary, do not free or modify it.
253 /*--------------------------------------------------------------------------*/
254 char * iniparser_getstring(dictionary
* d
, const char * key
, char * def
)
259 if (d
==NULL
|| key
==NULL
)
262 if (!(lc_key
= strdup(strlwc(key
)))) {
265 sval
= dictionary_get(d
, lc_key
, def
);
272 /*-------------------------------------------------------------------------*/
274 @brief Get the string associated to a key, convert to an int
275 @param d Dictionary to search
276 @param key Key string to look for
277 @param notfound Value to return in case of error
280 This function queries a dictionary for a key. A key as read from an
281 ini file is given as "section:key". If the key cannot be found,
282 the notfound value is returned.
284 Supported values for integers include the usual C notation
285 so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
286 are supported. Examples:
289 "042" -> 34 (octal -> decimal)
290 "0x42" -> 66 (hexa -> decimal)
292 Warning: the conversion may overflow in various ways. Conversion is
293 totally outsourced to strtol(), see the associated man page for overflow
296 Credits: Thanks to A. Becker for suggesting strtol()
298 /*--------------------------------------------------------------------------*/
299 int iniparser_getint(dictionary
* d
, const char * key
, int notfound
)
303 str
= iniparser_getstring(d
, key
, INI_INVALID_KEY
);
304 if (str
==INI_INVALID_KEY
) return notfound
;
305 return (int)strtol(str
, NULL
, 0);
309 /*-------------------------------------------------------------------------*/
311 @brief Get the string associated to a key, convert to a double
312 @param d Dictionary to search
313 @param key Key string to look for
314 @param notfound Value to return in case of error
317 This function queries a dictionary for a key. A key as read from an
318 ini file is given as "section:key". If the key cannot be found,
319 the notfound value is returned.
321 /*--------------------------------------------------------------------------*/
322 double iniparser_getdouble(dictionary
* d
, char * key
, double notfound
)
326 str
= iniparser_getstring(d
, key
, INI_INVALID_KEY
);
327 if (str
==INI_INVALID_KEY
) return notfound
;
333 /*-------------------------------------------------------------------------*/
335 @brief Get the string associated to a key, convert to a boolean
336 @param d Dictionary to search
337 @param key Key string to look for
338 @param notfound Value to return in case of error
341 This function queries a dictionary for a key. A key as read from an
342 ini file is given as "section:key". If the key cannot be found,
343 the notfound value is returned.
345 A true boolean is found if one of the following is matched:
347 - A string starting with 'y'
348 - A string starting with 'Y'
349 - A string starting with 't'
350 - A string starting with 'T'
351 - A string starting with '1'
353 A false boolean is found if one of the following is matched:
355 - A string starting with 'n'
356 - A string starting with 'N'
357 - A string starting with 'f'
358 - A string starting with 'F'
359 - A string starting with '0'
361 The notfound value returned if no boolean is identified, does not
362 necessarily have to be 0 or 1.
364 /*--------------------------------------------------------------------------*/
365 int iniparser_getboolean(dictionary
* d
, const char * key
, int notfound
)
370 c
= iniparser_getstring(d
, key
, INI_INVALID_KEY
);
371 if (c
==INI_INVALID_KEY
) return notfound
;
372 if (c
[0]=='y' || c
[0]=='Y' || c
[0]=='1' || c
[0]=='t' || c
[0]=='T') {
374 } else if (c
[0]=='n' || c
[0]=='N' || c
[0]=='0' || c
[0]=='f' || c
[0]=='F') {
383 /*-------------------------------------------------------------------------*/
385 @brief Finds out if a given entry exists in a dictionary
386 @param ini Dictionary to search
387 @param entry Name of the entry to look for
388 @return integer 1 if entry exists, 0 otherwise
390 Finds out if a given entry exists in the dictionary. Since sections
391 are stored as keys with NULL associated values, this is the only way
392 of querying for the presence of sections in a dictionary.
394 /*--------------------------------------------------------------------------*/
396 int iniparser_find_entry(
402 if (iniparser_getstring(ini
, entry
, INI_INVALID_KEY
)!=INI_INVALID_KEY
) {
410 /*-------------------------------------------------------------------------*/
412 @brief Set an entry in a dictionary.
413 @param ini Dictionary to modify.
414 @param entry Entry to modify (entry name)
415 @param val New value to associate to the entry.
416 @return int 0 if Ok, -1 otherwise.
418 If the given entry can be found in the dictionary, it is modified to
419 contain the provided value. If it cannot be found, -1 is returned.
420 It is Ok to set val to NULL.
422 /*--------------------------------------------------------------------------*/
424 int iniparser_setstr(dictionary
* ini
, char * entry
, char * val
)
426 dictionary_set(ini
, strlwc(entry
), val
);
430 /*-------------------------------------------------------------------------*/
432 @brief Delete an entry in a dictionary
433 @param ini Dictionary to modify
434 @param entry Entry to delete (entry name)
437 If the given entry can be found, it is deleted from the dictionary.
439 /*--------------------------------------------------------------------------*/
440 void iniparser_unset(dictionary
* ini
, char * entry
)
442 dictionary_unset(ini
, strlwc(entry
));
446 /*-------------------------------------------------------------------------*/
448 @brief Parse an ini file and return an allocated dictionary object
449 @param ininame Name of the ini file to read.
450 @return Pointer to newly allocated dictionary
452 This is the parser for ini files. This function is called, providing
453 the name of the file to be read. It returns a dictionary object that
454 should not be accessed directly, but through accessor functions
457 The returned dictionary must be freed using iniparser_freedict().
459 /*--------------------------------------------------------------------------*/
461 dictionary
* iniparser_load(const char * ininame
)
464 char lin
[ASCIILINESZ
+1];
465 char sec
[ASCIILINESZ
+1];
466 char key
[ASCIILINESZ
+1];
467 char val
[ASCIILINESZ
+1];
472 if ((ini
=fopen(ininame
, "r"))==NULL
) {
479 * Initialize a new dictionary entry
481 if (!(d
= dictionary_new(0))) {
486 while (fgets(lin
, ASCIILINESZ
, ini
)!=NULL
) {
488 where
= strskp(lin
); /* Skip leading spaces */
489 if (*where
==';' || *where
=='#' || *where
==0)
490 continue ; /* Comment lines */
492 if (sscanf(where
, "[%[^]]", sec
)==1) {
493 /* Valid section name */
494 strcpy(sec
, strlwc(sec
));
495 iniparser_add_entry(d
, sec
, NULL
, NULL
);
496 } else if (sscanf (where
, "%[^=] = \"%[^\"]\"", key
, val
) == 2
497 || sscanf (where
, "%[^=] = '%[^\']'", key
, val
) == 2
498 || sscanf (where
, "%[^=] = %[^;#]", key
, val
) == 2) {
499 strcpy(key
, strlwc(strcrop(key
)));
501 * sscanf cannot handle "" or '' as empty value,
504 if (!strcmp(val
, "\"\"") || !strcmp(val
, "''")) {
507 strcpy(val
, strcrop(val
));
509 iniparser_add_entry(d
, sec
, key
, val
);
519 /*-------------------------------------------------------------------------*/
521 @brief Free all memory associated to an ini dictionary
522 @param d Dictionary to free
525 Free all memory associated to an ini dictionary.
526 It is mandatory to call this function before the dictionary object
527 gets out of the current context.
529 /*--------------------------------------------------------------------------*/
531 void iniparser_freedict(dictionary
* d
)
536 /* vim: set ts=4 et sw=4 tw=75 */