ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / Soft / Lib / iniparser3.0b / src / iniparser.h
1
2 /*-------------------------------------------------------------------------*/
3 /**
4    @file    iniparser.h
5    @author  N. Devillard
6    @date    Sep 2007
7    @version 3.0
8    @brief   Parser for ini files.
9 */
10 /*--------------------------------------------------------------------------*/
11
12 /*
13         $Id: iniparser.h,v 1.1.2.1 2010-02-12 10:19:23 kleplat Exp $
14         $Revision: 1.1.2.1 $
15 */
16
17 #ifndef _INIPARSER_H_
18 #define _INIPARSER_H_
19
20 /*---------------------------------------------------------------------------
21                                                                 Includes
22  ---------------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 /*
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.
32  */
33 /* #include <unistd.h> */
34
35 #include "dictionary.h"
36
37 /*---------------------------------------------------------------------------
38                                                                 Macros
39  ---------------------------------------------------------------------------*/
40 /** For backwards compatibility only */
41 #define iniparser_getstr(d, k)  iniparser_getstring(d, k, NULL)
42 #define iniparser_setstr        iniparser_setstring
43
44 /*-------------------------------------------------------------------------*/
45 /**
46   @brief    Get number of sections in a dictionary
47   @param    d   Dictionary to examine
48   @return   int Number of sections found in dictionary
49
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
54   contain a colon.
55
56   This clearly fails in the case a section name contains a colon, but
57   this should simply be avoided.
58
59   This function returns -1 in case of error.
60  */
61 /*--------------------------------------------------------------------------*/
62
63 int iniparser_getnsec(dictionary * d);
64
65
66 /*-------------------------------------------------------------------------*/
67 /**
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
72
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!
76
77   This function returns NULL in case of error.
78  */
79 /*--------------------------------------------------------------------------*/
80
81 char * iniparser_getsecname(dictionary * d, int n);
82
83
84 /*-------------------------------------------------------------------------*/
85 /**
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
89   @return   void
90
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.
93  */
94 /*--------------------------------------------------------------------------*/
95
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);
99
100 /*-------------------------------------------------------------------------*/
101 /**
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.
105   @return   void
106
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
110   purposes mostly.
111  */
112 /*--------------------------------------------------------------------------*/
113
114 void iniparser_dump(dictionary * d);
115
116 /*-------------------------------------------------------------------------*/
117 /**
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.
124
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.
130  */
131 /*--------------------------------------------------------------------------*/
132
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);
135
136 /*-------------------------------------------------------------------------*/
137 /**
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
143
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.
149  */
150 /*--------------------------------------------------------------------------*/
151 char* iniparser_getstring(dictionary* d, const char* key, char* def);
152
153 /*-------------------------------------------------------------------------*/
154 /**
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
159   @return   integer
160
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.
164
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:
168
169   - "42"      ->  42
170   - "042"     ->  34 (octal -> decimal)
171   - "0x42"    ->  66 (hexa  -> decimal)
172
173   Warning: the conversion may overflow in various ways. Conversion is
174   totally outsourced to strtol(), see the associated man page for overflow
175   handling.
176
177   Credits: Thanks to A. Becker for suggesting strtol()
178  */
179 /*--------------------------------------------------------------------------*/
180 int iniparser_getint(dictionary * d, const char * key, int notfound);
181
182 /*-------------------------------------------------------------------------*/
183 /**
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
188   @return   double
189
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.
193  */
194 /*--------------------------------------------------------------------------*/
195 double iniparser_getdouble(dictionary * d, char * key, double notfound);
196
197 /*-------------------------------------------------------------------------*/
198 /**
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
203   @return   integer
204
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.
208
209   A true boolean is found if one of the following is matched:
210
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'
216
217   A false boolean is found if one of the following is matched:
218
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'
224
225   The notfound value returned if no boolean is identified, does not
226   necessarily have to be 0 or 1.
227  */
228 /*--------------------------------------------------------------------------*/
229 int iniparser_getboolean(dictionary * d, const char * key, int notfound);
230
231
232 /*-------------------------------------------------------------------------*/
233 /**
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.
239
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.
243  */
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);
247
248 /*-------------------------------------------------------------------------*/
249 /**
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.
257
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.
261  */
262 /*--------------------------------------------------------------------------*/
263 int iniparser_setvalue(dictionary* ini, char* entry, char* val, int type, void* ptr);
264
265 /*-------------------------------------------------------------------------*/
266 /**
267   @brief    Delete an entry in a dictionary
268   @param    ini     Dictionary to modify
269   @param    entry   Entry to delete (entry name)
270   @return   void
271
272   If the given entry can be found, it is deleted from the dictionary.
273  */
274 /*--------------------------------------------------------------------------*/
275 void iniparser_unset(dictionary * ini, char * entry);
276
277 /*-------------------------------------------------------------------------*/
278 /**
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
283
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.
287  */
288 /*--------------------------------------------------------------------------*/
289 int iniparser_find_entry(dictionary * ini, char * entry) ;
290
291 /*-------------------------------------------------------------------------*/
292 /**
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.
297
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
301   instead.
302
303   The returned dictionary must be freed using iniparser_freedict().
304  */
305 /*--------------------------------------------------------------------------*/
306 dictionary * iniparser_load(FILE* fp, dictionary * dict);
307
308 /*-------------------------------------------------------------------------*/
309 /**
310   @brief    Free all memory associated to an ini dictionary
311   @param    d Dictionary to free
312   @return   void
313
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.
317  */
318 /*--------------------------------------------------------------------------*/
319 void iniparser_freedict(dictionary * d);
320
321 void iniparser_vals2ptrs(dictionary * d , int scope);
322 void iniparser_ptrs2vals(dictionary * d , int scope);
323
324
325 #endif