ini.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/file.h"
  13. #include "faux/ini.h"
  14. /** @brief Allocates new INI object.
  15. *
  16. * Before working with INI object it must be allocated and initialized.
  17. *
  18. * @return Allocated and initialized INI object or NULL on error.
  19. */
  20. faux_ini_t *faux_ini_new(void)
  21. {
  22. faux_ini_t *ini;
  23. ini = faux_zmalloc(sizeof(*ini));
  24. if (!ini)
  25. return NULL;
  26. // Init
  27. ini->list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  28. faux_pair_compare, faux_pair_kcompare, faux_pair_free);
  29. return ini;
  30. }
  31. /** @brief Frees the INI object.
  32. *
  33. * After using the INI object must be freed. Function frees INI objecr itself
  34. * and all pairs 'name/value' stored within INI object.
  35. */
  36. void faux_ini_free(faux_ini_t *ini)
  37. {
  38. assert(ini);
  39. if (!ini)
  40. return;
  41. faux_list_free(ini->list);
  42. faux_free(ini);
  43. }
  44. /** @brief Adds pair 'name/value' to INI object.
  45. *
  46. * The 'name' field is a key. The key must be unique. Each key has its
  47. * correspondent value.
  48. *
  49. * If key is new for the INI object then the pair 'name/value' will be added
  50. * to it. If INI object already contain the same key then value of this pair
  51. * will be replaced by newer one. If new specified value is NULL then the
  52. * entry with the correspondent key will be removed from the INI object.
  53. *
  54. * @param [in] ini Allocated and initialized INI object.
  55. * @param [in] name Name field for pair 'name/value'.
  56. * @param [in] value Value field for pair 'name/value'.
  57. * @return
  58. * Newly created pair object.
  59. * NULL if entry was removed (value == NULL)
  60. * NULL on error
  61. */
  62. const faux_pair_t *faux_ini_set(
  63. faux_ini_t *ini, const char *name, const char *value)
  64. {
  65. faux_pair_t *pair = NULL;
  66. faux_list_node_t *node = NULL;
  67. faux_pair_t *found_pair = NULL;
  68. assert(ini);
  69. assert(name);
  70. if (!ini || !name)
  71. return NULL;
  72. // NULL 'value' means: remove entry from list
  73. if (!value) {
  74. node = faux_list_kfind_node(ini->list, name);
  75. if (node)
  76. faux_list_del(ini->list, node);
  77. return NULL;
  78. }
  79. pair = faux_pair_new(name, value);
  80. assert(pair);
  81. if (!pair)
  82. return NULL;
  83. // Try to add new entry or find existent entry with the same 'name'
  84. node = faux_list_add_find(ini->list, pair);
  85. if (!node) { // Something went wrong
  86. faux_pair_free(pair);
  87. return NULL;
  88. }
  89. found_pair = faux_list_data(node);
  90. if (found_pair != pair) { // Item already exists so use existent
  91. faux_pair_free(pair);
  92. faux_pair_set_value(
  93. found_pair, value); // Replace value by new one
  94. return found_pair;
  95. }
  96. // The new entry was added
  97. return pair;
  98. }
  99. /** @brief Removes pair 'name/value' from INI object.
  100. *
  101. * Function search for the pair with specified name within INI object and
  102. * removes it.
  103. *
  104. * @param [in] ini Allocated and initialized INI object.
  105. * @param [in] name Name field to search for the entry.
  106. */
  107. void faux_ini_unset(faux_ini_t *ini, const char *name)
  108. {
  109. faux_ini_set(ini, name, NULL);
  110. }
  111. /** @brief Searches for pair by name.
  112. *
  113. * The name field is a key to search INI object for pair 'name/value'.
  114. *
  115. * @param [in] ini Allocated and initialized INI object.
  116. * @param [in] name Name field to search for.
  117. * @return
  118. * Found pair 'name/value'.
  119. * NULL on errror.
  120. */
  121. const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name)
  122. {
  123. assert(ini);
  124. assert(name);
  125. if (!ini || !name)
  126. return NULL;
  127. return faux_list_kfind(ini->list, name);
  128. }
  129. /** @brief Searches for pair by name and returns correspondent value.
  130. *
  131. * The name field is a key to search INI object for pair 'name/value'.
  132. *
  133. * @param [in] ini Allocated and initialized INI object.
  134. * @param [in] name Name field to search for.
  135. * @return
  136. * Found value field.
  137. * NULL on errror.
  138. */
  139. const char *faux_ini_find(const faux_ini_t *ini, const char *name)
  140. {
  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_iter(const faux_ini_t *ini)
  156. {
  157. assert(ini);
  158. if (!ini)
  159. return NULL;
  160. return (faux_ini_node_t *)faux_list_head(ini->list);
  161. }
  162. /** @brief Iterate entire INI object for pairs 'name/value'.
  163. *
  164. * Before iteration the iterator must be initialized by faux_ini_iter()
  165. * function. Doesn't use faux_ini_each() with uninitialized iterator.
  166. *
  167. * On each call function returns pair 'name/value' and modify iterator.
  168. * Stop iteration when function returns NULL.
  169. *
  170. * @param [in,out] iter Iterator.
  171. * @return Pair 'name/value'.
  172. * @sa faux_ini_iter()
  173. */
  174. const faux_pair_t *faux_ini_each(faux_ini_node_t **iter)
  175. {
  176. return (const faux_pair_t *)faux_list_each((faux_list_node_t **)iter);
  177. }
  178. /** Service function to purify (clean out spaces, quotes) word.
  179. *
  180. * The 'word' in this case is a string without prepending or trailing spaces.
  181. * If 'word' is quoted then it can contain spaces within quoting. The qoutes
  182. * itself is not part of the 'word'. If 'word' is not quoted then it can't
  183. * contain spaces, so the end of 'word' is a first space after non-space
  184. * characters. The function searchs for the first occurence of 'word' within
  185. * specified string, allocates memory and returns purified copy of the word.
  186. * The return value must be faux_str_free()-ed later.
  187. *
  188. * Now the unclosed quoting is not an error. Suppose the end of the line can
  189. * close quoting.
  190. *
  191. * @param [in] str String to find word in it.
  192. * @return Purified copy of word or NULL.
  193. */
  194. static char *faux_ini_purify_word(const char *str)
  195. {
  196. const char *word;
  197. const char *string = str;
  198. bool_t quoted = BOOL_FALSE;
  199. size_t len = 0;
  200. assert(str);
  201. if (!str)
  202. return NULL;
  203. // Find the start of a word
  204. while (*string != '\0' && isspace(*string)) {
  205. string++;
  206. }
  207. // Is this the start of a quoted string?
  208. if ('"' == *string) {
  209. quoted = BOOL_TRUE;
  210. string++;
  211. }
  212. word = string; // Begin of purified word
  213. // Find the end of the word
  214. while (*string != '\0') {
  215. if ('\\' == *string) {
  216. string++;
  217. if ('\0' == *string) // Unfinished escaping
  218. break; // Don't increment 'len'
  219. len++;
  220. // Skip escaped char
  221. string++;
  222. len++;
  223. continue;
  224. }
  225. // End of word
  226. if (!quoted && isspace(*string))
  227. break;
  228. if ('"' == *string) {
  229. // End of a quoted string
  230. break;
  231. }
  232. string++;
  233. len++;
  234. }
  235. if (len == 0)
  236. return NULL;
  237. return faux_str_dupn(word, len);
  238. }
  239. /** @brief Parse string for pairs 'name/value'.
  240. *
  241. * String can contain an `name/value` pairs in following format:
  242. * @code
  243. * var1=value1
  244. * var2 = "value 2"
  245. * @endcode
  246. * Function parses that string and stores 'name/value' pairs to
  247. * the INI object.
  248. *
  249. * @param [in] ini Allocated and initialized INI object.
  250. * @param [in] string String to parse.
  251. * @return 0 - succes, < 0 - error
  252. */
  253. int faux_ini_parse_str(faux_ini_t *ini, const char *string)
  254. {
  255. char *buffer = NULL;
  256. char *saveptr = NULL;
  257. char *line = NULL;
  258. assert(ini);
  259. if (!ini)
  260. return -1;
  261. if (!string)
  262. return 0;
  263. buffer = faux_str_dup(string);
  264. // Now loop though each line
  265. for (line = strtok_r(buffer, "\n\r", &saveptr); line;
  266. line = strtok_r(NULL, "\n\r", &saveptr)) {
  267. // Now 'line' contain one 'name/value' pair. Single line.
  268. char *str = NULL;
  269. char *name = NULL;
  270. char *value = NULL;
  271. char *savestr = NULL;
  272. char *rname = NULL;
  273. char *rvalue = NULL;
  274. while ((*line != '\0') && isspace(*line)) // Skip spaces
  275. line++;
  276. if ('\0' == *line) // Empty line
  277. continue;
  278. if ('#' == *line) // Comment. Skip it.
  279. continue;
  280. str = faux_str_dup(line);
  281. // Find out name
  282. name = strtok_r(str, "=", &savestr);
  283. if (!name) {
  284. faux_str_free(str);
  285. continue;
  286. }
  287. rname = faux_ini_purify_word(name);
  288. if (!rname) {
  289. faux_str_free(str);
  290. continue;
  291. }
  292. // Find out value
  293. value = strtok_r(NULL, "=", &savestr);
  294. if (!value) { // Empty value
  295. rvalue = NULL;
  296. } else {
  297. rvalue = faux_ini_purify_word(value);
  298. }
  299. faux_ini_set(ini, rname, rvalue);
  300. faux_str_free(rname);
  301. faux_str_free(rvalue);
  302. faux_str_free(str);
  303. }
  304. faux_str_free(buffer);
  305. return 0;
  306. }
  307. /** @brief Parse file for pairs 'name/value'.
  308. *
  309. * File can contain an `name/value` pairs in following format:
  310. * @code
  311. * var1=value1
  312. * var2 = "value 2"
  313. * @endcode
  314. * Function parses file and stores 'name/value' pairs to
  315. * the INI object.
  316. *
  317. * @param [in] ini Allocated and initialized INI object.
  318. * @param [in] string String to parse.
  319. * @return 0 - succes, < 0 - error
  320. * @sa faux_ini_parse_str()
  321. */
  322. int faux_ini_parse_file(faux_ini_t *ini, const char *fn)
  323. {
  324. bool_t eof = BOOL_FALSE;
  325. faux_file_t *f = NULL;
  326. char *buf = NULL;
  327. assert(ini);
  328. assert(fn);
  329. if (!ini)
  330. return -1;
  331. if (!fn || '\0' == *fn)
  332. return -1;
  333. f = faux_file_open(fn, O_RDONLY, 0);
  334. if (!f)
  335. return -1;
  336. while ((buf = faux_file_getline(f))) {
  337. // Don't analyze retval because it's not obvious what
  338. // to do on error. May be next string will be ok.
  339. faux_ini_parse_str(ini, buf);
  340. faux_str_free(buf);
  341. }
  342. eof = faux_file_eof(f);
  343. faux_file_close(f);
  344. if (!eof) // File reading was interrupted before EOF
  345. return -1;
  346. return 0;
  347. }
  348. /** Writes INI file using INI object.
  349. *
  350. * Write pairs 'name/value' to INI file. The source of pairs is an INI object.
  351. * It's complementary operation to faux_ini_parse_file().
  352. *
  353. * @param [in] ini Allocated and initialized INI object.
  354. * @param [in] fn File name to write to.
  355. * @return 0 - success, < 0 - error
  356. */
  357. int faux_ini_write_file(const faux_ini_t *ini, const char *fn)
  358. {
  359. faux_file_t *f = NULL;
  360. faux_ini_node_t *iter = NULL;
  361. const faux_pair_t *pair = NULL;
  362. const char *spaces = " \t"; // String with spaces needs quotes
  363. assert(ini);
  364. assert(fn);
  365. if (!ini)
  366. return -1;
  367. if (!fn || '\0' == *fn)
  368. return -1;
  369. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  370. if (!f)
  371. return -1;
  372. iter = faux_ini_iter(ini);
  373. while ((pair = faux_ini_each(&iter))) {
  374. char *quote_name = NULL;
  375. char *quote_value = NULL;
  376. const char *name = faux_pair_name(pair);
  377. const char *value = faux_pair_value(pair);
  378. char *line = NULL;
  379. ssize_t bytes_written = 0;
  380. // Word with spaces needs quotes
  381. quote_name = faux_str_chars(name, spaces) ? "\"" : "";
  382. quote_value = faux_str_chars(value, spaces) ? "\"" : "";
  383. // Prepare INI line
  384. line = faux_str_sprintf("%s%s%s=%s%s%s\n",
  385. quote_name, name, quote_name,
  386. quote_value, value, quote_value);
  387. if (!line) {
  388. faux_file_close(f);
  389. return -1;
  390. }
  391. // Write to file
  392. bytes_written = faux_file_write(f, line, strlen(line));
  393. faux_str_free(line);
  394. if (bytes_written < 0) { // Can't write to file
  395. faux_file_close(f);
  396. return -1;
  397. }
  398. }
  399. faux_file_close(f);
  400. return 0;
  401. }