4 /* Copyright (c) 2000-2007 by Nicolas Devillard.
5 * Copyright (x) 2009 by Tim Post <tinkertim@gmail.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 /** @addtogroup ciniparser
33 * @author N. Devillard
36 * @brief Parser for ini files.
44 #include "dictionary.h"
46 #define ciniparser_getstr(d, k) ciniparser_getstring(d, k, NULL)
47 #define ciniparser_setstr ciniparser_setstring
50 * @brief Get number of sections in a dictionary
51 * @param d Dictionary to examine
52 * @return int Number of sections found in dictionary, -1 on error
54 * This function returns the number of sections found in a dictionary.
55 * The test to recognize sections is done on the string stored in the
56 * dictionary: a section name is given as "section" whereas a key is
57 * stored as "section:key", thus the test looks for entries that do not
60 * This clearly fails in the case a section name contains a colon, but
61 * this should simply be avoided.
63 int ciniparser_getnsec(dictionary *d);
66 * @brief Get name for section n in a dictionary.
67 * @param d Dictionary to examine
68 * @param n Section number (from 0 to nsec-1).
69 * @return Pointer to char string, NULL on error
71 * This function locates the n-th section in a dictionary and returns
72 * its name as a pointer to a string statically allocated inside the
73 * dictionary. Do not free or modify the returned string!
75 char *ciniparser_getsecname(dictionary *d, int n);
78 * @brief Save a dictionary to a loadable ini file
79 * @param d Dictionary to dump
80 * @param f Opened file pointer to dump to
83 * This function dumps a given dictionary into a loadable ini file.
84 * It is Ok to specify @c stderr or @c stdout as output files.
86 void ciniparser_dump_ini(dictionary *d, FILE *f);
89 * @brief Dump a dictionary to an opened file pointer.
90 * @param d Dictionary to dump.
91 * @param f Opened file pointer to dump to.
94 * This function prints out the contents of a dictionary, one element by
95 * line, onto the provided file pointer. It is OK to specify @c stderr
96 * or @c stdout as output files. This function is meant for debugging
99 void ciniparser_dump(dictionary *d, FILE *f);
102 * @brief Get the string associated to a key
103 * @param d Dictionary to search
104 * @param key Key string to look for
105 * @param def Default value to return if key not found.
106 * @return pointer to statically allocated character string
108 * This function queries a dictionary for a key. A key as read from an
109 * ini file is given as "section:key". If the key cannot be found,
110 * the pointer passed as 'def' is returned.
111 * The returned char pointer is pointing to a string allocated in
112 * the dictionary, do not free or modify it.
114 char *ciniparser_getstring(dictionary *d, const char *key, char *def);
117 * @brief Get the string associated to a key, convert to an int
118 * @param d Dictionary to search
119 * @param key Key string to look for
120 * @param notfound Value to return in case of error
123 * This function queries a dictionary for a key. A key as read from an
124 * ini file is given as "section:key". If the key cannot be found,
125 * the notfound value is returned.
127 * Supported values for integers include the usual C notation
128 * so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
129 * are supported. Examples:
132 * - "042" -> 34 (octal -> decimal)
133 * - "0x42" -> 66 (hexa -> decimal)
135 * Warning: the conversion may overflow in various ways. Conversion is
136 * totally outsourced to strtol(), see the associated man page for overflow
139 * Credits: Thanks to A. Becker for suggesting strtol()
141 int ciniparser_getint(dictionary *d, const char *key, int notfound);
144 * @brief Get the string associated to a key, convert to a double
145 * @param d Dictionary to search
146 * @param key Key string to look for
147 * @param notfound Value to return in case of error
150 * This function queries a dictionary for a key. A key as read from an
151 * ini file is given as "section:key". If the key cannot be found,
152 * the notfound value is returned.
154 double ciniparser_getdouble(dictionary *d, char *key, double notfound);
157 * @brief Get the string associated to a key, convert to a boolean
158 * @param d Dictionary to search
159 * @param key Key string to look for
160 * @param notfound Value to return in case of error
163 * This function queries a dictionary for a key. A key as read from an
164 * ini file is given as "section:key". If the key cannot be found,
165 * the notfound value is returned.
167 * A true boolean is found if one of the following is matched:
169 * - A string starting with 'y'
170 * - A string starting with 'Y'
171 * - A string starting with 't'
172 * - A string starting with 'T'
173 * - A string starting with '1'
175 * A false boolean is found if one of the following is matched:
177 * - A string starting with 'n'
178 * - A string starting with 'N'
179 * - A string starting with 'f'
180 * - A string starting with 'F'
181 * - A string starting with '0'
183 * The notfound value returned if no boolean is identified, does not
184 * necessarily have to be 0 or 1.
186 int ciniparser_getboolean(dictionary *d, const char *key, int notfound);
189 * @brief Set an entry in a dictionary.
190 * @param ini Dictionary to modify.
191 * @param entry Entry to modify (entry name)
192 * @param val New value to associate to the entry.
193 * @return int 0 if Ok, -1 otherwise.
195 * If the given entry can be found in the dictionary, it is modified to
196 * contain the provided value. If it cannot be found, -1 is returned.
197 * It is Ok to set val to NULL.
199 int ciniparser_setstring(dictionary *ini, char *entry, char *val);
202 * @brief Delete an entry in a dictionary
203 * @param ini Dictionary to modify
204 * @param entry Entry to delete (entry name)
207 * If the given entry can be found, it is deleted from the dictionary.
209 void ciniparser_unset(dictionary *ini, char *entry);
212 * @brief Finds out if a given entry exists in a dictionary
213 * @param ini Dictionary to search
214 * @param entry Name of the entry to look for
215 * @return integer 1 if entry exists, 0 otherwise
217 * Finds out if a given entry exists in the dictionary. Since sections
218 * are stored as keys with NULL associated values, this is the only way
219 * of querying for the presence of sections in a dictionary.
221 int ciniparser_find_entry(dictionary *ini, char *entry) ;
224 * @brief Parse an ini file and return an allocated dictionary object
225 * @param ininame Name of the ini file to read.
226 * @return Pointer to newly allocated dictionary
228 * This is the parser for ini files. This function is called, providing
229 * the name of the file to be read. It returns a dictionary object that
230 * should not be accessed directly, but through accessor functions
233 * The returned dictionary must be freed using ciniparser_freedict().
235 dictionary *ciniparser_load(const char *ininame);
238 * @brief Free all memory associated to an ini dictionary
239 * @param d Dictionary to free
242 * Free all memory associated to an ini dictionary.
243 * It is mandatory to call this function before the dictionary object
244 * gets out of the current context.
246 void ciniparser_freedict(dictionary *d);
249 * @brief Set an item in the dictionary
250 * @param d Dictionary object created by ciniparser_load()
251 * @param entry Entry in the dictionary to manipulate
252 * @param val Value to assign to the entry
253 * @return 0 on success, -1 on error
255 * Remember that string values are converted by ciniparser_getboolean(),
256 * ciniparser_getdouble(), etc. It is also OK to set an entry to NULL.
258 int ciniparser_set(dictionary *d, char *entry, char *val);