/** * Checks if the configuration is valid. The function checks if all * required parameters are set and adds default values if necessary. * @param cfg configuration */ void config_check(config_t *cfg) { int i, j; const char *s; double f; config_setting_t *cs, *vs; for (i = 0; defaults[i].name; i++) { /* Lookup setting group */ cs = config_lookup(cfg, defaults[i].group); if (!cs) cs = config_setting_add(config_root_setting(cfg), defaults[i].group, CONFIG_TYPE_GROUP); /* (1) Check for string */ if (defaults[i].type == CONFIG_TYPE_STRING) { if (config_setting_lookup_string(cs, defaults[i].name, &s)) continue; /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_STRING); config_setting_set_string(vs, defaults[i].val.sval); /* (2) Check for float */ } else if (defaults[i].type == CONFIG_TYPE_FLOAT) { if (config_setting_lookup_float(cs, defaults[i].name, &f)) continue; /* Check for mis-interpreted integer */ if (config_setting_lookup_int(cs, defaults[i].name, &j)) { config_setting_remove(cs, defaults[i].name); vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_FLOAT); config_setting_set_float(vs, (double) j); continue; } /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_FLOAT); config_setting_set_float(vs, defaults[i].val.fval); /* (3) Check for integer */ } else if (defaults[i].type == CONFIG_TYPE_INT) { if (config_setting_lookup_int(cs, defaults[i].name, &j)) continue; /* Check for mis-interpreted float */ if (config_setting_lookup_float(cs, defaults[i].name, &f)) { config_setting_remove(cs, defaults[i].name); vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_INT); config_setting_set_int(vs, (long) round(f)); continue; } /* Add default value */ vs = config_setting_add(cs, defaults[i].name, CONFIG_TYPE_INT); config_setting_set_int(vs, defaults[i].val.ival); } else { error("Unknown configuration type"); } } }
int main(int argc, char **argv) { static const char *output_file = "updated.cfg"; config_t cfg; config_setting_t *root, *setting, *movie; config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ if(! config_read_file(&cfg, "example.cfg")) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return(EXIT_FAILURE); } /* Find the 'movies' setting. Add intermediate settings if they don't yet * exist. */ root = config_root_setting(&cfg); setting = config_setting_get_member(root, "inventory"); if(!setting) setting = config_setting_add(root, "inventory", CONFIG_TYPE_GROUP); setting = config_setting_get_member(setting, "movies"); if(!setting) setting = config_setting_add(setting, "movies", CONFIG_TYPE_LIST); /* Create the new movie entry. */ movie = config_setting_add(setting, NULL, CONFIG_TYPE_GROUP); setting = config_setting_add(movie, "title", CONFIG_TYPE_STRING); config_setting_set_string(setting, "Buckaroo Banzai"); setting = config_setting_add(movie, "media", CONFIG_TYPE_STRING); config_setting_set_string(setting, "DVD"); setting = config_setting_add(movie, "price", CONFIG_TYPE_FLOAT); config_setting_set_float(setting, 12.99); setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT); config_setting_set_float(setting, 20); /* Write out the updated configuration. */ if(! config_write_file(&cfg, output_file)) { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); return(EXIT_FAILURE); } fprintf(stderr, "Updated configuration successfully written to: %s\n", output_file); config_destroy(&cfg); return(EXIT_SUCCESS); }
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); }
void config_setting_copy_simple(config_setting_t *parent, const config_setting_t *src) { if (config_setting_is_aggregate(src)) { config_setting_copy_aggregate(parent, src); } else { config_setting_t *set; if( config_setting_get_member(parent, config_setting_name(src)) != NULL ) return; if ((set = config_setting_add(parent, config_setting_name(src), config_setting_type(src))) == NULL) return; if (CONFIG_TYPE_INT == config_setting_type(src)) { config_setting_set_int(set, config_setting_get_int(src)); config_setting_set_format(set, src->format); } else if (CONFIG_TYPE_INT64 == config_setting_type(src)) { config_setting_set_int64(set, config_setting_get_int64(src)); config_setting_set_format(set, src->format); } else if (CONFIG_TYPE_FLOAT == config_setting_type(src)) { config_setting_set_float(set, config_setting_get_float(src)); } else if (CONFIG_TYPE_STRING == config_setting_type(src)) { config_setting_set_string(set, config_setting_get_string(src)); } else if (CONFIG_TYPE_BOOL == config_setting_type(src)) { config_setting_set_bool(set, config_setting_get_bool(src)); } } }
void MainDialog::configSetFloat(const char* key, double val) { config_setting_t* setting = config_lookup(&config_, key); if(!setting) { // setting not found // add a new setting for it config_setting_t* root = config_root_setting(&config_); setting = config_setting_add(root, key, CONFIG_TYPE_FLOAT); } config_setting_set_float(setting, val); }
/********************* return RET_NOK on error *********************/ ret_code_t entry_copy_config(config_setting_t * source, config_setting_t * dest) { config_setting_t * new_source; config_setting_t * new_dest; int index = -1; int int_value; long long long_value; double double_value; const char * string; while((new_source=config_setting_get_elem(source,index+1))!= NULL ) { index++; if(config_setting_is_group(new_source)) { if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_GROUP)) { return RET_NOK; } } else if(config_setting_is_array(new_source)) { if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_ARRAY)) { return RET_NOK; } } else if(config_setting_is_list(new_source)) { if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_LIST)) { return RET_NOK; } } else { switch(config_setting_type(new_source)) { case CONFIG_TYPE_INT: int_value = config_setting_get_int(new_source); new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT); config_setting_set_int(new_dest,int_value); continue; case CONFIG_TYPE_INT64: long_value = config_setting_get_int64(new_source); new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT64); config_setting_set_int64(new_dest,long_value); continue; case CONFIG_TYPE_FLOAT: double_value = config_setting_get_float(new_source); new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_FLOAT); config_setting_set_float(new_dest,double_value); continue; case CONFIG_TYPE_BOOL: int_value = config_setting_get_bool(new_source); new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_BOOL); config_setting_set_bool(new_dest,int_value); continue; case CONFIG_TYPE_STRING: string = config_setting_get_string(new_source); new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_STRING); config_setting_set_string(new_dest,string); continue; default: return RET_NOK; } } } return RET_OK; }
int writeCfgState(void) { config_t cfg; config_setting_t *ttempState = 0; config_setting_t *thumState = 0; config_setting_t *tRHeatTS = 0; config_setting_t *tRHumTS = 0; config_setting_t *tRHepaTS = 0; config_setting_t *tRFanTS = 0; config_setting_t *twfactor = 0; config_setting_t *trelay1o = 0; config_setting_t *trelay2o = 0; config_setting_t *trelay3o = 0; config_setting_t *trelay4o = 0; config_setting_t *trelay5o = 0; config_setting_t *trelay6o = 0; config_setting_t *trelay7o = 0; config_setting_t *trelay8o = 0; config_init(&cfg); if (!config_read_file(&cfg, config_state_path)) { printf("\n%s:%d - %s", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return -1; } /* lookup variables in config file */ ttempState = config_lookup(&cfg, "tempState"); thumState = config_lookup(&cfg, "humState"); trelay1o = config_lookup(&cfg, "relay1o"); trelay2o = config_lookup(&cfg, "relay2o"); trelay3o = config_lookup(&cfg, "relay3o"); trelay4o = config_lookup(&cfg, "relay4o"); trelay5o = config_lookup(&cfg, "relay5o"); trelay6o = config_lookup(&cfg, "relay6o"); trelay7o = config_lookup(&cfg, "relay7o"); trelay8o = config_lookup(&cfg, "relay8o"); tRHeatTS = config_lookup(&cfg, "RHeatTS"); tRHumTS = config_lookup(&cfg, "RHumTS"); tRHepaTS = config_lookup(&cfg, "RHepaTS"); tRFanTS = config_lookup(&cfg, "RFanTS"); twfactor = config_lookup(&cfg, "wfactor"); /* get variables from config file then print the variables that changed */ if (config_setting_get_int(ttempState) != tempState) { printf("\ntempState: %i -> ", config_setting_get_int(ttempState)); config_setting_set_int(ttempState, tempState); printf("%i", config_setting_get_int(ttempState)); } if (config_setting_get_int(thumState) != humState) { printf("\nhumState: %i -> ", config_setting_get_int(thumState)); config_setting_set_int(thumState, humState); printf("%i", config_setting_get_int(thumState)); } if (config_setting_get_int(trelay1o) != relay1o) { printf("\nrelay1o: %i -> ", config_setting_get_int(trelay1o)); config_setting_set_int(trelay1o, relay1o); printf("%i", config_setting_get_int(trelay1o)); } if (config_setting_get_int(trelay2o) != relay2o) { printf("\nrelay2o: %i -> ", config_setting_get_int(trelay2o)); config_setting_set_int(trelay2o, relay2o); printf("%i", config_setting_get_int(trelay2o)); } if (config_setting_get_int(trelay3o) != relay3o) { printf("\nrelay3o: %i -> ", config_setting_get_int(trelay3o)); config_setting_set_int(trelay3o, relay3o); printf("%i", config_setting_get_int(trelay3o)); } if (config_setting_get_int(trelay4o) != relay4o) { printf("\nrelay4o: %i -> ", config_setting_get_int(trelay4o)); config_setting_set_int(trelay4o, relay4o); printf("%i", config_setting_get_int(trelay4o)); } if (config_setting_get_int(trelay5o) != relay5o) { printf("\nrelay5o: %i -> ", config_setting_get_int(trelay5o)); config_setting_set_int(trelay5o, relay5o); printf("%i", config_setting_get_int(trelay5o)); } if (config_setting_get_int(trelay6o) != relay6o) { printf("\nrelay6o: %i -> ", config_setting_get_int(trelay6o)); config_setting_set_int(trelay6o, relay6o); printf("%i", config_setting_get_int(trelay6o)); } if (config_setting_get_int(trelay7o) != relay7o) { printf("\nrelay7o: %i -> ", config_setting_get_int(trelay7o)); config_setting_set_int(trelay7o, relay7o); printf("%i", config_setting_get_int(trelay7o)); } if (config_setting_get_int(trelay8o) != relay8o) { printf("\nrelay8o: %i -> ", config_setting_get_int(trelay8o)); config_setting_set_int(trelay8o, relay8o); printf("%i", config_setting_get_int(trelay8o)); } if (config_setting_get_int(tRHeatTS) != RHeatTS) { printf("\nRHeatTS: %i -> ", config_setting_get_int(tRHeatTS)); config_setting_set_int(tRHeatTS, RHeatTS); printf("%i", config_setting_get_int(tRHeatTS)); } if (config_setting_get_int(tRHumTS) != RHumTS) { printf("\nRHumTS: %i -> ", config_setting_get_int(tRHumTS)); config_setting_set_int(tRHumTS, RHumTS); printf("%i", config_setting_get_int(tRHumTS)); } if (config_setting_get_int(tRHepaTS) != RHepaTS) { printf("\nRHepaTS: %i -> ", config_setting_get_int(tRHepaTS)); config_setting_set_int(tRHepaTS, RHepaTS); printf("%i", config_setting_get_int(tRHepaTS)); } if (config_setting_get_int(tRFanTS) != RFanTS) { printf("\nRFanTS: %i -> ", config_setting_get_int(tRFanTS)); config_setting_set_int(tRFanTS, RFanTS); printf("%i", config_setting_get_int(tRFanTS)); } if (config_setting_get_float(twfactor) != wfactor) { printf("\nwfactor: %i -> ", config_setting_get_float(twfactor)); config_setting_set_float(twfactor, wfactor); printf("%i", config_setting_get_float(twfactor)); } /* write the modified config file */ config_write_file(&cfg, config_state_path); config_destroy(&cfg); return 0; }
// load the default configuration in cfg static void set_default_config(config_t * cfg) { static const unsigned AACCESSES = 4 * 1024 * 1024; static const unsigned BEGIN = BEGIN_INIT; static const char SPAWN[] = "linear"; static const long long END = END_INIT; static const float FREQUENCY = 1; static const bool LOGGING = false; static const char LOGFILE[] = "adhd_log"; static const char PATTERN[] = "random"; static const unsigned PROCESSES = 1; static const unsigned REPETITIONS = 50; static const long long STEP = STEP_INIT; static const char SCALING[] = "linear"; static const bool SILENT = false; static const unsigned THREADS = 1; static const long long STREAM_STEP = 10 * (1 << 20); static const long long STREAM_END = 100 * (1 << 20); static const long long FLOPS_STEP = 10 * (1 << 10); static const long long FLOPS_END = 1 * (1 << 20); static const long long FLOPS_CALCULATIONS = 1000 * 1000 * 1000; config_setting_t *setting; config_setting_t *root; root = config_root_setting(cfg); // generic group { config_setting_t *generic = config_setting_add(root, FLD_GENERIC_GROUP, CONFIG_TYPE_GROUP); // array of names of enabled programs (uses corresponding group names for // the sake of simplicity) { config_setting_t *programs = config_setting_add(generic, FLD_PROGRAMS_GROUP, CONFIG_TYPE_ARRAY); static const char *names[] = { FLD_WALKARRAY_GROUP, FLD_STREAMARRAY_GROUP, FLD_FLOPSARRAY_GROUP, NULL // mark end of list }; for (size_t i = 0; names[i]; ++i) { setting = config_setting_add(programs, NULL, CONFIG_TYPE_STRING); config_setting_set_string(setting, names[i]); } } // CPU frequency to calculate cycle estimate // TODO: use actual cycle counter in the CPU // (this is a feature with a design impact, as SMP complicates matters ... :)) setting = config_setting_add(generic, FLD_GENERIC_GPU_FREQUENCY, CONFIG_TYPE_FLOAT); config_setting_set_float(setting, FREQUENCY); // logging enabled? setting = config_setting_add(generic, FLD_GENERIC_LOGGING, CONFIG_TYPE_BOOL); config_setting_set_bool(setting, LOGGING); // base filename to log process timings to setting = config_setting_add(generic, FLD_GENERIC_LOGFILE, CONFIG_TYPE_STRING); config_setting_set_string(setting, LOGFILE); // process spawn method setting = config_setting_add(generic, FLD_GENERIC_SPAWN, CONFIG_TYPE_STRING); config_setting_set_string(setting, SPAWN); // number of processes to run: begin // TODO: just like arrays we could have a linear or exponential increment setting = config_setting_add(generic, FLD_GENERIC_PROCESSES_BEGIN, CONFIG_TYPE_INT64); config_setting_set_int64(setting, PROCESSES); // number of processes to run: end setting = config_setting_add(generic, FLD_GENERIC_PROCESSES_END, CONFIG_TYPE_INT64); config_setting_set_int64(setting, PROCESSES); // number of threads per process to run: begin // TODO: just like arrays we could have a linear or exponential increment setting = config_setting_add(generic, FLD_GENERIC_THREADS_BEGIN, CONFIG_TYPE_INT64); config_setting_set_int64(setting, THREADS); // number of threads per process to run: end setting = config_setting_add(generic, FLD_GENERIC_THREADS_END, CONFIG_TYPE_INT64); config_setting_set_int64(setting, THREADS); // silent mode setting = config_setting_add(generic, FLD_GENERIC_SILENT, CONFIG_TYPE_BOOL); config_setting_set_bool(setting, SILENT); } // walking array group { config_setting_t *array = config_setting_add(root, FLD_WALKARRAY_GROUP, CONFIG_TYPE_GROUP); // array accesses setting = config_setting_add(array, FLD_WALKARRAY_ACCESSES, CONFIG_TYPE_INT64); config_setting_set_int64(setting, AACCESSES); // repetitions of a single test, to be able to calculate an average // and a standard deviation, indicating run precision setting = config_setting_add(array, FLD_WALKARRAY_REPEAT, CONFIG_TYPE_INT64); config_setting_set_int64(setting, REPETITIONS); // initial array size setting = config_setting_add(array, FLD_WALKARRAY_BEGINLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, BEGIN); // maximal array size setting = config_setting_add(array, FLD_WALKARRAY_ENDLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, END); // increment factor setting = config_setting_add(array, FLD_WALKARRAY_INCREMENT, CONFIG_TYPE_INT64); config_setting_set_int64(setting, STEP); // increment type // (linear adds the increment, exponential multiplies the previous length) setting = config_setting_add(array, FLD_WALKARRAY_SCALING, CONFIG_TYPE_STRING); config_setting_set_string(setting, SCALING); // walking pattern // (sequentially increasing/decreasing or random) setting = config_setting_add(array, FLD_WALKARRAY_PATTERN, CONFIG_TYPE_STRING); config_setting_set_string(setting, PATTERN); } // streaming array group { config_setting_t *array = config_setting_add(root, FLD_STREAMARRAY_GROUP, CONFIG_TYPE_GROUP); // initial array size setting = config_setting_add(array, FLD_STREAMARRAY_BEGINLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, BEGIN); // maximal array size setting = config_setting_add(array, FLD_STREAMARRAY_ENDLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, STREAM_END); // increment factor setting = config_setting_add(array, FLD_STREAMARRAY_INCREMENT, CONFIG_TYPE_INT64); config_setting_set_int64(setting, STREAM_STEP); // increment type // (linear adds the increment, exponential multiplies the previous length) setting = config_setting_add(array, FLD_STREAMARRAY_SCALING, CONFIG_TYPE_STRING); config_setting_set_string(setting, SCALING); } // flops array group { config_setting_t *array = config_setting_add(root, FLD_FLOPSARRAY_GROUP, CONFIG_TYPE_GROUP); // initial array size setting = config_setting_add(array, FLD_FLOPSARRAY_BEGINLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, BEGIN); // maximal array size setting = config_setting_add(array, FLD_FLOPSARRAY_ENDLENGTH, CONFIG_TYPE_INT64); config_setting_set_int64(setting, FLOPS_END); // increment factor setting = config_setting_add(array, FLD_FLOPSARRAY_INCREMENT, CONFIG_TYPE_INT64); config_setting_set_int64(setting, FLOPS_STEP); // increment type // (linear adds the increment, exponential multiplies the previous length) setting = config_setting_add(array, FLD_FLOPSARRAY_SCALING, CONFIG_TYPE_STRING); config_setting_set_string(setting, SCALING); // number of calculations to perform setting = config_setting_add(array, FLD_FLOPSARRAY_CALCULATIONS, CONFIG_TYPE_INT64); config_setting_set_int64(setting, FLOPS_CALCULATIONS); } }