config-file.c 18 KB

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