12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619 |
- /* ----------------------------------------------------------------------------
- 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 Lesser General Public License
- as published by the Free Software Foundation; either version 2.1 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
- Lesser General Public License for more details.
- You should have received a copy of the GNU Library 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((int)*p) && (*p != '*'))
- return(CONFIG_FALSE);
- for(++p; *p; ++p)
- {
- if(! (isalpha((int)*p) || isdigit((int)*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 */
|