2
/*-------------------------------------------------------------------------*/
8
@brief Parser for ini files.
10
/*--------------------------------------------------------------------------*/
13
$Id: iniparser.h,v 1.26 2011-03-02 20:15:13 ndevilla 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
@brief Get number of sections in a dictionary
40
@param d Dictionary to examine
41
@return int Number of sections found in dictionary
43
This function returns the number of sections found in a dictionary.
44
The test to recognize sections is done on the string stored in the
45
dictionary: a section name is given as "section" whereas a key is
46
stored as "section:key", thus the test looks for entries that do not
49
This clearly fails in the case a section name contains a colon, but
50
this should simply be avoided.
52
This function returns -1 in case of error.
54
/*--------------------------------------------------------------------------*/
56
int iniparser_getnsec(dictionary * d);
59
/*-------------------------------------------------------------------------*/
61
@brief Get name for section n in a dictionary.
62
@param d Dictionary to examine
63
@param n Section number (from 0 to nsec-1).
64
@return Pointer to char string
66
This function locates the n-th section in a dictionary and returns
67
its name as a pointer to a string statically allocated inside the
68
dictionary. Do not free or modify the returned string!
70
This function returns NULL in case of error.
72
/*--------------------------------------------------------------------------*/
74
char * iniparser_getsecname(dictionary * d, int n);
77
/*-------------------------------------------------------------------------*/
79
@brief Save a dictionary to a loadable ini file
80
@param d Dictionary to dump
81
@param f Opened file pointer to dump to
84
This function dumps a given dictionary into a loadable ini file.
85
It is Ok to specify @c stderr or @c stdout as output files.
87
/*--------------------------------------------------------------------------*/
89
void iniparser_dump_ini(dictionary * d, FILE * f);
91
/*-------------------------------------------------------------------------*/
93
@brief Dump a dictionary to an opened file pointer.
94
@param d Dictionary to dump.
95
@param f Opened file pointer to dump to.
98
This function prints out the contents of a dictionary, one element by
99
line, onto the provided file pointer. It is OK to specify @c stderr
100
or @c stdout as output files. This function is meant for debugging
103
/*--------------------------------------------------------------------------*/
104
void iniparser_dump(dictionary * d, FILE * f);
106
/*-------------------------------------------------------------------------*/
108
@brief Get the string associated to a key
109
@param d Dictionary to search
110
@param key Key string to look for
111
@param def Default value to return if key not found.
112
@return pointer to statically allocated character string
114
This function queries a dictionary for a key. A key as read from an
115
ini file is given as "section:key". If the key cannot be found,
116
the pointer passed as 'def' is returned.
117
The returned char pointer is pointing to a string allocated in
118
the dictionary, do not free or modify it.
120
/*--------------------------------------------------------------------------*/
121
char * iniparser_getstring(dictionary * d, char * key, char * def);
123
/*-------------------------------------------------------------------------*/
125
@brief Get the string associated to a key, convert to an int
126
@param d Dictionary to search
127
@param key Key string to look for
128
@param notfound Value to return in case of error
131
This function queries a dictionary for a key. A key as read from an
132
ini file is given as "section:key". If the key cannot be found,
133
the notfound value is returned.
135
Supported values for integers include the usual C notation
136
so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
137
are supported. Examples:
140
- "042" -> 34 (octal -> decimal)
141
- "0x42" -> 66 (hexa -> decimal)
143
Warning: the conversion may overflow in various ways. Conversion is
144
totally outsourced to strtol(), see the associated man page for overflow
147
Credits: Thanks to A. Becker for suggesting strtol()
149
/*--------------------------------------------------------------------------*/
150
int iniparser_getint(dictionary * d, char * key, int notfound);
152
/*-------------------------------------------------------------------------*/
154
@brief Get the string associated to a key, convert to a double
155
@param d Dictionary to search
156
@param key Key string to look for
157
@param notfound Value to return in case of error
160
This function queries a dictionary for a key. A key as read from an
161
ini file is given as "section:key". If the key cannot be found,
162
the notfound value is returned.
164
/*--------------------------------------------------------------------------*/
165
double iniparser_getdouble(dictionary * d, char * key, double notfound);
167
/*-------------------------------------------------------------------------*/
169
@brief Get the string associated to a key, convert to a boolean
170
@param d Dictionary to search
171
@param key Key string to look for
172
@param notfound Value to return in case of error
175
This function queries a dictionary for a key. A key as read from an
176
ini file is given as "section:key". If the key cannot be found,
177
the notfound value is returned.
179
A true boolean is found if one of the following is matched:
181
- A string starting with 'y'
182
- A string starting with 'Y'
183
- A string starting with 't'
184
- A string starting with 'T'
185
- A string starting with '1'
187
A false boolean is found if one of the following is matched:
189
- A string starting with 'n'
190
- A string starting with 'N'
191
- A string starting with 'f'
192
- A string starting with 'F'
193
- A string starting with '0'
195
The notfound value returned if no boolean is identified, does not
196
necessarily have to be 0 or 1.
198
/*--------------------------------------------------------------------------*/
199
int iniparser_getboolean(dictionary * d, char * key, int notfound);
202
/*-------------------------------------------------------------------------*/
204
@brief Set an entry in a dictionary.
205
@param ini Dictionary to modify.
206
@param entry Entry to modify (entry name)
207
@param val New value to associate to the entry.
208
@return int 0 if Ok, -1 otherwise.
210
If the given entry can be found in the dictionary, it is modified to
211
contain the provided value. If it cannot be found, -1 is returned.
212
It is Ok to set val to NULL.
214
/*--------------------------------------------------------------------------*/
215
int iniparser_set(dictionary * ini, char * entry, char * val);
218
/*-------------------------------------------------------------------------*/
220
@brief Delete an entry in a dictionary
221
@param ini Dictionary to modify
222
@param entry Entry to delete (entry name)
225
If the given entry can be found, it is deleted from the dictionary.
227
/*--------------------------------------------------------------------------*/
228
void iniparser_unset(dictionary * ini, char * entry);
230
/*-------------------------------------------------------------------------*/
232
@brief Finds out if a given entry exists in a dictionary
233
@param ini Dictionary to search
234
@param entry Name of the entry to look for
235
@return integer 1 if entry exists, 0 otherwise
237
Finds out if a given entry exists in the dictionary. Since sections
238
are stored as keys with NULL associated values, this is the only way
239
of querying for the presence of sections in a dictionary.
241
/*--------------------------------------------------------------------------*/
242
int iniparser_find_entry(dictionary * ini, char * entry) ;
244
/*-------------------------------------------------------------------------*/
246
@brief Parse an ini file and return an allocated dictionary object
247
@param ininame Name of the ini file to read.
248
@return Pointer to newly allocated dictionary
250
This is the parser for ini files. This function is called, providing
251
the name of the file to be read. It returns a dictionary object that
252
should not be accessed directly, but through accessor functions
255
The returned dictionary must be freed using iniparser_freedict().
257
/*--------------------------------------------------------------------------*/
258
dictionary * iniparser_load(char * ininame);
260
/*-------------------------------------------------------------------------*/
262
@brief Free all memory associated to an ini dictionary
263
@param d Dictionary to free
266
Free all memory associated to an ini dictionary.
267
It is mandatory to call this function before the dictionary object
268
gets out of the current context.
270
/*--------------------------------------------------------------------------*/
271
void iniparser_freedict(dictionary * d);