ini.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  175. char *buffer = NULL;
  176. char *saveptr = NULL;
  177. char *line = NULL;
  178. assert(ini);
  179. if (!ini)
  180. return -1;
  181. if (!string)
  182. return 0;
  183. buffer = faux_str_dup(string);
  184. // Now loop though each line
  185. for (line = strtok_r(buffer, "\n", &saveptr);
  186. line; line = strtok_r(NULL, "\n", &saveptr)) {
  187. char *str = NULL;
  188. char *name = NULL;
  189. char *value = NULL;
  190. char *savestr = NULL;
  191. char *ns = line;
  192. const char *begin = NULL;
  193. size_t len = 0;
  194. size_t offset = 0;
  195. size_t quoted = 0;
  196. char *rname = NULL;
  197. char *rvalue = NULL;
  198. if (!*ns) // Empty
  199. continue;
  200. while (*ns && isspace(*ns))
  201. ns++;
  202. if ('#' == *ns) // Comment
  203. continue;
  204. if ('=' == *ns) // Broken string
  205. continue;
  206. str = faux_str_dup(ns);
  207. name = strtok_r(str, "=", &savestr);
  208. if (!name) {
  209. faux_str_free(str);
  210. continue;
  211. }
  212. value = strtok_r(NULL, "=", &savestr);
  213. begin = faux_str_nextword(name, &len, &offset, &quoted);
  214. rname = faux_str_dupn(begin, len);
  215. if (!value) { // Empty value
  216. rvalue = NULL;
  217. } else {
  218. begin = faux_str_nextword(value, &len, &offset, &quoted);
  219. rvalue = faux_str_dupn(begin, len);
  220. }
  221. faux_ini_set(ini, rname, rvalue);
  222. faux_str_free(rname);
  223. faux_str_free(rvalue);
  224. faux_str_free(str);
  225. }
  226. faux_str_free(buffer);
  227. return 0;
  228. }
  229. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  230. int ret = -1;
  231. FILE *f = NULL;
  232. char *buf = NULL;
  233. unsigned int p = 0;
  234. const int chunk_size = 128;
  235. int size = chunk_size;
  236. assert(ini);
  237. assert(fn);
  238. if (!ini)
  239. return -1;
  240. if (!fn || !*fn)
  241. return -1;
  242. f = fopen(fn, "r");
  243. if (!f)
  244. return -1;
  245. buf = faux_zmalloc(size);
  246. while (fgets(buf + p, size - p, f)) {
  247. char *tmp = NULL;
  248. if (feof(f) || strchr(buf + p, '\n') || strchr(buf + p, '\r')) {
  249. faux_ini_parse_str(ini, buf);
  250. p = 0;
  251. continue;
  252. }
  253. p = size - 1;
  254. size += chunk_size;
  255. tmp = realloc(buf, size);
  256. if (!tmp)
  257. goto error;
  258. buf = tmp;
  259. }
  260. ret = 0;
  261. error:
  262. faux_free(buf);
  263. fclose(f);
  264. return ret;
  265. }