config-file.c 12 KB

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