1
0

ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. /** Service function to purify (clean out spaces, quotes) word.
  175. *
  176. * The 'word' in this case is a string without prepending or trailing spaces.
  177. * If 'word' is quoted then it can contain spaces within quoting. The qoutes
  178. * itself is not part of the 'word'. If 'word' is not quoted then it can't
  179. * contain spaces, so the end of 'word' is a first space after non-space
  180. * characters. The function searchs for the first occurence of 'word' within
  181. * specified string, allocates memory and returns purified copy of the word.
  182. * The return value must be faux_str_free()-ed later.
  183. *
  184. * Now the unclosed quoting is not an error. Suppose the end of the line can
  185. * close quoting.
  186. *
  187. * @param [in] str String to find word in it.
  188. * @return Purified copy of word or NULL.
  189. */
  190. static char *faux_ini_purify_word(const char *str)
  191. {
  192. const char *word;
  193. const char *string = str;
  194. bool_t quoted = BOOL_FALSE;
  195. size_t len = 0;
  196. assert(str);
  197. if (!str)
  198. return NULL;
  199. // Find the start of a word
  200. while (*string != '\0' && isspace(*string)) {
  201. string++;
  202. }
  203. // Is this the start of a quoted string?
  204. if ('"' == *string) {
  205. quoted = BOOL_TRUE;
  206. string++;
  207. }
  208. word = string; // Begin of purified word
  209. // Find the end of the word
  210. while (*string != '\0') {
  211. if ('\\' == *string) {
  212. string++;
  213. if ('\0' == *string) // Unfinished escaping
  214. break; // Don't increment 'len'
  215. len++;
  216. // Skip escaped char
  217. string++;
  218. len++;
  219. continue;
  220. }
  221. // End of word
  222. if (!quoted && isspace(*string))
  223. break;
  224. if ('"' == *string) {
  225. // End of a quoted string
  226. break;
  227. }
  228. string++;
  229. len++;
  230. }
  231. if (len == 0)
  232. return NULL;
  233. return faux_str_dupn(word, len);
  234. }
  235. /** @brief Parse string for pairs 'name/value'.
  236. *
  237. * String can contain an `name/value` pairs in following format:
  238. * @code
  239. * var1=value1
  240. * var2 = "value 2"
  241. * @endcode
  242. * Function parses that string and stores 'name/value' pairs to
  243. * the INI object.
  244. *
  245. * @param [in] ini Allocated and initialized INI object.
  246. * @param [in] string String to parse.
  247. * @return 0 - succes, < 0 - error
  248. */
  249. int faux_ini_parse_str(faux_ini_t *ini, const char *string) {
  250. char *buffer = NULL;
  251. char *saveptr = NULL;
  252. char *line = NULL;
  253. assert(ini);
  254. if (!ini)
  255. return -1;
  256. if (!string)
  257. return 0;
  258. buffer = faux_str_dup(string);
  259. // Now loop though each line
  260. for (line = strtok_r(buffer, "\n\r", &saveptr);
  261. line; line = strtok_r(NULL, "\n\r", &saveptr)) {
  262. // Now 'line' contain one 'name/value' pair. Single line.
  263. char *str = NULL;
  264. char *name = NULL;
  265. char *value = NULL;
  266. char *savestr = NULL;
  267. char *rname = NULL;
  268. char *rvalue = NULL;
  269. while ((*line != '\0') && isspace(*line)) // Skip spaces
  270. line++;
  271. if ('\0' == *line) // Empty line
  272. continue;
  273. if ('#' == *line) // Comment. Skip it.
  274. continue;
  275. str = faux_str_dup(line);
  276. // Find out name
  277. name = strtok_r(str, "=", &savestr);
  278. if (!name) {
  279. faux_str_free(str);
  280. continue;
  281. }
  282. rname = faux_ini_purify_word(name);
  283. if (!rname) {
  284. faux_str_free(str);
  285. continue;
  286. }
  287. // Find out value
  288. value = strtok_r(NULL, "=", &savestr);
  289. if (!value) { // Empty value
  290. rvalue = NULL;
  291. } else {
  292. rvalue = faux_ini_purify_word(value);
  293. }
  294. faux_ini_set(ini, rname, rvalue);
  295. faux_str_free(rname);
  296. faux_str_free(rvalue);
  297. faux_str_free(str);
  298. }
  299. faux_str_free(buffer);
  300. return 0;
  301. }
  302. /** @brief Parse file for pairs 'name/value'.
  303. *
  304. * File can contain an `name/value` pairs in following format:
  305. * @code
  306. * var1=value1
  307. * var2 = "value 2"
  308. * @endcode
  309. * Function parses file and stores 'name/value' pairs to
  310. * the INI object.
  311. *
  312. * @param [in] ini Allocated and initialized INI object.
  313. * @param [in] string String to parse.
  314. * @return 0 - succes, < 0 - error
  315. * @sa faux_ini_parse_str()
  316. */
  317. int faux_ini_parse_file(faux_ini_t *ini, const char *fn) {
  318. int ret = -1; // Pessimistic retval
  319. FILE *fd = NULL;
  320. char *buf = NULL;
  321. unsigned int bytes_readed = 0;
  322. const int chunk_size = 128;
  323. int size = chunk_size; // Buffer size
  324. assert(ini);
  325. assert(fn);
  326. if (!ini)
  327. return -1;
  328. if (!fn || '\0' == *fn)
  329. return -1;
  330. fd = fopen(fn, "r");
  331. if (!fd)
  332. return -1;
  333. buf = faux_zmalloc(size);
  334. assert(buf);
  335. if (!buf)
  336. goto error;
  337. while (fgets(buf + bytes_readed, size - bytes_readed, fd)) {
  338. // Not enough space in buffer. Make it larger.
  339. if (feof(fd) == 0 &&
  340. !strchr(buf + bytes_readed, '\n') &&
  341. !strchr(buf + bytes_readed, '\r')) {
  342. char *tmp = NULL;
  343. bytes_readed = size - 1; // fgets() put '\0' to last byte
  344. size += chunk_size;
  345. tmp = realloc(buf, size);
  346. if (!tmp) // Memory problems
  347. goto error;
  348. buf = tmp;
  349. continue; // Read the rest of line
  350. }
  351. // Don't analyze retval because it's not obvious what
  352. // to do on error. May be next string will be ok.
  353. faux_ini_parse_str(ini, buf);
  354. bytes_readed = 0;
  355. }
  356. ret = 0;
  357. error:
  358. faux_free(buf);
  359. fclose(fd);
  360. return ret;
  361. }
  362. /** Writes INI file using INI object.
  363. *
  364. * Write pairs 'name/value' to INI file. The source of pairs is an INI object.
  365. * It's complementary operation to faux_ini_parse_file().
  366. *
  367. * @param [in] ini Allocated and initialized INI object.
  368. * @param [in] fn File name to write to.
  369. * @return 0 - success, < 0 - error
  370. */
  371. int faux_ini_write_file(const faux_ini_t *ini, const char *fn) {
  372. FILE *fd = NULL;
  373. faux_ini_node_t *iter = NULL;
  374. const faux_pair_t *pair = NULL;
  375. assert(ini);
  376. assert(fn);
  377. if (!ini)
  378. return -1;
  379. if (!fn || '\0' == *fn)
  380. return -1;
  381. fd = fopen(fn, "w");
  382. if (!fd)
  383. return -1;
  384. iter = faux_ini_init_iter(ini);
  385. while ((pair = faux_ini_each(&iter))) {
  386. char *quote = NULL;
  387. const char *name = faux_pair_name(pair);
  388. const char *value = faux_pair_value(pair);
  389. // Print name field
  390. quote = strchr(name, ' ') ? "" : "\""; // Word with spaces needs quotes
  391. fprintf(fd, "%s%s%s=", quote, name, quote);
  392. // Print value field
  393. quote = strchr(value, ' ') ? "" : "\""; // Word with spaces needs quotes
  394. fprintf(fd, "%s%s%s\n", quote, value, quote);
  395. }
  396. fclose(fd);
  397. return 0;
  398. }