config-file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 int config_parse(struct darray *sections, const char *file,
  140. bool always_open)
  141. {
  142. char *file_data;
  143. struct lexer lex;
  144. struct base_token token;
  145. struct strref section_name;
  146. FILE *f;
  147. f = os_fopen(file, "rb");
  148. if (always_open && !f)
  149. f = os_fopen(file, "w+");
  150. if (!f)
  151. return CONFIG_FILENOTFOUND;
  152. os_fread_utf8(f, &file_data);
  153. fclose(f);
  154. if (!file_data)
  155. return CONFIG_SUCCESS;
  156. lexer_init(&lex);
  157. lexer_start_move(&lex, file_data);
  158. base_token_clear(&token);
  159. while (lexer_getbasetoken(&lex, &token, PARSE_WHITESPACE)) {
  160. struct config_section *section;
  161. while (token.type == BASETOKEN_WHITESPACE) {
  162. if (!lexer_getbasetoken(&lex, &token, PARSE_WHITESPACE))
  163. goto complete;
  164. }
  165. if (*token.text.array != '[') {
  166. while (!is_newline(*token.text.array)) {
  167. if (!lexer_getbasetoken(&lex, &token,
  168. PARSE_WHITESPACE))
  169. goto complete;
  170. }
  171. continue;
  172. }
  173. strref_clear(&section_name);
  174. config_parse_string(&lex, &section_name, ']');
  175. if (!section_name.len)
  176. break;
  177. section = darray_push_back_new(sizeof(struct config_section),
  178. sections);
  179. section->name = bstrdup_n(section_name.array,
  180. section_name.len);
  181. config_parse_section(section, &lex);
  182. }
  183. complete:
  184. lexer_free(&lex);
  185. return CONFIG_SUCCESS;
  186. }
  187. int config_open(config_t *config, const char *file,
  188. enum config_open_type open_type)
  189. {
  190. int errorcode;
  191. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  192. if (!config)
  193. return CONFIG_ERROR;
  194. *config = bzalloc(sizeof(struct config_data));
  195. (*config)->file = bstrdup(file);
  196. errorcode = config_parse(&(*config)->sections, file, always_open);
  197. if (errorcode != CONFIG_SUCCESS) {
  198. config_close(*config);
  199. *config = NULL;
  200. }
  201. return errorcode;
  202. }
  203. int config_open_defaults(config_t config, const char *file)
  204. {
  205. if (!config)
  206. return CONFIG_ERROR;
  207. return config_parse(&config->defaults, file, false);
  208. }
  209. int config_save(config_t config)
  210. {
  211. FILE *f;
  212. struct dstr str;
  213. size_t i, j;
  214. if (!config)
  215. return CONFIG_ERROR;
  216. dstr_init(&str);
  217. f = os_fopen(config->file, "wb");
  218. if (!f)
  219. return CONFIG_FILENOTFOUND;
  220. for (i = 0; i < config->sections.num; i++) {
  221. struct config_section *section = darray_item(
  222. sizeof(struct config_section),
  223. &config->sections, i);
  224. if (i) dstr_cat(&str, "\n");
  225. dstr_cat(&str, "[");
  226. dstr_cat(&str, section->name);
  227. dstr_cat(&str, "]\n");
  228. for (j = 0; j < section->items.num; j++) {
  229. struct config_item *item = darray_item(
  230. sizeof(struct config_item),
  231. &section->items, j);
  232. dstr_cat(&str, item->name);
  233. dstr_cat(&str, "=");
  234. dstr_cat(&str, item->value);
  235. dstr_cat(&str, "\n");
  236. }
  237. }
  238. #ifdef _WIN32
  239. fwrite("\xEF\xBB\xBF", 1, 3, f);
  240. #endif
  241. fwrite(str.array, 1, str.len, f);
  242. fclose(f);
  243. dstr_free(&str);
  244. return CONFIG_SUCCESS;
  245. }
  246. void config_close(config_t config)
  247. {
  248. struct config_section *defaults, *sections;
  249. size_t i;
  250. if (!config) return;
  251. defaults = config->defaults.array;
  252. sections = config->sections.array;
  253. for (i = 0; i < config->defaults.num; i++)
  254. config_section_free(defaults+i);
  255. for (i = 0; i < config->sections.num; i++)
  256. config_section_free(sections+i);
  257. darray_free(&config->defaults);
  258. darray_free(&config->sections);
  259. bfree(config->file);
  260. bfree(config);
  261. }
  262. size_t config_num_sections(config_t config)
  263. {
  264. return config->sections.num;
  265. }
  266. const char *config_get_section(config_t config, size_t idx)
  267. {
  268. struct config_section *section;
  269. if (idx >= config->sections.num)
  270. return NULL;
  271. section = darray_item(sizeof(struct config_section), &config->sections,
  272. idx);
  273. return section->name;
  274. }
  275. static struct config_item *config_find_item(struct darray *sections,
  276. const char *section, const char *name)
  277. {
  278. size_t i, j;
  279. for (i = 0; i < sections->num; i++) {
  280. struct config_section *sec = darray_item(
  281. sizeof(struct config_section), sections, i);
  282. if (astrcmpi(sec->name, section) == 0) {
  283. for (j = 0; j < sec->items.num; j++) {
  284. struct config_item *item = darray_item(
  285. sizeof(struct config_item),
  286. &sec->items, j);
  287. if (astrcmpi(item->name, name) == 0)
  288. return item;
  289. }
  290. }
  291. }
  292. return NULL;
  293. }
  294. static void config_set_item(struct darray *sections, const char *section,
  295. const char *name, char *value)
  296. {
  297. struct config_section *sec = NULL;
  298. struct config_section *array = sections->array;
  299. struct config_item *item;
  300. size_t i, j;
  301. for (i = 0; i < sections->num; i++) {
  302. struct config_section *cur_sec = array+i;
  303. struct config_item *items = cur_sec->items.array;
  304. if (astrcmpi(cur_sec->name, section) == 0) {
  305. for (j = 0; j < cur_sec->items.num; j++) {
  306. item = items+j;
  307. if (astrcmpi(item->name, name) == 0) {
  308. bfree(item->value);
  309. item->value = value;
  310. return;
  311. }
  312. }
  313. sec = cur_sec;
  314. break;
  315. }
  316. }
  317. if (!sec) {
  318. sec = darray_push_back_new(sizeof(struct config_section),
  319. sections);
  320. sec->name = bstrdup(section);
  321. }
  322. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  323. item->name = bstrdup(name);
  324. item->value = value;
  325. }
  326. void config_set_string(config_t config, const char *section,
  327. const char *name, const char *value)
  328. {
  329. if (!value)
  330. value = "";
  331. config_set_item(&config->sections, section, name, bstrdup(value));
  332. }
  333. void config_set_int(config_t config, const char *section,
  334. const char *name, int64_t value)
  335. {
  336. struct dstr str;
  337. dstr_init(&str);
  338. dstr_printf(&str, "%lld", value);
  339. config_set_item(&config->sections, section, name, str.array);
  340. }
  341. void config_set_uint(config_t config, const char *section,
  342. const char *name, uint64_t value)
  343. {
  344. struct dstr str;
  345. dstr_init(&str);
  346. dstr_printf(&str, "%llu", value);
  347. config_set_item(&config->sections, section, name, str.array);
  348. }
  349. void config_set_bool(config_t config, const char *section,
  350. const char *name, bool value)
  351. {
  352. char *str = bstrdup(value ? "true" : "false");
  353. config_set_item(&config->sections, section, name, str);
  354. }
  355. void config_set_double(config_t config, const char *section,
  356. const char *name, double value)
  357. {
  358. struct dstr str;
  359. dstr_init(&str);
  360. dstr_printf(&str, "%g", value);
  361. config_set_item(&config->sections, section, name, str.array);
  362. }
  363. void config_set_default_string(config_t config, const char *section,
  364. const char *name, const char *value)
  365. {
  366. if (!value)
  367. value = "";
  368. config_set_item(&config->defaults, section, name, bstrdup(value));
  369. }
  370. void config_set_default_int(config_t config, const char *section,
  371. const char *name, int64_t value)
  372. {
  373. struct dstr str;
  374. dstr_init(&str);
  375. dstr_printf(&str, "%lld", value);
  376. config_set_item(&config->defaults, section, name, str.array);
  377. }
  378. void config_set_default_uint(config_t config, const char *section,
  379. const char *name, uint64_t value)
  380. {
  381. struct dstr str;
  382. dstr_init(&str);
  383. dstr_printf(&str, "%llu", value);
  384. config_set_item(&config->defaults, section, name, str.array);
  385. }
  386. void config_set_default_bool(config_t config, const char *section,
  387. const char *name, bool value)
  388. {
  389. char *str = bstrdup(value ? "true" : "false");
  390. config_set_item(&config->defaults, section, name, str);
  391. }
  392. void config_set_default_double(config_t config, const char *section,
  393. const char *name, double value)
  394. {
  395. struct dstr str;
  396. dstr_init(&str);
  397. dstr_printf(&str, "%g", value);
  398. config_set_item(&config->defaults, section, name, str.array);
  399. }
  400. const char *config_get_string(config_t config, const char *section,
  401. const char *name)
  402. {
  403. struct config_item *item = config_find_item(&config->sections,
  404. section, name);
  405. if (!item)
  406. item = config_find_item(&config->defaults, section, name);
  407. if (!item)
  408. return NULL;
  409. return item->value;
  410. }
  411. int64_t config_get_int(config_t config, const char *section,
  412. const char *name)
  413. {
  414. const char *value = config_get_string(config, section, name);
  415. if (value)
  416. return strtoll(value, NULL, 10);
  417. return 0;
  418. }
  419. uint64_t config_get_uint(config_t config, const char *section,
  420. const char *name)
  421. {
  422. const char *value = config_get_string(config, section, name);
  423. if (value)
  424. return strtoul(value, NULL, 10);
  425. return 0;
  426. }
  427. bool config_get_bool(config_t config, const char *section,
  428. const char *name)
  429. {
  430. const char *value = config_get_string(config, section, name);
  431. if (value)
  432. return astrcmpi(value, "true") == 0 ||
  433. strtoul(value, NULL, 10);
  434. return false;
  435. }
  436. double config_get_double(config_t config, const char *section,
  437. const char *name)
  438. {
  439. const char *value = config_get_string(config, section, name);
  440. if (value)
  441. return strtod(value, NULL);
  442. return 0.0;
  443. }