ini.c 11 KB

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