ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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(
  59. faux_ini_t *ini, const char *name, const char *value) {
  60. faux_pair_t *pair = NULL;
  61. faux_list_node_t *node = NULL;
  62. faux_pair_t *found_pair = NULL;
  63. assert(ini);
  64. assert(name);
  65. if (!ini || !name)
  66. return NULL;
  67. pair = faux_pair_new(name, value);
  68. assert(pair);
  69. if (!pair)
  70. return NULL;
  71. // NULL 'value' means: remove entry from list
  72. if (!value) {
  73. node = faux_list_find_node(ini->list, faux_pair_compare, pair);
  74. faux_pair_free(pair);
  75. if (node)
  76. faux_list_del(ini->list, node);
  77. return NULL;
  78. }
  79. // Try to add new entry or find existent entry with the same 'name'
  80. node = faux_list_add_find(ini->list, pair);
  81. if (!node) { // Something went wrong
  82. faux_pair_free(pair);
  83. return NULL;
  84. }
  85. found_pair = faux_list_data(node);
  86. if (found_pair != pair) { // Item already exists so use existent
  87. faux_pair_free(pair);
  88. faux_pair_set_value(
  89. found_pair, value); // Replace value by new one
  90. return found_pair;
  91. }
  92. // The new entry was added
  93. return pair;
  94. }
  95. /** @brief Removes pair 'name/value' from INI object.
  96. *
  97. * Function search for the pair with specified name within INI object and
  98. * removes it.
  99. *
  100. * @param [in] ini Allocated and initialized INI object.
  101. * @param [in] name Name field to search for the entry.
  102. */
  103. void faux_ini_unset(faux_ini_t *ini, const char *name) {
  104. faux_ini_set(ini, name, NULL);
  105. }
  106. /** @brief Searches for pair by name.
  107. *
  108. * The name field is a key to search INI object for pair 'name/value'.
  109. *
  110. * @param [in] ini Allocated and initialized INI object.
  111. * @param [in] name Name field to search for.
  112. * @return
  113. * Found pair 'name/value'.
  114. * NULL on errror.
  115. */
  116. const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name) {
  117. faux_list_node_t *iter = NULL;
  118. faux_pair_t *pair = NULL;
  119. assert(ini);
  120. assert(name);
  121. if (!ini || !name)
  122. return NULL;
  123. pair = faux_pair_new(name, NULL);
  124. if (!pair)
  125. return NULL;
  126. iter = faux_list_find_node(ini->list, faux_pair_compare, pair);
  127. faux_pair_free(pair);
  128. return faux_list_data(iter);
  129. }
  130. /** @brief Searches for pair by name and returns correspondent value.
  131. *
  132. * The name field is a key to search INI object for pair 'name/value'.
  133. *
  134. * @param [in] ini Allocated and initialized INI object.
  135. * @param [in] name Name field to search for.
  136. * @return
  137. * Found value field.
  138. * NULL on errror.
  139. */
  140. const char *faux_ini_find(const faux_ini_t *ini, const char *name) {
  141. const faux_pair_t *pair = faux_ini_find_pair(ini, name);
  142. if (!pair)
  143. return NULL;
  144. return faux_pair_value(pair);
  145. }
  146. /** @brief Initializes iterator to iterate through the entire INI object.
  147. *
  148. * Before iterating with the faux_ini_each() function the iterator must be
  149. * initialized. This function do it.
  150. *
  151. * @param [in] ini Allocated and initialized INI object.
  152. * @return Initialized iterator.
  153. * @sa faux_ini_each()
  154. */
  155. faux_ini_node_t *faux_ini_init_iter(const faux_ini_t *ini) {
  156. assert(ini);
  157. if (!ini)
  158. return NULL;
  159. return (faux_ini_node_t *)faux_list_head(ini->list);
  160. }
  161. /** @brief Iterate entire INI object for pairs 'name/value'.
  162. *
  163. * Before iteration the iterator must be initialized by faux_ini_init_iter()
  164. * function. Doesn't use faux_ini_each() with uninitialized iterator.
  165. *
  166. * On each call function returns pair 'name/value' and modify iterator.
  167. * Stop iteration when function returns NULL.
  168. *
  169. * @param [in,out] iter Iterator.
  170. * @return Pair 'name/value'.
  171. * @sa faux_ini_init_iter()
  172. */
  173. const faux_pair_t *faux_ini_each(faux_ini_node_t **iter) {
  174. return (const faux_pair_t *)faux_list_each((faux_list_node_t **)iter);
  175. }
  176. /** Service function to purify (clean out spaces, quotes) word.
  177. *
  178. * The 'word' in this case is a string without prepending or trailing spaces.
  179. * If 'word' is quoted then it can contain spaces within quoting. The qoutes
  180. * itself is not part of the 'word'. If 'word' is not quoted then it can't
  181. * contain spaces, so the end of 'word' is a first space after non-space
  182. * characters. The function searchs for the first occurence of 'word' within
  183. * specified string, allocates memory and returns purified copy of the word.
  184. * The return value must be faux_str_free()-ed later.
  185. *
  186. * Now the unclosed quoting is not an error. Suppose the end of the line can
  187. * close quoting.
  188. *
  189. * @param [in] str String to find word in it.
  190. * @return Purified copy of word or NULL.
  191. */
  192. static char *faux_ini_purify_word(const char *str) {
  193. const char *word;
  194. const char *string = str;
  195. bool_t quoted = BOOL_FALSE;
  196. size_t len = 0;
  197. assert(str);
  198. if (!str)
  199. return NULL;
  200. // Find the start of a word
  201. while (*string != '\0' && isspace(*string)) {
  202. string++;
  203. }
  204. // Is this the start of a quoted string?
  205. if ('"' == *string) {
  206. quoted = BOOL_TRUE;
  207. string++;
  208. }
  209. word = string; // Begin of purified word
  210. // Find the end of the word
  211. while (*string != '\0') {
  212. if ('\\' == *string) {
  213. string++;
  214. if ('\0' == *string) // Unfinished escaping
  215. break; // Don't increment 'len'
  216. len++;
  217. // Skip escaped char
  218. string++;
  219. len++;
  220. continue;
  221. }
  222. // End of word
  223. if (!quoted && isspace(*string))
  224. break;
  225. if ('"' == *string) {
  226. // End of a quoted string
  227. break;
  228. }
  229. string++;
  230. len++;
  231. }
  232. if (len == 0)
  233. return NULL;
  234. return faux_str_dupn(word, len);
  235. }
  236. /** @brief Parse string for pairs 'name/value'.
  237. *
  238. * String can contain an `name/value` pairs in following format:
  239. * @code
  240. * var1=value1
  241. * var2 = "value 2"
  242. * @endcode
  243. * Function parses that string and stores 'name/value' pairs to
  244. * the INI object.
  245. *
  246. * @param [in] ini Allocated and initialized INI object.
  247. * @param [in] string String to parse.
  248. * @return 0 - succes, < 0 - error
  249. */
  250. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  251. char *buffer = NULL;
  252. char *saveptr = NULL;
  253. char *line = NULL;
  254. assert(ini);
  255. if (!ini)
  256. return -1;
  257. if (!string)
  258. return 0;
  259. buffer = faux_str_dup(string);
  260. // Now loop though each line
  261. for (line = strtok_r(buffer, "\n\r", &saveptr); line;
  262. line = strtok_r(NULL, "\n\r", &saveptr)) {
  263. // Now 'line' contain one 'name/value' pair. Single line.
  264. char *str = NULL;
  265. char *name = NULL;
  266. char *value = NULL;
  267. char *savestr = NULL;
  268. char *rname = NULL;
  269. char *rvalue = NULL;
  270. while ((*line != '\0') && isspace(*line)) // Skip spaces
  271. line++;
  272. if ('\0' == *line) // Empty line
  273. continue;
  274. if ('#' == *line) // Comment. Skip it.
  275. continue;
  276. str = faux_str_dup(line);
  277. // Find out name
  278. name = strtok_r(str, "=", &savestr);
  279. if (!name) {
  280. faux_str_free(str);
  281. continue;
  282. }
  283. rname = faux_ini_purify_word(name);
  284. if (!rname) {
  285. faux_str_free(str);
  286. continue;
  287. }
  288. // Find out value
  289. value = strtok_r(NULL, "=", &savestr);
  290. if (!value) { // Empty value
  291. rvalue = NULL;
  292. } else {
  293. rvalue = faux_ini_purify_word(value);
  294. }
  295. faux_ini_set(ini, rname, rvalue);
  296. faux_str_free(rname);
  297. faux_str_free(rvalue);
  298. faux_str_free(str);
  299. }
  300. faux_str_free(buffer);
  301. return 0;
  302. }
  303. /** @brief Parse file for pairs 'name/value'.
  304. *
  305. * File can contain an `name/value` pairs in following format:
  306. * @code
  307. * var1=value1
  308. * var2 = "value 2"
  309. * @endcode
  310. * Function parses file and stores 'name/value' pairs to
  311. * the INI object.
  312. *
  313. * @param [in] ini Allocated and initialized INI object.
  314. * @param [in] string String to parse.
  315. * @return 0 - succes, < 0 - error
  316. * @sa faux_ini_parse_str()
  317. */
  318. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  319. int ret = -1; // Pessimistic retval
  320. FILE *fd = NULL;
  321. char *buf = NULL;
  322. unsigned int bytes_readed = 0;
  323. const int chunk_size = 128;
  324. int size = chunk_size; // Buffer size
  325. assert(ini);
  326. assert(fn);
  327. if (!ini)
  328. return -1;
  329. if (!fn || '\0' == *fn)
  330. return -1;
  331. fd = fopen(fn, "r");
  332. if (!fd)
  333. return -1;
  334. buf = faux_zmalloc(size);
  335. assert(buf);
  336. if (!buf)
  337. goto error;
  338. while (fgets(buf + bytes_readed, size - bytes_readed, fd)) {
  339. // Not enough space in buffer. Make it larger.
  340. if (feof(fd) == 0 && !strchr(buf + bytes_readed, '\n') &&
  341. !strchr(buf + bytes_readed, '\r')) {
  342. char *tmp = NULL;
  343. bytes_readed =
  344. size - 1; // fgets() put '\0' to last byte
  345. size += chunk_size;
  346. tmp = realloc(buf, size);
  347. if (!tmp) // Memory problems
  348. goto error;
  349. buf = tmp;
  350. continue; // Read the rest of line
  351. }
  352. // Don't analyze retval because it's not obvious what
  353. // to do on error. May be next string will be ok.
  354. faux_ini_parse_str(ini, buf);
  355. bytes_readed = 0;
  356. }
  357. ret = 0;
  358. error:
  359. faux_free(buf);
  360. fclose(fd);
  361. return ret;
  362. }
  363. /** Writes INI file using INI object.
  364. *
  365. * Write pairs 'name/value' to INI file. The source of pairs is an INI object.
  366. * It's complementary operation to faux_ini_parse_file().
  367. *
  368. * @param [in] ini Allocated and initialized INI object.
  369. * @param [in] fn File name to write to.
  370. * @return 0 - success, < 0 - error
  371. */
  372. int faux_ini_write_file(const faux_ini_t *ini, const char *fn) {
  373. FILE *fd = NULL;
  374. faux_ini_node_t *iter = NULL;
  375. const faux_pair_t *pair = NULL;
  376. assert(ini);
  377. assert(fn);
  378. if (!ini)
  379. return -1;
  380. if (!fn || '\0' == *fn)
  381. return -1;
  382. fd = fopen(fn, "w");
  383. if (!fd)
  384. return -1;
  385. iter = faux_ini_init_iter(ini);
  386. while ((pair = faux_ini_each(&iter))) {
  387. char *quote = NULL;
  388. const char *name = faux_pair_name(pair);
  389. const char *value = faux_pair_value(pair);
  390. // Print name field
  391. // Word with spaces needs quotes
  392. quote = strchr(name, ' ') ? "" : "\"";
  393. fprintf(fd, "%s%s%s=", quote, name, quote);
  394. // Print value field
  395. // Word with spaces needs quotes
  396. quote = strchr(value, ' ') ? "" : "\"";
  397. fprintf(fd, "%s%s%s\n", quote, value, quote);
  398. }
  399. fclose(fd);
  400. return 0;
  401. }