config-file.c 14 KB

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