michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @file iniparser.h michael@0: @author N. Devillard michael@0: @date Sep 2007 michael@0: @version 3.0 michael@0: @brief Parser for ini files. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: /* michael@0: $Id: iniparser.h,v 1.26 2011-03-02 20:15:13 ndevilla Exp $ michael@0: $Revision: 1.26 $ michael@0: */ michael@0: michael@0: #ifndef _INIPARSER_H_ michael@0: #define _INIPARSER_H_ michael@0: michael@0: /*--------------------------------------------------------------------------- michael@0: Includes michael@0: ---------------------------------------------------------------------------*/ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: * The following #include is necessary on many Unixes but not Linux. michael@0: * It is not needed for Windows platforms. michael@0: * Uncomment it if needed. michael@0: */ michael@0: /* #include */ michael@0: michael@0: #include "dictionary.h" michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get number of sections in a dictionary michael@0: @param d Dictionary to examine michael@0: @return int Number of sections found in dictionary michael@0: michael@0: This function returns the number of sections found in a dictionary. michael@0: The test to recognize sections is done on the string stored in the michael@0: dictionary: a section name is given as "section" whereas a key is michael@0: stored as "section:key", thus the test looks for entries that do not michael@0: contain a colon. michael@0: michael@0: This clearly fails in the case a section name contains a colon, but michael@0: this should simply be avoided. michael@0: michael@0: This function returns -1 in case of error. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: int iniparser_getnsec(dictionary * d); michael@0: michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get name for section n in a dictionary. michael@0: @param d Dictionary to examine michael@0: @param n Section number (from 0 to nsec-1). michael@0: @return Pointer to char string michael@0: michael@0: This function locates the n-th section in a dictionary and returns michael@0: its name as a pointer to a string statically allocated inside the michael@0: dictionary. Do not free or modify the returned string! michael@0: michael@0: This function returns NULL in case of error. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: char * iniparser_getsecname(dictionary * d, int n); michael@0: michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Save a dictionary to a loadable ini file michael@0: @param d Dictionary to dump michael@0: @param f Opened file pointer to dump to michael@0: @return void michael@0: michael@0: This function dumps a given dictionary into a loadable ini file. michael@0: It is Ok to specify @c stderr or @c stdout as output files. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: void iniparser_dump_ini(dictionary * d, FILE * f); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Dump a dictionary to an opened file pointer. michael@0: @param d Dictionary to dump. michael@0: @param f Opened file pointer to dump to. michael@0: @return void michael@0: michael@0: This function prints out the contents of a dictionary, one element by michael@0: line, onto the provided file pointer. It is OK to specify @c stderr michael@0: or @c stdout as output files. This function is meant for debugging michael@0: purposes mostly. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: void iniparser_dump(dictionary * d, FILE * f); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get the string associated to a key michael@0: @param d Dictionary to search michael@0: @param key Key string to look for michael@0: @param def Default value to return if key not found. michael@0: @return pointer to statically allocated character string michael@0: michael@0: This function queries a dictionary for a key. A key as read from an michael@0: ini file is given as "section:key". If the key cannot be found, michael@0: the pointer passed as 'def' is returned. michael@0: The returned char pointer is pointing to a string allocated in michael@0: the dictionary, do not free or modify it. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: char * iniparser_getstring(dictionary * d, char * key, char * def); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get the string associated to a key, convert to an int michael@0: @param d Dictionary to search michael@0: @param key Key string to look for michael@0: @param notfound Value to return in case of error michael@0: @return integer michael@0: michael@0: This function queries a dictionary for a key. A key as read from an michael@0: ini file is given as "section:key". If the key cannot be found, michael@0: the notfound value is returned. michael@0: michael@0: Supported values for integers include the usual C notation michael@0: so decimal, octal (starting with 0) and hexadecimal (starting with 0x) michael@0: are supported. Examples: michael@0: michael@0: - "42" -> 42 michael@0: - "042" -> 34 (octal -> decimal) michael@0: - "0x42" -> 66 (hexa -> decimal) michael@0: michael@0: Warning: the conversion may overflow in various ways. Conversion is michael@0: totally outsourced to strtol(), see the associated man page for overflow michael@0: handling. michael@0: michael@0: Credits: Thanks to A. Becker for suggesting strtol() michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: int iniparser_getint(dictionary * d, char * key, int notfound); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get the string associated to a key, convert to a double michael@0: @param d Dictionary to search michael@0: @param key Key string to look for michael@0: @param notfound Value to return in case of error michael@0: @return double michael@0: michael@0: This function queries a dictionary for a key. A key as read from an michael@0: ini file is given as "section:key". If the key cannot be found, michael@0: the notfound value is returned. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: double iniparser_getdouble(dictionary * d, char * key, double notfound); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Get the string associated to a key, convert to a boolean michael@0: @param d Dictionary to search michael@0: @param key Key string to look for michael@0: @param notfound Value to return in case of error michael@0: @return integer michael@0: michael@0: This function queries a dictionary for a key. A key as read from an michael@0: ini file is given as "section:key". If the key cannot be found, michael@0: the notfound value is returned. michael@0: michael@0: A true boolean is found if one of the following is matched: michael@0: michael@0: - A string starting with 'y' michael@0: - A string starting with 'Y' michael@0: - A string starting with 't' michael@0: - A string starting with 'T' michael@0: - A string starting with '1' michael@0: michael@0: A false boolean is found if one of the following is matched: michael@0: michael@0: - A string starting with 'n' michael@0: - A string starting with 'N' michael@0: - A string starting with 'f' michael@0: - A string starting with 'F' michael@0: - A string starting with '0' michael@0: michael@0: The notfound value returned if no boolean is identified, does not michael@0: necessarily have to be 0 or 1. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: int iniparser_getboolean(dictionary * d, char * key, int notfound); michael@0: michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Set an entry in a dictionary. michael@0: @param ini Dictionary to modify. michael@0: @param entry Entry to modify (entry name) michael@0: @param val New value to associate to the entry. michael@0: @return int 0 if Ok, -1 otherwise. michael@0: michael@0: If the given entry can be found in the dictionary, it is modified to michael@0: contain the provided value. If it cannot be found, -1 is returned. michael@0: It is Ok to set val to NULL. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: int iniparser_set(dictionary * ini, char * entry, char * val); michael@0: michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Delete an entry in a dictionary michael@0: @param ini Dictionary to modify michael@0: @param entry Entry to delete (entry name) michael@0: @return void michael@0: michael@0: If the given entry can be found, it is deleted from the dictionary. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: void iniparser_unset(dictionary * ini, char * entry); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Finds out if a given entry exists in a dictionary michael@0: @param ini Dictionary to search michael@0: @param entry Name of the entry to look for michael@0: @return integer 1 if entry exists, 0 otherwise michael@0: michael@0: Finds out if a given entry exists in the dictionary. Since sections michael@0: are stored as keys with NULL associated values, this is the only way michael@0: of querying for the presence of sections in a dictionary. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: int iniparser_find_entry(dictionary * ini, char * entry) ; michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Parse an ini file and return an allocated dictionary object michael@0: @param ininame Name of the ini file to read. michael@0: @return Pointer to newly allocated dictionary michael@0: michael@0: This is the parser for ini files. This function is called, providing michael@0: the name of the file to be read. It returns a dictionary object that michael@0: should not be accessed directly, but through accessor functions michael@0: instead. michael@0: michael@0: The returned dictionary must be freed using iniparser_freedict(). michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: dictionary * iniparser_load(char * ininame); michael@0: michael@0: /*-------------------------------------------------------------------------*/ michael@0: /** michael@0: @brief Free all memory associated to an ini dictionary michael@0: @param d Dictionary to free michael@0: @return void michael@0: michael@0: Free all memory associated to an ini dictionary. michael@0: It is mandatory to call this function before the dictionary object michael@0: gets out of the current context. michael@0: */ michael@0: /*--------------------------------------------------------------------------*/ michael@0: void iniparser_freedict(dictionary * d); michael@0: michael@0: #endif