ini.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. static int faux_ini_compare(const void *first, const void *second)
  15. {
  16. const faux_pair_t *f = (const faux_pair_t *)first;
  17. const faux_pair_t *s = (const faux_pair_t *)second;
  18. return strcmp(f->name, s->name);
  19. }
  20. static int faux_ini_kcompare(const void *key, const void *list_item)
  21. {
  22. const char *f = (const char *)key;
  23. const faux_pair_t *s = (const faux_pair_t *)list_item;
  24. return strcmp(f, s->name);
  25. }
  26. static int faux_ini_kcompare_prefix(const void *key, const void *list_item)
  27. {
  28. const char *prefix = (const char *)key;
  29. const faux_pair_t *s = (const faux_pair_t *)list_item;
  30. int res = 0;
  31. size_t prefix_len = 0;
  32. size_t entry_len = 0;
  33. size_t len = 0;
  34. if (faux_str_is_empty(prefix))
  35. return -1; // No chance to find anything
  36. prefix_len = strlen(prefix);
  37. entry_len = strlen(s->name);
  38. len = (prefix_len < entry_len) ? prefix_len : entry_len;
  39. res = strncmp(prefix, s->name, len);
  40. // If entry name will not have any chars after prefix removing
  41. // then don't include it to sub-ini. Because it will not have a name at all.
  42. if ((0 == res) && (prefix_len == entry_len))
  43. return 1; // Let's search for the next entry
  44. return res;
  45. }
  46. /** @brief Allocates new INI object.
  47. *
  48. * Before working with INI object it must be allocated and initialized.
  49. *
  50. * @return Allocated and initialized INI object or NULL on error.
  51. */
  52. faux_ini_t *faux_ini_new(void)
  53. {
  54. faux_ini_t *ini = NULL;
  55. ini = faux_zmalloc(sizeof(*ini));
  56. if (!ini)
  57. return NULL;
  58. // Init
  59. ini->list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  60. faux_ini_compare, faux_ini_kcompare, faux_pair_free);
  61. return ini;
  62. }
  63. /** @brief Frees the INI object.
  64. *
  65. * After using the INI object must be freed. Function frees INI object itself
  66. * and all pairs 'name/value' stored within INI object.
  67. *
  68. * @param [in] ini Allocated and initialized INI object.
  69. */
  70. void faux_ini_free(faux_ini_t *ini)
  71. {
  72. if (!ini)
  73. return;
  74. faux_list_free(ini->list);
  75. faux_free(ini);
  76. }
  77. /** Checks if INI object is empty.
  78. *
  79. * @param [in] ini Allocated and initialized INI object.
  80. * @return BOOL_TRUE - empty, BOOL_FALSE - not empty.
  81. */
  82. bool_t faux_ini_is_empty(const faux_ini_t *ini)
  83. {
  84. assert(ini);
  85. if (!ini)
  86. return BOOL_TRUE;
  87. return faux_list_is_empty(ini->list);
  88. }
  89. /** @brief Adds pair 'name/value' to INI object.
  90. *
  91. * The 'name' field is a key. The key must be unique. Each key has its
  92. * correspondent value.
  93. *
  94. * If key is new for the INI object then the pair 'name/value' will be added
  95. * to it. If INI object already contain the same key then value of this pair
  96. * will be replaced by newer one. If new specified value is NULL then the
  97. * entry with the correspondent key will be removed from the INI object.
  98. *
  99. * @param [in] ini Allocated and initialized INI object.
  100. * @param [in] name Name field for pair 'name/value'.
  101. * @param [in] value Value field for pair 'name/value'.
  102. * @return
  103. * Newly created pair object.
  104. * NULL if entry was removed (value == NULL)
  105. * NULL on error
  106. */
  107. const faux_pair_t *faux_ini_set(
  108. faux_ini_t *ini, const char *name, const char *value)
  109. {
  110. faux_pair_t *pair = NULL;
  111. faux_list_node_t *node = NULL;
  112. faux_pair_t *found_pair = NULL;
  113. assert(ini);
  114. assert(name);
  115. if (!ini || !name)
  116. return NULL;
  117. // NULL 'value' means: remove entry from list
  118. if (!value) {
  119. node = faux_list_kfind_node(ini->list, name);
  120. if (node)
  121. faux_list_del(ini->list, node);
  122. return NULL;
  123. }
  124. pair = faux_pair_new(name, value);
  125. assert(pair);
  126. if (!pair)
  127. return NULL;
  128. // Try to add new entry or find existent entry with the same 'name'
  129. node = faux_list_add_find(ini->list, pair);
  130. if (!node) { // Something went wrong
  131. faux_pair_free(pair);
  132. return NULL;
  133. }
  134. found_pair = faux_list_data(node);
  135. if (found_pair != pair) { // Item already exists so use existent
  136. faux_pair_free(pair);
  137. faux_pair_set_value(
  138. found_pair, value); // Replace value by new one
  139. return found_pair;
  140. }
  141. // The new entry was added
  142. return pair;
  143. }
  144. /** @brief Removes pair 'name/value' from INI object.
  145. *
  146. * Function search for the pair with specified name within INI object and
  147. * removes it.
  148. *
  149. * @param [in] ini Allocated and initialized INI object.
  150. * @param [in] name Name field to search for the entry.
  151. */
  152. void faux_ini_unset(faux_ini_t *ini, const char *name)
  153. {
  154. faux_ini_set(ini, name, NULL);
  155. }
  156. /** @brief Searches for pair by name.
  157. *
  158. * The name field is a key to search INI object for pair 'name/value'.
  159. *
  160. * @param [in] ini Allocated and initialized INI object.
  161. * @param [in] name Name field to search for.
  162. * @return
  163. * Found pair 'name/value'.
  164. * NULL on errror.
  165. */
  166. const faux_pair_t *faux_ini_find_pair(const faux_ini_t *ini, const char *name)
  167. {
  168. assert(ini);
  169. assert(name);
  170. if (!ini || !name)
  171. return NULL;
  172. return faux_list_kfind(ini->list, name);
  173. }
  174. /** @brief Searches for pair by name and returns correspondent value.
  175. *
  176. * The name field is a key to search INI object for pair 'name/value'.
  177. *
  178. * @param [in] ini Allocated and initialized INI object.
  179. * @param [in] name Name field to search for.
  180. * @return
  181. * Found value field.
  182. * NULL on errror.
  183. */
  184. const char *faux_ini_find(const faux_ini_t *ini, const char *name)
  185. {
  186. const faux_pair_t *pair = faux_ini_find_pair(ini, name);
  187. if (!pair)
  188. return NULL;
  189. return faux_pair_value(pair);
  190. }
  191. /** @brief Initializes iterator to iterate through the entire INI object.
  192. *
  193. * Before iterating with the faux_ini_each() function the iterator must be
  194. * initialized. This function do it.
  195. *
  196. * @param [in] ini Allocated and initialized INI object.
  197. * @return Initialized iterator.
  198. * @sa faux_ini_each()
  199. */
  200. faux_ini_node_t *faux_ini_iter(const faux_ini_t *ini)
  201. {
  202. assert(ini);
  203. if (!ini)
  204. return NULL;
  205. return (faux_ini_node_t *)faux_list_head(ini->list);
  206. }
  207. /** @brief Iterate entire INI object for pairs 'name/value'.
  208. *
  209. * Before iteration the iterator must be initialized by faux_ini_iter()
  210. * function. Doesn't use faux_ini_each() with uninitialized iterator.
  211. *
  212. * On each call function returns pair 'name/value' and modifies iterator.
  213. * Stop iteration when function returns NULL.
  214. *
  215. * @param [in,out] iter Iterator.
  216. * @return Pair 'name/value'.
  217. * @sa faux_ini_iter()
  218. */
  219. const faux_pair_t *faux_ini_each(faux_ini_node_t **iter)
  220. {
  221. return (const faux_pair_t *)faux_list_each((faux_list_node_t **)iter);
  222. }
  223. /** Service function to purify (clean out spaces, quotes) word.
  224. *
  225. * The 'word' in this case is a string without prepending or trailing spaces.
  226. * If 'word' is quoted then it can contain spaces within quoting. The qoutes
  227. * itself is not part of the 'word'. If 'word' is not quoted then it can't
  228. * contain spaces, so the end of 'word' is a first space after non-space
  229. * characters. The function searchs for the first occurence of 'word' within
  230. * specified string, allocates memory and returns purified copy of the word.
  231. * The return value must be faux_str_free()-ed later.
  232. *
  233. * Now the unclosed quoting is not an error. Suppose the end of the line can
  234. * close quoting.
  235. *
  236. * @param [in] str String to find word in it.
  237. * @return Purified copy of word or NULL.
  238. */
  239. static char *faux_ini_purify_word(const char *str)
  240. {
  241. const char *word = NULL;
  242. const char *string = str;
  243. bool_t quoted = BOOL_FALSE;
  244. size_t len = 0;
  245. assert(str);
  246. if (!str)
  247. return NULL;
  248. // Find the start of a word
  249. while (*string != '\0' && isspace(*string)) {
  250. string++;
  251. }
  252. // Is this the start of a quoted string?
  253. if ('"' == *string) {
  254. quoted = BOOL_TRUE;
  255. string++;
  256. }
  257. word = string; // Begin of purified word
  258. // Find the end of the word
  259. while (*string != '\0') {
  260. if ('\\' == *string) {
  261. string++;
  262. if ('\0' == *string) // Unfinished escaping
  263. break; // Don't increment 'len'
  264. len++;
  265. // Skip escaped char
  266. string++;
  267. len++;
  268. continue;
  269. }
  270. // End of word
  271. if (!quoted && isspace(*string))
  272. break;
  273. if ('"' == *string) {
  274. // End of a quoted string
  275. break;
  276. }
  277. string++;
  278. len++;
  279. }
  280. if (len == 0)
  281. return NULL;
  282. return faux_str_dupn(word, len);
  283. }
  284. /** @brief Parse string for pairs 'name/value'.
  285. *
  286. * String can contain an `name/value` pairs in following format:
  287. * @code
  288. * var1=value1
  289. * var2 = "value 2"
  290. * @endcode
  291. * Function parses that string and stores 'name/value' pairs to
  292. * the INI object.
  293. *
  294. * @param [in] ini Allocated and initialized INI object.
  295. * @param [in] string String to parse.
  296. * @return BOOL_TRUE - succes, BOOL_FALSE - error
  297. */
  298. bool_t faux_ini_parse_str(faux_ini_t *ini, const char *string)
  299. {
  300. char *buffer = NULL;
  301. char *saveptr = NULL;
  302. char *line = NULL;
  303. assert(ini);
  304. if (!ini)
  305. return BOOL_FALSE;
  306. if (!string)
  307. return BOOL_TRUE;
  308. buffer = faux_str_dup(string);
  309. // Now loop though each line
  310. for (line = strtok_r(buffer, "\n\r", &saveptr); line;
  311. line = strtok_r(NULL, "\n\r", &saveptr)) {
  312. // Now 'line' contain one 'name/value' pair. Single line.
  313. char *str = NULL;
  314. char *name = NULL;
  315. char *value = NULL;
  316. char *savestr = NULL;
  317. char *rname = NULL;
  318. char *rvalue = NULL;
  319. while ((*line != '\0') && isspace(*line)) // Skip spaces
  320. line++;
  321. if ('\0' == *line) // Empty line
  322. continue;
  323. if ('#' == *line) // Comment. Skip it.
  324. continue;
  325. str = faux_str_dup(line);
  326. // Find out name
  327. name = strtok_r(str, "=", &savestr);
  328. if (!name) {
  329. faux_str_free(str);
  330. continue;
  331. }
  332. rname = faux_ini_purify_word(name);
  333. if (!rname) {
  334. faux_str_free(str);
  335. continue;
  336. }
  337. // Find out value
  338. value = strtok_r(NULL, "=", &savestr);
  339. if (!value) { // Empty value
  340. rvalue = NULL;
  341. } else {
  342. rvalue = faux_ini_purify_word(value);
  343. }
  344. faux_ini_set(ini, rname, rvalue);
  345. faux_str_free(rname);
  346. faux_str_free(rvalue);
  347. faux_str_free(str);
  348. }
  349. faux_str_free(buffer);
  350. return BOOL_TRUE;
  351. }
  352. /** @brief Parse file for pairs 'name/value'.
  353. *
  354. * File can contain an `name/value` pairs in following format:
  355. * @code
  356. * var1=value1
  357. * var2 = "value 2"
  358. * @endcode
  359. * Function parses file and stores 'name/value' pairs to
  360. * the INI object.
  361. *
  362. * @param [in] ini Allocated and initialized INI object.
  363. * @param [in] string String to parse.
  364. * @return BOOL_TRUE - succes, BOOL_FALSE - error
  365. * @sa faux_ini_parse_str()
  366. */
  367. bool_t faux_ini_parse_file(faux_ini_t *ini, const char *fn)
  368. {
  369. bool_t eof = BOOL_FALSE;
  370. faux_file_t *f = NULL;
  371. char *buf = NULL;
  372. assert(ini);
  373. assert(fn);
  374. if (!ini)
  375. return BOOL_FALSE;
  376. if (!fn || '\0' == *fn)
  377. return BOOL_FALSE;
  378. f = faux_file_open(fn, O_RDONLY, 0);
  379. if (!f)
  380. return BOOL_FALSE;
  381. while ((buf = faux_file_getline(f))) {
  382. // Don't analyze retval because it's not obvious what
  383. // to do on error. May be next string will be ok.
  384. faux_ini_parse_str(ini, buf);
  385. faux_str_free(buf);
  386. }
  387. eof = faux_file_eof(f);
  388. faux_file_close(f);
  389. if (!eof) // File reading was interrupted before EOF
  390. return BOOL_FALSE;
  391. return BOOL_TRUE;
  392. }
  393. /** Writes INI content to string.
  394. *
  395. * Write pairs 'name/value' to string. The source of pairs is an INI object.
  396. * It's complementary operation to faux_ini_parse_str().
  397. *
  398. * @param [in] ini Allocated and initialized INI object.
  399. * @return Allocated string with INI content or NULL on error.
  400. */
  401. char *faux_ini_write_str(const faux_ini_t *ini)
  402. {
  403. faux_ini_node_t *iter = NULL;
  404. const faux_pair_t *pair = NULL;
  405. const char *spaces = " \t"; // String with spaces needs quotes
  406. char *str = NULL;
  407. assert(ini);
  408. if (!ini)
  409. return NULL;
  410. iter = faux_ini_iter(ini);
  411. while ((pair = faux_ini_each(&iter))) {
  412. char *quote_name = NULL;
  413. char *quote_value = NULL;
  414. const char *name = faux_pair_name(pair);
  415. const char *value = faux_pair_value(pair);
  416. char *line = NULL;
  417. // Word with spaces needs quotes
  418. quote_name = faux_str_chars(name, spaces) ? "\"" : "";
  419. quote_value = faux_str_chars(value, spaces) ? "\"" : "";
  420. // Prepare INI line
  421. line = faux_str_sprintf("%s%s%s=%s%s%s\n",
  422. quote_name, name, quote_name,
  423. quote_value, value, quote_value);
  424. if (!line) {
  425. faux_str_free(str);
  426. return NULL;
  427. }
  428. // Add to string
  429. if (!faux_str_cat(&str, line)) {
  430. faux_str_free(line);
  431. faux_str_free(str);
  432. return NULL;
  433. }
  434. faux_str_free(line);
  435. }
  436. return str;
  437. }
  438. /** Writes INI file using INI object.
  439. *
  440. * Write pairs 'name/value' to INI file. The source of pairs is an INI object.
  441. * It's complementary operation to faux_ini_parse_file().
  442. *
  443. * @param [in] ini Allocated and initialized INI object.
  444. * @param [in] fn File name to write to.
  445. * @return BOOL_TRUE - success, BOOL_FALSE - error
  446. */
  447. bool_t faux_ini_write_file(const faux_ini_t *ini, const char *fn)
  448. {
  449. faux_file_t *f = NULL;
  450. char *str = NULL;
  451. ssize_t bytes_written = 0;
  452. assert(ini);
  453. assert(fn);
  454. if (!ini)
  455. return BOOL_FALSE;
  456. if (faux_str_is_empty(fn))
  457. return BOOL_FALSE;
  458. str = faux_ini_write_str(ini);
  459. if (!str)
  460. return BOOL_FALSE;
  461. f = faux_file_open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  462. if (!f)
  463. return BOOL_FALSE;
  464. // Write to file
  465. bytes_written = faux_file_write_block(f, str, strlen(str));
  466. faux_file_close(f);
  467. faux_str_free(str);
  468. if (bytes_written < 0) // Can't write to file
  469. return BOOL_FALSE;
  470. return BOOL_TRUE;
  471. }
  472. /** Extracts new sub-INI object including entries with given prefix.
  473. *
  474. * For example user has a file like this:
  475. * var1=value1
  476. * var2.a=value2
  477. * var2.b=value3
  478. * var3=value4
  479. *
  480. * If user specifies prefix with "var2." then function will create new INI
  481. * object and put the following entries to it:
  482. * a=value2
  483. * b=value3
  484. *
  485. * Note that entry with name "var2." will not be included to resulting INI
  486. * object because such entry will have empty name field (name = prefix).
  487. *
  488. * Note returned INI object can be empty.
  489. *
  490. * @param [in] ini Allocated and initialized INI object.
  491. * @param [in] prefix The prefix to search for.
  492. * @return New INI object or NULL on error.
  493. */
  494. faux_ini_t *faux_ini_extract_subini(const faux_ini_t *ini, const char *prefix)
  495. {
  496. faux_ini_t *subini = NULL;
  497. faux_ini_node_t *iter = NULL;
  498. const faux_pair_t *pair = NULL;
  499. size_t prefix_len = 0;
  500. assert(ini);
  501. if (!ini)
  502. return NULL;
  503. assert(ini->list);
  504. if (!ini->list)
  505. return NULL;
  506. subini = faux_ini_new();
  507. assert(subini);
  508. if (!subini)
  509. return NULL;
  510. prefix_len = strlen(prefix);
  511. iter = faux_list_head(ini->list);
  512. while ((pair = (faux_pair_t *)faux_list_match(ini->list,
  513. faux_ini_kcompare_prefix, prefix, &iter))) {
  514. const char *name = faux_pair_name(pair);
  515. const char *new_name = name + prefix_len; // Remove prefix
  516. faux_ini_set(subini, new_name, faux_pair_value(pair));
  517. }
  518. return subini;
  519. }