1
0

config-file.c 18 KB

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