iniparser.h 17 KB

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