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