ini.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 = NULL;
  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 object itself
  34. * and all pairs 'name/value' stored within INI object.
  35. */
  36. void faux_ini_free(faux_ini_t *ini)
  37. {
  38. if (!ini)
  39. return;
  40. faux_list_free(ini->list);
  41. faux_free(ini);
  42. }
  43. /** @brief Adds pair 'name/value' to INI object.
  44. *
  45. * The 'name' field is a key. The key must be unique. Each key has its
  46. * correspondent value.
  47. *
  48. * If key is new for the INI object then the pair 'name/value' will be added
  49. * to it. If INI object already contain the same key then value of this pair
  50. * will be replaced by newer one. If new specified value is NULL then the
  51. * entry with the correspondent key will be removed from the INI object.
  52. *
  53. * @param [in] ini Allocated and initialized INI object.
  54. * @param [in] name Name field for pair 'name/value'.
  55. * @param [in] value Value field for pair 'name/value'.
  56. * @return
  57. * Newly created pair object.
  58. * NULL if entry was removed (value == NULL)
  59. * NULL on error
  60. */
  61. const faux_pair_t *faux_ini_set(
  62. faux_ini_t *ini, const char *name, const char *value)
  63. {
  64. faux_pair_t *pair = NULL;
  65. faux_list_node_t *node = NULL;
  66. faux_pair_t *found_pair = NULL;
  67. assert(ini);
  68. assert(name);
  69. if (!ini || !name)
  70. return NULL;
  71. // NULL 'value' means: remove entry from list
  72. if (!value) {
  73. node = faux_list_kfind_node(ini->list, name);
  74. if (node)
  75. faux_list_del(ini->list, node);
  76. return NULL;
  77. }
  78. pair = faux_pair_new(name, value);
  79. assert(pair);
  80. if (!pair)
  81. return NULL;
  82. // Try to add new entry or find existent entry with the same 'name'
  83. node = faux_list_add_find(ini->list, pair);
  84. if (!node) { // Something went wrong
  85. faux_pair_free(pair);
  86. return NULL;
  87. }
  88. found_pair = faux_list_data(node);
  89. if (found_pair != pair) { // Item already exists so use existent
  90. faux_pair_free(pair);
  91. faux_pair_set_value(
  92. found_pair, value); // Replace value by new one
  93. return found_pair;
  94. }
  95. // The new entry was added
  96. return pair;
  97. }
  98. /** @brief Removes pair 'name/value' from INI object.
  99. *
  100. * Function search for the pair with specified name within INI object and
  101. * removes it.
  102. *
  103. * @param [in] ini Allocated and initialized INI object.
  104. * @param [in] name Name field to search for the entry.
  105. */
  106. void faux_ini_unset(faux_ini_t *ini, const char *name)
  107. {
  108. faux_ini_set(ini, name, NULL);
  109. }
  110. /** @brief Searches for pair by name.
  111. *
  112. * The name field is a key to search INI object for pair 'name/value'.
  113. *
  114. * @param [in] ini Allocated and initialized INI object.
  115. * @param [in] name Name field to search for.
  116. * @return
  117. * Found pair 'name/value'.
  118. * NULL on errror.
  119. */
  120. const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name)
  121. {
  122. assert(ini);
  123. assert(name);
  124. if (!ini || !name)
  125. return NULL;
  126. return faux_list_kfind(ini->list, name);
  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. {
  140. const faux_pair_t *pair = faux_ini_find_pair(ini, name);
  141. if (!pair)
  142. return NULL;
  143. return faux_pair_value(pair);
  144. }
  145. /** @brief Initializes iterator to iterate through the entire INI object.
  146. *
  147. * Before iterating with the faux_ini_each() function the iterator must be
  148. * initialized. This function do it.
  149. *
  150. * @param [in] ini Allocated and initialized INI object.
  151. * @return Initialized iterator.
  152. * @sa faux_ini_each()
  153. */
  154. faux_ini_node_t *faux_ini_iter(const faux_ini_t *ini)
  155. {
  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_iter()
  164. * function. Doesn't use faux_ini_each() with uninitialized iterator.
  165. *
  166. * On each call function returns pair 'name/value' and modifies iterator.
  167. * Stop iteration when function returns NULL.
  168. *
  169. * @param [in,out] iter Iterator.
  170. * @return Pair 'name/value'.
  171. * @sa faux_ini_iter()
  172. */
  173. const faux_pair_t *faux_ini_each(faux_ini_node_t **iter)
  174. {
  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. {
  195. const char *word = NULL;
  196. const char *string = str;
  197. bool_t quoted = BOOL_FALSE;
  198. size_t len = 0;
  199. assert(str);
  200. if (!str)
  201. return NULL;
  202. // Find the start of a word
  203. while (*string != '\0' && isspace(*string)) {
  204. string++;
  205. }
  206. // Is this the start of a quoted string?
  207. if ('"' == *string) {
  208. quoted = BOOL_TRUE;
  209. string++;
  210. }
  211. word = string; // Begin of purified word
  212. // Find the end of the word
  213. while (*string != '\0') {
  214. if ('\\' == *string) {
  215. string++;
  216. if ('\0' == *string) // Unfinished escaping
  217. break; // Don't increment 'len'
  218. len++;
  219. // Skip escaped char
  220. string++;
  221. len++;
  222. continue;
  223. }
  224. // End of word
  225. if (!quoted && isspace(*string))
  226. break;
  227. if ('"' == *string) {
  228. // End of a quoted string
  229. break;
  230. }
  231. string++;
  232. len++;
  233. }
  234. if (len == 0)
  235. return NULL;
  236. return faux_str_dupn(word, len);
  237. }
  238. /** @brief Parse string for pairs 'name/value'.
  239. *
  240. * String can contain an `name/value` pairs in following format:
  241. * @code
  242. * var1=value1
  243. * var2 = "value 2"
  244. * @endcode
  245. * Function parses that string and stores 'name/value' pairs to
  246. * the INI object.
  247. *
  248. * @param [in] ini Allocated and initialized INI object.
  249. * @param [in] string String to parse.
  250. * @return BOOL_TRUE - succes, BOOL_FALSE - error
  251. */
  252. bool_t faux_ini_parse_str(faux_ini_t *ini, const char *string)
  253. {
  254. char *buffer = NULL;
  255. char *saveptr = NULL;
  256. char *line = NULL;
  257. assert(ini);
  258. if (!ini)
  259. return BOOL_FALSE;
  260. if (!string)
  261. return BOOL_TRUE;
  262. buffer = faux_str_dup(string);
  263. // Now loop though each line
  264. for (line = strtok_r(buffer, "\n\r", &saveptr); line;
  265. line = strtok_r(NULL, "\n\r", &saveptr)) {
  266. // Now 'line' contain one 'name/value' pair. Single line.
  267. char *str = NULL;
  268. char *name = NULL;
  269. char *value = NULL;
  270. char *savestr = NULL;
  271. char *rname = NULL;
  272. char *rvalue = NULL;
  273. while ((*line != '\0') && isspace(*line)) // Skip spaces
  274. line++;
  275. if ('\0' == *line) // Empty line
  276. continue;
  277. if ('#' == *line) // Comment. Skip it.
  278. continue;
  279. str = faux_str_dup(line);
  280. // Find out name
  281. name = strtok_r(str, "=", &savestr);
  282. if (!name) {
  283. faux_str_free(str);
  284. continue;
  285. }
  286. rname = faux_ini_purify_word(name);
  287. if (!rname) {
  288. faux_str_free(str);
  289. continue;
  290. }
  291. // Find out value
  292. value = strtok_r(NULL, "=", &savestr);
  293. if (!value) { // Empty value
  294. rvalue = NULL;
  295. } else {
  296. rvalue = faux_ini_purify_word(value);
  297. }
  298. faux_ini_set(ini, rname, rvalue);
  299. faux_str_free(rname);
  300. faux_str_free(rvalue);
  301. faux_str_free(str);
  302. }
  303. faux_str_free(buffer);
  304. return BOOL_TRUE;
  305. }
  306. /** @brief Parse file for pairs 'name/value'.
  307. *
  308. * File can contain an `name/value` pairs in following format:
  309. * @code
  310. * var1=value1
  311. * var2 = "value 2"
  312. * @endcode
  313. * Function parses file and stores 'name/value' pairs to
  314. * the INI object.
  315. *
  316. * @param [in] ini Allocated and initialized INI object.
  317. * @param [in] string String to parse.
  318. * @return BOOL_TRUE - succes, BOOL_FALSE - error
  319. * @sa faux_ini_parse_str()
  320. */
  321. bool_t faux_ini_parse_file(faux_ini_t *ini, const char *fn)
  322. {
  323. bool_t eof = BOOL_FALSE;
  324. faux_file_t *f = NULL;
  325. char *buf = NULL;
  326. assert(ini);
  327. assert(fn);
  328. if (!ini)
  329. return BOOL_FALSE;
  330. if (!fn || '\0' == *fn)
  331. return BOOL_FALSE;
  332. f = faux_file_open(fn, O_RDONLY, 0);
  333. if (!f)
  334. return BOOL_FALSE;
  335. while ((buf = faux_file_getline(f))) {
  336. // Don't analyze retval because it's not obvious what
  337. // to do on error. May be next string will be ok.
  338. faux_ini_parse_str(ini, buf);
  339. faux_str_free(buf);
  340. }
  341. eof = faux_file_eof(f);
  342. faux_file_close(f);
  343. if (!eof) // File reading was interrupted before EOF
  344. return BOOL_FALSE;
  345. return BOOL_TRUE;
  346. }
  347. /** Writes INI content to string.
  348. *
  349. * Write pairs 'name/value' to string. The source of pairs is an INI object.
  350. * It's complementary operation to faux_ini_parse_str().
  351. *
  352. * @param [in] ini Allocated and initialized INI object.
  353. * @return Allocated string with INI content or NULL on error.
  354. */
  355. char *faux_ini_write_str(const faux_ini_t *ini)
  356. {
  357. faux_ini_node_t *iter = NULL;
  358. const faux_pair_t *pair = NULL;
  359. const char *spaces = " \t"; // String with spaces needs quotes
  360. char *str = NULL;
  361. assert(ini);
  362. if (!ini)
  363. return NULL;
  364. iter = faux_ini_iter(ini);
  365. while ((pair = faux_ini_each(&iter))) {
  366. char *quote_name = NULL;
  367. char *quote_value = NULL;
  368. const char *name = faux_pair_name(pair);
  369. const char *value = faux_pair_value(pair);
  370. char *line = NULL;
  371. // Word with spaces needs quotes
  372. quote_name = faux_str_chars(name, spaces) ? "\"" : "";
  373. quote_value = faux_str_chars(value, spaces) ? "\"" : "";
  374. // Prepare INI line
  375. line = faux_str_sprintf("%s%s%s=%s%s%s\n",
  376. quote_name, name, quote_name,
  377. quote_value, value, quote_value);
  378. if (!line) {
  379. faux_str_free(str);
  380. return NULL;
  381. }
  382. // Add to string
  383. if (!faux_str_cat(&str, line)) {
  384. faux_str_free(line);
  385. faux_str_free(str);
  386. return NULL;
  387. }
  388. faux_str_free(line);
  389. }
  390. return str;
  391. }
  392. /** Writes INI file using INI object.
  393. *
  394. * Write pairs 'name/value' to INI file. The source of pairs is an INI object.
  395. * It's complementary operation to faux_ini_parse_file().
  396. *
  397. * @param [in] ini Allocated and initialized INI object.
  398. * @param [in] fn File name to write to.
  399. * @return BOOL_TRUE - success, BOOL_FALSE - error
  400. */
  401. bool_t faux_ini_write_file(const faux_ini_t *ini, const char *fn)
  402. {
  403. faux_file_t *f = NULL;
  404. char *str = NULL;
  405. ssize_t bytes_written = 0;
  406. assert(ini);
  407. assert(fn);
  408. if (!ini)
  409. return BOOL_FALSE;
  410. if (faux_str_is_empty(fn))
  411. return BOOL_FALSE;
  412. str = faux_ini_write_str(ini);
  413. if (!str)
  414. return BOOL_FALSE;
  415. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  416. if (!f)
  417. return BOOL_FALSE;
  418. // Write to file
  419. bytes_written = faux_file_write_block(f, str, strlen(str));
  420. faux_file_close(f);
  421. faux_str_free(str);
  422. if (bytes_written < 0) // Can't write to file
  423. return BOOL_FALSE;
  424. return BOOL_TRUE;
  425. }
  426. //faux_ini_t *