config-file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <wchar.h>
  18. #include "config-file.h"
  19. #include "platform.h"
  20. #include "base.h"
  21. #include "bmem.h"
  22. #include "darray.h"
  23. #include "lexer.h"
  24. #include "dstr.h"
  25. struct config_item {
  26. char *name;
  27. char *value;
  28. };
  29. static inline void config_item_free(struct config_item *item)
  30. {
  31. bfree(item->name);
  32. bfree(item->value);
  33. }
  34. struct config_section {
  35. char *name;
  36. struct darray items; /* struct config_item */
  37. };
  38. static inline void config_section_free(struct config_section *section)
  39. {
  40. struct config_item *items = section->items.array;
  41. size_t i;
  42. for (i = 0; i < section->items.num; i++)
  43. config_item_free(items+i);
  44. darray_free(&section->items);
  45. bfree(section->name);
  46. }
  47. struct config_data {
  48. char *file;
  49. struct darray sections; /* struct config_section */
  50. struct darray defaults; /* struct config_section */
  51. };
  52. config_t *config_create(const char *file)
  53. {
  54. struct config_data *config;
  55. FILE *f;
  56. f = os_fopen(file, "wb");
  57. if (!f)
  58. return NULL;
  59. fclose(f);
  60. config = bzalloc(sizeof(struct config_data));
  61. return config;
  62. }
  63. static inline void remove_ref_whitespace(struct strref *ref)
  64. {
  65. if (ref->array) {
  66. while (is_whitespace(*ref->array)) {
  67. ref->array++;
  68. ref->len--;
  69. }
  70. while (ref->len && is_whitespace(ref->array[ref->len-1]))
  71. ref->len--;
  72. }
  73. }
  74. static bool config_parse_string(struct lexer *lex, struct strref *ref,
  75. char end)
  76. {
  77. bool success = end != 0;
  78. struct base_token token;
  79. base_token_clear(&token);
  80. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  81. if (end) {
  82. if (*token.text.array == end) {
  83. success = true;
  84. break;
  85. } else if (is_newline(*token.text.array)) {
  86. success = false;
  87. break;
  88. }
  89. } else {
  90. if (is_newline(*token.text.array)) {
  91. success = true;
  92. break;
  93. }
  94. }
  95. strref_add(ref, &token.text);
  96. }
  97. remove_ref_whitespace(ref);
  98. return success;
  99. }
  100. static void config_add_item(struct darray *items, struct strref *name,
  101. struct strref *value)
  102. {
  103. struct config_item item;
  104. struct dstr item_value;
  105. dstr_init_copy_strref(&item_value, value);
  106. dstr_replace(&item_value, "\\n", "\n");
  107. dstr_replace(&item_value, "\\r", "\r");
  108. dstr_replace(&item_value, "\\\\", "\\");
  109. item.name = bstrdup_n(name->array, name->len);
  110. item.value = item_value.array;
  111. darray_push_back(sizeof(struct config_item), items, &item);
  112. }
  113. static void config_parse_section(struct config_section *section,
  114. struct lexer *lex)
  115. {
  116. struct base_token token;
  117. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  118. struct strref name, value;
  119. while (token.type == BASETOKEN_WHITESPACE) {
  120. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  121. return;
  122. }
  123. if (token.type == BASETOKEN_OTHER) {
  124. if (*token.text.array == '#') {
  125. do {
  126. if (!lexer_getbasetoken(lex, &token,
  127. PARSE_WHITESPACE))
  128. return;
  129. } while (!is_newline(*token.text.array));
  130. continue;
  131. } else if (*token.text.array == '[') {
  132. lex->offset--;
  133. return;
  134. }
  135. }
  136. strref_copy(&name, &token.text);
  137. if (!config_parse_string(lex, &name, '='))
  138. continue;
  139. strref_clear(&value);
  140. config_parse_string(lex, &value, 0);
  141. if (!strref_is_empty(&value))
  142. config_add_item(&section->items, &name, &value);
  143. }
  144. }
  145. static void parse_config_data(struct darray *sections, struct lexer *lex)
  146. {
  147. struct strref section_name;
  148. struct base_token token;
  149. base_token_clear(&token);
  150. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  151. struct config_section *section;
  152. while (token.type == BASETOKEN_WHITESPACE) {
  153. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  154. return;
  155. }
  156. if (*token.text.array != '[') {
  157. while (!is_newline(*token.text.array)) {
  158. if (!lexer_getbasetoken(lex, &token,
  159. PARSE_WHITESPACE))
  160. return;
  161. }
  162. continue;
  163. }
  164. strref_clear(&section_name);
  165. config_parse_string(lex, &section_name, ']');
  166. if (!section_name.len)
  167. return;
  168. section = darray_push_back_new(sizeof(struct config_section),
  169. sections);
  170. section->name = bstrdup_n(section_name.array,
  171. section_name.len);
  172. config_parse_section(section, lex);
  173. }
  174. }
  175. static int config_parse_file(struct darray *sections, const char *file,
  176. bool always_open)
  177. {
  178. char *file_data;
  179. struct lexer lex;
  180. FILE *f;
  181. f = os_fopen(file, "rb");
  182. if (always_open && !f)
  183. f = os_fopen(file, "w+");
  184. if (!f)
  185. return CONFIG_FILENOTFOUND;
  186. os_fread_utf8(f, &file_data);
  187. fclose(f);
  188. if (!file_data)
  189. return CONFIG_SUCCESS;
  190. lexer_init(&lex);
  191. lexer_start_move(&lex, file_data);
  192. parse_config_data(sections, &lex);
  193. lexer_free(&lex);
  194. return CONFIG_SUCCESS;
  195. }
  196. int config_open(config_t **config, const char *file,
  197. enum config_open_type open_type)
  198. {
  199. int errorcode;
  200. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  201. if (!config)
  202. return CONFIG_ERROR;
  203. *config = bzalloc(sizeof(struct config_data));
  204. if (!*config)
  205. return CONFIG_ERROR;
  206. (*config)->file = bstrdup(file);
  207. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  208. if (errorcode != CONFIG_SUCCESS) {
  209. config_close(*config);
  210. *config = NULL;
  211. }
  212. return errorcode;
  213. }
  214. int config_open_string(config_t **config, const char *str)
  215. {
  216. struct lexer lex;
  217. if (!config)
  218. return CONFIG_ERROR;
  219. *config = bzalloc(sizeof(struct config_data));
  220. if (!*config)
  221. return CONFIG_ERROR;
  222. (*config)->file = NULL;
  223. lexer_init(&lex);
  224. lexer_start(&lex, str);
  225. parse_config_data(&(*config)->sections, &lex);
  226. lexer_free(&lex);
  227. return CONFIG_SUCCESS;
  228. }
  229. int config_open_defaults(config_t *config, const char *file)
  230. {
  231. if (!config)
  232. return CONFIG_ERROR;
  233. return config_parse_file(&config->defaults, file, false);
  234. }
  235. int config_save(config_t *config)
  236. {
  237. FILE *f;
  238. struct dstr str, tmp;
  239. size_t i, j;
  240. if (!config)
  241. return CONFIG_ERROR;
  242. if (!config->file)
  243. return CONFIG_ERROR;
  244. dstr_init(&str);
  245. dstr_init(&tmp);
  246. f = os_fopen(config->file, "wb");
  247. if (!f)
  248. return CONFIG_FILENOTFOUND;
  249. for (i = 0; i < config->sections.num; i++) {
  250. struct config_section *section = darray_item(
  251. sizeof(struct config_section),
  252. &config->sections, i);
  253. if (i) dstr_cat(&str, "\n");
  254. dstr_cat(&str, "[");
  255. dstr_cat(&str, section->name);
  256. dstr_cat(&str, "]\n");
  257. for (j = 0; j < section->items.num; j++) {
  258. struct config_item *item = darray_item(
  259. sizeof(struct config_item),
  260. &section->items, j);
  261. dstr_copy(&tmp, item->value ? item->value : "");
  262. dstr_replace(&tmp, "\\", "\\\\");
  263. dstr_replace(&tmp, "\r", "\\r");
  264. dstr_replace(&tmp, "\n", "\\n");
  265. dstr_cat(&str, item->name);
  266. dstr_cat(&str, "=");
  267. dstr_cat(&str, tmp.array);
  268. dstr_cat(&str, "\n");
  269. }
  270. }
  271. #ifdef _WIN32
  272. fwrite("\xEF\xBB\xBF", 1, 3, f);
  273. #endif
  274. fwrite(str.array, 1, str.len, f);
  275. fclose(f);
  276. dstr_free(&tmp);
  277. dstr_free(&str);
  278. return CONFIG_SUCCESS;
  279. }
  280. void config_close(config_t *config)
  281. {
  282. struct config_section *defaults, *sections;
  283. size_t i;
  284. if (!config) return;
  285. defaults = config->defaults.array;
  286. sections = config->sections.array;
  287. for (i = 0; i < config->defaults.num; i++)
  288. config_section_free(defaults+i);
  289. for (i = 0; i < config->sections.num; i++)
  290. config_section_free(sections+i);
  291. darray_free(&config->defaults);
  292. darray_free(&config->sections);
  293. bfree(config->file);
  294. bfree(config);
  295. }
  296. size_t config_num_sections(config_t *config)
  297. {
  298. return config->sections.num;
  299. }
  300. const char *config_get_section(config_t *config, size_t idx)
  301. {
  302. struct config_section *section;
  303. if (idx >= config->sections.num)
  304. return NULL;
  305. section = darray_item(sizeof(struct config_section), &config->sections,
  306. idx);
  307. return section->name;
  308. }
  309. static const struct config_item *config_find_item(const struct darray *sections,
  310. const char *section, const char *name)
  311. {
  312. size_t i, j;
  313. for (i = 0; i < sections->num; i++) {
  314. const struct config_section *sec = darray_item(
  315. sizeof(struct config_section), sections, i);
  316. if (astrcmpi(sec->name, section) == 0) {
  317. for (j = 0; j < sec->items.num; j++) {
  318. struct config_item *item = darray_item(
  319. sizeof(struct config_item),
  320. &sec->items, j);
  321. if (astrcmpi(item->name, name) == 0)
  322. return item;
  323. }
  324. }
  325. }
  326. return NULL;
  327. }
  328. static void config_set_item(struct darray *sections, const char *section,
  329. const char *name, char *value)
  330. {
  331. struct config_section *sec = NULL;
  332. struct config_section *array = sections->array;
  333. struct config_item *item;
  334. size_t i, j;
  335. for (i = 0; i < sections->num; i++) {
  336. struct config_section *cur_sec = array+i;
  337. struct config_item *items = cur_sec->items.array;
  338. if (astrcmpi(cur_sec->name, section) == 0) {
  339. for (j = 0; j < cur_sec->items.num; j++) {
  340. item = items+j;
  341. if (astrcmpi(item->name, name) == 0) {
  342. bfree(item->value);
  343. item->value = value;
  344. return;
  345. }
  346. }
  347. sec = cur_sec;
  348. break;
  349. }
  350. }
  351. if (!sec) {
  352. sec = darray_push_back_new(sizeof(struct config_section),
  353. sections);
  354. sec->name = bstrdup(section);
  355. }
  356. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  357. item->name = bstrdup(name);
  358. item->value = value;
  359. }
  360. void config_set_string(config_t *config, const char *section,
  361. const char *name, const char *value)
  362. {
  363. if (!value)
  364. value = "";
  365. config_set_item(&config->sections, section, name, bstrdup(value));
  366. }
  367. void config_set_int(config_t *config, const char *section,
  368. const char *name, int64_t value)
  369. {
  370. struct dstr str;
  371. dstr_init(&str);
  372. dstr_printf(&str, "%lld", value);
  373. config_set_item(&config->sections, section, name, str.array);
  374. }
  375. void config_set_uint(config_t *config, const char *section,
  376. const char *name, uint64_t value)
  377. {
  378. struct dstr str;
  379. dstr_init(&str);
  380. dstr_printf(&str, "%llu", value);
  381. config_set_item(&config->sections, section, name, str.array);
  382. }
  383. void config_set_bool(config_t *config, const char *section,
  384. const char *name, bool value)
  385. {
  386. char *str = bstrdup(value ? "true" : "false");
  387. config_set_item(&config->sections, section, name, str);
  388. }
  389. void config_set_double(config_t *config, const char *section,
  390. const char *name, double value)
  391. {
  392. char *str = bzalloc(64);
  393. os_dtostr(value, str, 64);
  394. config_set_item(&config->sections, section, name, str);
  395. }
  396. void config_set_default_string(config_t *config, const char *section,
  397. const char *name, const char *value)
  398. {
  399. if (!value)
  400. value = "";
  401. config_set_item(&config->defaults, section, name, bstrdup(value));
  402. }
  403. void config_set_default_int(config_t *config, const char *section,
  404. const char *name, int64_t value)
  405. {
  406. struct dstr str;
  407. dstr_init(&str);
  408. dstr_printf(&str, "%lld", value);
  409. config_set_item(&config->defaults, section, name, str.array);
  410. }
  411. void config_set_default_uint(config_t *config, const char *section,
  412. const char *name, uint64_t value)
  413. {
  414. struct dstr str;
  415. dstr_init(&str);
  416. dstr_printf(&str, "%llu", value);
  417. config_set_item(&config->defaults, section, name, str.array);
  418. }
  419. void config_set_default_bool(config_t *config, const char *section,
  420. const char *name, bool value)
  421. {
  422. char *str = bstrdup(value ? "true" : "false");
  423. config_set_item(&config->defaults, section, name, str);
  424. }
  425. void config_set_default_double(config_t *config, const char *section,
  426. const char *name, double value)
  427. {
  428. struct dstr str;
  429. dstr_init(&str);
  430. dstr_printf(&str, "%g", value);
  431. config_set_item(&config->defaults, section, name, str.array);
  432. }
  433. const char *config_get_string(const config_t *config, const char *section,
  434. const char *name)
  435. {
  436. const struct config_item *item = config_find_item(&config->sections,
  437. section, name);
  438. if (!item)
  439. item = config_find_item(&config->defaults, section, name);
  440. if (!item)
  441. return NULL;
  442. return item->value;
  443. }
  444. static inline int64_t str_to_int64(const char *str)
  445. {
  446. if (!str || !*str)
  447. return 0;
  448. if (str[0] == '0' && str[1] == 'x')
  449. return strtoll(str + 2, NULL, 16);
  450. else
  451. return strtoll(str, NULL, 10);
  452. }
  453. static inline uint64_t str_to_uint64(const char *str)
  454. {
  455. if (!str || !*str)
  456. return 0;
  457. if (str[0] == '0' && str[1] == 'x')
  458. return strtoull(str + 2, NULL, 16);
  459. else
  460. return strtoull(str, NULL, 10);
  461. }
  462. int64_t config_get_int(const config_t *config, const char *section,
  463. const char *name)
  464. {
  465. const char *value = config_get_string(config, section, name);
  466. if (value)
  467. return str_to_int64(value);
  468. return 0;
  469. }
  470. uint64_t config_get_uint(const config_t *config, const char *section,
  471. const char *name)
  472. {
  473. const char *value = config_get_string(config, section, name);
  474. if (value)
  475. return str_to_uint64(value);
  476. return 0;
  477. }
  478. bool config_get_bool(const config_t *config, const char *section,
  479. const char *name)
  480. {
  481. const char *value = config_get_string(config, section, name);
  482. if (value)
  483. return astrcmpi(value, "true") == 0 ||
  484. !!str_to_uint64(value);
  485. return false;
  486. }
  487. double config_get_double(const config_t *config, const char *section,
  488. const char *name)
  489. {
  490. const char *value = config_get_string(config, section, name);
  491. if (value)
  492. return os_strtod(value);
  493. return 0.0;
  494. }
  495. bool config_remove_value(config_t *config, const char *section,
  496. const char *name)
  497. {
  498. struct darray *sections = &config->sections;
  499. for (size_t i = 0; i < sections->num; i++) {
  500. struct config_section *sec = darray_item(
  501. sizeof(struct config_section), sections, i);
  502. if (astrcmpi(sec->name, section) != 0)
  503. continue;
  504. for (size_t j = 0; j < sec->items.num; j++) {
  505. struct config_item *item = darray_item(
  506. sizeof(struct config_item),
  507. &sec->items, j);
  508. if (astrcmpi(item->name, name) == 0) {
  509. config_item_free(item);
  510. darray_erase(sizeof(struct config_item),
  511. &sec->items, j);
  512. return true;
  513. }
  514. }
  515. }
  516. return false;
  517. }
  518. const char *config_get_default_string(const config_t *config,
  519. const char *section, const char *name)
  520. {
  521. const struct config_item *item;
  522. item = config_find_item(&config->defaults, section, name);
  523. if (!item)
  524. return NULL;
  525. return item->value;
  526. }
  527. int64_t config_get_default_int(const config_t *config, const char *section,
  528. const char *name)
  529. {
  530. const char *value = config_get_default_string(config, section, name);
  531. if (value)
  532. return str_to_int64(value);
  533. return 0;
  534. }
  535. uint64_t config_get_default_uint(const config_t *config, const char *section,
  536. const char *name)
  537. {
  538. const char *value = config_get_default_string(config, section, name);
  539. if (value)
  540. return str_to_uint64(value);
  541. return 0;
  542. }
  543. bool config_get_default_bool(const config_t *config, const char *section,
  544. const char *name)
  545. {
  546. const char *value = config_get_default_string(config, section, name);
  547. if (value)
  548. return astrcmpi(value, "true") == 0 ||
  549. !!str_to_uint64(value);
  550. return false;
  551. }
  552. double config_get_default_double(const config_t *config, const char *section,
  553. const char *name)
  554. {
  555. const char *value = config_get_default_string(config, section, name);
  556. if (value)
  557. return os_strtod(value);
  558. return 0.0;
  559. }
  560. bool config_has_user_value(const config_t *config, const char *section,
  561. const char *name)
  562. {
  563. return config_find_item(&config->sections, section, name) != NULL;
  564. }
  565. bool config_has_default_value(const config_t *config, const char *section,
  566. const char *name)
  567. {
  568. return config_find_item(&config->defaults, section, name) != NULL;
  569. }