ciniparser.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef _INIPARSER_H_
  2. #define _INIPARSER_H_
  3. /* Copyright (c) 2000-2007 by Nicolas Devillard.
  4. * Copyright (x) 2009 by Tim Post <tinkertim@gmail.com>
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. /** @addtogroup ciniparser
  26. * @{
  27. */
  28. /**
  29. * @file ciniparser.h
  30. * @author N. Devillard
  31. * @date Sep 2007
  32. * @version 3.0
  33. * @brief Parser for ini files.
  34. */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "dictionary.h"
  40. #define ciniparser_getstr(d, k) ciniparser_getstring(d, k, NULL)
  41. #define ciniparser_setstr ciniparser_setstring
  42. /**
  43. * @brief Get number of sections in a dictionary
  44. * @param d Dictionary to examine
  45. * @return int Number of sections found in dictionary, -1 on error
  46. *
  47. * This function returns the number of sections found in a dictionary.
  48. * The test to recognize sections is done on the string stored in the
  49. * dictionary: a section name is given as "section" whereas a key is
  50. * stored as "section:key", thus the test looks for entries that do not
  51. * contain a colon.
  52. *
  53. * This clearly fails in the case a section name contains a colon, but
  54. * this should simply be avoided.
  55. */
  56. int ciniparser_getnsec(dictionary *d);
  57. /**
  58. * @brief Get name for section n in a dictionary.
  59. * @param d Dictionary to examine
  60. * @param n Section number (from 0 to nsec-1).
  61. * @return Pointer to char string, NULL on error
  62. *
  63. * This function locates the n-th section in a dictionary and returns
  64. * its name as a pointer to a string statically allocated inside the
  65. * dictionary. Do not free or modify the returned string!
  66. */
  67. char *ciniparser_getsecname(dictionary *d, int n);
  68. /**
  69. * @brief Save a dictionary to a loadable ini file
  70. * @param d Dictionary to dump
  71. * @param f Opened file pointer to dump to
  72. * @return void
  73. *
  74. * This function dumps a given dictionary into a loadable ini file.
  75. * It is Ok to specify @c stderr or @c stdout as output files.
  76. */
  77. void ciniparser_dump_ini(dictionary *d, FILE *f);
  78. /**
  79. * @brief Dump a dictionary to an opened file pointer.
  80. * @param d Dictionary to dump.
  81. * @param f Opened file pointer to dump to.
  82. * @return void
  83. *
  84. * This function prints out the contents of a dictionary, one element by
  85. * line, onto the provided file pointer. It is OK to specify @c stderr
  86. * or @c stdout as output files. This function is meant for debugging
  87. * purposes mostly.
  88. */
  89. void ciniparser_dump(dictionary *d, FILE *f);
  90. /**
  91. * @brief Get the string associated to a key
  92. * @param d Dictionary to search
  93. * @param key Key string to look for
  94. * @param def Default value to return if key not found.
  95. * @return pointer to statically allocated character string
  96. *
  97. * This function queries a dictionary for a key. A key as read from an
  98. * ini file is given as "section:key". If the key cannot be found,
  99. * the pointer passed as 'def' is returned.
  100. * The returned char pointer is pointing to a string allocated in
  101. * the dictionary, do not free or modify it.
  102. */
  103. char *ciniparser_getstring(dictionary *d, const char *key, char *def);
  104. /**
  105. * @brief Get the string associated to a key, convert to an int
  106. * @param d Dictionary to search
  107. * @param key Key string to look for
  108. * @param notfound Value to return in case of error
  109. * @return integer
  110. *
  111. * This function queries a dictionary for a key. A key as read from an
  112. * ini file is given as "section:key". If the key cannot be found,
  113. * the notfound value is returned.
  114. *
  115. * Supported values for integers include the usual C notation
  116. * so decimal, octal (starting with 0) and hexadecimal (starting with 0x)
  117. * are supported. Examples:
  118. *
  119. * - "42" -> 42
  120. * - "042" -> 34 (octal -> decimal)
  121. * - "0x42" -> 66 (hexa -> decimal)
  122. *
  123. * Warning: the conversion may overflow in various ways. Conversion is
  124. * totally outsourced to strtol(), see the associated man page for overflow
  125. * handling.
  126. *
  127. * Credits: Thanks to A. Becker for suggesting strtol()
  128. */
  129. int ciniparser_getint(dictionary *d, const char *key, int notfound);
  130. /**
  131. * @brief Get the string associated to a key, convert to a double
  132. * @param d Dictionary to search
  133. * @param key Key string to look for
  134. * @param notfound Value to return in case of error
  135. * @return double
  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 notfound value is returned.
  140. */
  141. double ciniparser_getdouble(dictionary *d, char *key, double notfound);
  142. /**
  143. * @brief Get the string associated to a key, convert to a boolean
  144. * @param d Dictionary to search
  145. * @param key Key string to look for
  146. * @param notfound Value to return in case of error
  147. * @return integer
  148. *
  149. * This function queries a dictionary for a key. A key as read from an
  150. * ini file is given as "section:key". If the key cannot be found,
  151. * the notfound value is returned.
  152. *
  153. * A true boolean is found if one of the following is matched:
  154. *
  155. * - A string starting with 'y'
  156. * - A string starting with 'Y'
  157. * - A string starting with 't'
  158. * - A string starting with 'T'
  159. * - A string starting with '1'
  160. *
  161. * A false boolean is found if one of the following is matched:
  162. *
  163. * - A string starting with 'n'
  164. * - A string starting with 'N'
  165. * - A string starting with 'f'
  166. * - A string starting with 'F'
  167. * - A string starting with '0'
  168. *
  169. * The notfound value returned if no boolean is identified, does not
  170. * necessarily have to be 0 or 1.
  171. */
  172. int ciniparser_getboolean(dictionary *d, const char *key, int notfound);
  173. /**
  174. * @brief Set an entry in a dictionary.
  175. * @param ini Dictionary to modify.
  176. * @param entry Entry to modify (entry name)
  177. * @param val New value to associate to the entry.
  178. * @return int 0 if Ok, -1 otherwise.
  179. *
  180. * If the given entry can be found in the dictionary, it is modified to
  181. * contain the provided value. If it cannot be found, -1 is returned.
  182. * It is Ok to set val to NULL.
  183. */
  184. int ciniparser_setstring(dictionary *ini, char *entry, char *val);
  185. /**
  186. * @brief Delete an entry in a dictionary
  187. * @param ini Dictionary to modify
  188. * @param entry Entry to delete (entry name)
  189. * @return void
  190. *
  191. * If the given entry can be found, it is deleted from the dictionary.
  192. */
  193. void ciniparser_unset(dictionary *ini, char *entry);
  194. /**
  195. * @brief Finds out if a given entry exists in a dictionary
  196. * @param ini Dictionary to search
  197. * @param entry Name of the entry to look for
  198. * @return integer 1 if entry exists, 0 otherwise
  199. *
  200. * Finds out if a given entry exists in the dictionary. Since sections
  201. * are stored as keys with NULL associated values, this is the only way
  202. * of querying for the presence of sections in a dictionary.
  203. */
  204. int ciniparser_find_entry(dictionary *ini, char *entry) ;
  205. /**
  206. * @brief Parse an ini file and return an allocated dictionary object
  207. * @param ininame Name of the ini file to read.
  208. * @return Pointer to newly allocated dictionary
  209. *
  210. * This is the parser for ini files. This function is called, providing
  211. * the name of the file to be read. It returns a dictionary object that
  212. * should not be accessed directly, but through accessor functions
  213. * instead.
  214. *
  215. * The returned dictionary must be freed using ciniparser_freedict().
  216. */
  217. dictionary *ciniparser_load(const char *ininame);
  218. /**
  219. * @brief Free all memory associated to an ini dictionary
  220. * @param d Dictionary to free
  221. * @return void
  222. *
  223. * Free all memory associated to an ini dictionary.
  224. * It is mandatory to call this function before the dictionary object
  225. * gets out of the current context.
  226. */
  227. void ciniparser_freedict(dictionary *d);
  228. /**
  229. * @brief Set an item in the dictionary
  230. * @param d Dictionary object created by ciniparser_load()
  231. * @param entry Entry in the dictionary to manipulate
  232. * @param val Value to assign to the entry
  233. * @return 0 on success, -1 on error
  234. *
  235. * Remember that string values are converted by ciniparser_getboolean(),
  236. * ciniparser_getdouble(), etc. It is also OK to set an entry to NULL.
  237. */
  238. int ciniparser_set(dictionary *d, char *entry, char *val);
  239. #endif
  240. /** @}
  241. */