From 179d8a02e231fe853831cdf886c9196929303adb Mon Sep 17 00:00:00 2001 From: cnlohr Date: Sun, 18 Mar 2018 02:45:09 -0400 Subject: Moving things over... still todo: * Make functions for changing temp configs. * Make functions for config_read_str but where it checks the temp one first. * Parse command-line options. --- src/survive.c | 19 ++++++++++++------- src/survive_cal.c | 4 ++-- src/survive_config.c | 30 ++++++++++++++++++++++++++++++ src/survive_config.h | 7 +++++++ 4 files changed, 51 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/survive.c b/src/survive.c index e075423..327b7f8 100755 --- a/src/survive.c +++ b/src/survive.c @@ -104,7 +104,7 @@ void survive_verify_FLT_size(uint32_t user_size) { } } -SurviveContext * survive_init_internal( SurviveInitData * initdata ) +SurviveContext * survive_init_internal( int argc, char * const * argv ) { #ifdef RUNTIME_SYMNUM @@ -130,16 +130,19 @@ SurviveContext * survive_init_internal( SurviveInitData * initdata ) ctx->state = SURVIVE_STOPPED; ctx->global_config_values = malloc( sizeof(config_group) ); + ctx->temporary_config_values = malloc( sizeof(config_group) ); ctx->lh_config = malloc( sizeof(config_group) * NUM_LIGHTHOUSES); //initdata // ->argc ->argp init_config_group(ctx->global_config_values,10); + init_config_group(ctx->temporary_config_values,20); init_config_group(&ctx->lh_config[0],10); init_config_group(&ctx->lh_config[1],10); - config_read(ctx, "config.json"); - ctx->activeLighthouses = config_read_uint32(ctx->global_config_values, "LighthouseCount", 2); + config_read(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); + ctx->activeLighthouses = survive_config_readi( ctx, "lighthousecount", 2 ); + config_read_lighthouse(ctx->lh_config, &(ctx->bsd[0]), 0); config_read_lighthouse(ctx->lh_config, &(ctx->bsd[1]), 1); @@ -169,8 +172,8 @@ int survive_startup( SurviveContext * ctx ) const char * DriverName; - //const char * PreferredPoser = config_read_str(ctx->global_config_values, "DefaultPoser", "PoserDummy"); - const char * PreferredPoser = config_read_str(ctx->global_config_values, "DefaultPoser", "PoserTurveyTori"); + //const char * PreferredPoser = survive_config_reads(ctx->global_config_values, "defaultposer", "PoserDummy"); + const char * PreferredPoser = survive_config_reads( ctx, "defaultposer", "PoserTurveyTori"); PoserCB PreferredPoserCB = 0; const char * FirstPoser = 0; printf( "Available posers:\n" ); @@ -203,7 +206,7 @@ int survive_startup( SurviveContext * ctx ) } // saving the config extra to make sure that the user has a config file they can change. - config_save(ctx, "config.json"); + config_save(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); ctx->state = SURVIVE_RUNNING; @@ -364,9 +367,10 @@ void survive_close( SurviveContext * ctx ) } - config_save(ctx, "config.json"); + config_save(ctx, survive_config_reads( ctx, "configfile", "config.json" ) ); destroy_config_group(ctx->global_config_values); + destroy_config_group(ctx->temporary_config_values); destroy_config_group(ctx->lh_config); for (i = 0; i < ctx->objs_ct; i++) { @@ -379,6 +383,7 @@ void survive_close( SurviveContext * ctx ) free( ctx->drivermagics ); free( ctx->drivercloses ); free( ctx->global_config_values ); + free( ctx->temporary_config_values ); free( ctx->lh_config ); free( ctx ); diff --git a/src/survive_cal.c b/src/survive_cal.c index 2fd96ef..6f556f3 100755 --- a/src/survive_cal.c +++ b/src/survive_cal.c @@ -178,8 +178,8 @@ void survive_cal_install( struct SurviveContext * ctx ) } const char * DriverName; -// const char * PreferredPoser = config_read_str(ctx->global_config_values, "ConfigPoser", "PoserCharlesSlow"); - const char * PreferredPoser = config_read_str(ctx->global_config_values, "ConfigPoser", "PoserTurveyTori"); +// const char * PreferredPoser = survive_config_reads(ctx, "configposer", "PoserCharlesSlow"); + const char * PreferredPoser = survive_config_reads(ctx, "configposer", "PoserTurveyTori"); PoserCB PreferredPoserCB = 0; const char * FirstPoser = 0; printf( "Available posers:\n" ); diff --git a/src/survive_config.c b/src/survive_config.c index d923ba4..4ef2994 100644 --- a/src/survive_config.c +++ b/src/survive_config.c @@ -181,6 +181,36 @@ FLT config_read_float(config_group *cg, const char *tag, const FLT def) { return config_set_float(cg, tag, def); } +static config_entry * sc_search(SurviveContext * ctx, const char *tag ) +{ + config_entry *cv = find_config_entry(ctx->temporary_config_values, tag); + if( !cv ) cv = find_config_entry(ctx->global_config_values, tag); + return cv; +} + +FLT survive_config_readf( SurviveContext * ctx, const char *tag, FLT def ) +{ + config_entry * cv = sc_search( ctx, tag ); + if( !cv ) return def; + return cv->numeric.f; +} + +uint32_t survive_config_readi( SurviveContext * ctx, const char *tag, uint32_t def ) +{ + config_entry * cv = sc_search( ctx, tag ); + if( !cv ) return def; + return cv->numeric.i; +} + +const char * survive_config_reads( SurviveContext * ctx, const char *tag, const char *def ) +{ + config_entry * cv = sc_search( ctx, tag ); + if( !cv ) return def; + return cv->data; +} + + + // TODO: Do something better than this: #define CFG_MIN(x,y) ((x) < (y)? (x): (y)) diff --git a/src/survive_config.h b/src/survive_config.h index e2686e9..1ae124d 100644 --- a/src/survive_config.h +++ b/src/survive_config.h @@ -52,9 +52,16 @@ const FLT config_set_float(config_group *cg, const char *tag, const FLT value); const uint32_t config_set_uint32(config_group *cg, const char *tag, const uint32_t value); const char* config_set_str(config_group *cg, const char *tag, const char* value); +//These functions look for a parameter in a specific group, and then chose the best to return. If the parameter does not exist, default will be written. FLT config_read_float(config_group *cg, const char *tag, const FLT def); uint16_t config_read_float_array(config_group *cg, const char *tag, FLT* values, const FLT* def, uint8_t count); uint32_t config_read_uint32(config_group *cg, const char *tag, const uint32_t def); const char* config_read_str(config_group *cg, const char *tag, const char *def); +//These functions search both the stored-general and temporary sections for a parameter and return it. +//FLT survive_config_readf( SurviveContext * ctx, const char *tag, FLT def ); +//uint32_t survive_config_readi( SurviveContext * ctx, const char *tag, uint32_t def ); +//const char * survive_config_reads( SurviveContext * ctx, const char *tag, const char *def ); +//They're actually defined in survive.h for users as well. + #endif -- cgit v1.2.3