port iniparser code for iRobotwithWIFI

Fork of iniparser by Toby Harris

Committer:
4180skrw
Date:
Tue Dec 10 02:16:00 2013 +0000
Revision:
1:452fd0d30ac6
Parent:
0:1a9f9f36242e
ported for our code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tobyspark 0:1a9f9f36242e 1
tobyspark 0:1a9f9f36242e 2 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 3 /**
tobyspark 0:1a9f9f36242e 4 @file iniparser.h
tobyspark 0:1a9f9f36242e 5 @author N. Devillard
tobyspark 0:1a9f9f36242e 6 @brief Parser for ini files.
tobyspark 0:1a9f9f36242e 7 */
tobyspark 0:1a9f9f36242e 8 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 9
tobyspark 0:1a9f9f36242e 10
tobyspark 0:1a9f9f36242e 11 /*---------------------------------------------------------------------------
tobyspark 0:1a9f9f36242e 12 Includes
tobyspark 0:1a9f9f36242e 13 ---------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 14
tobyspark 0:1a9f9f36242e 15 #include <stdio.h>
tobyspark 0:1a9f9f36242e 16 #include <stdlib.h>
tobyspark 0:1a9f9f36242e 17 #include <string.h>
tobyspark 0:1a9f9f36242e 18
tobyspark 0:1a9f9f36242e 19 /*
tobyspark 0:1a9f9f36242e 20 * The following #include is necessary on many Unixes but not Linux.
tobyspark 0:1a9f9f36242e 21 * It is not needed for Windows platforms.
tobyspark 0:1a9f9f36242e 22 * Uncomment it if needed.
tobyspark 0:1a9f9f36242e 23 */
tobyspark 0:1a9f9f36242e 24 /* #include <unistd.h> */
tobyspark 0:1a9f9f36242e 25
tobyspark 0:1a9f9f36242e 26 #include "dictionary.h"
tobyspark 0:1a9f9f36242e 27
tobyspark 0:1a9f9f36242e 28 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 29 /**
tobyspark 0:1a9f9f36242e 30 @brief Get number of sections in a dictionary
tobyspark 0:1a9f9f36242e 31 @param d Dictionary to examine
tobyspark 0:1a9f9f36242e 32 @return int Number of sections found in dictionary
tobyspark 0:1a9f9f36242e 33
tobyspark 0:1a9f9f36242e 34 This function returns the number of sections found in a dictionary.
tobyspark 0:1a9f9f36242e 35 The test to recognize sections is done on the string stored in the
tobyspark 0:1a9f9f36242e 36 dictionary: a section name is given as "section" whereas a key is
tobyspark 0:1a9f9f36242e 37 stored as "section:key", thus the test looks for entries that do not
tobyspark 0:1a9f9f36242e 38 contain a colon.
tobyspark 0:1a9f9f36242e 39
tobyspark 0:1a9f9f36242e 40 This clearly fails in the case a section name contains a colon, but
tobyspark 0:1a9f9f36242e 41 this should simply be avoided.
tobyspark 0:1a9f9f36242e 42
tobyspark 0:1a9f9f36242e 43 This function returns -1 in case of error.
tobyspark 0:1a9f9f36242e 44 */
tobyspark 0:1a9f9f36242e 45 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 46
tobyspark 0:1a9f9f36242e 47 int iniparser_getnsec(dictionary * d);
tobyspark 0:1a9f9f36242e 48
tobyspark 0:1a9f9f36242e 49
tobyspark 0:1a9f9f36242e 50 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 51 /**
tobyspark 0:1a9f9f36242e 52 @brief Get name for section n in a dictionary.
tobyspark 0:1a9f9f36242e 53 @param d Dictionary to examine
tobyspark 0:1a9f9f36242e 54 @param n Section number (from 0 to nsec-1).
tobyspark 0:1a9f9f36242e 55 @return Pointer to char string
tobyspark 0:1a9f9f36242e 56
tobyspark 0:1a9f9f36242e 57 This function locates the n-th section in a dictionary and returns
tobyspark 0:1a9f9f36242e 58 its name as a pointer to a string statically allocated inside the
tobyspark 0:1a9f9f36242e 59 dictionary. Do not free or modify the returned string!
tobyspark 0:1a9f9f36242e 60
tobyspark 0:1a9f9f36242e 61 This function returns NULL in case of error.
tobyspark 0:1a9f9f36242e 62 */
tobyspark 0:1a9f9f36242e 63 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 64
tobyspark 0:1a9f9f36242e 65 char * iniparser_getsecname(dictionary * d, int n);
tobyspark 0:1a9f9f36242e 66
tobyspark 0:1a9f9f36242e 67
tobyspark 0:1a9f9f36242e 68 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 69 /**
tobyspark 0:1a9f9f36242e 70 @brief Save a dictionary to a loadable ini file
tobyspark 0:1a9f9f36242e 71 @param d Dictionary to dump
tobyspark 0:1a9f9f36242e 72 @param f Opened file pointer to dump to
tobyspark 0:1a9f9f36242e 73 @return void
tobyspark 0:1a9f9f36242e 74
tobyspark 0:1a9f9f36242e 75 This function dumps a given dictionary into a loadable ini file.
tobyspark 0:1a9f9f36242e 76 It is Ok to specify @c stderr or @c stdout as output files.
tobyspark 0:1a9f9f36242e 77 */
tobyspark 0:1a9f9f36242e 78 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 79
tobyspark 0:1a9f9f36242e 80 void iniparser_dump_ini(dictionary * d, FILE * f);
tobyspark 0:1a9f9f36242e 81
tobyspark 0:1a9f9f36242e 82 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 83 /**
tobyspark 0:1a9f9f36242e 84 @brief Save a dictionary section to a loadable ini file
tobyspark 0:1a9f9f36242e 85 @param d Dictionary to dump
tobyspark 0:1a9f9f36242e 86 @param s Section name of dictionary to dump
tobyspark 0:1a9f9f36242e 87 @param f Opened file pointer to dump to
tobyspark 0:1a9f9f36242e 88 @return void
tobyspark 0:1a9f9f36242e 89
tobyspark 0:1a9f9f36242e 90 This function dumps a given section of a given dictionary into a loadable ini
tobyspark 0:1a9f9f36242e 91 file. It is Ok to specify @c stderr or @c stdout as output files.
tobyspark 0:1a9f9f36242e 92 */
tobyspark 0:1a9f9f36242e 93 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 94
tobyspark 0:1a9f9f36242e 95 void iniparser_dumpsection_ini(dictionary * d, char * s, FILE * f);
tobyspark 0:1a9f9f36242e 96
tobyspark 0:1a9f9f36242e 97 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 98 /**
tobyspark 0:1a9f9f36242e 99 @brief Dump a dictionary to an opened file pointer.
tobyspark 0:1a9f9f36242e 100 @param d Dictionary to dump.
tobyspark 0:1a9f9f36242e 101 @param f Opened file pointer to dump to.
tobyspark 0:1a9f9f36242e 102 @return void
tobyspark 0:1a9f9f36242e 103
tobyspark 0:1a9f9f36242e 104 This function prints out the contents of a dictionary, one element by
tobyspark 0:1a9f9f36242e 105 line, onto the provided file pointer. It is OK to specify @c stderr
tobyspark 0:1a9f9f36242e 106 or @c stdout as output files. This function is meant for debugging
tobyspark 0:1a9f9f36242e 107 purposes mostly.
tobyspark 0:1a9f9f36242e 108 */
tobyspark 0:1a9f9f36242e 109 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 110 void iniparser_dump(dictionary * d, FILE * f);
tobyspark 0:1a9f9f36242e 111
tobyspark 0:1a9f9f36242e 112 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 113 /**
tobyspark 0:1a9f9f36242e 114 @brief Get the number of keys in a section of a dictionary.
tobyspark 0:1a9f9f36242e 115 @param d Dictionary to examine
tobyspark 0:1a9f9f36242e 116 @param s Section name of dictionary to examine
tobyspark 0:1a9f9f36242e 117 @return Number of keys in section
tobyspark 0:1a9f9f36242e 118 */
tobyspark 0:1a9f9f36242e 119 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 120 int iniparser_getsecnkeys(dictionary * d, char * s);
tobyspark 0:1a9f9f36242e 121
tobyspark 0:1a9f9f36242e 122 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 123 /**
tobyspark 0:1a9f9f36242e 124 @brief Get the number of keys in a section of a dictionary.
tobyspark 0:1a9f9f36242e 125 @param d Dictionary to examine
tobyspark 0:1a9f9f36242e 126 @param s Section name of dictionary to examine
tobyspark 0:1a9f9f36242e 127 @return pointer to statically allocated character strings
tobyspark 0:1a9f9f36242e 128
tobyspark 0:1a9f9f36242e 129 This function queries a dictionary and finds all keys in a given section.
tobyspark 0:1a9f9f36242e 130 Each pointer in the returned char pointer-to-pointer is pointing to
tobyspark 0:1a9f9f36242e 131 a string allocated in the dictionary; do not free or modify them.
tobyspark 0:1a9f9f36242e 132
tobyspark 0:1a9f9f36242e 133 This function returns NULL in case of error.
tobyspark 0:1a9f9f36242e 134 */
tobyspark 0:1a9f9f36242e 135 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 136 char ** iniparser_getseckeys(dictionary * d, char * s);
tobyspark 0:1a9f9f36242e 137
tobyspark 0:1a9f9f36242e 138 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 139 /**
tobyspark 0:1a9f9f36242e 140 @brief Get the string associated to a key
tobyspark 0:1a9f9f36242e 141 @param d Dictionary to search
tobyspark 0:1a9f9f36242e 142 @param key Key string to look for
tobyspark 0:1a9f9f36242e 143 @param def Default value to return if key not found.
tobyspark 0:1a9f9f36242e 144 @return pointer to statically allocated character string
tobyspark 0:1a9f9f36242e 145
tobyspark 0:1a9f9f36242e 146 This function queries a dictionary for a key. A key as read from an
tobyspark 0:1a9f9f36242e 147 ini file is given as "section:key". If the key cannot be found,
tobyspark 0:1a9f9f36242e 148 the pointer passed as 'def' is returned.
tobyspark 0:1a9f9f36242e 149 The returned char pointer is pointing to a string allocated in
tobyspark 0:1a9f9f36242e 150 the dictionary, do not free or modify it.
tobyspark 0:1a9f9f36242e 151 */
tobyspark 0:1a9f9f36242e 152 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 153 char * iniparser_getstring(dictionary * d, const char * key, char * def);
tobyspark 0:1a9f9f36242e 154
tobyspark 0:1a9f9f36242e 155 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 156 /**
tobyspark 0:1a9f9f36242e 157 @brief Get the string associated to a key, convert to an int
tobyspark 0:1a9f9f36242e 158 @param d Dictionary to search
tobyspark 0:1a9f9f36242e 159 @param key Key string to look for
tobyspark 0:1a9f9f36242e 160 @param notfound Value to return in case of error
tobyspark 0:1a9f9f36242e 161 @return integer
tobyspark 0:1a9f9f36242e 162
tobyspark 0:1a9f9f36242e 163 This function queries a dictionary for a key. A key as read from an
tobyspark 0:1a9f9f36242e 164 ini file is given as "section:key". If the key cannot be found,
tobyspark 0:1a9f9f36242e 165 the notfound value is returned.
tobyspark 0:1a9f9f36242e 166
tobyspark 0:1a9f9f36242e 167 Supported values for integers include the usual C notation
tobyspark 0:1a9f9f36242e 168 so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
tobyspark 0:1a9f9f36242e 169 are supported. Examples:
tobyspark 0:1a9f9f36242e 170
tobyspark 0:1a9f9f36242e 171 - "42" -> 42
tobyspark 0:1a9f9f36242e 172 - "042" -> 34 (octal -> decimal)
tobyspark 0:1a9f9f36242e 173 - "0x42" -> 66 (hexa -> decimal)
tobyspark 0:1a9f9f36242e 174
tobyspark 0:1a9f9f36242e 175 Warning: the conversion may overflow in various ways. Conversion is
tobyspark 0:1a9f9f36242e 176 totally outsourced to strtol(), see the associated man page for overflow
tobyspark 0:1a9f9f36242e 177 handling.
tobyspark 0:1a9f9f36242e 178
tobyspark 0:1a9f9f36242e 179 Credits: Thanks to A. Becker for suggesting strtol()
tobyspark 0:1a9f9f36242e 180 */
tobyspark 0:1a9f9f36242e 181 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 182 int iniparser_getint(dictionary * d, const char * key, int notfound);
tobyspark 0:1a9f9f36242e 183
tobyspark 0:1a9f9f36242e 184 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 185 /**
tobyspark 0:1a9f9f36242e 186 @brief Get the string associated to a key, convert to a double
tobyspark 0:1a9f9f36242e 187 @param d Dictionary to search
tobyspark 0:1a9f9f36242e 188 @param key Key string to look for
tobyspark 0:1a9f9f36242e 189 @param notfound Value to return in case of error
tobyspark 0:1a9f9f36242e 190 @return double
tobyspark 0:1a9f9f36242e 191
tobyspark 0:1a9f9f36242e 192 This function queries a dictionary for a key. A key as read from an
tobyspark 0:1a9f9f36242e 193 ini file is given as "section:key". If the key cannot be found,
tobyspark 0:1a9f9f36242e 194 the notfound value is returned.
tobyspark 0:1a9f9f36242e 195 */
tobyspark 0:1a9f9f36242e 196 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 197 double iniparser_getdouble(dictionary * d, const char * key, double notfound);
tobyspark 0:1a9f9f36242e 198
tobyspark 0:1a9f9f36242e 199 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 200 /**
tobyspark 0:1a9f9f36242e 201 @brief Get the string associated to a key, convert to a boolean
tobyspark 0:1a9f9f36242e 202 @param d Dictionary to search
tobyspark 0:1a9f9f36242e 203 @param key Key string to look for
tobyspark 0:1a9f9f36242e 204 @param notfound Value to return in case of error
tobyspark 0:1a9f9f36242e 205 @return integer
tobyspark 0:1a9f9f36242e 206
tobyspark 0:1a9f9f36242e 207 This function queries a dictionary for a key. A key as read from an
tobyspark 0:1a9f9f36242e 208 ini file is given as "section:key". If the key cannot be found,
tobyspark 0:1a9f9f36242e 209 the notfound value is returned.
tobyspark 0:1a9f9f36242e 210
tobyspark 0:1a9f9f36242e 211 A true boolean is found if one of the following is matched:
tobyspark 0:1a9f9f36242e 212
tobyspark 0:1a9f9f36242e 213 - A string starting with 'y'
tobyspark 0:1a9f9f36242e 214 - A string starting with 'Y'
tobyspark 0:1a9f9f36242e 215 - A string starting with 't'
tobyspark 0:1a9f9f36242e 216 - A string starting with 'T'
tobyspark 0:1a9f9f36242e 217 - A string starting with '1'
tobyspark 0:1a9f9f36242e 218
tobyspark 0:1a9f9f36242e 219 A false boolean is found if one of the following is matched:
tobyspark 0:1a9f9f36242e 220
tobyspark 0:1a9f9f36242e 221 - A string starting with 'n'
tobyspark 0:1a9f9f36242e 222 - A string starting with 'N'
tobyspark 0:1a9f9f36242e 223 - A string starting with 'f'
tobyspark 0:1a9f9f36242e 224 - A string starting with 'F'
tobyspark 0:1a9f9f36242e 225 - A string starting with '0'
tobyspark 0:1a9f9f36242e 226
tobyspark 0:1a9f9f36242e 227 The notfound value returned if no boolean is identified, does not
tobyspark 0:1a9f9f36242e 228 necessarily have to be 0 or 1.
tobyspark 0:1a9f9f36242e 229 */
tobyspark 0:1a9f9f36242e 230 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 231 int iniparser_getboolean(dictionary * d, const char * key, int notfound);
tobyspark 0:1a9f9f36242e 232
tobyspark 0:1a9f9f36242e 233
tobyspark 0:1a9f9f36242e 234 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 235 /**
tobyspark 0:1a9f9f36242e 236 @brief Set an entry in a dictionary.
tobyspark 0:1a9f9f36242e 237 @param ini Dictionary to modify.
tobyspark 0:1a9f9f36242e 238 @param entry Entry to modify (entry name)
tobyspark 0:1a9f9f36242e 239 @param val New value to associate to the entry.
tobyspark 0:1a9f9f36242e 240 @return int 0 if Ok, -1 otherwise.
tobyspark 0:1a9f9f36242e 241
tobyspark 0:1a9f9f36242e 242 If the given entry can be found in the dictionary, it is modified to
tobyspark 0:1a9f9f36242e 243 contain the provided value. If it cannot be found, -1 is returned.
tobyspark 0:1a9f9f36242e 244 It is Ok to set val to NULL.
tobyspark 0:1a9f9f36242e 245 */
tobyspark 0:1a9f9f36242e 246 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 247 int iniparser_set(dictionary * ini, const char * entry, const char * val);
tobyspark 0:1a9f9f36242e 248
tobyspark 0:1a9f9f36242e 249
tobyspark 0:1a9f9f36242e 250 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 251 /**
tobyspark 0:1a9f9f36242e 252 @brief Delete an entry in a dictionary
tobyspark 0:1a9f9f36242e 253 @param ini Dictionary to modify
tobyspark 0:1a9f9f36242e 254 @param entry Entry to delete (entry name)
tobyspark 0:1a9f9f36242e 255 @return void
tobyspark 0:1a9f9f36242e 256
tobyspark 0:1a9f9f36242e 257 If the given entry can be found, it is deleted from the dictionary.
tobyspark 0:1a9f9f36242e 258 */
tobyspark 0:1a9f9f36242e 259 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 260 void iniparser_unset(dictionary * ini, const char * entry);
tobyspark 0:1a9f9f36242e 261
tobyspark 0:1a9f9f36242e 262 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 263 /**
tobyspark 0:1a9f9f36242e 264 @brief Finds out if a given entry exists in a dictionary
tobyspark 0:1a9f9f36242e 265 @param ini Dictionary to search
tobyspark 0:1a9f9f36242e 266 @param entry Name of the entry to look for
tobyspark 0:1a9f9f36242e 267 @return integer 1 if entry exists, 0 otherwise
tobyspark 0:1a9f9f36242e 268
tobyspark 0:1a9f9f36242e 269 Finds out if a given entry exists in the dictionary. Since sections
tobyspark 0:1a9f9f36242e 270 are stored as keys with NULL associated values, this is the only way
tobyspark 0:1a9f9f36242e 271 of querying for the presence of sections in a dictionary.
tobyspark 0:1a9f9f36242e 272 */
tobyspark 0:1a9f9f36242e 273 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 274 int iniparser_find_entry(dictionary * ini, const char * entry) ;
tobyspark 0:1a9f9f36242e 275
tobyspark 0:1a9f9f36242e 276 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 277 /**
tobyspark 0:1a9f9f36242e 278 @brief Parse an ini file and return an allocated dictionary object
tobyspark 0:1a9f9f36242e 279 @param ininame Name of the ini file to read.
tobyspark 0:1a9f9f36242e 280 @return Pointer to newly allocated dictionary
tobyspark 0:1a9f9f36242e 281
tobyspark 0:1a9f9f36242e 282 This is the parser for ini files. This function is called, providing
tobyspark 0:1a9f9f36242e 283 the name of the file to be read. It returns a dictionary object that
tobyspark 0:1a9f9f36242e 284 should not be accessed directly, but through accessor functions
tobyspark 0:1a9f9f36242e 285 instead.
tobyspark 0:1a9f9f36242e 286
tobyspark 0:1a9f9f36242e 287 The returned dictionary must be freed using iniparser_freedict().
tobyspark 0:1a9f9f36242e 288 */
tobyspark 0:1a9f9f36242e 289 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 290 dictionary * iniparser_load(const char * ininame);
tobyspark 0:1a9f9f36242e 291
tobyspark 0:1a9f9f36242e 292 /*-------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 293 /**
tobyspark 0:1a9f9f36242e 294 @brief Free all memory associated to an ini dictionary
tobyspark 0:1a9f9f36242e 295 @param d Dictionary to free
tobyspark 0:1a9f9f36242e 296 @return void
tobyspark 0:1a9f9f36242e 297
tobyspark 0:1a9f9f36242e 298 Free all memory associated to an ini dictionary.
tobyspark 0:1a9f9f36242e 299 It is mandatory to call this function before the dictionary object
tobyspark 0:1a9f9f36242e 300 gets out of the current context.
tobyspark 0:1a9f9f36242e 301 */
tobyspark 0:1a9f9f36242e 302 /*--------------------------------------------------------------------------*/
tobyspark 0:1a9f9f36242e 303 void iniparser_freedict(dictionary * d);
tobyspark 0:1a9f9f36242e 304