config-file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. }
  360. if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
  361. ret = CONFIG_ERROR;
  362. cleanup:
  363. pthread_mutex_unlock(&config->mutex);
  364. dstr_free(&temp_file);
  365. dstr_free(&backup_file);
  366. return ret;
  367. }
  368. void config_close(config_t *config)
  369. {
  370. struct config_section *defaults, *sections;
  371. size_t i;
  372. if (!config) return;
  373. defaults = config->defaults.array;
  374. sections = config->sections.array;
  375. for (i = 0; i < config->defaults.num; i++)
  376. config_section_free(defaults+i);
  377. for (i = 0; i < config->sections.num; i++)
  378. config_section_free(sections+i);
  379. darray_free(&config->defaults);
  380. darray_free(&config->sections);
  381. bfree(config->file);
  382. pthread_mutex_destroy(&config->mutex);
  383. bfree(config);
  384. }
  385. size_t config_num_sections(config_t *config)
  386. {
  387. return config->sections.num;
  388. }
  389. const char *config_get_section(config_t *config, size_t idx)
  390. {
  391. struct config_section *section;
  392. const char *name = NULL;
  393. pthread_mutex_lock(&config->mutex);
  394. if (idx >= config->sections.num)
  395. goto unlock;
  396. section = darray_item(sizeof(struct config_section), &config->sections,
  397. idx);
  398. name = section->name;
  399. unlock:
  400. pthread_mutex_unlock(&config->mutex);
  401. return name;
  402. }
  403. static const struct config_item *config_find_item(const struct darray *sections,
  404. const char *section, const char *name)
  405. {
  406. size_t i, j;
  407. for (i = 0; i < sections->num; i++) {
  408. const struct config_section *sec = darray_item(
  409. sizeof(struct config_section), sections, i);
  410. if (astrcmpi(sec->name, section) == 0) {
  411. for (j = 0; j < sec->items.num; j++) {
  412. struct config_item *item = darray_item(
  413. sizeof(struct config_item),
  414. &sec->items, j);
  415. if (astrcmpi(item->name, name) == 0)
  416. return item;
  417. }
  418. }
  419. }
  420. return NULL;
  421. }
  422. static void config_set_item(config_t *config, struct darray *sections,
  423. const char *section, const char *name, char *value)
  424. {
  425. struct config_section *sec = NULL;
  426. struct config_section *array = sections->array;
  427. struct config_item *item;
  428. size_t i, j;
  429. pthread_mutex_lock(&config->mutex);
  430. for (i = 0; i < sections->num; i++) {
  431. struct config_section *cur_sec = array+i;
  432. struct config_item *items = cur_sec->items.array;
  433. if (astrcmpi(cur_sec->name, section) == 0) {
  434. for (j = 0; j < cur_sec->items.num; j++) {
  435. item = items+j;
  436. if (astrcmpi(item->name, name) == 0) {
  437. bfree(item->value);
  438. item->value = value;
  439. goto unlock;
  440. }
  441. }
  442. sec = cur_sec;
  443. break;
  444. }
  445. }
  446. if (!sec) {
  447. sec = darray_push_back_new(sizeof(struct config_section),
  448. sections);
  449. sec->name = bstrdup(section);
  450. }
  451. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  452. item->name = bstrdup(name);
  453. item->value = value;
  454. unlock:
  455. pthread_mutex_unlock(&config->mutex);
  456. }
  457. void config_set_string(config_t *config, const char *section,
  458. const char *name, const char *value)
  459. {
  460. if (!value)
  461. value = "";
  462. config_set_item(config, &config->sections, section, name,
  463. bstrdup(value));
  464. }
  465. void config_set_int(config_t *config, const char *section,
  466. const char *name, int64_t value)
  467. {
  468. struct dstr str;
  469. dstr_init(&str);
  470. dstr_printf(&str, "%"PRId64, value);
  471. config_set_item(config, &config->sections, section, name, str.array);
  472. }
  473. void config_set_uint(config_t *config, const char *section,
  474. const char *name, uint64_t value)
  475. {
  476. struct dstr str;
  477. dstr_init(&str);
  478. dstr_printf(&str, "%"PRIu64, value);
  479. config_set_item(config, &config->sections, section, name, str.array);
  480. }
  481. void config_set_bool(config_t *config, const char *section,
  482. const char *name, bool value)
  483. {
  484. char *str = bstrdup(value ? "true" : "false");
  485. config_set_item(config, &config->sections, section, name, str);
  486. }
  487. void config_set_double(config_t *config, const char *section,
  488. const char *name, double value)
  489. {
  490. char *str = bzalloc(64);
  491. os_dtostr(value, str, 64);
  492. config_set_item(config, &config->sections, section, name, str);
  493. }
  494. void config_set_default_string(config_t *config, const char *section,
  495. const char *name, const char *value)
  496. {
  497. if (!value)
  498. value = "";
  499. config_set_item(config, &config->defaults, section, name,
  500. bstrdup(value));
  501. }
  502. void config_set_default_int(config_t *config, const char *section,
  503. const char *name, int64_t value)
  504. {
  505. struct dstr str;
  506. dstr_init(&str);
  507. dstr_printf(&str, "%"PRId64, value);
  508. config_set_item(config, &config->defaults, section, name, str.array);
  509. }
  510. void config_set_default_uint(config_t *config, const char *section,
  511. const char *name, uint64_t value)
  512. {
  513. struct dstr str;
  514. dstr_init(&str);
  515. dstr_printf(&str, "%"PRIu64, value);
  516. config_set_item(config, &config->defaults, section, name, str.array);
  517. }
  518. void config_set_default_bool(config_t *config, const char *section,
  519. const char *name, bool value)
  520. {
  521. char *str = bstrdup(value ? "true" : "false");
  522. config_set_item(config, &config->defaults, section, name, str);
  523. }
  524. void config_set_default_double(config_t *config, const char *section,
  525. const char *name, double value)
  526. {
  527. struct dstr str;
  528. dstr_init(&str);
  529. dstr_printf(&str, "%g", value);
  530. config_set_item(config, &config->defaults, section, name, str.array);
  531. }
  532. const char *config_get_string(config_t *config, const char *section,
  533. const char *name)
  534. {
  535. const struct config_item *item;
  536. const char *value = NULL;
  537. pthread_mutex_lock(&config->mutex);
  538. item = config_find_item(&config->sections, section, name);
  539. if (!item)
  540. item = config_find_item(&config->defaults, section, name);
  541. if (item)
  542. value = item->value;
  543. pthread_mutex_unlock(&config->mutex);
  544. return value;
  545. }
  546. static inline int64_t str_to_int64(const char *str)
  547. {
  548. if (!str || !*str)
  549. return 0;
  550. if (str[0] == '0' && str[1] == 'x')
  551. return strtoll(str + 2, NULL, 16);
  552. else
  553. return strtoll(str, NULL, 10);
  554. }
  555. static inline uint64_t str_to_uint64(const char *str)
  556. {
  557. if (!str || !*str)
  558. return 0;
  559. if (str[0] == '0' && str[1] == 'x')
  560. return strtoull(str + 2, NULL, 16);
  561. else
  562. return strtoull(str, NULL, 10);
  563. }
  564. int64_t config_get_int(config_t *config, const char *section,
  565. const char *name)
  566. {
  567. const char *value = config_get_string(config, section, name);
  568. if (value)
  569. return str_to_int64(value);
  570. return 0;
  571. }
  572. uint64_t config_get_uint(config_t *config, const char *section,
  573. const char *name)
  574. {
  575. const char *value = config_get_string(config, section, name);
  576. if (value)
  577. return str_to_uint64(value);
  578. return 0;
  579. }
  580. bool config_get_bool(config_t *config, const char *section,
  581. const char *name)
  582. {
  583. const char *value = config_get_string(config, section, name);
  584. if (value)
  585. return astrcmpi(value, "true") == 0 ||
  586. !!str_to_uint64(value);
  587. return false;
  588. }
  589. double config_get_double(config_t *config, const char *section,
  590. const char *name)
  591. {
  592. const char *value = config_get_string(config, section, name);
  593. if (value)
  594. return os_strtod(value);
  595. return 0.0;
  596. }
  597. bool config_remove_value(config_t *config, const char *section,
  598. const char *name)
  599. {
  600. struct darray *sections = &config->sections;
  601. bool success = false;
  602. pthread_mutex_lock(&config->mutex);
  603. for (size_t i = 0; i < sections->num; i++) {
  604. struct config_section *sec = darray_item(
  605. sizeof(struct config_section), sections, i);
  606. if (astrcmpi(sec->name, section) != 0)
  607. continue;
  608. for (size_t j = 0; j < sec->items.num; j++) {
  609. struct config_item *item = darray_item(
  610. sizeof(struct config_item),
  611. &sec->items, j);
  612. if (astrcmpi(item->name, name) == 0) {
  613. config_item_free(item);
  614. darray_erase(sizeof(struct config_item),
  615. &sec->items, j);
  616. success = true;
  617. goto unlock;
  618. }
  619. }
  620. }
  621. unlock:
  622. pthread_mutex_unlock(&config->mutex);
  623. return success;
  624. }
  625. const char *config_get_default_string(config_t *config,
  626. const char *section, const char *name)
  627. {
  628. const struct config_item *item;
  629. const char *value = NULL;
  630. pthread_mutex_lock(&config->mutex);
  631. item = config_find_item(&config->defaults, section, name);
  632. if (item)
  633. value = item->value;
  634. pthread_mutex_unlock(&config->mutex);
  635. return value;
  636. }
  637. int64_t config_get_default_int(config_t *config, const char *section,
  638. const char *name)
  639. {
  640. const char *value = config_get_default_string(config, section, name);
  641. if (value)
  642. return str_to_int64(value);
  643. return 0;
  644. }
  645. uint64_t config_get_default_uint(config_t *config, const char *section,
  646. const char *name)
  647. {
  648. const char *value = config_get_default_string(config, section, name);
  649. if (value)
  650. return str_to_uint64(value);
  651. return 0;
  652. }
  653. bool config_get_default_bool(config_t *config, const char *section,
  654. const char *name)
  655. {
  656. const char *value = config_get_default_string(config, section, name);
  657. if (value)
  658. return astrcmpi(value, "true") == 0 ||
  659. !!str_to_uint64(value);
  660. return false;
  661. }
  662. double config_get_default_double(config_t *config, const char *section,
  663. const char *name)
  664. {
  665. const char *value = config_get_default_string(config, section, name);
  666. if (value)
  667. return os_strtod(value);
  668. return 0.0;
  669. }
  670. bool config_has_user_value(config_t *config, const char *section,
  671. const char *name)
  672. {
  673. bool success;
  674. pthread_mutex_lock(&config->mutex);
  675. success = config_find_item(&config->sections, section, name) != NULL;
  676. pthread_mutex_unlock(&config->mutex);
  677. return success;
  678. }
  679. bool config_has_default_value(config_t *config, const char *section,
  680. const char *name)
  681. {
  682. bool success;
  683. pthread_mutex_lock(&config->mutex);
  684. success = config_find_item(&config->defaults, section, name) != NULL;
  685. pthread_mutex_unlock(&config->mutex);
  686. return success;
  687. }