123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617 |
- /* ----------------------------------------------------------------------------
- libconfig - A library for processing structured configuration files
- Copyright (C) 2005-2010 Mark A Lindner
- This file is part of libconfig.
- This library is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This library is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this library. If not, see <http://www.gnu.org/licenses/>.
- ----------------------------------------------------------------------------
- */
- #ifdef HAVE_CONFIG_H
- #include "ac_config.h"
- #endif
- #include "libconfig.h"
- #include "grammar.h"
- #include "scanner.h"
- #include "scanctx.h"
- #include "parsectx.h"
- #include "wincompat.h"
- #include <locale.h>
- #ifdef HAVE_XLOCALE_H
- #include <xlocale.h>
- #endif
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define PATH_TOKENS ":./"
- #define CHUNK_SIZE 16
- #define FLOAT_PRECISION 10
- #define _new(T) (T *)calloc(sizeof(T), 1) /* zeroed */
- #define _delete(P) free((void *)(P))
- /* ------------------------------------------------------------------------- */
- #ifndef LIBCONFIG_STATIC
- #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
- BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
- {
- return(TRUE);
- }
- #endif /* WIN32 */
- #endif /* LIBCONFIG_STATIC */
- /* ------------------------------------------------------------------------- */
- static const char *__io_error = "file I/O error";
- static void __config_list_destroy(config_list_t *list);
- static void __config_write_setting(const config_setting_t *setting,
- FILE *stream, int depth,
- unsigned short tab_width);
- extern int libconfig_yyparse(void *scanner, struct parse_context *ctx,
- struct scan_context *scan_ctx);
- extern int libconfig_yylex_init_extra(struct scan_context *scan_ctx,
- yyscan_t *scanner);
- /* ------------------------------------------------------------------------- */
- static void __config_locale_override(void)
- {
- #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \
- && ! defined(__MINGW32__)
- _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
- setlocale(LC_NUMERIC, "C");
- #elif defined(__APPLE__)
- locale_t loc = newlocale(LC_NUMERIC_MASK, "C", NULL);
- uselocale(loc);
- #elif ((defined HAVE_NEWLOCALE) && (defined HAVE_USELOCALE))
- locale_t loc = newlocale(LC_NUMERIC, "C", NULL);
- uselocale(loc);
- #else
- /* 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 */
- //#warning "No way to modify calling thread's locale!"
- #endif
- }
- /* ------------------------------------------------------------------------- */
- static void __config_locale_restore(void)
- {
- #if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) \
- && ! defined(__MINGW32__)
- _configthreadlocale(_DISABLE_PER_THREAD_LOCALE);
- #elif ((defined HAVE_USELOCALE) && (defined HAVE_FREELOCALE))
- locale_t loc = uselocale(LC_GLOBAL_LOCALE);
- freelocale(loc);
- #else
- /* 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 */
- //#warning "No way to modify calling thread's locale!"
- #endif
- }
- /* ------------------------------------------------------------------------- */
- static int __config_name_compare(const char *a, const char *b)
- {
- const char *p, *q;
- for(p = a, q = b; ; p++, q++)
- {
- int pd = ((! *p) || strchr(PATH_TOKENS, *p));
- int qd = ((! *q) || strchr(PATH_TOKENS, *q));
- if(pd && qd)
- break;
- else if(pd)
- return(-1);
- else if(qd)
- return(1);
- else if(*p < *q)
- return(-1);
- else if(*p > *q)
- return(1);
- }
- return(0);
- }
- /* ------------------------------------------------------------------------- */
- static void __config_indent(FILE *stream, int depth, unsigned short w)
- {
- if(w)
- fprintf(stream, "%*s", (depth - 1) * w, " ");
- else
- {
- int i;
- for(i = 0; i < (depth - 1); ++i)
- fputc('\t', stream);
- }
- }
- /* ------------------------------------------------------------------------- */
- static void __config_write_value(const config_value_t *value, int type,
- int format, int depth,
- unsigned short tab_width, FILE *stream)
- {
- char fbuf[64];
- switch(type)
- {
- /* boolean */
- case CONFIG_TYPE_BOOL:
- fputs(value->ival ? "true" : "false", stream);
- break;
- /* int */
- case CONFIG_TYPE_INT:
- switch(format)
- {
- case CONFIG_FORMAT_HEX:
- fprintf(stream, "0x%X", value->ival);
- break;
- case CONFIG_FORMAT_DEFAULT:
- default:
- fprintf(stream, "%d", value->ival);
- break;
- }
- break;
- /* 64-bit int */
- case CONFIG_TYPE_INT64:
- switch(format)
- {
- case CONFIG_FORMAT_HEX:
- fprintf(stream, "0x" INT64_HEX_FMT "L", value->llval);
- break;
- case CONFIG_FORMAT_DEFAULT:
- default:
- fprintf(stream, INT64_FMT "L", value->llval);
- break;
- }
- break;
- /* float */
- case CONFIG_TYPE_FLOAT:
- {
- char *q;
- snprintf(fbuf, sizeof(fbuf) - 3, "%.*g", FLOAT_PRECISION, value->fval);
- /* check for exponent */
- q = strchr(fbuf, 'e');
- if(! q)
- {
- /* no exponent */
- if(! strchr(fbuf, '.')) /* no decimal point */
- strcat(fbuf, ".0");
- else
- {
- /* has decimal point */
- char *p;
- for(p = fbuf + strlen(fbuf) - 1; p > fbuf; --p)
- {
- if(*p != '0')
- {
- *(++p) = '\0';
- break;
- }
- }
- }
- }
- fputs(fbuf, stream);
- break;
- }
- /* string */
- case CONFIG_TYPE_STRING:
- {
- char *p;
- fputc('\"', stream);
- if(value->sval)
- {
- for(p = value->sval; *p; p++)
- {
- int c = (int)*p & 0xFF;
- switch(c)
- {
- case '\"':
- case '\\':
- fputc('\\', stream);
- fputc(c, stream);
- break;
- case '\n':
- fputs("\\n", stream);
- break;
- case '\r':
- fputs("\\r", stream);
- break;
- case '\f':
- fputs("\\f", stream);
- break;
- case '\t':
- fputs("\\t", stream);
- break;
- default:
- if(c >= ' ')
- fputc(c, stream);
- else
- fprintf(stream, "\\x%02X", c);
- }
- }
- }
- fputc('\"', stream);
- break;
- }
- /* list */
- case CONFIG_TYPE_LIST:
- {
- config_list_t *list = value->list;
- fprintf(stream, "( ");
- if(list)
- {
- int len = list->length;
- config_setting_t **s;
- for(s = list->elements; len--; s++)
- {
- __config_write_value(&((*s)->value), (*s)->type,
- config_setting_get_format(*s),
- depth + 1, tab_width, stream);
- if(len)
- fputc(',', stream);
- fputc(' ', stream);
- }
- }
- fputc(')', stream);
- break;
- }
- /* array */
- case CONFIG_TYPE_ARRAY:
- {
- config_list_t *list = value->list;
- fprintf(stream, "[ ");
- if(list)
- {
- int len = list->length;
- config_setting_t **s;
- for(s = list->elements; len--; s++)
- {
- __config_write_value(&((*s)->value), (*s)->type,
- config_setting_get_format(*s),
- depth + 1, tab_width, stream);
- if(len)
- fputc(',', stream);
- fputc(' ', stream);
- }
- }
- fputc(']', stream);
- break;
- }
- /* group */
- case CONFIG_TYPE_GROUP:
- {
- config_list_t *list = value->list;
- if(depth > 0)
- {
- #ifdef K_AND_R_STYLE /* Horrendous, but many people like it. */
- fputc(' ', stream);
- #else
- fputc('\n', stream);
- if(depth > 1)
- __config_indent(stream, depth, tab_width);
- #endif
- fprintf(stream, "{\n");
- }
- if(list)
- {
- int len = list->length;
- config_setting_t **s;
- for(s = list->elements; len--; s++)
- __config_write_setting(*s, stream, depth + 1, tab_width);
- }
- if(depth > 1)
- __config_indent(stream, depth, tab_width);
- if(depth > 0)
- fputc('}', stream);
- break;
- }
- default:
- /* this shouldn't happen, but handle it gracefully... */
- fputs("???", stream);
- break;
- }
- }
- /* ------------------------------------------------------------------------- */
- static void __config_list_add(config_list_t *list, config_setting_t *setting)
- {
- if((list->length % CHUNK_SIZE) == 0)
- {
- list->elements = (config_setting_t **)realloc(
- list->elements,
- (list->length + CHUNK_SIZE) * sizeof(config_setting_t *));
- }
- list->elements[list->length] = setting;
- list->length++;
- }
- /* ------------------------------------------------------------------------- */
- static config_setting_t *__config_list_search(config_list_t *list,
- const char *name,
- unsigned int *idx)
- {
- config_setting_t **found = NULL;
- unsigned int i;
- if(! list)
- return(NULL);
- for(i = 0, found = list->elements; i < list->length; i++, found++)
- {
- if(! (*found)->name)
- continue;
- if(! __config_name_compare(name, (*found)->name))
- {
- if(idx)
- *idx = i;
- return(*found);
- }
- }
- return(NULL);
- }
- /* ------------------------------------------------------------------------- */
- static config_setting_t *__config_list_remove(config_list_t *list, int idx)
- {
- config_setting_t *removed = *(list->elements + idx);
- int offset = (idx * sizeof(config_setting_t *));
- int len = list->length - 1 - idx;
- char *base = (char *)list->elements + offset;
- memmove(base, base + sizeof(config_setting_t *),
- len * sizeof(config_setting_t *));
- list->length--;
- /* possibly realloc smaller? */
- return(removed);
- }
- /* ------------------------------------------------------------------------- */
- static void __config_setting_destroy(config_setting_t *setting)
- {
- if(setting)
- {
- if(setting->name)
- _delete(setting->name);
- if(setting->type == CONFIG_TYPE_STRING)
- _delete(setting->value.sval);
- else if((setting->type == CONFIG_TYPE_GROUP)
- || (setting->type == CONFIG_TYPE_ARRAY)
- || (setting->type == CONFIG_TYPE_LIST))
- {
- if(setting->value.list)
- __config_list_destroy(setting->value.list);
- }
- if(setting->hook && setting->config->destructor)
- setting->config->destructor(setting->hook);
- _delete(setting);
- }
- }
- /* ------------------------------------------------------------------------- */
- static void __config_list_destroy(config_list_t *list)
- {
- config_setting_t **p;
- unsigned int i;
- if(! list)
- return;
- if(list->elements)
- {
- for(p = list->elements, i = 0; i < list->length; p++, i++)
- __config_setting_destroy(*p);
- _delete(list->elements);
- }
- _delete(list);
- }
- /* ------------------------------------------------------------------------- */
- static int __config_vector_checktype(const config_setting_t *vector, int type)
- {
- /* if the array is empty, then it has no type yet */
- if(! vector->value.list)
- return(CONFIG_TRUE);
- if(vector->value.list->length == 0)
- return(CONFIG_TRUE);
- /* if it's a list, any type is allowed */
- if(vector->type == CONFIG_TYPE_LIST)
- return(CONFIG_TRUE);
- /* otherwise the first element added determines the type of the array */
- return((vector->value.list->elements[0]->type == type)
- ? CONFIG_TRUE : CONFIG_FALSE);
- }
- /* ------------------------------------------------------------------------- */
- static int __config_validate_name(const char *name)
- {
- const char *p = name;
- if(*p == '\0')
- return(CONFIG_FALSE);
- if(! isalpha((unsigned char)*p) && (*p != '*'))
- return(CONFIG_FALSE);
- for(++p; *p; ++p)
- {
- if(! (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || strchr("*_-", (int)*p)))
- return(CONFIG_FALSE);
- }
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- static int __config_read(config_t *config, FILE *stream, const char *filename,
- const char *str)
- {
- yyscan_t scanner;
- struct scan_context scan_ctx;
- struct parse_context parse_ctx;
- YY_BUFFER_STATE buffer = NULL;
- int r;
- /* Reinitialize the config */
- void (*destructor)(void *) = config->destructor;
- const char *include_dir = config->include_dir;
- unsigned short tab_width = config->tab_width;
- unsigned short flags = config->flags;
- config->include_dir = NULL;
- config_destroy(config);
- config_init(config);
- config->destructor = destructor;
- config->include_dir = include_dir;
- config->tab_width = tab_width;
- config->flags = flags;
- parsectx_init(&parse_ctx);
- parse_ctx.config = config;
- parse_ctx.parent = config->root;
- parse_ctx.setting = config->root;
- __config_locale_override();
- scanctx_init(&scan_ctx, filename);
- scan_ctx.config = config;
- libconfig_yylex_init_extra(&scan_ctx, &scanner);
- if(stream)
- libconfig_yyrestart(stream, scanner);
- else /* read from string */
- buffer = libconfig_yy_scan_string(str, scanner);
- libconfig_yyset_lineno(1, scanner);
- r = libconfig_yyparse(scanner, &parse_ctx, &scan_ctx);
- if(r != 0)
- {
- YY_BUFFER_STATE buf;
- config->error_file = scanctx_current_filename(&scan_ctx);
- config->error_type = CONFIG_ERR_PARSE;
- /* Unwind the include stack, freeing the buffers and closing the files. */
- while((buf = (YY_BUFFER_STATE)scanctx_pop_include(&scan_ctx)) != NULL)
- libconfig_yy_delete_buffer(buf, scanner);
- }
- libconfig_yylex_destroy(scanner);
- config->filenames = scanctx_cleanup(&scan_ctx, &(config->num_filenames));
- parsectx_cleanup(&parse_ctx);
- __config_locale_restore();
- return(r == 0 ? CONFIG_TRUE : CONFIG_FALSE);
- }
- /* ------------------------------------------------------------------------- */
- int config_read(config_t *config, FILE *stream)
- {
- return(__config_read(config, stream, NULL, NULL));
- }
- /* ------------------------------------------------------------------------- */
- int config_read_string(config_t *config, const char *str)
- {
- return(__config_read(config, NULL, NULL, str));
- }
- /* ------------------------------------------------------------------------- */
- static void __config_write_setting(const config_setting_t *setting,
- FILE *stream, int depth,
- unsigned short tab_width)
- {
- if(depth > 1)
- __config_indent(stream, depth, tab_width);
- if(setting->name)
- {
- fputs(setting->name, stream);
- fprintf(stream, " %c ", (setting->type == CONFIG_TYPE_GROUP ? ':' : '='));
- }
- __config_write_value(&(setting->value), setting->type,
- config_setting_get_format(setting),
- depth, tab_width, stream);
- if(depth > 0)
- {
- fputc(';', stream);
- fputc('\n', stream);
- }
- }
- /* ------------------------------------------------------------------------- */
- void config_write(const config_t *config, FILE *stream)
- {
- __config_locale_override();
- __config_write_setting(config->root, stream, 0, config->tab_width);
- __config_locale_restore();
- }
- /* ------------------------------------------------------------------------- */
- int config_read_file(config_t *config, const char *filename)
- {
- int ret;
- FILE *stream = fopen(filename, "rt");
- if(! stream)
- {
- config->error_text = __io_error;
- config->error_type = CONFIG_ERR_FILE_IO;
- return(CONFIG_FALSE);
- }
- ret = __config_read(config, stream, filename, NULL);
- fclose(stream);
- return(ret);
- }
- /* ------------------------------------------------------------------------- */
- int config_write_file(config_t *config, const char *filename)
- {
- FILE *f = fopen(filename, "wt");
- if(! f)
- {
- config->error_text = __io_error;
- config->error_type = CONFIG_ERR_FILE_IO;
- return(CONFIG_FALSE);
- }
- config_write(config, f);
- fclose(f);
- config->error_type = CONFIG_ERR_NONE;
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- void config_destroy(config_t *config)
- {
- unsigned int count = config->num_filenames;
- const char **f;
- __config_setting_destroy(config->root);
- for(f = config->filenames; count > 0; ++f, --count)
- _delete(*f);
- _delete(config->filenames);
- _delete(config->include_dir);
- memset((void *)config, 0, sizeof(config_t));
- }
- /* ------------------------------------------------------------------------- */
- void config_init(config_t *config)
- {
- memset((void *)config, 0, sizeof(config_t));
- config->root = _new(config_setting_t);
- config->root->type = CONFIG_TYPE_GROUP;
- config->root->config = config;
- config->tab_width = 2;
- }
- /* ------------------------------------------------------------------------- */
- void config_set_auto_convert(config_t *config, int flag)
- {
- if(flag)
- config->flags |= CONFIG_OPTION_AUTOCONVERT;
- else
- config->flags &= ~CONFIG_OPTION_AUTOCONVERT;
- }
- /* ------------------------------------------------------------------------- */
- int config_get_auto_convert(const config_t *config)
- {
- return((config->flags & CONFIG_OPTION_AUTOCONVERT) != 0);
- }
- /* ------------------------------------------------------------------------- */
- static config_setting_t *config_setting_create(config_setting_t *parent,
- const char *name, int type)
- {
- config_setting_t *setting;
- config_list_t *list;
- if((parent->type != CONFIG_TYPE_GROUP)
- && (parent->type != CONFIG_TYPE_ARRAY)
- && (parent->type != CONFIG_TYPE_LIST))
- return(NULL);
- setting = _new(config_setting_t);
- setting->parent = parent;
- setting->name = (name == NULL) ? NULL : strdup(name);
- setting->type = type;
- setting->config = parent->config;
- setting->hook = NULL;
- setting->line = 0;
- list = parent->value.list;
- if(! list)
- list = parent->value.list = _new(config_list_t);
- __config_list_add(list, setting);
- return(setting);
- }
- /* ------------------------------------------------------------------------- */
- static int __config_setting_get_int(const config_setting_t *setting,
- int *value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_INT:
- *value = setting->value.ival;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_INT64:
- if((setting->value.llval > INT32_MAX)
- || (setting->value.llval < INT32_MIN))
- *value = 0;
- else
- *value = (int)(setting->value.llval);
- return(CONFIG_TRUE);
- case CONFIG_TYPE_FLOAT:
- if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
- {
- *value = (int)(setting->value.fval);
- return(CONFIG_TRUE);
- }
- else
- { /* fall through */ }
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_get_int(const config_setting_t *setting)
- {
- int value = 0;
- __config_setting_get_int(setting, &value);
- return(value);
- }
- /* ------------------------------------------------------------------------- */
- static int __config_setting_get_int64(const config_setting_t *setting,
- long long *value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_INT64:
- *value = setting->value.llval;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_INT:
- *value = (long long)(setting->value.ival);
- return(CONFIG_TRUE);
- case CONFIG_TYPE_FLOAT:
- if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
- {
- *value = (long long)(setting->value.fval);
- return(CONFIG_TRUE);
- }
- else
- { /* fall through */ }
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- long long config_setting_get_int64(const config_setting_t *setting)
- {
- long long value = 0;
- __config_setting_get_int64(setting, &value);
- return(value);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_lookup_int(const config_setting_t *setting,
- const char *name, int *value)
- {
- config_setting_t *member = config_setting_get_member(setting, name);
- if(! member)
- return(CONFIG_FALSE);
- return(__config_setting_get_int(member, value));
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_lookup_int64(const config_setting_t *setting,
- const char *name, long long *value)
- {
- config_setting_t *member = config_setting_get_member(setting, name);
- if(! member)
- return(CONFIG_FALSE);
- return(__config_setting_get_int64(member, value));
- }
- /* ------------------------------------------------------------------------- */
- static int __config_setting_get_float(const config_setting_t *setting,
- double *value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_FLOAT:
- *value = setting->value.fval;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_INT:
- if(config_get_auto_convert(setting->config))
- {
- *value = (double)(setting->value.ival);
- return(CONFIG_TRUE);
- }
- else
- return(CONFIG_FALSE);
- case CONFIG_TYPE_INT64:
- if(config_get_auto_convert(setting->config))
- {
- *value = (double)(setting->value.llval);
- return(CONFIG_TRUE);
- }
- else
- { /* fall through */ }
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- double config_setting_get_float(const config_setting_t *setting)
- {
- double value = 0.0;
- __config_setting_get_float(setting, &value);
- return(value);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_lookup_float(const config_setting_t *setting,
- const char *name, double *value)
- {
- config_setting_t *member = config_setting_get_member(setting, name);
- if(! member)
- return(CONFIG_FALSE);
- return(__config_setting_get_float(member, value));
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_lookup_string(const config_setting_t *setting,
- const char *name, const char **value)
- {
- config_setting_t *member = config_setting_get_member(setting, name);
- if(! member)
- return(CONFIG_FALSE);
- if(config_setting_type(member) != CONFIG_TYPE_STRING)
- return(CONFIG_FALSE);
- *value = config_setting_get_string(member);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_lookup_bool(const config_setting_t *setting,
- const char *name, int *value)
- {
- config_setting_t *member = config_setting_get_member(setting, name);
- if(! member)
- return(CONFIG_FALSE);
- if(config_setting_type(member) != CONFIG_TYPE_BOOL)
- return(CONFIG_FALSE);
- *value = config_setting_get_bool(member);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_int(config_setting_t *setting, int value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_NONE:
- setting->type = CONFIG_TYPE_INT;
- /* fall through */
- case CONFIG_TYPE_INT:
- setting->value.ival = value;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_FLOAT:
- if(config_get_auto_convert(setting->config))
- {
- setting->value.fval = (float)value;
- return(CONFIG_TRUE);
- }
- else
- return(CONFIG_FALSE);
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_int64(config_setting_t *setting, long long value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_NONE:
- setting->type = CONFIG_TYPE_INT64;
- /* fall through */
- case CONFIG_TYPE_INT64:
- setting->value.llval = value;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_INT:
- if((value > INT32_MAX) || (value < INT32_MIN))
- setting->value.ival = 0;
- else
- setting->value.ival = (int)value;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_FLOAT:
- if(config_get_auto_convert(setting->config))
- {
- setting->value.fval = (float)value;
- return(CONFIG_TRUE);
- }
- else
- return(CONFIG_FALSE);
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_float(config_setting_t *setting, double value)
- {
- switch(setting->type)
- {
- case CONFIG_TYPE_NONE:
- setting->type = CONFIG_TYPE_FLOAT;
- /* fall through */
- case CONFIG_TYPE_FLOAT:
- setting->value.fval = value;
- return(CONFIG_TRUE);
- case CONFIG_TYPE_INT:
- if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
- {
- setting->value.ival = (int)value;
- return(CONFIG_TRUE);
- }
- else
- return(CONFIG_FALSE);
- case CONFIG_TYPE_INT64:
- if((setting->config->flags & CONFIG_OPTION_AUTOCONVERT) != 0)
- {
- setting->value.llval = (long long)value;
- return(CONFIG_TRUE);
- }
- else
- return(CONFIG_FALSE);
- default:
- return(CONFIG_FALSE);
- }
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_get_bool(const config_setting_t *setting)
- {
- return((setting->type == CONFIG_TYPE_BOOL) ? setting->value.ival : 0);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_bool(config_setting_t *setting, int value)
- {
- if(setting->type == CONFIG_TYPE_NONE)
- setting->type = CONFIG_TYPE_BOOL;
- else if(setting->type != CONFIG_TYPE_BOOL)
- return(CONFIG_FALSE);
- setting->value.ival = value;
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- const char *config_setting_get_string(const config_setting_t *setting)
- {
- return((setting->type == CONFIG_TYPE_STRING) ? setting->value.sval : NULL);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_string(config_setting_t *setting, const char *value)
- {
- if(setting->type == CONFIG_TYPE_NONE)
- setting->type = CONFIG_TYPE_STRING;
- else if(setting->type != CONFIG_TYPE_STRING)
- return(CONFIG_FALSE);
- if(setting->value.sval)
- _delete(setting->value.sval);
- setting->value.sval = (value == NULL) ? NULL : strdup(value);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_set_format(config_setting_t *setting, short format)
- {
- if(((setting->type != CONFIG_TYPE_INT)
- && (setting->type != CONFIG_TYPE_INT64))
- || ((format != CONFIG_FORMAT_DEFAULT) && (format != CONFIG_FORMAT_HEX)))
- return(CONFIG_FALSE);
- setting->format = format;
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- short config_setting_get_format(const config_setting_t *setting)
- {
- return(setting->format != 0 ? setting->format
- : setting->config->default_format);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_lookup_from(config_setting_t *setting,
- const char *path)
- {
- const char *p = path;
- config_setting_t *found;
- for(;;)
- {
- while(*p && strchr(PATH_TOKENS, *p))
- p++;
- if(! *p)
- break;
- if(*p == '[')
- found = config_setting_get_elem(setting, atoi(++p));
- else
- found = config_setting_get_member(setting, p);
- if(! found)
- break;
- setting = found;
- while(! strchr(PATH_TOKENS, *p))
- p++;
- }
- return(*p ? NULL : setting);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_lookup(const config_t *config, const char *path)
- {
- return(config_lookup_from(config->root, path));
- }
- /* ------------------------------------------------------------------------- */
- int config_lookup_string(const config_t *config, const char *path,
- const char **value)
- {
- const config_setting_t *s = config_lookup(config, path);
- if(! s)
- return(CONFIG_FALSE);
- if(config_setting_type(s) != CONFIG_TYPE_STRING)
- return(CONFIG_FALSE);
- *value = config_setting_get_string(s);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_lookup_int(const config_t *config, const char *path,
- int *value)
- {
- const config_setting_t *s = config_lookup(config, path);
- if(! s)
- return(CONFIG_FALSE);
- return(__config_setting_get_int(s, value));
- }
- /* ------------------------------------------------------------------------- */
- int config_lookup_int64(const config_t *config, const char *path,
- long long *value)
- {
- const config_setting_t *s = config_lookup(config, path);
- if(! s)
- return(CONFIG_FALSE);
- return(__config_setting_get_int64(s, value));
- }
- /* ------------------------------------------------------------------------- */
- int config_lookup_float(const config_t *config, const char *path,
- double *value)
- {
- const config_setting_t *s = config_lookup(config, path);
- if(! s)
- return(CONFIG_FALSE);
- return(__config_setting_get_float(s, value));
- }
- /* ------------------------------------------------------------------------- */
- int config_lookup_bool(const config_t *config, const char *path, int *value)
- {
- const config_setting_t *s = config_lookup(config, path);
- if(! s)
- return(CONFIG_FALSE);
- if(config_setting_type(s) != CONFIG_TYPE_BOOL)
- return(CONFIG_FALSE);
- *value = config_setting_get_bool(s);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_get_int_elem(const config_setting_t *vector, int idx)
- {
- const config_setting_t *element = config_setting_get_elem(vector, idx);
- return(element ? config_setting_get_int(element) : 0);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_set_int_elem(config_setting_t *vector,
- int idx, int value)
- {
- config_setting_t *element = NULL;
- if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
- return(NULL);
- if(idx < 0)
- {
- if(! __config_vector_checktype(vector, CONFIG_TYPE_INT))
- return(NULL);
- element = config_setting_create(vector, NULL, CONFIG_TYPE_INT);
- }
- else
- {
- element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- }
- if(! config_setting_set_int(element, value))
- return(NULL);
- return(element);
- }
- /* ------------------------------------------------------------------------- */
- long long config_setting_get_int64_elem(const config_setting_t *vector,
- int idx)
- {
- const config_setting_t *element = config_setting_get_elem(vector, idx);
- return(element ? config_setting_get_int64(element) : 0);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_set_int64_elem(config_setting_t *vector,
- int idx, long long value)
- {
- config_setting_t *element = NULL;
- if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
- return(NULL);
- if(idx < 0)
- {
- if(! __config_vector_checktype(vector, CONFIG_TYPE_INT64))
- return(NULL);
- element = config_setting_create(vector, NULL, CONFIG_TYPE_INT64);
- }
- else
- {
- element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- }
- if(! config_setting_set_int64(element, value))
- return(NULL);
- return(element);
- }
- /* ------------------------------------------------------------------------- */
- double config_setting_get_float_elem(const config_setting_t *vector, int idx)
- {
- config_setting_t *element = config_setting_get_elem(vector, idx);
- return(element ? config_setting_get_float(element) : 0.0);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_set_float_elem(config_setting_t *vector,
- int idx, double value)
- {
- config_setting_t *element = NULL;
- if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
- return(NULL);
- if(idx < 0)
- {
- if(! __config_vector_checktype(vector, CONFIG_TYPE_FLOAT))
- return(NULL);
- element = config_setting_create(vector, NULL, CONFIG_TYPE_FLOAT);
- }
- else
- element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- if(! config_setting_set_float(element, value))
- return(NULL);
- return(element);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_get_bool_elem(const config_setting_t *vector, int idx)
- {
- config_setting_t *element = config_setting_get_elem(vector, idx);
- if(! element)
- return(CONFIG_FALSE);
- if(element->type != CONFIG_TYPE_BOOL)
- return(CONFIG_FALSE);
- return(element->value.ival);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_set_bool_elem(config_setting_t *vector,
- int idx, int value)
- {
- config_setting_t *element = NULL;
- if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
- return(NULL);
- if(idx < 0)
- {
- if(! __config_vector_checktype(vector, CONFIG_TYPE_BOOL))
- return(NULL);
- element = config_setting_create(vector, NULL, CONFIG_TYPE_BOOL);
- }
- else
- element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- if(! config_setting_set_bool(element, value))
- return(NULL);
- return(element);
- }
- /* ------------------------------------------------------------------------- */
- const char *config_setting_get_string_elem(const config_setting_t *vector,
- int idx)
- {
- config_setting_t *element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- if(element->type != CONFIG_TYPE_STRING)
- return(NULL);
- return(element->value.sval);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_set_string_elem(config_setting_t *vector,
- int idx, const char *value)
- {
- config_setting_t *element = NULL;
- if((vector->type != CONFIG_TYPE_ARRAY) && (vector->type != CONFIG_TYPE_LIST))
- return(NULL);
- if(idx < 0)
- {
- if(! __config_vector_checktype(vector, CONFIG_TYPE_STRING))
- return(NULL);
- element = config_setting_create(vector, NULL, CONFIG_TYPE_STRING);
- }
- else
- element = config_setting_get_elem(vector, idx);
- if(! element)
- return(NULL);
- if(! config_setting_set_string(element, value))
- return(NULL);
- return(element);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_get_elem(const config_setting_t *vector,
- unsigned int idx)
- {
- config_list_t *list = vector->value.list;
- if(((vector->type != CONFIG_TYPE_ARRAY)
- && (vector->type != CONFIG_TYPE_LIST)
- && (vector->type != CONFIG_TYPE_GROUP)) || ! list)
- return(NULL);
- if(idx >= list->length)
- return(NULL);
- return(list->elements[idx]);
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_get_member(const config_setting_t *setting,
- const char *name)
- {
- if(setting->type != CONFIG_TYPE_GROUP)
- return(NULL);
- return(__config_list_search(setting->value.list, name, NULL));
- }
- /* ------------------------------------------------------------------------- */
- void config_set_destructor(config_t *config, void (*destructor)(void *))
- {
- config->destructor = destructor;
- }
- /* ------------------------------------------------------------------------- */
- void config_set_include_dir(config_t *config, const char *include_dir)
- {
- _delete(config->include_dir);
- config->include_dir = strdup(include_dir);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_length(const config_setting_t *setting)
- {
- if((setting->type != CONFIG_TYPE_GROUP)
- && (setting->type != CONFIG_TYPE_ARRAY)
- && (setting->type != CONFIG_TYPE_LIST))
- return(0);
- if(! setting->value.list)
- return(0);
- return(setting->value.list->length);
- }
- /* ------------------------------------------------------------------------- */
- void config_setting_set_hook(config_setting_t *setting, void *hook)
- {
- setting->hook = hook;
- }
- /* ------------------------------------------------------------------------- */
- config_setting_t *config_setting_add(config_setting_t *parent,
- const char *name, int type)
- {
- if((type < CONFIG_TYPE_NONE) || (type > CONFIG_TYPE_LIST))
- return(NULL);
- if(! parent)
- return(NULL);
- if((parent->type == CONFIG_TYPE_ARRAY) || (parent->type == CONFIG_TYPE_LIST))
- name = NULL;
- if(name)
- {
- if(! __config_validate_name(name))
- return(NULL);
- }
- if(config_setting_get_member(parent, name) != NULL)
- return(NULL); /* already exists */
- return(config_setting_create(parent, name, type));
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_remove(config_setting_t *parent, const char *name)
- {
- unsigned int idx;
- config_setting_t *setting;
- if(! parent)
- return(CONFIG_FALSE);
- if(parent->type != CONFIG_TYPE_GROUP)
- return(CONFIG_FALSE);
- if(! (setting = __config_list_search(parent->value.list, name, &idx)))
- return(CONFIG_FALSE);
- __config_list_remove(parent->value.list, idx);
- __config_setting_destroy(setting);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_remove_elem(config_setting_t *parent, unsigned int idx)
- {
- config_list_t *list;
- config_setting_t *removed = NULL;
- if(! parent)
- return(CONFIG_FALSE);
- list = parent->value.list;
- if(((parent->type != CONFIG_TYPE_ARRAY)
- && (parent->type != CONFIG_TYPE_LIST)
- && (parent->type != CONFIG_TYPE_GROUP)) || ! list)
- return(CONFIG_FALSE);
- if(idx >= list->length)
- return(CONFIG_FALSE);
- removed = __config_list_remove(list, idx);
- __config_setting_destroy(removed);
- return(CONFIG_TRUE);
- }
- /* ------------------------------------------------------------------------- */
- int config_setting_index(const config_setting_t *setting)
- {
- config_setting_t **found = NULL;
- config_list_t *list;
- int i;
- if(! setting->parent)
- return(-1);
- list = setting->parent->value.list;
- for(i = 0, found = list->elements; i < (int)list->length; ++i, ++found)
- {
- if(*found == setting)
- return(i);
- }
- return(-1);
- }
- /* ------------------------------------------------------------------------- */
- /* eof */
|