ini.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. faux_ini_t *faux_ini_new(void) {
  14. faux_ini_t *ini;
  15. ini = faux_zmalloc(sizeof(*ini));
  16. if (!ini)
  17. return NULL;
  18. // Init
  19. ini->list = faux_list_new(faux_pair_compare, faux_pair_free);
  20. return ini;
  21. }
  22. void faux_ini_free(faux_ini_t *ini) {
  23. assert(ini);
  24. if (!ini)
  25. return;
  26. faux_list_free(ini->list);
  27. faux_free(ini);
  28. }
  29. static int faux_ini_del(faux_ini_t *ini, faux_ini_node_t *node) {
  30. assert(ini);
  31. assert(node);
  32. if (!ini || !node)
  33. return -1;
  34. return faux_list_del(ini->list, (faux_list_node_t *)node);
  35. }
  36. faux_pair_t *faux_ini_set(faux_ini_t *ini, const char *name, const char *value) {
  37. faux_pair_t *pair = NULL;
  38. faux_list_node_t *node = NULL;
  39. faux_pair_t *found_pair = NULL;
  40. assert(ini);
  41. assert(name);
  42. if (!ini || !name)
  43. return NULL;
  44. pair = faux_pair_new(name, value);
  45. assert(pair);
  46. if (!pair)
  47. return NULL;
  48. // NULL 'value' means: remove entry from list
  49. if (!value) {
  50. node = faux_list_find_node(ini->list, faux_pair_compare, pair);
  51. faux_pair_free(pair);
  52. if (node)
  53. faux_list_del(ini->list, node);
  54. return NULL;
  55. }
  56. // Try to add new entry or find existent entry with the same 'name'
  57. node = faux_list_add_find(ini->list, pair);
  58. if (!node) { // Something went wrong
  59. faux_pair_free(pair);
  60. return NULL;
  61. }
  62. found_pair = faux_list_data(node);
  63. if (found_pair != pair) { // Item already exists so use existent
  64. faux_pair_free(pair);
  65. faux_pair_set_value(found_pair, value); // Replace value by new one
  66. return found_pair;
  67. }
  68. // The new entry was added
  69. return pair;
  70. }
  71. void faux_ini_unset(faux_ini_t *ini, const char *name) {
  72. faux_ini_set(ini, name, NULL);
  73. }
  74. /* Find pair by name */
  75. faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name) {
  76. faux_list_node_t *iter = NULL;
  77. faux_pair_t *pair = NULL;
  78. assert(ini);
  79. assert(name);
  80. if (!ini || !name)
  81. return NULL;
  82. pair = faux_pair_new(name, NULL);
  83. if (!pair)
  84. return NULL;
  85. iter = faux_list_find_node(ini->list, faux_pair_compare, pair);
  86. faux_pair_free(pair);
  87. return faux_list_data(iter);
  88. }
  89. /* Find value by name */
  90. const char *faux_ini_find(const faux_ini_t *ini, const char *name) {
  91. faux_pair_t *pair = faux_ini_find_pair(ini, name);
  92. if (!pair)
  93. return NULL;
  94. return faux_pair_value(pair);
  95. }
  96. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  97. char *buffer = NULL;
  98. char *saveptr = NULL;
  99. char *line = NULL;
  100. assert(ini);
  101. if (!ini)
  102. return -1;
  103. if (!string)
  104. return 0;
  105. buffer = faux_str_dup(string);
  106. // Now loop though each line
  107. for (line = strtok_r(buffer, "\n", &saveptr);
  108. line; line = strtok_r(NULL, "\n", &saveptr)) {
  109. char *str = NULL;
  110. char *name = NULL;
  111. char *value = NULL;
  112. char *savestr = NULL;
  113. char *ns = line;
  114. const char *begin = NULL;
  115. size_t len = 0;
  116. size_t offset = 0;
  117. size_t quoted = 0;
  118. char *rname = NULL;
  119. char *rvalue = NULL;
  120. if (!*ns) // Empty
  121. continue;
  122. while (*ns && isspace(*ns))
  123. ns++;
  124. if ('#' == *ns) // Comment
  125. continue;
  126. if ('=' == *ns) // Broken string
  127. continue;
  128. str = faux_str_dup(ns);
  129. name = strtok_r(str, "=", &savestr);
  130. if (!name) {
  131. faux_str_free(str);
  132. continue;
  133. }
  134. value = strtok_r(NULL, "=", &savestr);
  135. begin = faux_str_nextword(name, &len, &offset, &quoted);
  136. rname = faux_str_dupn(begin, len);
  137. if (!value) { // Empty value
  138. rvalue = NULL;
  139. } else {
  140. begin = faux_str_nextword(value, &len, &offset, &quoted);
  141. rvalue = faux_str_dupn(begin, len);
  142. }
  143. faux_ini_set(ini, rname, rvalue);
  144. faux_str_free(rname);
  145. faux_str_free(rvalue);
  146. faux_str_free(str);
  147. }
  148. faux_str_free(buffer);
  149. return 0;
  150. }
  151. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  152. int ret = -1;
  153. FILE *f = NULL;
  154. char *buf = NULL;
  155. unsigned int p = 0;
  156. const int chunk_size = 128;
  157. int size = chunk_size;
  158. assert(ini);
  159. assert(fn);
  160. if (!ini)
  161. return -1;
  162. if (!fn || !*fn)
  163. return -1;
  164. f = fopen(fn, "r");
  165. if (!f)
  166. return -1;
  167. buf = faux_zmalloc(size);
  168. while (fgets(buf + p, size - p, f)) {
  169. char *tmp = NULL;
  170. if (feof(f) || strchr(buf + p, '\n') || strchr(buf + p, '\r')) {
  171. faux_ini_parse_str(ini, buf);
  172. p = 0;
  173. continue;
  174. }
  175. p = size - 1;
  176. size += chunk_size;
  177. tmp = realloc(buf, size);
  178. if (!tmp)
  179. goto error;
  180. buf = tmp;
  181. }
  182. ret = 0;
  183. error:
  184. faux_free(buf);
  185. fclose(f);
  186. return ret;
  187. }
  188. faux_ini_node_t *faux_ini_head(const faux_ini_t *ini) {
  189. assert(ini);
  190. if (!ini)
  191. return NULL;
  192. return faux_list_head(ini->list);
  193. }
  194. faux_ini_node_t *faux_ini_tail(const faux_ini_t *ini) {
  195. assert(ini);
  196. if (!ini)
  197. return NULL;
  198. return faux_list_tail(ini->list);
  199. }
  200. faux_ini_node_t *faux_ini_next(const faux_ini_node_t *node) {
  201. assert(node);
  202. if (!node)
  203. return NULL;
  204. return faux_list_next_node(node);
  205. }
  206. faux_ini_node_t *faux_ini_prev(const faux_ini_node_t *node) {
  207. assert(node);
  208. if (!node)
  209. return NULL;
  210. return faux_list_prev_node(node);
  211. }
  212. faux_pair_t *faux_ini_data(const faux_ini_node_t *node) {
  213. assert(node);
  214. if (!node)
  215. return NULL;
  216. return (faux_pair_t *)faux_list_data(node);
  217. }