config-file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 <inttypes.h>
  17. #include <stdio.h>
  18. #include <wchar.h>
  19. #include "config-file.h"
  20. #include "threading.h"
  21. #include "platform.h"
  22. #include "base.h"
  23. #include "bmem.h"
  24. #include "darray.h"
  25. #include "lexer.h"
  26. #include "dstr.h"
  27. struct config_item {
  28. char *name;
  29. char *value;
  30. };
  31. static inline void config_item_free(struct config_item *item)
  32. {
  33. bfree(item->name);
  34. bfree(item->value);
  35. }
  36. struct config_section {
  37. char *name;
  38. struct darray items; /* struct config_item */
  39. };
  40. static inline void config_section_free(struct config_section *section)
  41. {
  42. struct config_item *items = section->items.array;
  43. size_t i;
  44. for (i = 0; i < section->items.num; i++)
  45. config_item_free(items + i);
  46. darray_free(&section->items);
  47. bfree(section->name);
  48. }
  49. struct config_data {
  50. char *file;
  51. struct darray sections; /* struct config_section */
  52. struct darray defaults; /* struct config_section */
  53. pthread_mutex_t mutex;
  54. };
  55. config_t *config_create(const char *file)
  56. {
  57. struct config_data *config;
  58. FILE *f;
  59. f = os_fopen(file, "wb");
  60. if (!f)
  61. return NULL;
  62. fclose(f);
  63. config = bzalloc(sizeof(struct config_data));
  64. if (pthread_mutex_init_recursive(&config->mutex) != 0) {
  65. bfree(config);
  66. return NULL;
  67. }
  68. config->file = bstrdup(file);
  69. return config;
  70. }
  71. static inline void remove_ref_whitespace(struct strref *ref)
  72. {
  73. if (ref->array) {
  74. while (ref->len && is_whitespace(*ref->array)) {
  75. ref->array++;
  76. ref->len--;
  77. }
  78. while (ref->len && is_whitespace(ref->array[ref->len - 1]))
  79. ref->len--;
  80. }
  81. }
  82. static bool config_parse_string(struct lexer *lex, struct strref *ref, char end)
  83. {
  84. bool success = end != 0;
  85. struct base_token token;
  86. base_token_clear(&token);
  87. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  88. if (end) {
  89. if (*token.text.array == end) {
  90. success = true;
  91. break;
  92. } else if (is_newline(*token.text.array)) {
  93. success = false;
  94. break;
  95. }
  96. } else {
  97. if (is_newline(*token.text.array)) {
  98. success = true;
  99. break;
  100. }
  101. }
  102. strref_add(ref, &token.text);
  103. }
  104. //remove_ref_whitespace(ref);
  105. return success;
  106. }
  107. static void unescape(struct dstr *str)
  108. {
  109. char *read = str->array;
  110. char *write = str->array;
  111. for (; *read; read++, write++) {
  112. char cur = *read;
  113. if (cur == '\\') {
  114. char next = read[1];
  115. if (next == '\\') {
  116. read++;
  117. } else if (next == 'r') {
  118. cur = '\r';
  119. read++;
  120. } else if (next == 'n') {
  121. cur = '\n';
  122. read++;
  123. }
  124. }
  125. if (read != write)
  126. *write = cur;
  127. }
  128. if (read != write)
  129. *write = '\0';
  130. }
  131. static void config_add_item(struct darray *items, struct strref *name,
  132. struct strref *value)
  133. {
  134. struct config_item item;
  135. struct dstr item_value;
  136. dstr_init_copy_strref(&item_value, value);
  137. unescape(&item_value);
  138. item.name = bstrdup_n(name->array, name->len);
  139. item.value = item_value.array;
  140. darray_push_back(sizeof(struct config_item), items, &item);
  141. }
  142. static void config_parse_section(struct config_section *section,
  143. struct lexer *lex)
  144. {
  145. struct base_token token;
  146. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  147. struct strref name, value;
  148. while (token.type == BASETOKEN_WHITESPACE) {
  149. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  150. return;
  151. }
  152. if (token.type == BASETOKEN_OTHER) {
  153. if (*token.text.array == '#') {
  154. do {
  155. if (!lexer_getbasetoken(
  156. lex, &token,
  157. PARSE_WHITESPACE))
  158. return;
  159. } while (!is_newline(*token.text.array));
  160. continue;
  161. } else if (*token.text.array == '[') {
  162. lex->offset--;
  163. return;
  164. }
  165. }
  166. strref_copy(&name, &token.text);
  167. if (!config_parse_string(lex, &name, '='))
  168. continue;
  169. strref_clear(&value);
  170. config_parse_string(lex, &value, 0);
  171. if (strref_is_empty(&value)) {
  172. struct config_item item;
  173. item.name = bstrdup_n(name.array, name.len);
  174. item.value = bzalloc(1);
  175. darray_push_back(sizeof(struct config_item),
  176. &section->items, &item);
  177. } else {
  178. config_add_item(&section->items, &name, &value);
  179. }
  180. }
  181. }
  182. static void parse_config_data(struct darray *sections, struct lexer *lex)
  183. {
  184. struct strref section_name;
  185. struct base_token token;
  186. base_token_clear(&token);
  187. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  188. struct config_section *section;
  189. while (token.type == BASETOKEN_WHITESPACE) {
  190. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  191. return;
  192. }
  193. if (*token.text.array != '[') {
  194. while (!is_newline(*token.text.array)) {
  195. if (!lexer_getbasetoken(lex, &token,
  196. PARSE_WHITESPACE))
  197. return;
  198. }
  199. continue;
  200. }
  201. strref_clear(&section_name);
  202. config_parse_string(lex, &section_name, ']');
  203. if (!section_name.len)
  204. return;
  205. section = darray_push_back_new(sizeof(struct config_section),
  206. sections);
  207. section->name = bstrdup_n(section_name.array, section_name.len);
  208. config_parse_section(section, lex);
  209. }
  210. }
  211. static int config_parse_file(struct darray *sections, const char *file,
  212. bool always_open)
  213. {
  214. char *file_data;
  215. struct lexer lex;
  216. FILE *f;
  217. f = os_fopen(file, "rb");
  218. if (always_open && !f)
  219. f = os_fopen(file, "w+");
  220. if (!f)
  221. return CONFIG_FILENOTFOUND;
  222. os_fread_utf8(f, &file_data);
  223. fclose(f);
  224. if (!file_data)
  225. return CONFIG_SUCCESS;
  226. lexer_init(&lex);
  227. lexer_start_move(&lex, file_data);
  228. parse_config_data(sections, &lex);
  229. lexer_free(&lex);
  230. return CONFIG_SUCCESS;
  231. }
  232. int config_open(config_t **config, const char *file,
  233. enum config_open_type open_type)
  234. {
  235. int errorcode;
  236. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  237. if (!config)
  238. return CONFIG_ERROR;
  239. *config = bzalloc(sizeof(struct config_data));
  240. if (!*config)
  241. return CONFIG_ERROR;
  242. if (pthread_mutex_init_recursive(&(*config)->mutex) != 0) {
  243. bfree(*config);
  244. return CONFIG_ERROR;
  245. }
  246. (*config)->file = bstrdup(file);
  247. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  248. if (errorcode != CONFIG_SUCCESS) {
  249. config_close(*config);
  250. *config = NULL;
  251. }
  252. return errorcode;
  253. }
  254. int config_open_string(config_t **config, const char *str)
  255. {
  256. struct lexer lex;
  257. if (!config)
  258. return CONFIG_ERROR;
  259. *config = bzalloc(sizeof(struct config_data));
  260. if (!*config)
  261. return CONFIG_ERROR;
  262. if (pthread_mutex_init_recursive(&(*config)->mutex) != 0) {
  263. bfree(*config);
  264. return CONFIG_ERROR;
  265. }
  266. (*config)->file = NULL;
  267. lexer_init(&lex);
  268. lexer_start(&lex, str);
  269. parse_config_data(&(*config)->sections, &lex);
  270. lexer_free(&lex);
  271. return CONFIG_SUCCESS;
  272. }
  273. int config_open_defaults(config_t *config, const char *file)
  274. {
  275. if (!config)
  276. return CONFIG_ERROR;
  277. return config_parse_file(&config->defaults, file, false);
  278. }
  279. int config_save(config_t *config)
  280. {
  281. FILE *f;
  282. struct dstr str, tmp;
  283. size_t i, j;
  284. int ret = CONFIG_ERROR;
  285. if (!config)
  286. return CONFIG_ERROR;
  287. if (!config->file)
  288. return CONFIG_ERROR;
  289. dstr_init(&str);
  290. dstr_init(&tmp);
  291. pthread_mutex_lock(&config->mutex);
  292. f = os_fopen(config->file, "wb");
  293. if (!f) {
  294. pthread_mutex_unlock(&config->mutex);
  295. return CONFIG_FILENOTFOUND;
  296. }
  297. for (i = 0; i < config->sections.num; i++) {
  298. struct config_section *section = darray_item(
  299. sizeof(struct config_section), &config->sections, i);
  300. if (i)
  301. dstr_cat(&str, "\n");
  302. dstr_cat(&str, "[");
  303. dstr_cat(&str, section->name);
  304. dstr_cat(&str, "]\n");
  305. for (j = 0; j < section->items.num; j++) {
  306. struct config_item *item = darray_item(
  307. sizeof(struct config_item), &section->items, j);
  308. dstr_copy(&tmp, item->value ? item->value : "");
  309. dstr_replace(&tmp, "\\", "\\\\");
  310. dstr_replace(&tmp, "\r", "\\r");
  311. dstr_replace(&tmp, "\n", "\\n");
  312. dstr_cat(&str, item->name);
  313. dstr_cat(&str, "=");
  314. dstr_cat(&str, tmp.array);
  315. dstr_cat(&str, "\n");
  316. }
  317. }
  318. #ifdef _WIN32
  319. if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1)
  320. goto cleanup;
  321. #endif
  322. if (fwrite(str.array, str.len, 1, f) != 1)
  323. goto cleanup;
  324. ret = CONFIG_SUCCESS;
  325. cleanup:
  326. fclose(f);
  327. pthread_mutex_unlock(&config->mutex);
  328. dstr_free(&tmp);
  329. dstr_free(&str);
  330. return ret;
  331. }
  332. int config_save_safe(config_t *config, const char *temp_ext,
  333. const char *backup_ext)
  334. {
  335. struct dstr temp_file = {0};
  336. struct dstr backup_file = {0};
  337. char *file = config->file;
  338. int ret;
  339. if (!temp_ext || !*temp_ext) {
  340. blog(LOG_ERROR, "config_save_safe: invalid "
  341. "temporary extension specified");
  342. return CONFIG_ERROR;
  343. }
  344. pthread_mutex_lock(&config->mutex);
  345. dstr_copy(&temp_file, config->file);
  346. if (*temp_ext != '.')
  347. dstr_cat(&temp_file, ".");
  348. dstr_cat(&temp_file, temp_ext);
  349. config->file = temp_file.array;
  350. ret = config_save(config);
  351. config->file = file;
  352. if (ret != CONFIG_SUCCESS) {
  353. blog(LOG_ERROR,
  354. "config_save_safe: failed to "
  355. "write to %s",
  356. temp_file.array);
  357. goto cleanup;
  358. }
  359. if (backup_ext && *backup_ext) {
  360. dstr_copy(&backup_file, config->file);
  361. if (*backup_ext != '.')
  362. dstr_cat(&backup_file, ".");
  363. dstr_cat(&backup_file, backup_ext);
  364. }
  365. if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
  366. ret = CONFIG_ERROR;
  367. cleanup:
  368. pthread_mutex_unlock(&config->mutex);
  369. dstr_free(&temp_file);
  370. dstr_free(&backup_file);
  371. return ret;
  372. }
  373. void config_close(config_t *config)
  374. {
  375. struct config_section *defaults, *sections;
  376. size_t i;
  377. if (!config)
  378. return;
  379. defaults = config->defaults.array;
  380. sections = config->sections.array;
  381. for (i = 0; i < config->defaults.num; i++)
  382. config_section_free(defaults + i);
  383. for (i = 0; i < config->sections.num; i++)
  384. config_section_free(sections + i);
  385. darray_free(&config->defaults);
  386. darray_free(&config->sections);
  387. bfree(config->file);
  388. pthread_mutex_destroy(&config->mutex);
  389. bfree(config);
  390. }
  391. size_t config_num_sections(config_t *config)
  392. {
  393. return config->sections.num;
  394. }
  395. const char *config_get_section(config_t *config, size_t idx)
  396. {
  397. struct config_section *section;
  398. const char *name = NULL;
  399. pthread_mutex_lock(&config->mutex);
  400. if (idx >= config->sections.num)
  401. goto unlock;
  402. section = darray_item(sizeof(struct config_section), &config->sections,
  403. idx);
  404. name = section->name;
  405. unlock:
  406. pthread_mutex_unlock(&config->mutex);
  407. return name;
  408. }
  409. static const struct config_item *config_find_item(const struct darray *sections,
  410. const char *section,
  411. const char *name)
  412. {
  413. size_t i, j;
  414. for (i = 0; i < sections->num; i++) {
  415. const struct config_section *sec =
  416. darray_item(sizeof(struct config_section), sections, i);
  417. if (astrcmpi(sec->name, section) == 0) {
  418. for (j = 0; j < sec->items.num; j++) {
  419. struct config_item *item =
  420. darray_item(sizeof(struct config_item),
  421. &sec->items, j);
  422. if (astrcmpi(item->name, name) == 0)
  423. return item;
  424. }
  425. }
  426. }
  427. return NULL;
  428. }
  429. static void config_set_item(config_t *config, struct darray *sections,
  430. const char *section, const char *name, char *value)
  431. {
  432. struct config_section *sec = NULL;
  433. struct config_section *array = sections->array;
  434. struct config_item *item;
  435. size_t i, j;
  436. pthread_mutex_lock(&config->mutex);
  437. for (i = 0; i < sections->num; i++) {
  438. struct config_section *cur_sec = array + i;
  439. struct config_item *items = cur_sec->items.array;
  440. if (astrcmpi(cur_sec->name, section) == 0) {
  441. for (j = 0; j < cur_sec->items.num; j++) {
  442. item = items + j;
  443. if (astrcmpi(item->name, name) == 0) {
  444. bfree(item->value);
  445. item->value = value;
  446. goto unlock;
  447. }
  448. }
  449. sec = cur_sec;
  450. break;
  451. }
  452. }
  453. if (!sec) {
  454. sec = darray_push_back_new(sizeof(struct config_section),
  455. sections);
  456. sec->name = bstrdup(section);
  457. }
  458. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  459. item->name = bstrdup(name);
  460. item->value = value;
  461. unlock:
  462. pthread_mutex_unlock(&config->mutex);
  463. }
  464. void config_set_string(config_t *config, const char *section, const char *name,
  465. const char *value)
  466. {
  467. if (!value)
  468. value = "";
  469. config_set_item(config, &config->sections, section, name,
  470. bstrdup(value));
  471. }
  472. void config_set_int(config_t *config, const char *section, const char *name,
  473. int64_t value)
  474. {
  475. struct dstr str;
  476. dstr_init(&str);
  477. dstr_printf(&str, "%" PRId64, value);
  478. config_set_item(config, &config->sections, section, name, str.array);
  479. }
  480. void config_set_uint(config_t *config, const char *section, const char *name,
  481. uint64_t value)
  482. {
  483. struct dstr str;
  484. dstr_init(&str);
  485. dstr_printf(&str, "%" PRIu64, value);
  486. config_set_item(config, &config->sections, section, name, str.array);
  487. }
  488. void config_set_bool(config_t *config, const char *section, const char *name,
  489. bool value)
  490. {
  491. char *str = bstrdup(value ? "true" : "false");
  492. config_set_item(config, &config->sections, section, name, str);
  493. }
  494. void config_set_double(config_t *config, const char *section, const char *name,
  495. double value)
  496. {
  497. char *str = bzalloc(64);
  498. os_dtostr(value, str, 64);
  499. config_set_item(config, &config->sections, section, name, str);
  500. }
  501. void config_set_default_string(config_t *config, const char *section,
  502. const char *name, const char *value)
  503. {
  504. if (!value)
  505. value = "";
  506. config_set_item(config, &config->defaults, section, name,
  507. bstrdup(value));
  508. }
  509. void config_set_default_int(config_t *config, const char *section,
  510. const char *name, int64_t value)
  511. {
  512. struct dstr str;
  513. dstr_init(&str);
  514. dstr_printf(&str, "%" PRId64, value);
  515. config_set_item(config, &config->defaults, section, name, str.array);
  516. }
  517. void config_set_default_uint(config_t *config, const char *section,
  518. const char *name, uint64_t value)
  519. {
  520. struct dstr str;
  521. dstr_init(&str);
  522. dstr_printf(&str, "%" PRIu64, value);
  523. config_set_item(config, &config->defaults, section, name, str.array);
  524. }
  525. void config_set_default_bool(config_t *config, const char *section,
  526. const char *name, bool value)
  527. {
  528. char *str = bstrdup(value ? "true" : "false");
  529. config_set_item(config, &config->defaults, section, name, str);
  530. }
  531. void config_set_default_double(config_t *config, const char *section,
  532. const char *name, double value)
  533. {
  534. struct dstr str;
  535. dstr_init(&str);
  536. dstr_printf(&str, "%g", value);
  537. config_set_item(config, &config->defaults, section, name, str.array);
  538. }
  539. const char *config_get_string(config_t *config, const char *section,
  540. const char *name)
  541. {
  542. const struct config_item *item;
  543. const char *value = NULL;
  544. pthread_mutex_lock(&config->mutex);
  545. item = config_find_item(&config->sections, section, name);
  546. if (!item)
  547. item = config_find_item(&config->defaults, section, name);
  548. if (item)
  549. value = item->value;
  550. pthread_mutex_unlock(&config->mutex);
  551. return value;
  552. }
  553. static inline int64_t str_to_int64(const char *str)
  554. {
  555. if (!str || !*str)
  556. return 0;
  557. if (str[0] == '0' && str[1] == 'x')
  558. return strtoll(str + 2, NULL, 16);
  559. else
  560. return strtoll(str, NULL, 10);
  561. }
  562. static inline uint64_t str_to_uint64(const char *str)
  563. {
  564. if (!str || !*str)
  565. return 0;
  566. if (str[0] == '0' && str[1] == 'x')
  567. return strtoull(str + 2, NULL, 16);
  568. else
  569. return strtoull(str, NULL, 10);
  570. }
  571. int64_t config_get_int(config_t *config, const char *section, const char *name)
  572. {
  573. const char *value = config_get_string(config, section, name);
  574. if (value)
  575. return str_to_int64(value);
  576. return 0;
  577. }
  578. uint64_t config_get_uint(config_t *config, const char *section,
  579. const char *name)
  580. {
  581. const char *value = config_get_string(config, section, name);
  582. if (value)
  583. return str_to_uint64(value);
  584. return 0;
  585. }
  586. bool config_get_bool(config_t *config, const char *section, const char *name)
  587. {
  588. const char *value = config_get_string(config, section, name);
  589. if (value)
  590. return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  591. return false;
  592. }
  593. double config_get_double(config_t *config, const char *section,
  594. const char *name)
  595. {
  596. const char *value = config_get_string(config, section, name);
  597. if (value)
  598. return os_strtod(value);
  599. return 0.0;
  600. }
  601. bool config_remove_value(config_t *config, const char *section,
  602. const char *name)
  603. {
  604. struct darray *sections = &config->sections;
  605. bool success = false;
  606. pthread_mutex_lock(&config->mutex);
  607. for (size_t i = 0; i < sections->num; i++) {
  608. struct config_section *sec =
  609. darray_item(sizeof(struct config_section), sections, i);
  610. if (astrcmpi(sec->name, section) != 0)
  611. continue;
  612. for (size_t j = 0; j < sec->items.num; j++) {
  613. struct config_item *item = darray_item(
  614. sizeof(struct config_item), &sec->items, j);
  615. if (astrcmpi(item->name, name) == 0) {
  616. config_item_free(item);
  617. darray_erase(sizeof(struct config_item),
  618. &sec->items, j);
  619. success = true;
  620. goto unlock;
  621. }
  622. }
  623. }
  624. unlock:
  625. pthread_mutex_unlock(&config->mutex);
  626. return success;
  627. }
  628. const char *config_get_default_string(config_t *config, const char *section,
  629. const char *name)
  630. {
  631. const struct config_item *item;
  632. const char *value = NULL;
  633. pthread_mutex_lock(&config->mutex);
  634. item = config_find_item(&config->defaults, section, name);
  635. if (item)
  636. value = item->value;
  637. pthread_mutex_unlock(&config->mutex);
  638. return value;
  639. }
  640. int64_t config_get_default_int(config_t *config, const char *section,
  641. const char *name)
  642. {
  643. const char *value = config_get_default_string(config, section, name);
  644. if (value)
  645. return str_to_int64(value);
  646. return 0;
  647. }
  648. uint64_t config_get_default_uint(config_t *config, const char *section,
  649. const char *name)
  650. {
  651. const char *value = config_get_default_string(config, section, name);
  652. if (value)
  653. return str_to_uint64(value);
  654. return 0;
  655. }
  656. bool config_get_default_bool(config_t *config, const char *section,
  657. const char *name)
  658. {
  659. const char *value = config_get_default_string(config, section, name);
  660. if (value)
  661. return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  662. return false;
  663. }
  664. double config_get_default_double(config_t *config, const char *section,
  665. const char *name)
  666. {
  667. const char *value = config_get_default_string(config, section, name);
  668. if (value)
  669. return os_strtod(value);
  670. return 0.0;
  671. }
  672. bool config_has_user_value(config_t *config, const char *section,
  673. const char *name)
  674. {
  675. bool success;
  676. pthread_mutex_lock(&config->mutex);
  677. success = config_find_item(&config->sections, section, name) != NULL;
  678. pthread_mutex_unlock(&config->mutex);
  679. return success;
  680. }
  681. bool config_has_default_value(config_t *config, const char *section,
  682. const char *name)
  683. {
  684. bool success;
  685. pthread_mutex_lock(&config->mutex);
  686. success = config_find_item(&config->defaults, section, name) != NULL;
  687. pthread_mutex_unlock(&config->mutex);
  688. return success;
  689. }