ini.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /** @file ini.c
  2. * @brief Functions for working with INI files.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include "private.h"
  10. #include "faux/faux.h"
  11. #include "faux/str.h"
  12. #include "faux/ini.h"
  13. /** @brief Allocates new INI object.
  14. *
  15. * Before working with INI object it must be allocated and initialized.
  16. *
  17. * @return Allocated and initialized INI object or NULL on error.
  18. */
  19. faux_ini_t *faux_ini_new(void) {
  20. faux_ini_t *ini;
  21. ini = faux_zmalloc(sizeof(*ini));
  22. if (!ini)
  23. return NULL;
  24. // Init
  25. ini->list = faux_list_new(faux_pair_compare, faux_pair_free);
  26. return ini;
  27. }
  28. /** @brief Frees the INI object.
  29. *
  30. * After using the INI object must be freed. Function frees INI objecr itself
  31. * and all pairs 'name/value' stored within INI object.
  32. */
  33. void faux_ini_free(faux_ini_t *ini) {
  34. assert(ini);
  35. if (!ini)
  36. return;
  37. faux_list_free(ini->list);
  38. faux_free(ini);
  39. }
  40. /** @brief Adds pair 'name/value' to INI object.
  41. *
  42. * The 'name' field is a key. The key must be unique. Each key has its
  43. * correspondent value.
  44. *
  45. * If key is new for the INI object then the pair 'name/value' will be added
  46. * to it. If INI object already contain the same key then value of this pair
  47. * will be replaced by newer one. If new specified value is NULL then the
  48. * entry with the correspondent key will be removed from the INI object.
  49. *
  50. * @param [in] ini Allocated and initialized INI object.
  51. * @param [in] name Name field for pair 'name/value'.
  52. * @param [in] value Value field for pair 'name/value'.
  53. * @return
  54. * Newly created pair object.
  55. * NULL if entry was removed (value == NULL)
  56. * NULL on error
  57. */
  58. const faux_pair_t *faux_ini_set(faux_ini_t *ini, const char *name, const char *value) {
  59. faux_pair_t *pair = NULL;
  60. faux_list_node_t *node = NULL;
  61. faux_pair_t *found_pair = NULL;
  62. assert(ini);
  63. assert(name);
  64. if (!ini || !name)
  65. return NULL;
  66. pair = faux_pair_new(name, value);
  67. assert(pair);
  68. if (!pair)
  69. return NULL;
  70. // NULL 'value' means: remove entry from list
  71. if (!value) {
  72. node = faux_list_find_node(ini->list, faux_pair_compare, pair);
  73. faux_pair_free(pair);
  74. if (node)
  75. faux_list_del(ini->list, node);
  76. return NULL;
  77. }
  78. // Try to add new entry or find existent entry with the same 'name'
  79. node = faux_list_add_find(ini->list, pair);
  80. if (!node) { // Something went wrong
  81. faux_pair_free(pair);
  82. return NULL;
  83. }
  84. found_pair = faux_list_data(node);
  85. if (found_pair != pair) { // Item already exists so use existent
  86. faux_pair_free(pair);
  87. faux_pair_set_value(found_pair, value); // Replace value by new one
  88. return found_pair;
  89. }
  90. // The new entry was added
  91. return pair;
  92. }
  93. /** @brief Removes pair 'name/value' from INI object.
  94. *
  95. * Function search for the pair with specified name within INI object and
  96. * removes it.
  97. *
  98. * @param [in] ini Allocated and initialized INI object.
  99. * @param [in] name Name field to search for the entry.
  100. */
  101. void faux_ini_unset(faux_ini_t *ini, const char *name) {
  102. faux_ini_set(ini, name, NULL);
  103. }
  104. /** @brief Searches for pair by name.
  105. *
  106. * The name field is a key to search INI object for pair 'name/value'.
  107. *
  108. * @param [in] ini Allocated and initialized INI object.
  109. * @param [in] name Name field to search for.
  110. * @return
  111. * Found pair 'name/value'.
  112. * NULL on errror.
  113. */
  114. const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name) {
  115. faux_list_node_t *iter = NULL;
  116. faux_pair_t *pair = NULL;
  117. assert(ini);
  118. assert(name);
  119. if (!ini || !name)
  120. return NULL;
  121. pair = faux_pair_new(name, NULL);
  122. if (!pair)
  123. return NULL;
  124. iter = faux_list_find_node(ini->list, faux_pair_compare, pair);
  125. faux_pair_free(pair);
  126. return faux_list_data(iter);
  127. }
  128. /** @brief Searches for pair by name and returns correspondent value.
  129. *
  130. * The name field is a key to search INI object for pair 'name/value'.
  131. *
  132. * @param [in] ini Allocated and initialized INI object.
  133. * @param [in] name Name field to search for.
  134. * @return
  135. * Found value field.
  136. * NULL on errror.
  137. */
  138. const char *faux_ini_find(const faux_ini_t *ini, const char *name) {
  139. const faux_pair_t *pair = faux_ini_find_pair(ini, name);
  140. if (!pair)
  141. return NULL;
  142. return faux_pair_value(pair);
  143. }
  144. /** @brief Initializes iterator to iterate through the entire INI object.
  145. *
  146. * Before iterating with the faux_ini_each() function the iterator must be
  147. * initialized. This function do it.
  148. *
  149. * @param [in] ini Allocated and initialized INI object.
  150. * @return Initialized iterator.
  151. * @sa faux_ini_each()
  152. */
  153. faux_ini_node_t *faux_ini_init_iter(const faux_ini_t *ini) {
  154. assert(ini);
  155. if (!ini)
  156. return NULL;
  157. return (faux_ini_node_t *)faux_list_head(ini->list);
  158. }
  159. /** @brief Iterate entire INI object for pairs 'name/value'.
  160. *
  161. * Before iteration the iterator must be initialized by faux_ini_init_iter()
  162. * function. Doesn't use faux_ini_each() with uninitialized iterator.
  163. *
  164. * On each call function returns pair 'name/value' and modify iterator.
  165. * Stop iteration when function returns NULL.
  166. *
  167. * @param [in,out] iter Iterator.
  168. * @return Pair 'name/value'.
  169. * @sa faux_ini_init_iter()
  170. */
  171. const faux_pair_t *faux_ini_each(faux_ini_node_t **iter) {
  172. return (const faux_pair_t *)faux_list_each((faux_list_node_t **)iter);
  173. }
  174. /** @brief Parse string for pairs 'name/value'.
  175. *
  176. * String can contain an `name/value` pairs in following format:
  177. * @code
  178. * var1=value1
  179. * var2 = "value 2"
  180. * @endcode
  181. * Function parses that string and stores 'name/value' pairs to
  182. * the INI object.
  183. *
  184. * @param [in] ini Allocated and initialized INI object.
  185. * @param [in] string String to parse.
  186. * @return 0 - succes, < 0 - error
  187. */
  188. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  189. char *buffer = NULL;
  190. char *saveptr = NULL;
  191. char *line = NULL;
  192. assert(ini);
  193. if (!ini)
  194. return -1;
  195. if (!string)
  196. return 0;
  197. buffer = faux_str_dup(string);
  198. // Now loop though each line
  199. for (line = strtok_r(buffer, "\n", &saveptr);
  200. line; line = strtok_r(NULL, "\n", &saveptr)) {
  201. char *str = NULL;
  202. char *name = NULL;
  203. char *value = NULL;
  204. char *savestr = NULL;
  205. char *ns = line;
  206. const char *begin = NULL;
  207. size_t len = 0;
  208. size_t offset = 0;
  209. size_t quoted = 0;
  210. char *rname = NULL;
  211. char *rvalue = NULL;
  212. if (!*ns) // Empty
  213. continue;
  214. while (*ns && isspace(*ns))
  215. ns++;
  216. if ('#' == *ns) // Comment
  217. continue;
  218. if ('=' == *ns) // Broken string
  219. continue;
  220. str = faux_str_dup(ns);
  221. name = strtok_r(str, "=", &savestr);
  222. if (!name) {
  223. faux_str_free(str);
  224. continue;
  225. }
  226. value = strtok_r(NULL, "=", &savestr);
  227. begin = faux_str_nextword(name, &len, &offset, &quoted);
  228. rname = faux_str_dupn(begin, len);
  229. if (!value) { // Empty value
  230. rvalue = NULL;
  231. } else {
  232. begin = faux_str_nextword(value, &len, &offset, &quoted);
  233. rvalue = faux_str_dupn(begin, len);
  234. }
  235. faux_ini_set(ini, rname, rvalue);
  236. faux_str_free(rname);
  237. faux_str_free(rvalue);
  238. faux_str_free(str);
  239. }
  240. faux_str_free(buffer);
  241. return 0;
  242. }
  243. /** @brief Parse file for pairs 'name/value'.
  244. *
  245. * File can contain an `name/value` pairs in following format:
  246. * @code
  247. * var1=value1
  248. * var2 = "value 2"
  249. * @endcode
  250. * Function parses file and stores 'name/value' pairs to
  251. * the INI object.
  252. *
  253. * @param [in] ini Allocated and initialized INI object.
  254. * @param [in] string String to parse.
  255. * @return 0 - succes, < 0 - error
  256. * @sa faux_ini_parse_str()
  257. */
  258. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  259. int ret = -1; // Pessimistic retval
  260. FILE *fd = NULL;
  261. char *buf = NULL;
  262. unsigned int bytes_readed = 0;
  263. const int chunk_size = 128;
  264. int size = chunk_size; // Buffer size
  265. assert(ini);
  266. assert(fn);
  267. if (!ini)
  268. return -1;
  269. if (!fn || '\0' == *fn)
  270. return -1;
  271. fd = fopen(fn, "r");
  272. if (!fd)
  273. return -1;
  274. buf = faux_zmalloc(size);
  275. while (fgets(buf + bytes_readed, size - bytes_readed, fd)) {
  276. // Not enough space in buffer. Make it larger.
  277. if (feof(fd) == 0 &&
  278. !strchr(buf + bytes_readed, '\n') &&
  279. !strchr(buf + bytes_readed, '\r')) {
  280. char *tmp = NULL;
  281. bytes_readed = size - 1; // fgets() put '\0' to last byte
  282. size += chunk_size;
  283. tmp = realloc(buf, size);
  284. if (!tmp) // Memory problems
  285. goto error;
  286. buf = tmp;
  287. continue; // Read the rest of line
  288. }
  289. // Don't analyze retval because it's not obvious what
  290. // to do on error. May be next string will be ok.
  291. faux_ini_parse_str(ini, buf);
  292. bytes_readed = 0;
  293. }
  294. ret = 0;
  295. error:
  296. faux_free(buf);
  297. fclose(fd);
  298. return ret;
  299. }