config-file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. struct config_item item;
  182. item.name = bstrdup_n(name.array, name.len);
  183. item.value = bzalloc(1);
  184. darray_push_back(sizeof(struct config_item),
  185. &section->items, &item);
  186. } else {
  187. config_add_item(&section->items, &name, &value);
  188. }
  189. }
  190. }
  191. static void parse_config_data(struct darray *sections, struct lexer *lex)
  192. {
  193. struct strref section_name;
  194. struct base_token token;
  195. base_token_clear(&token);
  196. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  197. struct config_section *section;
  198. while (token.type == BASETOKEN_WHITESPACE) {
  199. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  200. return;
  201. }
  202. if (*token.text.array != '[') {
  203. while (!is_newline(*token.text.array)) {
  204. if (!lexer_getbasetoken(lex, &token,
  205. PARSE_WHITESPACE))
  206. return;
  207. }
  208. continue;
  209. }
  210. strref_clear(&section_name);
  211. config_parse_string(lex, &section_name, ']');
  212. if (!section_name.len)
  213. return;
  214. section = darray_push_back_new(sizeof(struct config_section),
  215. sections);
  216. section->name = bstrdup_n(section_name.array,
  217. section_name.len);
  218. config_parse_section(section, lex);
  219. }
  220. }
  221. static int config_parse_file(struct darray *sections, const char *file,
  222. bool always_open)
  223. {
  224. char *file_data;
  225. struct lexer lex;
  226. FILE *f;
  227. f = os_fopen(file, "rb");
  228. if (always_open && !f)
  229. f = os_fopen(file, "w+");
  230. if (!f)
  231. return CONFIG_FILENOTFOUND;
  232. os_fread_utf8(f, &file_data);
  233. fclose(f);
  234. if (!file_data)
  235. return CONFIG_SUCCESS;
  236. lexer_init(&lex);
  237. lexer_start_move(&lex, file_data);
  238. parse_config_data(sections, &lex);
  239. lexer_free(&lex);
  240. return CONFIG_SUCCESS;
  241. }
  242. int config_open(config_t **config, const char *file,
  243. enum config_open_type open_type)
  244. {
  245. int errorcode;
  246. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  247. if (!config)
  248. return CONFIG_ERROR;
  249. *config = bzalloc(sizeof(struct config_data));
  250. if (!*config)
  251. return CONFIG_ERROR;
  252. if (!init_mutex(*config)) {
  253. bfree(*config);
  254. return CONFIG_ERROR;
  255. }
  256. (*config)->file = bstrdup(file);
  257. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  258. if (errorcode != CONFIG_SUCCESS) {
  259. config_close(*config);
  260. *config = NULL;
  261. }
  262. return errorcode;
  263. }
  264. int config_open_string(config_t **config, const char *str)
  265. {
  266. struct lexer lex;
  267. if (!config)
  268. return CONFIG_ERROR;
  269. *config = bzalloc(sizeof(struct config_data));
  270. if (!*config)
  271. return CONFIG_ERROR;
  272. if (!init_mutex(*config)) {
  273. bfree(*config);
  274. return CONFIG_ERROR;
  275. }
  276. (*config)->file = NULL;
  277. lexer_init(&lex);
  278. lexer_start(&lex, str);
  279. parse_config_data(&(*config)->sections, &lex);
  280. lexer_free(&lex);
  281. return CONFIG_SUCCESS;
  282. }
  283. int config_open_defaults(config_t *config, const char *file)
  284. {
  285. if (!config)
  286. return CONFIG_ERROR;
  287. return config_parse_file(&config->defaults, file, false);
  288. }
  289. int config_save(config_t *config)
  290. {
  291. FILE *f;
  292. struct dstr str, tmp;
  293. size_t i, j;
  294. if (!config)
  295. return CONFIG_ERROR;
  296. if (!config->file)
  297. return CONFIG_ERROR;
  298. dstr_init(&str);
  299. dstr_init(&tmp);
  300. pthread_mutex_lock(&config->mutex);
  301. f = os_fopen(config->file, "wb");
  302. if (!f) {
  303. pthread_mutex_unlock(&config->mutex);
  304. return CONFIG_FILENOTFOUND;
  305. }
  306. for (i = 0; i < config->sections.num; i++) {
  307. struct config_section *section = darray_item(
  308. sizeof(struct config_section),
  309. &config->sections, i);
  310. if (i) dstr_cat(&str, "\n");
  311. dstr_cat(&str, "[");
  312. dstr_cat(&str, section->name);
  313. dstr_cat(&str, "]\n");
  314. for (j = 0; j < section->items.num; j++) {
  315. struct config_item *item = darray_item(
  316. sizeof(struct config_item),
  317. &section->items, j);
  318. dstr_copy(&tmp, item->value ? item->value : "");
  319. dstr_replace(&tmp, "\\", "\\\\");
  320. dstr_replace(&tmp, "\r", "\\r");
  321. dstr_replace(&tmp, "\n", "\\n");
  322. dstr_cat(&str, item->name);
  323. dstr_cat(&str, "=");
  324. dstr_cat(&str, tmp.array);
  325. dstr_cat(&str, "\n");
  326. }
  327. }
  328. #ifdef _WIN32
  329. fwrite("\xEF\xBB\xBF", 1, 3, f);
  330. #endif
  331. fwrite(str.array, 1, str.len, f);
  332. fclose(f);
  333. pthread_mutex_unlock(&config->mutex);
  334. dstr_free(&tmp);
  335. dstr_free(&str);
  336. return CONFIG_SUCCESS;
  337. }
  338. int config_save_safe(config_t *config, const char *temp_ext,
  339. const char *backup_ext)
  340. {
  341. struct dstr temp_file = {0};
  342. struct dstr backup_file = {0};
  343. char *file = config->file;
  344. int ret;
  345. if (!temp_ext || !*temp_ext) {
  346. blog(LOG_ERROR, "config_save_safe: invalid "
  347. "temporary extension specified");
  348. return CONFIG_ERROR;
  349. }
  350. pthread_mutex_lock(&config->mutex);
  351. dstr_copy(&temp_file, config->file);
  352. if (*temp_ext != '.')
  353. dstr_cat(&temp_file, ".");
  354. dstr_cat(&temp_file, temp_ext);
  355. config->file = temp_file.array;
  356. ret = config_save(config);
  357. config->file = file;
  358. if (ret != CONFIG_SUCCESS) {
  359. goto cleanup;
  360. }
  361. if (backup_ext && *backup_ext) {
  362. dstr_copy(&backup_file, config->file);
  363. if (*backup_ext != '.')
  364. dstr_cat(&backup_file, ".");
  365. dstr_cat(&backup_file, backup_ext);
  366. }
  367. if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
  368. ret = CONFIG_ERROR;
  369. cleanup:
  370. pthread_mutex_unlock(&config->mutex);
  371. dstr_free(&temp_file);
  372. dstr_free(&backup_file);
  373. return ret;
  374. }
  375. void config_close(config_t *config)
  376. {
  377. struct config_section *defaults, *sections;
  378. size_t i;
  379. if (!config) return;
  380. defaults = config->defaults.array;
  381. sections = config->sections.array;
  382. for (i = 0; i < config->defaults.num; i++)
  383. config_section_free(defaults+i);
  384. for (i = 0; i < config->sections.num; i++)
  385. config_section_free(sections+i);
  386. darray_free(&config->defaults);
  387. darray_free(&config->sections);
  388. bfree(config->file);
  389. pthread_mutex_destroy(&config->mutex);
  390. bfree(config);
  391. }
  392. size_t config_num_sections(config_t *config)
  393. {
  394. return config->sections.num;
  395. }
  396. const char *config_get_section(config_t *config, size_t idx)
  397. {
  398. struct config_section *section;
  399. const char *name = NULL;
  400. pthread_mutex_lock(&config->mutex);
  401. if (idx >= config->sections.num)
  402. goto unlock;
  403. section = darray_item(sizeof(struct config_section), &config->sections,
  404. idx);
  405. name = section->name;
  406. unlock:
  407. pthread_mutex_unlock(&config->mutex);
  408. return name;
  409. }
  410. static const struct config_item *config_find_item(const struct darray *sections,
  411. const char *section, const char *name)
  412. {
  413. size_t i, j;
  414. for (i = 0; i < sections->num; i++) {
  415. const struct config_section *sec = darray_item(
  416. 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 = darray_item(
  420. 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,
  465. const char *name, 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,
  473. const char *name, 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,
  481. const char *name, 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,
  489. const char *name, 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,
  495. const char *name, 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,
  572. const char *name)
  573. {
  574. const char *value = config_get_string(config, section, name);
  575. if (value)
  576. return str_to_int64(value);
  577. return 0;
  578. }
  579. uint64_t config_get_uint(config_t *config, const char *section,
  580. const char *name)
  581. {
  582. const char *value = config_get_string(config, section, name);
  583. if (value)
  584. return str_to_uint64(value);
  585. return 0;
  586. }
  587. bool config_get_bool(config_t *config, const char *section,
  588. const char *name)
  589. {
  590. const char *value = config_get_string(config, section, name);
  591. if (value)
  592. return astrcmpi(value, "true") == 0 ||
  593. !!str_to_uint64(value);
  594. return false;
  595. }
  596. double config_get_double(config_t *config, const char *section,
  597. const char *name)
  598. {
  599. const char *value = config_get_string(config, section, name);
  600. if (value)
  601. return os_strtod(value);
  602. return 0.0;
  603. }
  604. bool config_remove_value(config_t *config, const char *section,
  605. const char *name)
  606. {
  607. struct darray *sections = &config->sections;
  608. bool success = false;
  609. pthread_mutex_lock(&config->mutex);
  610. for (size_t i = 0; i < sections->num; i++) {
  611. struct config_section *sec = darray_item(
  612. sizeof(struct config_section), sections, i);
  613. if (astrcmpi(sec->name, section) != 0)
  614. continue;
  615. for (size_t j = 0; j < sec->items.num; j++) {
  616. struct config_item *item = darray_item(
  617. sizeof(struct config_item),
  618. &sec->items, j);
  619. if (astrcmpi(item->name, name) == 0) {
  620. config_item_free(item);
  621. darray_erase(sizeof(struct config_item),
  622. &sec->items, j);
  623. success = true;
  624. goto unlock;
  625. }
  626. }
  627. }
  628. unlock:
  629. pthread_mutex_unlock(&config->mutex);
  630. return success;
  631. }
  632. const char *config_get_default_string(config_t *config,
  633. const char *section, const char *name)
  634. {
  635. const struct config_item *item;
  636. const char *value = NULL;
  637. pthread_mutex_lock(&config->mutex);
  638. item = config_find_item(&config->defaults, section, name);
  639. if (item)
  640. value = item->value;
  641. pthread_mutex_unlock(&config->mutex);
  642. return value;
  643. }
  644. int64_t config_get_default_int(config_t *config, const char *section,
  645. const char *name)
  646. {
  647. const char *value = config_get_default_string(config, section, name);
  648. if (value)
  649. return str_to_int64(value);
  650. return 0;
  651. }
  652. uint64_t config_get_default_uint(config_t *config, const char *section,
  653. const char *name)
  654. {
  655. const char *value = config_get_default_string(config, section, name);
  656. if (value)
  657. return str_to_uint64(value);
  658. return 0;
  659. }
  660. bool config_get_default_bool(config_t *config, const char *section,
  661. const char *name)
  662. {
  663. const char *value = config_get_default_string(config, section, name);
  664. if (value)
  665. return astrcmpi(value, "true") == 0 ||
  666. !!str_to_uint64(value);
  667. return false;
  668. }
  669. double config_get_default_double(config_t *config, const char *section,
  670. const char *name)
  671. {
  672. const char *value = config_get_default_string(config, section, name);
  673. if (value)
  674. return os_strtod(value);
  675. return 0.0;
  676. }
  677. bool config_has_user_value(config_t *config, const char *section,
  678. const char *name)
  679. {
  680. bool success;
  681. pthread_mutex_lock(&config->mutex);
  682. success = config_find_item(&config->sections, section, name) != NULL;
  683. pthread_mutex_unlock(&config->mutex);
  684. return success;
  685. }
  686. bool config_has_default_value(config_t *config, const char *section,
  687. const char *name)
  688. {
  689. bool success;
  690. pthread_mutex_lock(&config->mutex);
  691. success = config_find_item(&config->defaults, section, name) != NULL;
  692. pthread_mutex_unlock(&config->mutex);
  693. return success;
  694. }