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