ini.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. static char *faux_ini_purify_word(const char *str)
  175. {
  176. const char *word;
  177. const char *string = str;
  178. bool_t quoted = BOOL_FALSE;
  179. size_t len = 0;
  180. assert(str);
  181. if (!str)
  182. return NULL;
  183. // Find the start of a word
  184. while (*string != '\0' && isspace(*string)) {
  185. string++;
  186. }
  187. // Is this the start of a quoted string?
  188. if ('"' == *string) {
  189. quoted = BOOL_TRUE;
  190. string++;
  191. }
  192. word = string; // Begin of purified word
  193. // Find the end of the word
  194. while (*string != '\0') {
  195. if ('\\' == *string) {
  196. string++;
  197. if ('\0' == *string) // Unfinished escaping
  198. break; // Don't increment 'len'
  199. len++;
  200. // Skip escaped char
  201. string++;
  202. len++;
  203. continue;
  204. }
  205. // End of word
  206. if (!quoted && isspace(*string))
  207. break;
  208. if ('"' == *string) {
  209. // End of a quoted string
  210. break;
  211. }
  212. string++;
  213. len++;
  214. }
  215. if (len == 0)
  216. return NULL;
  217. return faux_str_dupn(word, len);
  218. }
  219. /** @brief Parse string for pairs 'name/value'.
  220. *
  221. * String can contain an `name/value` pairs in following format:
  222. * @code
  223. * var1=value1
  224. * var2 = "value 2"
  225. * @endcode
  226. * Function parses that string and stores 'name/value' pairs to
  227. * the INI object.
  228. *
  229. * @param [in] ini Allocated and initialized INI object.
  230. * @param [in] string String to parse.
  231. * @return 0 - succes, < 0 - error
  232. */
  233. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  234. char *buffer = NULL;
  235. char *saveptr = NULL;
  236. char *line = NULL;
  237. assert(ini);
  238. if (!ini)
  239. return -1;
  240. if (!string)
  241. return 0;
  242. buffer = faux_str_dup(string);
  243. // Now loop though each line
  244. for (line = strtok_r(buffer, "\n\r", &saveptr);
  245. line; line = strtok_r(NULL, "\n\r", &saveptr)) {
  246. // Now 'line' contain one 'name/value' pair. Single line.
  247. char *str = NULL;
  248. char *name = NULL;
  249. char *value = NULL;
  250. char *savestr = NULL;
  251. char *rname = NULL;
  252. char *rvalue = NULL;
  253. while ((*line != '\0') && isspace(*line)) // Skip spaces
  254. line++;
  255. if ('\0' == *line) // Empty line
  256. continue;
  257. if ('#' == *line) // Comment. Skip it.
  258. continue;
  259. str = faux_str_dup(line);
  260. // Find out name
  261. name = strtok_r(str, "=", &savestr);
  262. if (!name) {
  263. faux_str_free(str);
  264. continue;
  265. }
  266. rname = faux_ini_purify_word(name);
  267. if (!rname) {
  268. faux_str_free(str);
  269. continue;
  270. }
  271. // Find out value
  272. value = strtok_r(NULL, "=", &savestr);
  273. if (!value) { // Empty value
  274. rvalue = NULL;
  275. } else {
  276. rvalue = faux_ini_purify_word(value);
  277. }
  278. faux_ini_set(ini, rname, rvalue);
  279. faux_str_free(rname);
  280. faux_str_free(rvalue);
  281. faux_str_free(str);
  282. }
  283. faux_str_free(buffer);
  284. return 0;
  285. }
  286. /** @brief Parse file for pairs 'name/value'.
  287. *
  288. * File can contain an `name/value` pairs in following format:
  289. * @code
  290. * var1=value1
  291. * var2 = "value 2"
  292. * @endcode
  293. * Function parses file and stores 'name/value' pairs to
  294. * the INI object.
  295. *
  296. * @param [in] ini Allocated and initialized INI object.
  297. * @param [in] string String to parse.
  298. * @return 0 - succes, < 0 - error
  299. * @sa faux_ini_parse_str()
  300. */
  301. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  302. int ret = -1; // Pessimistic retval
  303. FILE *fd = NULL;
  304. char *buf = NULL;
  305. unsigned int bytes_readed = 0;
  306. const int chunk_size = 128;
  307. int size = chunk_size; // Buffer size
  308. assert(ini);
  309. assert(fn);
  310. if (!ini)
  311. return -1;
  312. if (!fn || '\0' == *fn)
  313. return -1;
  314. fd = fopen(fn, "r");
  315. if (!fd)
  316. return -1;
  317. buf = faux_zmalloc(size);
  318. while (fgets(buf + bytes_readed, size - bytes_readed, fd)) {
  319. // Not enough space in buffer. Make it larger.
  320. if (feof(fd) == 0 &&
  321. !strchr(buf + bytes_readed, '\n') &&
  322. !strchr(buf + bytes_readed, '\r')) {
  323. char *tmp = NULL;
  324. bytes_readed = size - 1; // fgets() put '\0' to last byte
  325. size += chunk_size;
  326. tmp = realloc(buf, size);
  327. if (!tmp) // Memory problems
  328. goto error;
  329. buf = tmp;
  330. continue; // Read the rest of line
  331. }
  332. // Don't analyze retval because it's not obvious what
  333. // to do on error. May be next string will be ok.
  334. faux_ini_parse_str(ini, buf);
  335. bytes_readed = 0;
  336. }
  337. ret = 0;
  338. error:
  339. faux_free(buf);
  340. fclose(fd);
  341. return ret;
  342. }