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