config-file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 (ref->len && 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, char end)
  92. {
  93. bool success = end != 0;
  94. struct base_token token;
  95. base_token_clear(&token);
  96. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  97. if (end) {
  98. if (*token.text.array == end) {
  99. success = true;
  100. break;
  101. } else if (is_newline(*token.text.array)) {
  102. success = false;
  103. break;
  104. }
  105. } else {
  106. if (is_newline(*token.text.array)) {
  107. success = true;
  108. break;
  109. }
  110. }
  111. strref_add(ref, &token.text);
  112. }
  113. //remove_ref_whitespace(ref);
  114. return success;
  115. }
  116. static void unescape(struct dstr *str)
  117. {
  118. char *read = str->array;
  119. char *write = str->array;
  120. for (; *read; read++, write++) {
  121. char cur = *read;
  122. if (cur == '\\') {
  123. char next = read[1];
  124. if (next == '\\') {
  125. read++;
  126. } else if (next == 'r') {
  127. cur = '\r';
  128. read++;
  129. } else if (next == 'n') {
  130. cur = '\n';
  131. read++;
  132. }
  133. }
  134. if (read != write)
  135. *write = cur;
  136. }
  137. if (read != write)
  138. *write = '\0';
  139. }
  140. static void config_add_item(struct darray *items, struct strref *name,
  141. struct strref *value)
  142. {
  143. struct config_item item;
  144. struct dstr item_value;
  145. dstr_init_copy_strref(&item_value, value);
  146. unescape(&item_value);
  147. item.name = bstrdup_n(name->array, name->len);
  148. item.value = item_value.array;
  149. darray_push_back(sizeof(struct config_item), items, &item);
  150. }
  151. static void config_parse_section(struct config_section *section,
  152. struct lexer *lex)
  153. {
  154. struct base_token token;
  155. while (lexer_getbasetoken(lex, &token, PARSE_WHITESPACE)) {
  156. struct strref name, value;
  157. while (token.type == BASETOKEN_WHITESPACE) {
  158. if (!lexer_getbasetoken(lex, &token, PARSE_WHITESPACE))
  159. return;
  160. }
  161. if (token.type == BASETOKEN_OTHER) {
  162. if (*token.text.array == '#') {
  163. do {
  164. if (!lexer_getbasetoken(
  165. 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, section_name.len);
  217. config_parse_section(section, lex);
  218. }
  219. }
  220. static int config_parse_file(struct darray *sections, const char *file,
  221. bool always_open)
  222. {
  223. char *file_data;
  224. struct lexer lex;
  225. FILE *f;
  226. f = os_fopen(file, "rb");
  227. if (always_open && !f)
  228. f = os_fopen(file, "w+");
  229. if (!f)
  230. return CONFIG_FILENOTFOUND;
  231. os_fread_utf8(f, &file_data);
  232. fclose(f);
  233. if (!file_data)
  234. return CONFIG_SUCCESS;
  235. lexer_init(&lex);
  236. lexer_start_move(&lex, file_data);
  237. parse_config_data(sections, &lex);
  238. lexer_free(&lex);
  239. return CONFIG_SUCCESS;
  240. }
  241. int config_open(config_t **config, const char *file,
  242. enum config_open_type open_type)
  243. {
  244. int errorcode;
  245. bool always_open = open_type == CONFIG_OPEN_ALWAYS;
  246. if (!config)
  247. return CONFIG_ERROR;
  248. *config = bzalloc(sizeof(struct config_data));
  249. if (!*config)
  250. return CONFIG_ERROR;
  251. if (!init_mutex(*config)) {
  252. bfree(*config);
  253. return CONFIG_ERROR;
  254. }
  255. (*config)->file = bstrdup(file);
  256. errorcode = config_parse_file(&(*config)->sections, file, always_open);
  257. if (errorcode != CONFIG_SUCCESS) {
  258. config_close(*config);
  259. *config = NULL;
  260. }
  261. return errorcode;
  262. }
  263. int config_open_string(config_t **config, const char *str)
  264. {
  265. struct lexer lex;
  266. if (!config)
  267. return CONFIG_ERROR;
  268. *config = bzalloc(sizeof(struct config_data));
  269. if (!*config)
  270. return CONFIG_ERROR;
  271. if (!init_mutex(*config)) {
  272. bfree(*config);
  273. return CONFIG_ERROR;
  274. }
  275. (*config)->file = NULL;
  276. lexer_init(&lex);
  277. lexer_start(&lex, str);
  278. parse_config_data(&(*config)->sections, &lex);
  279. lexer_free(&lex);
  280. return CONFIG_SUCCESS;
  281. }
  282. int config_open_defaults(config_t *config, const char *file)
  283. {
  284. if (!config)
  285. return CONFIG_ERROR;
  286. return config_parse_file(&config->defaults, file, false);
  287. }
  288. int config_save(config_t *config)
  289. {
  290. FILE *f;
  291. struct dstr str, tmp;
  292. size_t i, j;
  293. int ret = CONFIG_ERROR;
  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), &config->sections, i);
  309. if (i)
  310. 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), &section->items, j);
  317. dstr_copy(&tmp, item->value ? item->value : "");
  318. dstr_replace(&tmp, "\\", "\\\\");
  319. dstr_replace(&tmp, "\r", "\\r");
  320. dstr_replace(&tmp, "\n", "\\n");
  321. dstr_cat(&str, item->name);
  322. dstr_cat(&str, "=");
  323. dstr_cat(&str, tmp.array);
  324. dstr_cat(&str, "\n");
  325. }
  326. }
  327. #ifdef _WIN32
  328. if (fwrite("\xEF\xBB\xBF", 3, 1, f) != 1)
  329. goto cleanup;
  330. #endif
  331. if (fwrite(str.array, str.len, 1, f) != 1)
  332. goto cleanup;
  333. ret = CONFIG_SUCCESS;
  334. cleanup:
  335. fclose(f);
  336. pthread_mutex_unlock(&config->mutex);
  337. dstr_free(&tmp);
  338. dstr_free(&str);
  339. return ret;
  340. }
  341. int config_save_safe(config_t *config, const char *temp_ext,
  342. const char *backup_ext)
  343. {
  344. struct dstr temp_file = {0};
  345. struct dstr backup_file = {0};
  346. char *file = config->file;
  347. int ret;
  348. if (!temp_ext || !*temp_ext) {
  349. blog(LOG_ERROR, "config_save_safe: invalid "
  350. "temporary extension specified");
  351. return CONFIG_ERROR;
  352. }
  353. pthread_mutex_lock(&config->mutex);
  354. dstr_copy(&temp_file, config->file);
  355. if (*temp_ext != '.')
  356. dstr_cat(&temp_file, ".");
  357. dstr_cat(&temp_file, temp_ext);
  358. config->file = temp_file.array;
  359. ret = config_save(config);
  360. config->file = file;
  361. if (ret != CONFIG_SUCCESS) {
  362. blog(LOG_ERROR,
  363. "config_save_safe: failed to "
  364. "write to %s",
  365. temp_file.array);
  366. goto cleanup;
  367. }
  368. if (backup_ext && *backup_ext) {
  369. dstr_copy(&backup_file, config->file);
  370. if (*backup_ext != '.')
  371. dstr_cat(&backup_file, ".");
  372. dstr_cat(&backup_file, backup_ext);
  373. }
  374. if (os_safe_replace(file, temp_file.array, backup_file.array) != 0)
  375. ret = CONFIG_ERROR;
  376. cleanup:
  377. pthread_mutex_unlock(&config->mutex);
  378. dstr_free(&temp_file);
  379. dstr_free(&backup_file);
  380. return ret;
  381. }
  382. void config_close(config_t *config)
  383. {
  384. struct config_section *defaults, *sections;
  385. size_t i;
  386. if (!config)
  387. return;
  388. defaults = config->defaults.array;
  389. sections = config->sections.array;
  390. for (i = 0; i < config->defaults.num; i++)
  391. config_section_free(defaults + i);
  392. for (i = 0; i < config->sections.num; i++)
  393. config_section_free(sections + i);
  394. darray_free(&config->defaults);
  395. darray_free(&config->sections);
  396. bfree(config->file);
  397. pthread_mutex_destroy(&config->mutex);
  398. bfree(config);
  399. }
  400. size_t config_num_sections(config_t *config)
  401. {
  402. return config->sections.num;
  403. }
  404. const char *config_get_section(config_t *config, size_t idx)
  405. {
  406. struct config_section *section;
  407. const char *name = NULL;
  408. pthread_mutex_lock(&config->mutex);
  409. if (idx >= config->sections.num)
  410. goto unlock;
  411. section = darray_item(sizeof(struct config_section), &config->sections,
  412. idx);
  413. name = section->name;
  414. unlock:
  415. pthread_mutex_unlock(&config->mutex);
  416. return name;
  417. }
  418. static const struct config_item *config_find_item(const struct darray *sections,
  419. const char *section,
  420. const char *name)
  421. {
  422. size_t i, j;
  423. for (i = 0; i < sections->num; i++) {
  424. const struct config_section *sec =
  425. darray_item(sizeof(struct config_section), sections, i);
  426. if (astrcmpi(sec->name, section) == 0) {
  427. for (j = 0; j < sec->items.num; j++) {
  428. struct config_item *item =
  429. darray_item(sizeof(struct config_item),
  430. &sec->items, j);
  431. if (astrcmpi(item->name, name) == 0)
  432. return item;
  433. }
  434. }
  435. }
  436. return NULL;
  437. }
  438. static void config_set_item(config_t *config, struct darray *sections,
  439. const char *section, const char *name, char *value)
  440. {
  441. struct config_section *sec = NULL;
  442. struct config_section *array = sections->array;
  443. struct config_item *item;
  444. size_t i, j;
  445. pthread_mutex_lock(&config->mutex);
  446. for (i = 0; i < sections->num; i++) {
  447. struct config_section *cur_sec = array + i;
  448. struct config_item *items = cur_sec->items.array;
  449. if (astrcmpi(cur_sec->name, section) == 0) {
  450. for (j = 0; j < cur_sec->items.num; j++) {
  451. item = items + j;
  452. if (astrcmpi(item->name, name) == 0) {
  453. bfree(item->value);
  454. item->value = value;
  455. goto unlock;
  456. }
  457. }
  458. sec = cur_sec;
  459. break;
  460. }
  461. }
  462. if (!sec) {
  463. sec = darray_push_back_new(sizeof(struct config_section),
  464. sections);
  465. sec->name = bstrdup(section);
  466. }
  467. item = darray_push_back_new(sizeof(struct config_item), &sec->items);
  468. item->name = bstrdup(name);
  469. item->value = value;
  470. unlock:
  471. pthread_mutex_unlock(&config->mutex);
  472. }
  473. void config_set_string(config_t *config, const char *section, const char *name,
  474. const char *value)
  475. {
  476. if (!value)
  477. value = "";
  478. config_set_item(config, &config->sections, section, name,
  479. bstrdup(value));
  480. }
  481. void config_set_int(config_t *config, const char *section, const char *name,
  482. int64_t value)
  483. {
  484. struct dstr str;
  485. dstr_init(&str);
  486. dstr_printf(&str, "%" PRId64, value);
  487. config_set_item(config, &config->sections, section, name, str.array);
  488. }
  489. void config_set_uint(config_t *config, const char *section, const char *name,
  490. uint64_t value)
  491. {
  492. struct dstr str;
  493. dstr_init(&str);
  494. dstr_printf(&str, "%" PRIu64, value);
  495. config_set_item(config, &config->sections, section, name, str.array);
  496. }
  497. void config_set_bool(config_t *config, const char *section, const char *name,
  498. bool value)
  499. {
  500. char *str = bstrdup(value ? "true" : "false");
  501. config_set_item(config, &config->sections, section, name, str);
  502. }
  503. void config_set_double(config_t *config, const char *section, const char *name,
  504. double value)
  505. {
  506. char *str = bzalloc(64);
  507. os_dtostr(value, str, 64);
  508. config_set_item(config, &config->sections, section, name, str);
  509. }
  510. void config_set_default_string(config_t *config, const char *section,
  511. const char *name, const char *value)
  512. {
  513. if (!value)
  514. value = "";
  515. config_set_item(config, &config->defaults, section, name,
  516. bstrdup(value));
  517. }
  518. void config_set_default_int(config_t *config, const char *section,
  519. const char *name, int64_t value)
  520. {
  521. struct dstr str;
  522. dstr_init(&str);
  523. dstr_printf(&str, "%" PRId64, value);
  524. config_set_item(config, &config->defaults, section, name, str.array);
  525. }
  526. void config_set_default_uint(config_t *config, const char *section,
  527. const char *name, uint64_t value)
  528. {
  529. struct dstr str;
  530. dstr_init(&str);
  531. dstr_printf(&str, "%" PRIu64, value);
  532. config_set_item(config, &config->defaults, section, name, str.array);
  533. }
  534. void config_set_default_bool(config_t *config, const char *section,
  535. const char *name, bool value)
  536. {
  537. char *str = bstrdup(value ? "true" : "false");
  538. config_set_item(config, &config->defaults, section, name, str);
  539. }
  540. void config_set_default_double(config_t *config, const char *section,
  541. const char *name, double value)
  542. {
  543. struct dstr str;
  544. dstr_init(&str);
  545. dstr_printf(&str, "%g", value);
  546. config_set_item(config, &config->defaults, section, name, str.array);
  547. }
  548. const char *config_get_string(config_t *config, const char *section,
  549. const char *name)
  550. {
  551. const struct config_item *item;
  552. const char *value = NULL;
  553. pthread_mutex_lock(&config->mutex);
  554. item = config_find_item(&config->sections, section, name);
  555. if (!item)
  556. item = config_find_item(&config->defaults, section, name);
  557. if (item)
  558. value = item->value;
  559. pthread_mutex_unlock(&config->mutex);
  560. return value;
  561. }
  562. static inline int64_t str_to_int64(const char *str)
  563. {
  564. if (!str || !*str)
  565. return 0;
  566. if (str[0] == '0' && str[1] == 'x')
  567. return strtoll(str + 2, NULL, 16);
  568. else
  569. return strtoll(str, NULL, 10);
  570. }
  571. static inline uint64_t str_to_uint64(const char *str)
  572. {
  573. if (!str || !*str)
  574. return 0;
  575. if (str[0] == '0' && str[1] == 'x')
  576. return strtoull(str + 2, NULL, 16);
  577. else
  578. return strtoull(str, NULL, 10);
  579. }
  580. int64_t config_get_int(config_t *config, const char *section, const char *name)
  581. {
  582. const char *value = config_get_string(config, section, name);
  583. if (value)
  584. return str_to_int64(value);
  585. return 0;
  586. }
  587. uint64_t config_get_uint(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 str_to_uint64(value);
  593. return 0;
  594. }
  595. bool config_get_bool(config_t *config, const char *section, const char *name)
  596. {
  597. const char *value = config_get_string(config, section, name);
  598. if (value)
  599. return astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  600. return false;
  601. }
  602. double config_get_double(config_t *config, const char *section,
  603. const char *name)
  604. {
  605. const char *value = config_get_string(config, section, name);
  606. if (value)
  607. return os_strtod(value);
  608. return 0.0;
  609. }
  610. bool config_remove_value(config_t *config, const char *section,
  611. const char *name)
  612. {
  613. struct darray *sections = &config->sections;
  614. bool success = false;
  615. pthread_mutex_lock(&config->mutex);
  616. for (size_t i = 0; i < sections->num; i++) {
  617. struct config_section *sec =
  618. darray_item(sizeof(struct config_section), sections, i);
  619. if (astrcmpi(sec->name, section) != 0)
  620. continue;
  621. for (size_t j = 0; j < sec->items.num; j++) {
  622. struct config_item *item = darray_item(
  623. sizeof(struct config_item), &sec->items, j);
  624. if (astrcmpi(item->name, name) == 0) {
  625. config_item_free(item);
  626. darray_erase(sizeof(struct config_item),
  627. &sec->items, j);
  628. success = true;
  629. goto unlock;
  630. }
  631. }
  632. }
  633. unlock:
  634. pthread_mutex_unlock(&config->mutex);
  635. return success;
  636. }
  637. const char *config_get_default_string(config_t *config, const char *section,
  638. const char *name)
  639. {
  640. const struct config_item *item;
  641. const char *value = NULL;
  642. pthread_mutex_lock(&config->mutex);
  643. item = config_find_item(&config->defaults, section, name);
  644. if (item)
  645. value = item->value;
  646. pthread_mutex_unlock(&config->mutex);
  647. return value;
  648. }
  649. int64_t config_get_default_int(config_t *config, const char *section,
  650. const char *name)
  651. {
  652. const char *value = config_get_default_string(config, section, name);
  653. if (value)
  654. return str_to_int64(value);
  655. return 0;
  656. }
  657. uint64_t config_get_default_uint(config_t *config, const char *section,
  658. const char *name)
  659. {
  660. const char *value = config_get_default_string(config, section, name);
  661. if (value)
  662. return str_to_uint64(value);
  663. return 0;
  664. }
  665. bool config_get_default_bool(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 astrcmpi(value, "true") == 0 || !!str_to_uint64(value);
  671. return false;
  672. }
  673. double config_get_default_double(config_t *config, const char *section,
  674. const char *name)
  675. {
  676. const char *value = config_get_default_string(config, section, name);
  677. if (value)
  678. return os_strtod(value);
  679. return 0.0;
  680. }
  681. bool config_has_user_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->sections, section, name) != NULL;
  687. pthread_mutex_unlock(&config->mutex);
  688. return success;
  689. }
  690. bool config_has_default_value(config_t *config, const char *section,
  691. const char *name)
  692. {
  693. bool success;
  694. pthread_mutex_lock(&config->mutex);
  695. success = config_find_item(&config->defaults, section, name) != NULL;
  696. pthread_mutex_unlock(&config->mutex);
  697. return success;
  698. }