iniparser.h 13 KB

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