libconfig.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  1. /* ----------------------------------------------------------------------------
  2. libconfig - A library for processing structured configuration files
  3. Copyright (C) 2005-2010 Mark A Lindner
  4. This file is part of libconfig.
  5. This library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this library. If not, see <http://www.gnu.org/licenses/>.
  15. ----------------------------------------------------------------------------
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "ac_config.h"
  19. #endif
  20. #include "libconfig.h"
  21. #include "grammar.h"
  22. #include "scanner.h"
  23. #include "scanctx.h"
  24. #include "parsectx.h"
  25. #include "wincompat.h"
  26. #include <locale.h>
  27. #ifdef HAVE_XLOCALE_H
  28. #include <xlocale.h>
  29. #endif
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #define PATH_TOKENS ":./"
  34. #define CHUNK_SIZE 16
  35. #define FLOAT_PRECISION 10
  36. #define _new(T) (T *)calloc(sizeof(T), 1) /* zeroed */
  37. #define _delete(P) free((void *)(P))
  38. /* ------------------------------------------------------------------------- */
  39. #ifndef LIBCONFIG_STATIC
  40. #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
  41. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  42. {
  43. return(TRUE);
  44. }
  45. #endif /* WIN32 */
  46. #endif /* LIBCONFIG_STATIC */
  47. /* ------------------------------------------------------------------------- */
  48. static const char *__io_error = "file I/O error";
  49. static void __config_list_destroy(config_list_t *list);
  50. static void __config_write_setting(const config_setting_t *setting,
  51. FILE *stream, int depth,
  52. unsigned short tab_width);
  53. extern int libconfig_yyparse(void *scanner, struct parse_context *ctx,
  54. struct scan_context *scan_ctx);
  55. extern int libconfig_yylex_init_extra(struct scan_context *scan_ctx,
  56. yyscan_t *scanner);
  57. /* ------------------------------------------------------------------------- */
  58. static void __config_locale_override(void)
  59. {
  60. #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \
  61. && ! defined(__MINGW32__)
  62. _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
  63. setlocale(LC_NUMERIC, "C");
  64. #elif defined(__APPLE__)
  65. locale_t loc = newlocale(LC_NUMERIC_MASK, "C", NULL);
  66. uselocale(loc);
  67. #elif ((defined HAVE_NEWLOCALE) && (defined HAVE_USELOCALE))
  68. locale_t loc = newlocale(LC_NUMERIC, "C", NULL);
  69. uselocale(loc);
  70. #else
  71. /* locale overriding is pretty pointless (rathena doesn't make use of the area that uses locale functionality), but I'm actually removing it because it floods the buildbot with warnings */
  72. //#warning "No way to modify calling thread's locale!"
  73. #endif
  74. }
  75. /* ------------------------------------------------------------------------- */
  76. static void __config_locale_restore(void)
  77. {
  78. #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \
  79. && ! defined(__MINGW32__)
  80. _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
  81. #elif ((defined HAVE_USELOCALE) && (defined HAVE_FREELOCALE))
  82. locale_t loc = uselocale(LC_GLOBAL_LOCALE);
  83. freelocale(loc);
  84. #else
  85. /* locale overriding is pretty pointless (rathena doesn't make use of the area that uses locale functionality), but I'm actually removing it because it floods the buildbot with warnings */
  86. //#warning "No way to modify calling thread's locale!"
  87. #endif
  88. }
  89. /* ------------------------------------------------------------------------- */
  90. static int __config_name_compare(const char *a, const char *b)
  91. {
  92. const char *p, *q;
  93. for(p = a, q = b; ; p++, q++)
  94. {
  95. int pd = ((! *p) || strchr(PATH_TOKENS, *p));
  96. int qd = ((! *q) || strchr(PATH_TOKENS, *q));
  97. if(pd && qd)
  98. break;
  99. else if(pd)
  100. return(-1);
  101. else if(qd)
  102. return(1);
  103. else if(*p < *q)
  104. return(-1);
  105. else if(*p > *q)
  106. return(1);
  107. }
  108. return(0);
  109. }
  110. /* ------------------------------------------------------------------------- */
  111. static void __config_indent(FILE *stream, int depth, unsigned short w)
  112. {
  113. if(w)
  114. fprintf(stream, "%*s", (depth - 1) * w, " ");
  115. else
  116. {
  117. int i;
  118. for(i = 0; i < (depth - 1); ++i)
  119. fputc('\t', stream);
  120. }
  121. }
  122. /* ------------------------------------------------------------------------- */
  123. static void __config_write_value(const config_value_t *value, int type,
  124. int format, int depth,
  125. unsigned short tab_width, FILE *stream)
  126. {
  127. char fbuf[64];
  128. switch(type)
  129. {
  130. /* boolean */
  131. case CONFIG_TYPE_BOOL:
  132. fputs(value->ival ? "true" : "false", stream);
  133. break;
  134. /* int */
  135. case CONFIG_TYPE_INT:
  136. switch(format)
  137. {
  138. case CONFIG_FORMAT_HEX:
  139. fprintf(stream, "0x%X", value->ival);
  140. break;
  141. case CONFIG_FORMAT_DEFAULT:
  142. default:
  143. fprintf(stream, "%d", value->ival);
  144. break;
  145. }
  146. break;
  147. /* 64-bit int */
  148. case CONFIG_TYPE_INT64:
  149. switch(format)
  150. {
  151. case CONFIG_FORMAT_HEX:
  152. fprintf(stream, "0x" INT64_HEX_FMT "L", value->llval);
  153. break;
  154. case CONFIG_FORMAT_DEFAULT:
  155. default:
  156. fprintf(stream, INT64_FMT "L", value->llval);
  157. break;
  158. }
  159. break;
  160. /* float */
  161. case CONFIG_TYPE_FLOAT:
  162. {
  163. char *q;
  164. snprintf(fbuf, sizeof(fbuf) - 3, "%.*g", FLOAT_PRECISION, value->fval);
  165. /* check for exponent */
  166. q = strchr(fbuf, 'e');
  167. if(! q)
  168. {
  169. /* no exponent */
  170. if(! strchr(fbuf, '.')) /* no decimal point */
  171. strcat(fbuf, ".0");
  172. else
  173. {
  174. /* has decimal point */
  175. char *p;
  176. for(p = fbuf + strlen(fbuf) - 1; p > fbuf; --p)
  177. {
  178. if(*p != '0')
  179. {
  180. *(++p) = '\0';
  181. break;
  182. }
  183. }
  184. }
  185. }
  186. fputs(fbuf, stream);
  187. break;
  188. }
  189. /* string */
  190. case CONFIG_TYPE_STRING:
  191. {
  192. char *p;
  193. fputc('\"', stream);
  194. if(value->sval)
  195. {
  196. for(p = value->sval; *p; p++)
  197. {
  198. int c = (int)*p & 0xFF;
  199. switch(c)
  200. {
  201. case '\"':
  202. case '\\':
  203. fputc('\\', stream);
  204. fputc(c, stream);
  205. break;
  206. case '\n':
  207. fputs("\\n", stream);
  208. break;
  209. case '\r':
  210. fputs("\\r", stream);
  211. break;
  212. case '\f':
  213. fputs("\\f", stream);
  214. break;
  215. case '\t':
  216. fputs("\\t", stream);
  217. break;
  218. default:
  219. if(c >= ' ')
  220. fputc(c, stream);
  221. else
  222. fprintf(stream, "\\x%02X", c);
  223. }
  224. }
  225. }
  226. fputc('\"', stream);
  227. break;
  228. }
  229. /* list */
  230. case CONFIG_TYPE_LIST:
  231. {
  232. config_list_t *list = value->list;
  233. fprintf(stream, "( ");
  234. if(list)
  235. {
  236. int len = list->length;
  237. config_setting_t **s;
  238. for(s = list->elements; len--; s++)
  239. {
  240. __config_write_value(&((*s)->value), (*s)->type,
  241. config_setting_get_format(*s),
  242. depth + 1, tab_width, stream);
  243. if(len)
  244. fputc(',', stream);
  245. fputc(' ', stream);
  246. }
  247. }
  248. fputc(')', stream);
  249. break;
  250. }
  251. /* array */
  252. case CONFIG_TYPE_ARRAY:
  253. {
  254. config_list_t *list = value->list;
  255. fprintf(stream, "[ ");
  256. if(list)
  257. {
  258. int len = list->length;
  259. config_setting_t **s;
  260. for(s = list->elements; len--; s++)
  261. {
  262. __config_write_value(&((*s)->value), (*s)->type,
  263. config_setting_get_format(*s),
  264. depth + 1, tab_width, stream);
  265. if(len)
  266. fputc(',', stream);
  267. fputc(' ', stream);
  268. }
  269. }
  270. fputc(']', stream);
  271. break;
  272. }
  273. /* group */
  274. case CONFIG_TYPE_GROUP:
  275. {
  276. config_list_t *list = value->list;
  277. if(depth > 0)
  278. {
  279. #ifdef K_AND_R_STYLE /* Horrendous, but many people like it. */
  280. fputc(' ', stream);
  281. #else
  282. fputc('\n', stream);
  283. if(depth > 1)
  284. __config_indent(stream, depth, tab_width);
  285. #endif
  286. fprintf(stream, "{\n");
  287. }
  288. if(list)
  289. {
  290. int len = list->length;
  291. config_setting_t **s;
  292. for(s = list->elements; len--; s++)
  293. __config_write_setting(*s, stream, depth + 1, tab_width);
  294. }
  295. if(depth > 1)
  296. __config_indent(stream, depth, tab_width);
  297. if(depth > 0)
  298. fputc('}', stream);
  299. break;
  300. }
  301. default:
  302. /* this shouldn't happen, but handle it gracefully... */
  303. fputs("???", stream);
  304. break;
  305. }
  306. }
  307. /* ------------------------------------------------------------------------- */
  308. static void __config_list_add(config_list_t *list, config_setting_t *setting)
  309. {
  310. if((list->length % CHUNK_SIZE) == 0)
  311. {
  312. list->elements = (config_setting_t **)realloc(
  313. list->elements,
  314. (list->length + CHUNK_SIZE) * sizeof(config_setting_t *));
  315. }
  316. list->elements[list->length] = setting;
  317. list->length++;
  318. }
  319. /* ------------------------------------------------------------------------- */
  320. static config_setting_t *__config_list_search(config_list_t *list,
  321. const char *name,
  322. unsigned int *idx)
  323. {
  324. config_setting_t **found = NULL;
  325. unsigned int i;
  326. if(! list)
  327. return(NULL);
  328. for(i = 0, found = list->elements; i < list->length; i++, found++)
  329. {
  330. if(! (*found)->name)
  331. continue;
  332. if(! __config_name_compare(name, (*found)->name))
  333. {
  334. if(idx)
  335. *idx = i;
  336. return(*found);
  337. }
  338. }
  339. return(NULL);
  340. }
  341. /* ------------------------------------------------------------------------- */
  342. static config_setting_t *__config_list_remove(config_list_t *list, int idx)
  343. {
  344. config_setting_t *removed = *(list->elements + idx);
  345. int offset = (idx * sizeof(config_setting_t *));
  346. int len = list->length - 1 - idx;
  347. char *base = (char *)list->elements + offset;
  348. memmove(base, base + sizeof(config_setting_t *),
  349. len * sizeof(config_setting_t *));
  350. list->length--;
  351. /* possibly realloc smaller? */
  352. return(removed);
  353. }
  354. /* ------------------------------------------------------------------------- */
  355. static void __config_setting_destroy(config_setting_t *setting)
  356. {
  357. if(setting)
  358. {
  359. if(setting->name)
  360. _delete(setting->name);
  361. if(setting->type == CONFIG_TYPE_STRING)
  362. _delete(setting->value.sval);
  363. else if((setting->type == CONFIG_TYPE_GROUP)
  364. || (setting->type == CONFIG_TYPE_ARRAY)
  365. || (setting->type == CONFIG_TYPE_LIST))
  366. {
  367. if(setting->value.list)
  368. __config_list_destroy(setting->value.list);
  369. }
  370. if(setting->hook && setting->config->destructor)
  371. setting->config->destructor(setting->hook);
  372. _delete(setting);
  373. }
  374. }
  375. /* ------------------------------------------------------------------------- */
  376. static void __config_list_destroy(config_list_t *list)
  377. {
  378. config_setting_t **p;
  379. unsigned int i;
  380. if(! list)
  381. return;
  382. if(list->elements)
  383. {
  384. for(p = list->elements, i = 0; i < list->length; p++, i++)
  385. __config_setting_destroy(*p);
  386. _delete(list->elements);
  387. }
  388. _delete(list);
  389. }
  390. /* ------------------------------------------------------------------------- */
  391. static int __config_vector_checktype(const config_setting_t *vector, int type)
  392. {
  393. /* if the array is empty, then it has no type yet */
  394. if(! vector->value.list)
  395. return(CONFIG_TRUE);
  396. if(vector->value.list->length == 0)
  397. return(CONFIG_TRUE);
  398. /* if it's a list, any type is allowed */
  399. if(vector->type == CONFIG_TYPE_LIST)
  400. return(CONFIG_TRUE);
  401. /* otherwise the first element added determines the type of the array */
  402. return((vector->value.list->elements[0]->type == type)
  403. ? CONFIG_TRUE : CONFIG_FALSE);
  404. }
  405. /* ------------------------------------------------------------------------- */
  406. static int __config_validate_name(const char *name)
  407. {
  408. const char *p = name;
  409. if(*p == '\0')
  410. return(CONFIG_FALSE);
  411. if(! isalpha((unsigned char)*p) && (*p != '*'))
  412. return(CONFIG_FALSE);
  413. for(++p; *p; ++p)
  414. {
  415. if(! (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || strchr("*_-", (int)*p)))
  416. return(CONFIG_FALSE);
  417. }
  418. return(CONFIG_TRUE);
  419. }
  420. /* ------------------------------------------------------------------------- */
  421. static int __config_read(config_t *config, FILE *stream, const char *filename,
  422. const char *str)
  423. {
  424. yyscan_t scanner;
  425. struct scan_context scan_ctx;
  426. struct parse_context parse_ctx;
  427. YY_BUFFER_STATE buffer = NULL;
  428. int r;
  429. /* Reinitialize the config */
  430. void (*destructor)(void *) = config->destructor;
  431. const char *include_dir = config->include_dir;
  432. unsigned short tab_width = config->tab_width;
  433. unsigned short flags = config->flags;
  434. config->include_dir = NULL;
  435. config_destroy(config);
  436. config_init(config);
  437. config->destructor = destructor;
  438. config->include_dir = include_dir;
  439. config->tab_width = tab_width;
  440. config->flags = flags;
  441. parsectx_init(&parse_ctx);
  442. parse_ctx.config = config;
  443. parse_ctx.parent = config->root;
  444. parse_ctx.setting = config->root;
  445. __config_locale_override();
  446. scanctx_init(&scan_ctx, filename);
  447. scan_ctx.config = config;
  448. libconfig_yylex_init_extra(&scan_ctx, &scanner);
  449. if(stream)
  450. libconfig_yyrestart(stream, scanner);
  451. else /* read from string */
  452. buffer = libconfig_yy_scan_string(str, scanner);
  453. libconfig_yyset_lineno(1, scanner);
  454. r = libconfig_yyparse(scanner, &parse_ctx, &scan_ctx);
  455. if(r != 0)
  456. {
  457. YY_BUFFER_STATE buf;
  458. config->error_file = scanctx_current_filename(&scan_ctx);
  459. config->error_type = CONFIG_ERR_PARSE;
  460. /* Unwind the include stack, freeing the buffers and closing the files. */
  461. while((buf = (YY_BUFFER_STATE)scanctx_pop_include(&scan_ctx)) != NULL)
  462. libconfig_yy_delete_buffer(buf, scanner);
  463. }
  464. libconfig_yylex_destroy(scanner);
  465. config->filenames = scanctx_cleanup(&scan_ctx, &(config->num_filenames));
  466. parsectx_cleanup(&parse_ctx);
  467. __config_locale_restore();
  468. return(r == 0 ? CONFIG_TRUE : CONFIG_FALSE);
  469. }
  470. /* ------------------------------------------------------------------------- */
  471. int config_read(config_t *config, FILE *stream)
  472. {
  473. return(__config_read(config, stream, NULL, NULL));
  474. }
  475. /* ------------------------------------------------------------------------- */
  476. int config_read_string(config_t *config, const char *str)
  477. {
  478. return(__config_read(config, NULL, NULL, str));
  479. }
  480. /* ------------------------------------------------------------------------- */
  481. static void __config_write_setting(const config_setting_t *setting,
  482. FILE *stream, int depth,
  483. unsigned short tab_width)
  484. {
  485. if(depth > 1)
  486. __config_indent(stream, depth, tab_width);
  487. if(setting->name)
  488. {
  489. fputs(setting->name, stream);
  490. fprintf(stream, " %c ", (setting->type == CONFIG_TYPE_GROUP ? ':' : '='));
  491. }
  492. __config_write_value(&(setting->value), setting->type,
  493. config_setting_get_format(setting),
  494. depth, tab_width, stream);
  495. if(depth > 0)
  496. {
  497. fputc(';', stream);
  498. fputc('\n', stream);
  499. }
  500. }
  501. /* ------------------------------------------------------------------------- */
  502. void config_write(const config_t *config, FILE *stream)
  503. {
  504. __config_locale_override();
  505. __config_write_setting(config->root, stream, 0, config->tab_width);
  506. __config_locale_restore();
  507. }
  508. /* ------------------------------------------------------------------------- */
  509. int config_read_file(config_t *config, const char *filename)
  510. {
  511. int ret;
  512. FILE *stream = fopen(filename, "rt");
  513. if(! stream)
  514. {
  515. config->error_text = __io_error;
  516. config->error_type = CONFIG_ERR_FILE_IO;
  517. return(CONFIG_FALSE);
  518. }
  519. ret = __config_read(config, stream, filename, NULL);
  520. fclose(stream);
  521. return(ret);
  522. }
  523. /* ------------------------------------------------------------------------- */
  524. int config_write_file(config_t *config, const char *filename)
  525. {
  526. FILE *f = fopen(filename, "wt");
  527. if(! f)
  528. {
  529. config->error_text = __io_error;
  530. config->error_type = CONFIG_ERR_FILE_IO;
  531. return(CONFIG_FALSE);
  532. }
  533. config_write(config, f);
  534. fclose(f);
  535. config->error_type = CONFIG_ERR_NONE;
  536. return(CONFIG_TRUE);
  537. }
  538. /* ------------------------------------------------------------------------- */
  539. void config_destroy(config_t *config)
  540. {
  541. unsigned int count = config->num_filenames;
  542. const char **f;
  543. __config_setting_destroy(config->root);
  544. for(f = config->filenames; count > 0; ++f, --count)
  545. _delete(*f);
  546. _delete(config->filenames);
  547. _delete(config->include_dir);
  548. memset((void *)config, 0, sizeof(config_t));
  549. }
  550. /* ------------------------------------------------------------------------- */
  551. void config_init(config_t *config)
  552. {
  553. memset((void *)config, 0, sizeof(config_t));
  554. config->root = _new(config_setting_t);
  555. config->root->type = CONFIG_TYPE_GROUP;
  556. config->root->config = config;
  557. config->tab_width = 2;
  558. }
  559. /* ------------------------------------------------------------------------- */
  560. void config_set_auto_convert(config_t *config, int flag)
  561. {
  562. if(flag)
  563. config->flags |= CONFIG_OPTION_AUTOCONVERT;
  564. else
  565. config->flags &= ~CONFIG_OPTION_AUTOCONVERT;
  566. }
  567. /* ------------------------------------------------------------------------- */
  568. int config_get_auto_convert(const config_t *config)
  569. {
  570. return((config->flags & CONFIG_OPTION_AUTOCONVERT) != 0);
  571. }
  572. /* ------------------------------------------------------------------------- */
  573. static config_setting_t *config_setting_create(config_setting_t *parent,
  574. const char *name, int type)
  575. {
  576. config_setting_t *setting;
  577. config_list_t *list;
  578. if((parent->type != CONFIG_TYPE_GROUP)
  579. && (parent->type != CONFIG_TYPE_ARRAY)
  580. && (parent->type != CONFIG_TYPE_LIST))
  581. return(NULL);
  582. setting = _new(config_setting_t);
  583. setting->parent = parent;
  584. setting->name = (name == NULL) ? NULL : strdup(name);
  585. setting->type = type;
  586. setting->config = parent->config;
  587. setting->hook = NULL;
  588. setting->line = 0;
  589. list = parent->value.list;
  590. if(! list)
  591. list = parent->value.list = _new(config_list_t);
  592. __config_list_add(list, setting);
  593. return(setting);
  594. }
  595. /* ------------------------------------------------------------------------- */
  596. static int __config_setting_get_int(const config_setting_t *setting,
  597. int *value)
  598. {
  599. switch(setting->type)
  600. {
  601. case CONFIG_TYPE_INT:
  602. *value = setting->value.ival;
  603. return(CONFIG_TRUE);
  604. case CONFIG_TYPE_INT64:
  605. if((setting->value.llval > INT32_MAX)
  606. || (setting->value.llval < INT32_MIN))
  607. *value = 0;
  608. else
  609. *value = (int)(setting->value.llval);
  610. return(CONFIG_TRUE);
  611. case CONFIG_TYPE_FLOAT:
  612. if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
  613. {
  614. *value = (int)(setting->value.fval);
  615. return(CONFIG_TRUE);
  616. }
  617. else
  618. { /* fall through */ }
  619. default:
  620. return(CONFIG_FALSE);
  621. }
  622. }
  623. /* ------------------------------------------------------------------------- */
  624. int config_setting_get_int(const config_setting_t *setting)
  625. {
  626. int value = 0;
  627. __config_setting_get_int(setting, &value);
  628. return(value);
  629. }
  630. /* ------------------------------------------------------------------------- */
  631. static int __config_setting_get_int64(const config_setting_t *setting,
  632. long long *value)
  633. {
  634. switch(setting->type)
  635. {
  636. case CONFIG_TYPE_INT64:
  637. *value = setting->value.llval;
  638. return(CONFIG_TRUE);
  639. case CONFIG_TYPE_INT:
  640. *value = (long long)(setting->value.ival);
  641. return(CONFIG_TRUE);
  642. case CONFIG_TYPE_FLOAT:
  643. if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
  644. {
  645. *value = (long long)(setting->value.fval);
  646. return(CONFIG_TRUE);
  647. }
  648. else
  649. { /* fall through */ }
  650. default:
  651. return(CONFIG_FALSE);
  652. }
  653. }
  654. /* ------------------------------------------------------------------------- */
  655. long long config_setting_get_int64(const config_setting_t *setting)
  656. {
  657. long long value = 0;
  658. __config_setting_get_int64(setting, &value);
  659. return(value);
  660. }
  661. /* ------------------------------------------------------------------------- */
  662. int config_setting_lookup_int(const config_setting_t *setting,
  663. const char *name, int *value)
  664. {
  665. config_setting_t *member = config_setting_get_member(setting, name);
  666. if(! member)
  667. return(CONFIG_FALSE);
  668. return(__config_setting_get_int(member, value));
  669. }
  670. /* ------------------------------------------------------------------------- */
  671. int config_setting_lookup_int64(const config_setting_t *setting,
  672. const char *name, long long *value)
  673. {
  674. config_setting_t *member = config_setting_get_member(setting, name);
  675. if(! member)
  676. return(CONFIG_FALSE);
  677. return(__config_setting_get_int64(member, value));
  678. }
  679. /* ------------------------------------------------------------------------- */
  680. static int __config_setting_get_float(const config_setting_t *setting,
  681. double *value)
  682. {
  683. switch(setting->type)
  684. {
  685. case CONFIG_TYPE_FLOAT:
  686. *value = setting->value.fval;
  687. return(CONFIG_TRUE);
  688. case CONFIG_TYPE_INT:
  689. if(config_get_auto_convert(setting->config))
  690. {
  691. *value = (double)(setting->value.ival);
  692. return(CONFIG_TRUE);
  693. }
  694. else
  695. return(CONFIG_FALSE);
  696. case CONFIG_TYPE_INT64:
  697. if(config_get_auto_convert(setting->config))
  698. {
  699. *value = (double)(setting->value.llval);
  700. return(CONFIG_TRUE);
  701. }
  702. else
  703. { /* fall through */ }
  704. default:
  705. return(CONFIG_FALSE);
  706. }
  707. }
  708. /* ------------------------------------------------------------------------- */
  709. double config_setting_get_float(const config_setting_t *setting)
  710. {
  711. double value = 0.0;
  712. __config_setting_get_float(setting, &value);
  713. return(value);
  714. }
  715. /* ------------------------------------------------------------------------- */
  716. int config_setting_lookup_float(const config_setting_t *setting,
  717. const char *name, double *value)
  718. {
  719. config_setting_t *member = config_setting_get_member(setting, name);
  720. if(! member)
  721. return(CONFIG_FALSE);
  722. return(__config_setting_get_float(member, value));
  723. }
  724. /* ------------------------------------------------------------------------- */
  725. int config_setting_lookup_string(const config_setting_t *setting,
  726. const char *name, const char **value)
  727. {
  728. config_setting_t *member = config_setting_get_member(setting, name);
  729. if(! member)
  730. return(CONFIG_FALSE);
  731. if(config_setting_type(member) != CONFIG_TYPE_STRING)
  732. return(CONFIG_FALSE);
  733. *value = config_setting_get_string(member);
  734. return(CONFIG_TRUE);
  735. }
  736. /* ------------------------------------------------------------------------- */
  737. int config_setting_lookup_bool(const config_setting_t *setting,
  738. const char *name, int *value)
  739. {
  740. config_setting_t *member = config_setting_get_member(setting, name);
  741. if(! member)
  742. return(CONFIG_FALSE);
  743. if(config_setting_type(member) != CONFIG_TYPE_BOOL)
  744. return(CONFIG_FALSE);
  745. *value = config_setting_get_bool(member);
  746. return(CONFIG_TRUE);
  747. }
  748. /* ------------------------------------------------------------------------- */
  749. int config_setting_set_int(config_setting_t *setting, int value)
  750. {
  751. switch(setting->type)
  752. {
  753. case CONFIG_TYPE_NONE:
  754. setting->type = CONFIG_TYPE_INT;
  755. /* fall through */
  756. case CONFIG_TYPE_INT:
  757. setting->value.ival = value;
  758. return(CONFIG_TRUE);
  759. case CONFIG_TYPE_FLOAT:
  760. if(config_get_auto_convert(setting->config))
  761. {
  762. setting->value.fval = (float)value;
  763. return(CONFIG_TRUE);
  764. }
  765. else
  766. return(CONFIG_FALSE);
  767. default:
  768. return(CONFIG_FALSE);
  769. }
  770. }
  771. /* ------------------------------------------------------------------------- */
  772. int config_setting_set_int64(config_setting_t *setting, long long value)
  773. {
  774. switch(setting->type)
  775. {
  776. case CONFIG_TYPE_NONE:
  777. setting->type = CONFIG_TYPE_INT64;
  778. /* fall through */
  779. case CONFIG_TYPE_INT64:
  780. setting->value.llval = value;
  781. return(CONFIG_TRUE);
  782. case CONFIG_TYPE_INT:
  783. if((value > INT32_MAX) || (value < INT32_MIN))
  784. setting->value.ival = 0;
  785. else
  786. setting->value.ival = (int)value;
  787. return(CONFIG_TRUE);
  788. case CONFIG_TYPE_FLOAT:
  789. if(config_get_auto_convert(setting->config))
  790. {
  791. setting->value.fval = (float)value;
  792. return(CONFIG_TRUE);
  793. }
  794. else
  795. return(CONFIG_FALSE);
  796. default:
  797. return(CONFIG_FALSE);
  798. }
  799. }
  800. /* ------------------------------------------------------------------------- */
  801. int config_setting_set_float(config_setting_t *setting, double value)
  802. {
  803. switch(setting->type)
  804. {
  805. case CONFIG_TYPE_NONE:
  806. setting->type = CONFIG_TYPE_FLOAT;
  807. /* fall through */
  808. case CONFIG_TYPE_FLOAT:
  809. setting->value.fval = value;
  810. return(CONFIG_TRUE);
  811. case CONFIG_TYPE_INT:
  812. if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
  813. {
  814. setting->value.ival = (int)value;
  815. return(CONFIG_TRUE);
  816. }
  817. else
  818. return(CONFIG_FALSE);
  819. case CONFIG_TYPE_INT64:
  820. if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
  821. {
  822. setting->value.llval = (long long)value;
  823. return(CONFIG_TRUE);
  824. }
  825. else
  826. return(CONFIG_FALSE);
  827. default:
  828. return(CONFIG_FALSE);
  829. }
  830. }
  831. /* ------------------------------------------------------------------------- */
  832. int config_setting_get_bool(const config_setting_t *setting)
  833. {
  834. return((setting->type == CONFIG_TYPE_BOOL) ? setting->value.ival : 0);
  835. }
  836. /* ------------------------------------------------------------------------- */
  837. int config_setting_set_bool(config_setting_t *setting, int value)
  838. {
  839. if(setting->type == CONFIG_TYPE_NONE)
  840. setting->type = CONFIG_TYPE_BOOL;
  841. else if(setting->type != CONFIG_TYPE_BOOL)
  842. return(CONFIG_FALSE);
  843. setting->value.ival = value;
  844. return(CONFIG_TRUE);
  845. }
  846. /* ------------------------------------------------------------------------- */
  847. const char *config_setting_get_string(const config_setting_t *setting)
  848. {
  849. return((setting->type == CONFIG_TYPE_STRING) ? setting->value.sval : NULL);
  850. }
  851. /* ------------------------------------------------------------------------- */
  852. int config_setting_set_string(config_setting_t *setting, const char *value)
  853. {
  854. if(setting->type == CONFIG_TYPE_NONE)
  855. setting->type = CONFIG_TYPE_STRING;
  856. else if(setting->type != CONFIG_TYPE_STRING)
  857. return(CONFIG_FALSE);
  858. if(setting->value.sval)
  859. _delete(setting->value.sval);
  860. setting->value.sval = (value == NULL) ? NULL : strdup(value);
  861. return(CONFIG_TRUE);
  862. }
  863. /* ------------------------------------------------------------------------- */
  864. int config_setting_set_format(config_setting_t *setting, short format)
  865. {
  866. if(((setting->type != CONFIG_TYPE_INT)
  867. && (setting->type != CONFIG_TYPE_INT64))
  868. || ((format != CONFIG_FORMAT_DEFAULT) && (format != CONFIG_FORMAT_HEX)))
  869. return(CONFIG_FALSE);
  870. setting->format = format;
  871. return(CONFIG_TRUE);
  872. }
  873. /* ------------------------------------------------------------------------- */
  874. short config_setting_get_format(const config_setting_t *setting)
  875. {
  876. return(setting->format != 0 ? setting->format
  877. : setting->config->default_format);
  878. }
  879. /* ------------------------------------------------------------------------- */
  880. config_setting_t *config_lookup_from(config_setting_t *setting,
  881. const char *path)
  882. {
  883. const char *p = path;
  884. config_setting_t *found;
  885. for(;;)
  886. {
  887. while(*p && strchr(PATH_TOKENS, *p))
  888. p++;
  889. if(! *p)
  890. break;
  891. if(*p == '[')
  892. found = config_setting_get_elem(setting, atoi(++p));
  893. else
  894. found = config_setting_get_member(setting, p);
  895. if(! found)
  896. break;
  897. setting = found;
  898. while(! strchr(PATH_TOKENS, *p))
  899. p++;
  900. }
  901. return(*p ? NULL : setting);
  902. }
  903. /* ------------------------------------------------------------------------- */
  904. config_setting_t *config_lookup(const config_t *config, const char *path)
  905. {
  906. return(config_lookup_from(config->root, path));
  907. }
  908. /* ------------------------------------------------------------------------- */
  909. int config_lookup_string(const config_t *config, const char *path,
  910. const char **value)
  911. {
  912. const config_setting_t *s = config_lookup(config, path);
  913. if(! s)
  914. return(CONFIG_FALSE);
  915. if(config_setting_type(s) != CONFIG_TYPE_STRING)
  916. return(CONFIG_FALSE);
  917. *value = config_setting_get_string(s);
  918. return(CONFIG_TRUE);
  919. }
  920. /* ------------------------------------------------------------------------- */
  921. int config_lookup_int(const config_t *config, const char *path,
  922. int *value)
  923. {
  924. const config_setting_t *s = config_lookup(config, path);
  925. if(! s)
  926. return(CONFIG_FALSE);
  927. return(__config_setting_get_int(s, value));
  928. }
  929. /* ------------------------------------------------------------------------- */
  930. int config_lookup_int64(const config_t *config, const char *path,
  931. long long *value)
  932. {
  933. const config_setting_t *s = config_lookup(config, path);
  934. if(! s)
  935. return(CONFIG_FALSE);
  936. return(__config_setting_get_int64(s, value));
  937. }
  938. /* ------------------------------------------------------------------------- */
  939. int config_lookup_float(const config_t *config, const char *path,
  940. double *value)
  941. {
  942. const config_setting_t *s = config_lookup(config, path);
  943. if(! s)
  944. return(CONFIG_FALSE);
  945. return(__config_setting_get_float(s, value));
  946. }
  947. /* ------------------------------------------------------------------------- */
  948. int config_lookup_bool(const config_t *config, const char *path, int *value)
  949. {
  950. const config_setting_t *s = config_lookup(config, path);
  951. if(! s)
  952. return(CONFIG_FALSE);
  953. if(config_setting_type(s) != CONFIG_TYPE_BOOL)
  954. return(CONFIG_FALSE);
  955. *value = config_setting_get_bool(s);
  956. return(CONFIG_TRUE);
  957. }
  958. /* ------------------------------------------------------------------------- */
  959. int config_setting_get_int_elem(const config_setting_t *vector, int idx)
  960. {
  961. const config_setting_t *element = config_setting_get_elem(vector, idx);
  962. return(element ? config_setting_get_int(element) : 0);
  963. }
  964. /* ------------------------------------------------------------------------- */
  965. config_setting_t *config_setting_set_int_elem(config_setting_t *vector,
  966. int idx, int value)
  967. {
  968. config_setting_t *element = NULL;
  969. if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
  970. return(NULL);
  971. if(idx < 0)
  972. {
  973. if(! __config_vector_checktype(vector, CONFIG_TYPE_INT))
  974. return(NULL);
  975. element = config_setting_create(vector, NULL, CONFIG_TYPE_INT);
  976. }
  977. else
  978. {
  979. element = config_setting_get_elem(vector, idx);
  980. if(! element)
  981. return(NULL);
  982. }
  983. if(! config_setting_set_int(element, value))
  984. return(NULL);
  985. return(element);
  986. }
  987. /* ------------------------------------------------------------------------- */
  988. long long config_setting_get_int64_elem(const config_setting_t *vector,
  989. int idx)
  990. {
  991. const config_setting_t *element = config_setting_get_elem(vector, idx);
  992. return(element ? config_setting_get_int64(element) : 0);
  993. }
  994. /* ------------------------------------------------------------------------- */
  995. config_setting_t *config_setting_set_int64_elem(config_setting_t *vector,
  996. int idx, long long value)
  997. {
  998. config_setting_t *element = NULL;
  999. if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
  1000. return(NULL);
  1001. if(idx < 0)
  1002. {
  1003. if(! __config_vector_checktype(vector, CONFIG_TYPE_INT64))
  1004. return(NULL);
  1005. element = config_setting_create(vector, NULL, CONFIG_TYPE_INT64);
  1006. }
  1007. else
  1008. {
  1009. element = config_setting_get_elem(vector, idx);
  1010. if(! element)
  1011. return(NULL);
  1012. }
  1013. if(! config_setting_set_int64(element, value))
  1014. return(NULL);
  1015. return(element);
  1016. }
  1017. /* ------------------------------------------------------------------------- */
  1018. double config_setting_get_float_elem(const config_setting_t *vector, int idx)
  1019. {
  1020. config_setting_t *element = config_setting_get_elem(vector, idx);
  1021. return(element ? config_setting_get_float(element) : 0.0);
  1022. }
  1023. /* ------------------------------------------------------------------------- */
  1024. config_setting_t *config_setting_set_float_elem(config_setting_t *vector,
  1025. int idx, double value)
  1026. {
  1027. config_setting_t *element = NULL;
  1028. if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
  1029. return(NULL);
  1030. if(idx < 0)
  1031. {
  1032. if(! __config_vector_checktype(vector, CONFIG_TYPE_FLOAT))
  1033. return(NULL);
  1034. element = config_setting_create(vector, NULL, CONFIG_TYPE_FLOAT);
  1035. }
  1036. else
  1037. element = config_setting_get_elem(vector, idx);
  1038. if(! element)
  1039. return(NULL);
  1040. if(! config_setting_set_float(element, value))
  1041. return(NULL);
  1042. return(element);
  1043. }
  1044. /* ------------------------------------------------------------------------- */
  1045. int config_setting_get_bool_elem(const config_setting_t *vector, int idx)
  1046. {
  1047. config_setting_t *element = config_setting_get_elem(vector, idx);
  1048. if(! element)
  1049. return(CONFIG_FALSE);
  1050. if(element->type != CONFIG_TYPE_BOOL)
  1051. return(CONFIG_FALSE);
  1052. return(element->value.ival);
  1053. }
  1054. /* ------------------------------------------------------------------------- */
  1055. config_setting_t *config_setting_set_bool_elem(config_setting_t *vector,
  1056. int idx, int value)
  1057. {
  1058. config_setting_t *element = NULL;
  1059. if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
  1060. return(NULL);
  1061. if(idx < 0)
  1062. {
  1063. if(! __config_vector_checktype(vector, CONFIG_TYPE_BOOL))
  1064. return(NULL);
  1065. element = config_setting_create(vector, NULL, CONFIG_TYPE_BOOL);
  1066. }
  1067. else
  1068. element = config_setting_get_elem(vector, idx);
  1069. if(! element)
  1070. return(NULL);
  1071. if(! config_setting_set_bool(element, value))
  1072. return(NULL);
  1073. return(element);
  1074. }
  1075. /* ------------------------------------------------------------------------- */
  1076. const char *config_setting_get_string_elem(const config_setting_t *vector,
  1077. int idx)
  1078. {
  1079. config_setting_t *element = config_setting_get_elem(vector, idx);
  1080. if(! element)
  1081. return(NULL);
  1082. if(element->type != CONFIG_TYPE_STRING)
  1083. return(NULL);
  1084. return(element->value.sval);
  1085. }
  1086. /* ------------------------------------------------------------------------- */
  1087. config_setting_t *config_setting_set_string_elem(config_setting_t *vector,
  1088. int idx, const char *value)
  1089. {
  1090. config_setting_t *element = NULL;
  1091. if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
  1092. return(NULL);
  1093. if(idx < 0)
  1094. {
  1095. if(! __config_vector_checktype(vector, CONFIG_TYPE_STRING))
  1096. return(NULL);
  1097. element = config_setting_create(vector, NULL, CONFIG_TYPE_STRING);
  1098. }
  1099. else
  1100. element = config_setting_get_elem(vector, idx);
  1101. if(! element)
  1102. return(NULL);
  1103. if(! config_setting_set_string(element, value))
  1104. return(NULL);
  1105. return(element);
  1106. }
  1107. /* ------------------------------------------------------------------------- */
  1108. config_setting_t *config_setting_get_elem(const config_setting_t *vector,
  1109. unsigned int idx)
  1110. {
  1111. config_list_t *list = vector->value.list;
  1112. if(((vector->type != CONFIG_TYPE_ARRAY)
  1113. && (vector->type != CONFIG_TYPE_LIST)
  1114. && (vector->type != CONFIG_TYPE_GROUP)) || ! list)
  1115. return(NULL);
  1116. if(idx >= list->length)
  1117. return(NULL);
  1118. return(list->elements[idx]);
  1119. }
  1120. /* ------------------------------------------------------------------------- */
  1121. config_setting_t *config_setting_get_member(const config_setting_t *setting,
  1122. const char *name)
  1123. {
  1124. if(setting->type != CONFIG_TYPE_GROUP)
  1125. return(NULL);
  1126. return(__config_list_search(setting->value.list, name, NULL));
  1127. }
  1128. /* ------------------------------------------------------------------------- */
  1129. void config_set_destructor(config_t *config, void (*destructor)(void *))
  1130. {
  1131. config->destructor = destructor;
  1132. }
  1133. /* ------------------------------------------------------------------------- */
  1134. void config_set_include_dir(config_t *config, const char *include_dir)
  1135. {
  1136. _delete(config->include_dir);
  1137. config->include_dir = strdup(include_dir);
  1138. }
  1139. /* ------------------------------------------------------------------------- */
  1140. int config_setting_length(const config_setting_t *setting)
  1141. {
  1142. if((setting->type != CONFIG_TYPE_GROUP)
  1143. && (setting->type != CONFIG_TYPE_ARRAY)
  1144. && (setting->type != CONFIG_TYPE_LIST))
  1145. return(0);
  1146. if(! setting->value.list)
  1147. return(0);
  1148. return(setting->value.list->length);
  1149. }
  1150. /* ------------------------------------------------------------------------- */
  1151. void config_setting_set_hook(config_setting_t *setting, void *hook)
  1152. {
  1153. setting->hook = hook;
  1154. }
  1155. /* ------------------------------------------------------------------------- */
  1156. config_setting_t *config_setting_add(config_setting_t *parent,
  1157. const char *name, int type)
  1158. {
  1159. if((type < CONFIG_TYPE_NONE) || (type > CONFIG_TYPE_LIST))
  1160. return(NULL);
  1161. if(! parent)
  1162. return(NULL);
  1163. if((parent->type == CONFIG_TYPE_ARRAY) || (parent->type == CONFIG_TYPE_LIST))
  1164. name = NULL;
  1165. if(name)
  1166. {
  1167. if(! __config_validate_name(name))
  1168. return(NULL);
  1169. }
  1170. if(config_setting_get_member(parent, name) != NULL)
  1171. return(NULL); /* already exists */
  1172. return(config_setting_create(parent, name, type));
  1173. }
  1174. /* ------------------------------------------------------------------------- */
  1175. int config_setting_remove(config_setting_t *parent, const char *name)
  1176. {
  1177. unsigned int idx;
  1178. config_setting_t *setting;
  1179. if(! parent)
  1180. return(CONFIG_FALSE);
  1181. if(parent->type != CONFIG_TYPE_GROUP)
  1182. return(CONFIG_FALSE);
  1183. if(! (setting = __config_list_search(parent->value.list, name, &idx)))
  1184. return(CONFIG_FALSE);
  1185. __config_list_remove(parent->value.list, idx);
  1186. __config_setting_destroy(setting);
  1187. return(CONFIG_TRUE);
  1188. }
  1189. /* ------------------------------------------------------------------------- */
  1190. int config_setting_remove_elem(config_setting_t *parent, unsigned int idx)
  1191. {
  1192. config_list_t *list;
  1193. config_setting_t *removed = NULL;
  1194. if(! parent)
  1195. return(CONFIG_FALSE);
  1196. list = parent->value.list;
  1197. if(((parent->type != CONFIG_TYPE_ARRAY)
  1198. && (parent->type != CONFIG_TYPE_LIST)
  1199. && (parent->type != CONFIG_TYPE_GROUP)) || ! list)
  1200. return(CONFIG_FALSE);
  1201. if(idx >= list->length)
  1202. return(CONFIG_FALSE);
  1203. removed = __config_list_remove(list, idx);
  1204. __config_setting_destroy(removed);
  1205. return(CONFIG_TRUE);
  1206. }
  1207. /* ------------------------------------------------------------------------- */
  1208. int config_setting_index(const config_setting_t *setting)
  1209. {
  1210. config_setting_t **found = NULL;
  1211. config_list_t *list;
  1212. int i;
  1213. if(! setting->parent)
  1214. return(-1);
  1215. list = setting->parent->value.list;
  1216. for(i = 0, found = list->elements; i < (int)list->length; ++i, ++found)
  1217. {
  1218. if(*found == setting)
  1219. return(i);
  1220. }
  1221. return(-1);
  1222. }
  1223. /* ------------------------------------------------------------------------- */
  1224. /* eof */