2 /*-------------------------------------------------------------------------*/
8 @brief Parser for ini files.
10 /*--------------------------------------------------------------------------*/
13 $Id: iniparser.h,v 1.1.2.1 2010-02-12 10:19:23 kleplat Exp $
20 /*---------------------------------------------------------------------------
22 ---------------------------------------------------------------------------*/
29 * The following #include is necessary on many Unixes but not Linux.
30 * It is not needed for Windows platforms.
31 * Uncomment it if needed.
33 /* #include <unistd.h> */
35 #include "dictionary.h"
37 /*---------------------------------------------------------------------------
39 ---------------------------------------------------------------------------*/
40 /** For backwards compatibility only */
41 #define iniparser_getstr(d, k) iniparser_getstring(d, k, NULL)
42 #define iniparser_setstr iniparser_setstring
44 /*-------------------------------------------------------------------------*/
46 @brief Get number of sections in a dictionary
47 @param d Dictionary to examine
48 @return int Number of sections found in dictionary
50 This function returns the number of sections found in a dictionary.
51 The test to recognize sections is done on the string stored in the
52 dictionary: a section name is given as "section" whereas a key is
53 stored as "section:key", thus the test looks for entries that do not
56 This clearly fails in the case a section name contains a colon, but
57 this should simply be avoided.
59 This function returns -1 in case of error.
61 /*--------------------------------------------------------------------------*/
63 int iniparser_getnsec(dictionary * d);
66 /*-------------------------------------------------------------------------*/
68 @brief Get name for section n in a dictionary.
69 @param d Dictionary to examine
70 @param n Section number (from 0 to nsec-1).
71 @return Pointer to char string
73 This function locates the n-th section in a dictionary and returns
74 its name as a pointer to a string statically allocated inside the
75 dictionary. Do not free or modify the returned string!
77 This function returns NULL in case of error.
79 /*--------------------------------------------------------------------------*/
81 char * iniparser_getsecname(dictionary * d, int n);
84 /*-------------------------------------------------------------------------*/
86 @brief Save a dictionary to a loadable ini file
87 @param d Dictionary to dump
88 @param f Opened file pointer to dump to
91 This function dumps a given dictionary into a loadable ini file.
92 It is Ok to specify @c stderr or @c stdout as output files.
94 /*--------------------------------------------------------------------------*/
96 void iniparser_dump_ini(dictionary * d, FILE * f);
97 void iniparser_dump_ini_a3(dictionary * d, FILE * f , int flag_dump_k_shallows);
98 void iniparser_dump_ini_a4(dictionary * d, FILE * f , int flag_dump_k_shallows, int update_values_from_memory);
100 /*-------------------------------------------------------------------------*/
102 @brief Dump a dictionary to an opened file pointer.
103 @param d Dictionary to dump.
104 @param f Opened file pointer to dump to.
107 This function prints out the contents of a dictionary, one element by
108 line, onto the provided file pointer. It is OK to specify @c stderr
109 or @c stdout as output files. This function is meant for debugging
112 /*--------------------------------------------------------------------------*/
114 void iniparser_dump(dictionary * d);
116 /*-------------------------------------------------------------------------*/
118 @brief Initialise a key with a type and a pointer to the value.
119 @param d Dictionary to fill.
120 @param key Key string to look for
121 @param int type associated with this key
122 @param void* pointer to a valid data of the declared type
123 @return int 0 if Ok, -1 otherwise.
125 This function is the basis of the introspection mechanism. It binds a
126 data type and a valid address to a key. If the key is not present and
127 ptr is NULL, the key is created. On failure (for example when you try
128 to bind a valid pointer to a non existing key) the function returns -1
129 otherwise it returns 0.
131 /*--------------------------------------------------------------------------*/
133 int iniparser_alias(dictionary * d, const char* kkey, int type, void* ptr,void (*cb)(void),char rw);
134 int iniparser_alias_ex(dictionary * d, const char* kkey, int type, void* ptr, void (*cb)(void), char rw,int scope);
136 /*-------------------------------------------------------------------------*/
138 @brief Get the string associated to a key
139 @param d Dictionary to search
140 @param key Key string to look for
141 @param def Default value to return if key not found.
142 @return pointer to statically allocated character string
144 This function queries a dictionary for a key. A key as read from an
145 ini file is given as "section:key". If the key cannot be found,
146 the pointer passed as 'def' is returned.
147 The returned char pointer is pointing to a string allocated in
148 the dictionary, do not free or modify it.
150 /*--------------------------------------------------------------------------*/
151 char* iniparser_getstring(dictionary* d, const char* key, char* def);
153 /*-------------------------------------------------------------------------*/
155 @brief Get the string associated to a key, convert to an int
156 @param d Dictionary to search
157 @param key Key string to look for
158 @param notfound Value to return in case of error
161 This function queries a dictionary for a key. A key as read from an
162 ini file is given as "section:key". If the key cannot be found,
163 the notfound value is returned.
165 Supported values for integers include the usual C notation
166 so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
167 are supported. Examples:
170 - "042" -> 34 (octal -> decimal)
171 - "0x42" -> 66 (hexa -> decimal)
173 Warning: the conversion may overflow in various ways. Conversion is
174 totally outsourced to strtol(), see the associated man page for overflow
177 Credits: Thanks to A. Becker for suggesting strtol()
179 /*--------------------------------------------------------------------------*/
180 int iniparser_getint(dictionary * d, const char * key, int notfound);
182 /*-------------------------------------------------------------------------*/
184 @brief Get the string associated to a key, convert to a double
185 @param d Dictionary to search
186 @param key Key string to look for
187 @param notfound Value to return in case of error
190 This function queries a dictionary for a key. A key as read from an
191 ini file is given as "section:key". If the key cannot be found,
192 the notfound value is returned.
194 /*--------------------------------------------------------------------------*/
195 double iniparser_getdouble(dictionary * d, char * key, double notfound);
197 /*-------------------------------------------------------------------------*/
199 @brief Get the string associated to a key, convert to a boolean
200 @param d Dictionary to search
201 @param key Key string to look for
202 @param notfound Value to return in case of error
205 This function queries a dictionary for a key. A key as read from an
206 ini file is given as "section:key". If the key cannot be found,
207 the notfound value is returned.
209 A true boolean is found if one of the following is matched:
211 - A string starting with 'y'
212 - A string starting with 'Y'
213 - A string starting with 't'
214 - A string starting with 'T'
215 - A string starting with '1'
217 A false boolean is found if one of the following is matched:
219 - A string starting with 'n'
220 - A string starting with 'N'
221 - A string starting with 'f'
222 - A string starting with 'F'
223 - A string starting with '0'
225 The notfound value returned if no boolean is identified, does not
226 necessarily have to be 0 or 1.
228 /*--------------------------------------------------------------------------*/
229 int iniparser_getboolean(dictionary * d, const char * key, int notfound);
232 /*-------------------------------------------------------------------------*/
234 @brief Set an entry in a dictionary.
235 @param ini Dictionary to modify.
236 @param entry Entry to modify (entry name)
237 @param val New value to associate to the entry.
238 @return int 0 if Ok, -1 otherwise.
240 If the given entry can be found in the dictionary, it is modified to
241 contain the provided value. If it cannot be found, -1 is returned.
242 It is Ok to set val to NULL.
244 /*--------------------------------------------------------------------------*/
245 int iniparser_setstring(dictionary * ini, const char * entry, const char * val);
246 int iniparser_setstring_a4(dictionary * ini, const char * entry, const char * val , int trigger_callback);
248 /*-------------------------------------------------------------------------*/
250 @brief Set an entry in a dictionary.
251 @param ini Dictionary to modify.
252 @param entry Entry to modify (entry name)
253 @param val New value to associate to the entry.
254 @param int type associated with this key
255 @param void* pointer to a valid data of the declared type
256 @return int 0 if Ok, -1 otherwise.
258 If the given entry can be found in the dictionary, it is modified to
259 contain the provided value. Type & ptr are binded If it cannot be found, -1 is returned.
260 It is Ok to set val to NULL.
262 /*--------------------------------------------------------------------------*/
263 int iniparser_setvalue(dictionary* ini, char* entry, char* val, int type, void* ptr);
265 /*-------------------------------------------------------------------------*/
267 @brief Delete an entry in a dictionary
268 @param ini Dictionary to modify
269 @param entry Entry to delete (entry name)
272 If the given entry can be found, it is deleted from the dictionary.
274 /*--------------------------------------------------------------------------*/
275 void iniparser_unset(dictionary * ini, char * entry);
277 /*-------------------------------------------------------------------------*/
279 @brief Finds out if a given entry exists in a dictionary
280 @param ini Dictionary to search
281 @param entry Name of the entry to look for
282 @return integer 1 if entry exists, 0 otherwise
284 Finds out if a given entry exists in the dictionary. Since sections
285 are stored as keys with NULL associated values, this is the only way
286 of querying for the presence of sections in a dictionary.
288 /*--------------------------------------------------------------------------*/
289 int iniparser_find_entry(dictionary * ini, char * entry) ;
291 /*-------------------------------------------------------------------------*/
293 @brief Parse an ini file and return an allocated dictionary object
294 @param fp ini file to read.
295 @param dict Pointer to an existing dictionnary. This parameter can be NULL.
296 @return Pointer to newly allocated dictionary.
298 This is the parser for ini files. This function is called, providing
299 the name of the file to be read. It returns a dictionary object that
300 should not be accessed directly, but through accessor functions
303 The returned dictionary must be freed using iniparser_freedict().
305 /*--------------------------------------------------------------------------*/
306 dictionary * iniparser_load(FILE* fp, dictionary * dict);
308 /*-------------------------------------------------------------------------*/
310 @brief Free all memory associated to an ini dictionary
311 @param d Dictionary to free
314 Free all memory associated to an ini dictionary.
315 It is mandatory to call this function before the dictionary object
316 gets out of the current context.
318 /*--------------------------------------------------------------------------*/
319 void iniparser_freedict(dictionary * d);
321 void iniparser_vals2ptrs(dictionary * d , int scope);
322 void iniparser_ptrs2vals(dictionary * d , int scope);