config-file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. config_add_item(&section->items, &name, &value);
  142. }
  143. }
  144. static void parse_config_data(struct darray *sections, struct lexer *lex)
  145. {
  146. struct strref section_name;
  147. struct base_token token;
  148. base_token_clear(&token);
  149. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  150. struct config_section *section;
  151. while (token.type == BASETOKEN_WHITESPACE) {
  152. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  153. return;
  154. }
  155. if (*token.text.array != '[') {
  156. while (!is_newline(*token.text.array)) {
  157. if (!lexer_getbasetoken(lex, &token,
  158. PARSE_WHITESPACE))
  159. return;
  160. }
  161. continue;
  162. }
  163. strref_clear(&section_name);
  164. config_parse_string(lex, &section_name, ']');
  165. if (!section_name.len)
  166. return;
  167. section = darray_push_back_new(sizeof(struct config_section),
  168. sections);
  169. section->name = bstrdup_n(section_name.array,
  170. section_name.len);
  171. config_parse_section(section, lex);
  172. }
  173. }
  174. static int config_parse_file(struct darray *sections, const char *file,
  175. bool always_open)
  176. {
  177. char *file_data;
  178. struct lexer lex;
  179. FILE *f;
  180. f = os_fopen(file, "rb");
  181. if (always_open && !f)
  182. f = os_fopen(file, "w+");
  183. if (!f)
  184. return CONFIG_FILENOTFOUND;
  185. os_fread_utf8(f, &file_data);
  186. fclose(f);
  187. if (!file_data)
  188. return CONFIG_SUCCESS;
  189. lexer_init(&lex);
  190. lexer_start_move(&lex, file_data);
  191. parse_config_data(sections, &lex);
  192. lexer_free(&lex);
  193. return CONFIG_SUCCESS;
  194. }
  195. int config_open(config_t **config, const char *file,
  196. enum config_open_type open_type)
  197. {
  198. int errorcode;
  199. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  200. if (!config)
  201. return CONFIG_ERROR;
  202. *config = bzalloc(sizeof(struct config_data));
  203. if (!*config)
  204. return CONFIG_ERROR;
  205. (*config)->file = bstrdup(file);
  206. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  207. if (errorcode != CONFIG_SUCCESS) {
  208. config_close(*config);
  209. *config = NULL;
  210. }
  211. return errorcode;
  212. }
  213. int config_open_string(config_t **config, const char *str)
  214. {
  215. struct lexer lex;
  216. if (!config)
  217. return CONFIG_ERROR;
  218. *config = bzalloc(sizeof(struct config_data));
  219. if (!*config)
  220. return CONFIG_ERROR;
  221. (*config)->file = NULL;
  222. lexer_init(&lex);
  223. lexer_start(&lex, str);
  224. parse_config_data(&(*config)->sections, &lex);
  225. lexer_free(&lex);
  226. return CONFIG_SUCCESS;
  227. }
  228. int config_open_defaults(config_t *config, const char *file)
  229. {
  230. if (!config)
  231. return CONFIG_ERROR;
  232. return config_parse_file(&config->defaults, file, false);
  233. }
  234. int config_save(config_t *config)
  235. {
  236. FILE *f;
  237. struct dstr str, tmp;
  238. size_t i, j;
  239. if (!config)
  240. return CONFIG_ERROR;
  241. if (!config->file)
  242. return CONFIG_ERROR;
  243. dstr_init(&str);
  244. dstr_init(&tmp);
  245. f = os_fopen(config->file, "wb");
  246. if (!f)
  247. return CONFIG_FILENOTFOUND;
  248. for (i = 0; i < config->sections.num; i++) {
  249. struct config_section *section = darray_item(
  250. sizeof(struct config_section),
  251. &config->sections, i);
  252. if (i) dstr_cat(&str, "\n");
  253. dstr_cat(&str, "[");
  254. dstr_cat(&str, section->name);
  255. dstr_cat(&str, "]\n");
  256. for (j = 0; j < section->items.num; j++) {
  257. struct config_item *item = darray_item(
  258. sizeof(struct config_item),
  259. &section->items, j);
  260. dstr_copy(&tmp, item->value ? item->value : "");
  261. dstr_replace(&tmp, "\\", "\\\\");
  262. dstr_replace(&tmp, "\r", "\\r");
  263. dstr_replace(&tmp, "\n", "\\n");
  264. dstr_cat(&str, item->name);
  265. dstr_cat(&str, "=");
  266. dstr_cat(&str, tmp.array);
  267. dstr_cat(&str, "\n");
  268. }
  269. }
  270. #ifdef _WIN32
  271. fwrite("\xEF\xBB\xBF", 1, 3, f);
  272. #endif
  273. fwrite(str.array, 1, str.len, f);
  274. fclose(f);
  275. dstr_free(&tmp);
  276. dstr_free(&str);
  277. return CONFIG_SUCCESS;
  278. }
  279. void config_close(config_t *config)
  280. {
  281. struct config_section *defaults, *sections;
  282. size_t i;
  283. if (!config) return;
  284. defaults = config->defaults.array;
  285. sections = config->sections.array;
  286. for (i = 0; i < config->defaults.num; i++)
  287. config_section_free(defaults+i);
  288. for (i = 0; i < config->sections.num; i++)
  289. config_section_free(sections+i);
  290. darray_free(&config->defaults);
  291. darray_free(&config->sections);
  292. bfree(config->file);
  293. bfree(config);
  294. }
  295. size_t config_num_sections(config_t *config)
  296. {
  297. return config->sections.num;
  298. }
  299. const char *config_get_section(config_t *config, size_t idx)
  300. {
  301. struct config_section *section;
  302. if (idx >= config->sections.num)
  303. return NULL;
  304. section = darray_item(sizeof(struct config_section), &config->sections,
  305. idx);
  306. return section->name;
  307. }
  308. static const struct config_item *config_find_item(const struct darray *sections,
  309. const char *section, const char *name)
  310. {
  311. size_t i, j;
  312. for (i = 0; i < sections->num; i++) {
  313. const struct config_section *sec = darray_item(
  314. sizeof(struct config_section), sections, i);
  315. if (astrcmpi(sec->name, section) == 0) {
  316. for (j = 0; j < sec->items.num; j++) {
  317. struct config_item *item = darray_item(
  318. sizeof(struct config_item),
  319. &sec->items, j);
  320. if (astrcmpi(item->name, name) == 0)
  321. return item;
  322. }
  323. }
  324. }
  325. return NULL;
  326. }
  327. static void config_set_item(struct darray *sections, const char *section,
  328. const char *name, char *value)
  329. {
  330. struct config_section *sec = NULL;
  331. struct config_section *array = sections->array;
  332. struct config_item *item;
  333. size_t i, j;
  334. for (i = 0; i < sections->num; i++) {
  335. struct config_section *cur_sec = array+i;
  336. struct config_item *items = cur_sec->items.array;
  337. if (astrcmpi(cur_sec->name, section) == 0) {
  338. for (j = 0; j < cur_sec->items.num; j++) {
  339. item = items+j;
  340. if (astrcmpi(item->name, name) == 0) {
  341. bfree(item->value);
  342. item->value = value;
  343. return;
  344. }
  345. }
  346. sec = cur_sec;
  347. break;
  348. }
  349. }
  350. if (!sec) {
  351. sec = darray_push_back_new(sizeof(struct config_section),
  352. sections);
  353. sec->name = bstrdup(section);
  354. }
  355. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  356. item->name = bstrdup(name);
  357. item->value = value;
  358. }
  359. void config_set_string(config_t *config, const char *section,
  360. const char *name, const char *value)
  361. {
  362. if (!value)
  363. value = "";
  364. config_set_item(&config->sections, section, name, bstrdup(value));
  365. }
  366. void config_set_int(config_t *config, const char *section,
  367. const char *name, int64_t value)
  368. {
  369. struct dstr str;
  370. dstr_init(&str);
  371. dstr_printf(&str, "%lld", value);
  372. config_set_item(&config->sections, section, name, str.array);
  373. }
  374. void config_set_uint(config_t *config, const char *section,
  375. const char *name, uint64_t value)
  376. {
  377. struct dstr str;
  378. dstr_init(&str);
  379. dstr_printf(&str, "%llu", value);
  380. config_set_item(&config->sections, section, name, str.array);
  381. }
  382. void config_set_bool(config_t *config, const char *section,
  383. const char *name, bool value)
  384. {
  385. char *str = bstrdup(value ? "true" : "false");
  386. config_set_item(&config->sections, section, name, str);
  387. }
  388. void config_set_double(config_t *config, const char *section,
  389. const char *name, double value)
  390. {
  391. char *str = bzalloc(64);
  392. os_dtostr(value, str, 64);
  393. config_set_item(&config->sections, section, name, str);
  394. }
  395. void config_set_default_string(config_t *config, const char *section,
  396. const char *name, const char *value)
  397. {
  398. if (!value)
  399. value = "";
  400. config_set_item(&config->defaults, section, name, bstrdup(value));
  401. }
  402. void config_set_default_int(config_t *config, const char *section,
  403. const char *name, int64_t value)
  404. {
  405. struct dstr str;
  406. dstr_init(&str);
  407. dstr_printf(&str, "%lld", value);
  408. config_set_item(&config->defaults, section, name, str.array);
  409. }
  410. void config_set_default_uint(config_t *config, const char *section,
  411. const char *name, uint64_t value)
  412. {
  413. struct dstr str;
  414. dstr_init(&str);
  415. dstr_printf(&str, "%llu", value);
  416. config_set_item(&config->defaults, section, name, str.array);
  417. }
  418. void config_set_default_bool(config_t *config, const char *section,
  419. const char *name, bool value)
  420. {
  421. char *str = bstrdup(value ? "true" : "false");
  422. config_set_item(&config->defaults, section, name, str);
  423. }
  424. void config_set_default_double(config_t *config, const char *section,
  425. const char *name, double value)
  426. {
  427. struct dstr str;
  428. dstr_init(&str);
  429. dstr_printf(&str, "%g", value);
  430. config_set_item(&config->defaults, section, name, str.array);
  431. }
  432. const char *config_get_string(const config_t *config, const char *section,
  433. const char *name)
  434. {
  435. const struct config_item *item = config_find_item(&config->sections,
  436. section, name);
  437. if (!item)
  438. item = config_find_item(&config->defaults, section, name);
  439. if (!item)
  440. return NULL;
  441. return item->value;
  442. }
  443. static inline int64_t str_to_int64(const char *str)
  444. {
  445. if (!str || !*str)
  446. return 0;
  447. if (str[0] == '0' && str[1] == 'x')
  448. return strtoll(str + 2, NULL, 16);
  449. else
  450. return strtoll(str, NULL, 10);
  451. }
  452. static inline uint64_t str_to_uint64(const char *str)
  453. {
  454. if (!str || !*str)
  455. return 0;
  456. if (str[0] == '0' && str[1] == 'x')
  457. return strtoull(str + 2, NULL, 16);
  458. else
  459. return strtoull(str, NULL, 10);
  460. }
  461. int64_t config_get_int(const config_t *config, const char *section,
  462. const char *name)
  463. {
  464. const char *value = config_get_string(config, section, name);
  465. if (value)
  466. return str_to_int64(value);
  467. return 0;
  468. }
  469. uint64_t config_get_uint(const config_t *config, const char *section,
  470. const char *name)
  471. {
  472. const char *value = config_get_string(config, section, name);
  473. if (value)
  474. return str_to_uint64(value);
  475. return 0;
  476. }
  477. bool config_get_bool(const config_t *config, const char *section,
  478. const char *name)
  479. {
  480. const char *value = config_get_string(config, section, name);
  481. if (value)
  482. return astrcmpi(value, "true") == 0 ||
  483. !!str_to_uint64(value);
  484. return false;
  485. }
  486. double config_get_double(const config_t *config, const char *section,
  487. const char *name)
  488. {
  489. const char *value = config_get_string(config, section, name);
  490. if (value)
  491. return os_strtod(value);
  492. return 0.0;
  493. }
  494. const char *config_get_default_string(const config_t *config,
  495. const char *section, const char *name)
  496. {
  497. const struct config_item *item;
  498. item = config_find_item(&config->defaults, section, name);
  499. if (!item)
  500. return NULL;
  501. return item->value;
  502. }
  503. int64_t config_get_default_int(const config_t *config, const char *section,
  504. const char *name)
  505. {
  506. const char *value = config_get_default_string(config, section, name);
  507. if (value)
  508. return str_to_int64(value);
  509. return 0;
  510. }
  511. uint64_t config_get_default_uint(const config_t *config, const char *section,
  512. const char *name)
  513. {
  514. const char *value = config_get_default_string(config, section, name);
  515. if (value)
  516. return str_to_uint64(value);
  517. return 0;
  518. }
  519. bool config_get_default_bool(const config_t *config, const char *section,
  520. const char *name)
  521. {
  522. const char *value = config_get_default_string(config, section, name);
  523. if (value)
  524. return astrcmpi(value, "true") == 0 ||
  525. !!str_to_uint64(value);
  526. return false;
  527. }
  528. double config_get_default_double(const config_t *config, const char *section,
  529. const char *name)
  530. {
  531. const char *value = config_get_default_string(config, section, name);
  532. if (value)
  533. return os_strtod(value);
  534. return 0.0;
  535. }
  536. bool config_has_user_value(const config_t *config, const char *section,
  537. const char *name)
  538. {
  539. return config_find_item(&config->sections, section, name) != NULL;
  540. }
  541. bool config_has_default_value(const config_t *config, const char *section,
  542. const char *name)
  543. {
  544. return config_find_item(&config->defaults, section, name) != NULL;
  545. }