ini.c 11 KB

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