int main(int argc, char ** argv) { char * recname; config_t cfg; if(argc != 2) usage(argv[0]); recname = argv[1]; err = open(STATEFILE,O_RDWR); CHECK_ERR_LTZ("Open schedule file"); fd = err; err = flock(fd, LOCK_SH); CHECK_ERR("Lock statefile"); err = atexit(&exitfunc); config_init(&cfg); config_read_file(&cfg, STATEFILE); config_setting_t *root; root = config_root_setting(&cfg); err = config_setting_remove(root, recname); CHECK_CFG("Remove recording from schedule"); err = config_write_file(&cfg, STATEFILE); CHECK_CFG("Write config file"); config_destroy(&cfg); return 0; }
void config_setup(char *cfg_file){ config_init(&userCfg.cfg); userCfg.root = config_root_setting(&userCfg.cfg); userCfg.show_line_numbers=1; userCfg.show_lang=0; userCfg.show_spelling_errors=0; userCfg.defualt_window_width=800; userCfg.default_window_height=900; userCfg.undo_level=0; userCfg.show_full_path=1; #ifdef AUTO_TAB_TOGGLE userCfg.auto_tab=0; #endif userCfg.gave_up=0; if(access(cfg_file, R_OK|W_OK ) != -1) cfg_read_in(cfg_file); else userCfg.gave_up=store_settings(cfg_file, userCfg.show_line_numbers, userCfg.show_lang, userCfg.show_spelling_errors, userCfg.defualt_window_width, userCfg.default_window_height, userCfg.undo_level, userCfg.show_full_path #ifdef AUTO_TAB_TOGGLE , userCfg.auto_tab #endif ); }
//####################################### autoconfig void ipv4_init_params(struct fins_module *module) { metadata_element *root = config_root_setting(module->params); //int status; //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, "exec", CONFIG_TYPE_GROUP); if (exec_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, "get", CONFIG_TYPE_GROUP); if (get_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //elem_add_param(get_elem, LOGGER_GET_INTERVAL__str, LOGGER_GET_INTERVAL__id, LOGGER_GET_INTERVAL__type); //elem_add_param(get_elem, LOGGER_GET_REPEATS__str, LOGGER_GET_REPEATS__id, LOGGER_GET_REPEATS__type); //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, "set", CONFIG_TYPE_GROUP); if (set_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } //elem_add_param(set_elem, LOGGER_SET_INTERVAL__str, LOGGER_SET_INTERVAL__id, LOGGER_SET_INTERVAL__type); //elem_add_param(set_elem, LOGGER_SET_REPEATS__str, LOGGER_SET_REPEATS__id, LOGGER_SET_REPEATS__type); }
/** * Load configuration file. * @param[in] path Configuration file location. * @param[in,out] zconf Parsed configuration storage. * @return True on success. */ bool zconfig_load(const char *path, zconfig_t *zconf) { if (NULL == path) { ZLOG(LOG_ERR, "config: configuration file not specified"); return false; } bool loaded = false; config_t config; config_init(&config); if (!config_read_file(&config, path)) { ZLOG(LOG_ERR, "config: failed to parse %s (error: %s at %d line)", path, config_error_text(&config), config_error_line(&config)); goto end; } const config_setting_t *root = config_root_setting(&config); if (!zconfig_load_sections(root, zconf)) { zconfig_destroy(zconf); } else { loaded = true; } end: config_destroy(&config); return loaded; }
void switch_init_knobs(struct fins_module *module) { metadata_element *root = config_root_setting(module->knobs); //int status; //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, OP_EXEC_STR, META_TYPE_GROUP); if (exec_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, OP_GET_STR, META_TYPE_GROUP); if (get_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //elem_add_param(get_elem, LOGGER_GET_INTERVAL__str, LOGGER_GET_INTERVAL__id, LOGGER_GET_INTERVAL__type); //elem_add_param(get_elem, LOGGER_GET_REPEATS__str, LOGGER_GET_REPEATS__id, LOGGER_GET_REPEATS__type); //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, OP_SET_STR, META_TYPE_GROUP); if (set_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //elem_add_param(set_elem, LOGGER_SET_INTERVAL__str, LOGGER_SET_INTERVAL__id, LOGGER_SET_INTERVAL__type); //elem_add_param(set_elem, LOGGER_SET_REPEATS__str, LOGGER_SET_REPEATS__id, LOGGER_SET_REPEATS__type); }
/********************** __get_unused_group_on_path find an unused group, DO NOT CREATE IT and return its name returned string must be freed return NULL on error **********************/ char * __get_unused_group_on_path(const char * table, const char * file, char * path) { char tag[7]; int index = 0; config_setting_t * setting; const config_t * config; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return NULL; } sprintf(tag,"A%05x",index); if(path != NULL) { setting = config_lookup(config,path); free(path); } else { setting = config_root_setting(config); } while( config_setting_add(setting,tag,CONFIG_TYPE_GROUP) == NULL ) { index++; sprintf(tag,"A%05x",index); } config_setting_remove(setting,tag); SDL_UnlockMutex(entry_mutex); return strdup(tag); }
void rtm_init_knobs(struct fins_module *module) { metadata_element *root = config_root_setting(module->knobs); //------------------------------------------------------------------------------------------- metadata_element *exec_elem = config_setting_add(root, OP_EXEC_STR, META_TYPE_GROUP); if (exec_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *get_elem = config_setting_add(root, OP_GET_STR, META_TYPE_GROUP); if (get_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *set_elem = config_setting_add(root, OP_SET_STR, META_TYPE_GROUP); if (set_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } //------------------------------------------------------------------------------------------- metadata_element *listen_elem = config_setting_add(root, OP_LISTEN_STR, META_TYPE_GROUP); if (listen_elem == NULL) { PRINT_ERROR("todo error"); exit(-1); } }
/** * 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); }
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); }
void MainDialog::configSetBool(const char* key, bool 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_BOOL); } config_setting_set_bool(setting, val); }
/** * Load configuration file. * @param[in] path Location of configuration file. * @param[in,out] zconf Loaded options will be stored here. * @return Zero on success. */ int zero_config_load(const char *path, struct zero_config *zconf) { int ret = 0; config_t config; config_init(&config); if (NULL == path) { path = ZCFG_DEFAULT_PATH; } if (config_read_file(&config, path)) { const config_setting_t *root = config_root_setting(&config); ret = ret || load_interfaces(root, ZCFG_INTERFACES, &zconf->interfaces) || load_uint_req(root, ZCFG_IFACE_WAIT_TIME, &zconf->iface_wait_time) || load_uint_req(root, ZCFG_OVERLORD_THREADS, &zconf->overlord_threads) || load_kmgt(root, ZCFG_UNAUTH_BW_LIMIT_DOWN, &zconf->unauth_bw_limit[DIR_DOWN], 1024) || load_kmgt(root, ZCFG_UNAUTH_BW_LIMIT_UP, &zconf->unauth_bw_limit[DIR_UP], 1024) || load_string_req(root, ZCFG_RADIUS_CONFIG_FILE, &zconf->radius_config_file) || load_string_req(root, ZCFG_RADIUS_NAS_IDENTIFIER, &zconf->radius_nas_identifier) || load_uint64_req(root, ZCFG_SESSION_TIMEOUT, &zconf->session_timeout) || load_uint64_req(root, ZCFG_SESSION_ACCT_INTERVAL, &zconf->session_acct_interval) || load_uint64_req(root, ZCFG_SESSION_AUTH_INTERVAL, &zconf->session_auth_interval) || load_string_req(root, ZCFG_RC_LISTEN_ADDR, &zconf->rc_listen_addr) || load_kmgt(root, ZCFG_UPSTREAM_P2P_BW_DOWN, &zconf->upstream_p2p_bw[DIR_DOWN], 1024) || load_kmgt(root, ZCFG_UPSTREAM_P2P_BW_UP, &zconf->upstream_p2p_bw[DIR_UP], 1024) || load_ip_mask_list(root, ZCFG_IP_WHITELIST, &zconf->ip_whitelist) || load_uint16_list(root, ZCFG_P2P_PORTS_WHITELIST, &zconf->p2p_ports_whitelist) || load_uint16_list(root, ZCFG_P2P_PORTS_BLACKLIST, &zconf->p2p_ports_blacklist) || load_kmgt(root, ZCFG_NON_CLIENT_BW_DOWN, &zconf->non_client_bw[DIR_DOWN], 1024) || load_kmgt(root, ZCFG_NON_CLIENT_BW_UP, &zconf->non_client_bw[DIR_UP], 1024) || load_kmgt(root, ZCFG_INITIAL_CLIENT_BUCKET_SIZE, &zconf->initial_client_bucket_size, 1024) ; // convert from bits to bytes zconf->unauth_bw_limit[DIR_DOWN] /= 8; zconf->unauth_bw_limit[DIR_UP] /= 8; zconf->upstream_p2p_bw[DIR_DOWN] /= 8; zconf->upstream_p2p_bw[DIR_UP] /= 8; zconf->non_client_bw[DIR_DOWN] /= 8; zconf->non_client_bw[DIR_UP] /= 8; // convert from seconds to microseconds zconf->session_timeout *= 1000000; zconf->session_acct_interval *= 1000000; zconf->session_auth_interval *= 1000000; } else { ZERO_LOG(LOG_ERR, "config: failed to parse %s (error:%s at %d line)", path, config_error_text(&config), config_error_line(&config)); ret = -1; } config_destroy(&config); return ret; }
int write_cfg_for_rec(struct opt_s *opt, char *filename) { int err; config_t cfg; config_init(&cfg); stub_rec_cfg(config_root_setting(&cfg), opt); err = write_cfg(&cfg, filename); config_destroy(&cfg); return err; }
int store_settings(char *cfg_file, int show_line_numbers, int show_lang, int show_spelling_errors, int defualt_window_width, int default_window_height, int undo_level, int show_full_path #ifdef AUTO_TAB_TOGGLE , int auto_tab #endif ){ config_t cfg; config_setting_t *root, *setting, *group, *UI; config_init(&cfg); int ret=0; root = config_root_setting(&cfg); group = config_setting_add(root,"User_Pref", CONFIG_TYPE_GROUP); setting = config_setting_add(group,"LineNumbers", CONFIG_TYPE_INT); config_setting_set_int(setting, show_line_numbers); setting = config_setting_add(group,"Spelling", CONFIG_TYPE_INT); config_setting_set_int(setting, show_spelling_errors); setting = config_setting_add(group,"ShowLang", CONFIG_TYPE_INT); config_setting_set_int(setting, show_lang); #ifdef AUTO_TAB_TOGGLE setting = config_setting_add(group,"Auto_Tab", CONFIG_TYPE_INT); config_setting_set_int(setting, auto_tab); #endif setting = config_setting_add(group,"Show_Full_Path", CONFIG_TYPE_INT); config_setting_set_int(setting, show_full_path); UI = config_setting_add(root,"User_Interface_Settings", CONFIG_TYPE_GROUP); setting = config_setting_add(UI,"Default_Window_Width", CONFIG_TYPE_INT); config_setting_set_int(setting, defualt_window_width); setting = config_setting_add(UI,"Default_Window_Height", CONFIG_TYPE_INT); config_setting_set_int(setting, default_window_height); setting = config_setting_add(UI,"Undo_Level", CONFIG_TYPE_INT); config_setting_set_int(setting, undo_level); if (! config_write_file(&cfg,(char*)cfg_file)) ret=0; else ret=1; config_destroy(&cfg); return(ret); }
static const char *test_libconfig_init_destroy(void) { struct config_t config; libconfig->init(&config); if (config.root == NULL || config.root != config_root_setting(&config)) { return "Unable to create config."; } libconfig->destroy(&config); if (config.root != NULL) { return "Unable to destroy config."; } return NULL; }
void patch_save() { config_setting_t* root_setting = config_root_setting(&patch_config); config_setting_remove(root_setting, CFG_MOD_MATRIX_CONTROLLER); config_setting_t* mod_matrix_patch = config_setting_add(root_setting, CFG_MOD_MATRIX_CONTROLLER, CONFIG_TYPE_GROUP); mod_matrix_controller_save(mod_matrix_patch); if (config_write_file(&patch_config, PATCH_FILE) != CONFIG_TRUE) { LOG_ERROR("Patch write error to %s", PATCH_FILE); } }
/******************************************************************************* 函数名称 : webauth_rdx_read_setting 功能描述 : 读取服务器配置 输入参数 : 无 输出参数 : 服务器配置列表 返 回 值 : -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : 王群 修改目的 : 新添加函数 修改日期 : 2010-08-16 *******************************************************************************/ webauth_rdx_server_conf_t *webauth_rdx_read_setting (void) { config_t cfg; webauth_rdx_server_conf_t *server_cfg, *head; config_setting_t *server_set; config_setting_t *root_set; int server_cnt, i; int max_index; config_init (&cfg); head = NULL; if (0 == config_read_file (&cfg, RADIUS_CFG_FILE)) { config_destroy (&cfg); return head; } root_set = config_root_setting (&cfg); server_cnt = config_setting_length (root_set); max_index = 0; for (i = 0; i < server_cnt; i++) { /* 遍历所有的RADIUS服务器配置,找到指定的服务器 */ server_set = config_setting_get_elem (root_set, (unsigned int) i); if (NULL != server_set) { server_cfg = webauth_rdx_new_setting (config_setting_name (server_set)); if (NULL != server_cfg) { webauth_rdx_read_server_setting (server_set, server_cfg); server_cfg->next = head; head = server_cfg; if (max_index < server_cfg->index) { max_index = server_cfg->index; } } } } /* 如果配置的server没有指定index, 则指定之 */ for (server_cfg = head; NULL != server_cfg; server_cfg = server_cfg->next) { if (-1 == server_cfg->index) { server_cfg->index = ++max_index; } } config_destroy (&cfg); return head; }
/********************** Return a list of the name of the elements in the specified group res must be freed (deep_free) return RET_NOK on error **********************/ ret_code_t entry_get_group_list(const char * table, const char * file, char *** res, ...) { char * path; const config_t * config; va_list ap; config_setting_t * setting; config_setting_t * elem_setting; int index = 0; *res = NULL; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } va_start(ap,res); path = get_path(ap); va_end(ap); if(path != NULL && path[0] != 0) { setting = config_lookup(config,path); free(path); } else { setting = config_root_setting(config); } /* The path does not exist in the conf file */ if(setting == NULL ) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } while((elem_setting=config_setting_get_elem(setting,index))!= NULL ) { index++; *res = (char **)realloc(*res,(index+1)*sizeof(char *)); (*res)[index-1] = strdup(config_setting_name(elem_setting)); (*res)[index] = NULL; } SDL_UnlockMutex(entry_mutex); if( *res == NULL) { return RET_NOK; } return RET_OK; }
static config_setting_t * create_tree(const config_t * config, config_setting_t * prev_setting, char * prev_entry, char * path, int type, va_list ap) { char * entry = NULL; char * new_path; config_setting_t * setting = NULL; config_setting_t * ret = NULL; if( prev_setting == NULL) { prev_setting = config_root_setting(config); } entry = va_arg(ap,char*); /* no more leaf */ if(entry == NULL) { /* check if leaf exists */ setting = config_lookup(config, path); if( setting == NULL) { /* create the leaf */ setting=config_setting_add(prev_setting, prev_entry,type); } return setting; } /* still not finished */ /* check if we need to create a group */ if( path ) { setting = config_lookup(config, path); if( setting == NULL ) { /* create a group with previous path */ setting = config_setting_add(prev_setting, prev_entry, CONFIG_TYPE_GROUP); if(setting == NULL) { return NULL; } } } /* update path */ if( path ) { new_path = strconcat(path,".",entry,NULL); } else { new_path = strdup(entry); } ret = create_tree(config,setting,entry,new_path,type,ap); free(new_path); return ret; }
int main(int argc, char **argv) { static const char *output_file = "newconfig.cfg"; config_t cfg; config_setting_t *root, *setting, *group, *array; int i; config_init(&cfg); root = config_root_setting(&cfg); /* Add some settings to the configuration. */ group = config_setting_add(root, "address", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "street", CONFIG_TYPE_STRING); config_setting_set_string(setting, "1 Woz Way"); setting = config_setting_add(group, "city", CONFIG_TYPE_STRING); config_setting_set_string(setting, "San Jose"); setting = config_setting_add(group, "state", CONFIG_TYPE_STRING); config_setting_set_string(setting, "CA"); setting = config_setting_add(group, "zip", CONFIG_TYPE_INT); config_setting_set_int(setting, 95110); array = config_setting_add(root, "numbers", CONFIG_TYPE_ARRAY); for(i = 0; i < 10; ++i) { setting = config_setting_add(array, NULL, CONFIG_TYPE_INT); config_setting_set_int(setting, 10 * i); } /* Write out the new configuration. */ if(! config_write_file(&cfg, output_file)) { fprintf(stderr, "Error while writing file.\n"); config_destroy(&cfg); return(EXIT_FAILURE); } fprintf(stderr, "New configuration successfully written to: %s\n", output_file); config_destroy(&cfg); return(EXIT_SUCCESS); }
/******************************************************************************* 函数名称 : webauth_rdx_save_setting 功能描述 : 保存服务器配置 输入参数 : cli_rdx_server_conf_t * list 服务器配置列表 输出参数 : 无 返 回 值 : -------------------------------------------------------------------------------- 最近一次修改记录 : 修改作者 : 王群 修改目的 : 新添加函数 修改日期 : 2010-08-16 *******************************************************************************/ void webauth_rdx_save_setting (webauth_rdx_server_conf_t * list) { webauth_rdx_server_conf_t *server_cfg = NULL; config_t cfg; config_setting_t *server_set = NULL; config_setting_t *root_set = NULL; config_init (&cfg); root_set = config_root_setting (&cfg); for (server_cfg = list; NULL != server_cfg; server_cfg = server_cfg->next) { server_set = config_setting_add (root_set, server_cfg->name, CONFIG_TYPE_GROUP); webauth_rdx_build_server_setting (server_set, server_cfg); } config_write_file (&cfg, RADIUS_CFG_FILE); config_destroy (&cfg); return; }
static void read_app_config(void) { char *app_name = getenv("MACSPOOF_APPLICATION"); if (app_name == NULL) app_name = DEFAULT_APPLICATION_NAME; config_setting_t *root = config_root_setting(config); assert(root != NULL); app_config = config_setting_get_member(root, app_name); if (app_config == NULL) die("Application \"%s\" not found.", app_name); switch(config_setting_type(app_config)) { case CONFIG_TYPE_ARRAY: typecheck_config_array(app_config); break; case CONFIG_TYPE_LIST: typecheck_config_ifgrouplist(app_config); break; default: die("Config for application \"%s\" must be an array or group.", app_name); } }
void rtm_init_params(struct fins_module *module) { metadata_element *root = config_root_setting(module->params); metadata_element *exec_elem = config_setting_add(root, "exec", CONFIG_TYPE_GROUP); if (exec_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *get_elem = config_setting_add(root, "get", CONFIG_TYPE_GROUP); if (get_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *set_elem = config_setting_add(root, "set", CONFIG_TYPE_GROUP); if (set_elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *sub = config_setting_add(exec_elem, "test", CONFIG_TYPE_GROUP); if (sub == NULL) { PRINT_DEBUG("todo error"); exit(-1); } metadata_element *elem = config_setting_add(sub, "key", CONFIG_TYPE_INT); if (elem == NULL) { PRINT_DEBUG("todo error"); exit(-1); } uint32_t value = 10; int status = config_setting_set_int(elem, *(int *) &value); if (status == CONFIG_FALSE) { PRINT_DEBUG("todo error"); exit(-1); } }
void Settings::Save() { config_t cfg; config_setting_t *root, *setting, *group; config_init(&cfg); root = config_root_setting(&cfg); group = config_setting_add(root, "Graphics", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "Fullscreen", CONFIG_TYPE_INT); config_setting_set_int(setting, FullScreen); group = config_setting_add(root, "Audio", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "PlaySFX", CONFIG_TYPE_INT); config_setting_set_int(setting, PlaySFX); setting = config_setting_add(group, "PlayMusic", CONFIG_TYPE_INT); config_setting_set_int(setting, PlayMusic); group = config_setting_add(root, "Input", CONFIG_TYPE_GROUP); setting = config_setting_add(group, "keyLeft", CONFIG_TYPE_INT); config_setting_set_int(setting, keyLeft); setting = config_setting_add(group, "keyRight", CONFIG_TYPE_INT); config_setting_set_int(setting, keyRight); setting = config_setting_add(group, "keyUp", CONFIG_TYPE_INT); config_setting_set_int(setting, keyUp); setting = config_setting_add(group, "keyDown", CONFIG_TYPE_INT); config_setting_set_int(setting, keyDown); setting = config_setting_add(group, "keyFire1", CONFIG_TYPE_INT); config_setting_set_int(setting, keyFire1); setting = config_setting_add(group, "keyFire2", CONFIG_TYPE_INT); config_setting_set_int(setting, keyFire2); setting = config_setting_add(group, "keyQuit", CONFIG_TYPE_INT); config_setting_set_int(setting, keyQuit); config_write_file( &cfg, "settings.cfg" ); config_destroy(&cfg); }
/********************* return RET_NOK on error *********************/ static ret_code_t __remove_group(const char * table, const char * file, const char * group, va_list ap) { const config_t * config; config_setting_t * setting = NULL; char * path; SDL_LockMutex(entry_mutex); config = get_config(table,file); if(config==NULL) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } path = get_path(ap); if(path != NULL) { setting = config_lookup(config,path); free(path); } else { setting = config_root_setting(config); } if( setting == NULL ) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } if( config_setting_remove(setting,group) == CONFIG_FALSE) { SDL_UnlockMutex(entry_mutex); return RET_NOK; } write_config(config,table,file); SDL_UnlockMutex(entry_mutex); return RET_OK; }
/** * Initialize the application configuration based on the config file content * Read the config file, get mandatory variables and devices */ int build_config_from_file(struct config_elements * config) { config_t cfg; config_setting_t * root, * database; const char * cur_prefix, * cur_log_mode, * cur_log_level, * cur_log_file = NULL, * one_log_mode, * db_type, * db_sqlite_path, * db_mariadb_host = NULL, * db_mariadb_user = NULL, * db_mariadb_password = NULL, * db_mariadb_dbname = NULL; int db_mariadb_port = 0; config_init(&cfg); if (!config_read_file(&cfg, config->config_file)) { fprintf(stderr, "Error parsing config file %s\nOn line %d error: %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); return 0; } if (config->instance->port == -1) { // Get Port number to listen to int port; config_lookup_int(&cfg, "port", &port); config->instance->port = port; } if (config->url_prefix == NULL) { // Get prefix url if (config_lookup_string(&cfg, "url_prefix", &cur_prefix)) { config->url_prefix = o_strdup(cur_prefix); if (config->url_prefix == NULL) { fprintf(stderr, "Error allocating config->url_prefix, exiting\n"); config_destroy(&cfg); return 0; } } } if (config->log_mode == Y_LOG_MODE_NONE) { // Get log mode if (config_lookup_string(&cfg, "log_mode", &cur_log_mode)) { one_log_mode = strtok((char *)cur_log_mode, ","); while (one_log_mode != NULL) { if (0 == strncmp("console", one_log_mode, strlen("console"))) { config->log_mode |= Y_LOG_MODE_CONSOLE; } else if (0 == strncmp("syslog", one_log_mode, strlen("syslog"))) { config->log_mode |= Y_LOG_MODE_SYSLOG; } else if (0 == strncmp("file", one_log_mode, strlen("file"))) { config->log_mode |= Y_LOG_MODE_FILE; // Get log file path if (config->log_file == NULL) { if (config_lookup_string(&cfg, "log_file", &cur_log_file)) { config->log_file = o_strdup(cur_log_file); if (config->log_file == NULL) { fprintf(stderr, "Error allocating config->log_file, exiting\n"); config_destroy(&cfg); return 0; } } } } one_log_mode = strtok(NULL, ","); } } } if (config->log_level == Y_LOG_LEVEL_NONE) { // Get log level if (config_lookup_string(&cfg, "log_level", &cur_log_level)) { if (0 == strncmp("NONE", cur_log_level, strlen("NONE"))) { config->log_level = Y_LOG_LEVEL_NONE; } else if (0 == strncmp("ERROR", cur_log_level, strlen("ERROR"))) { config->log_level = Y_LOG_LEVEL_ERROR; } else if (0 == strncmp("WARNING", cur_log_level, strlen("WARNING"))) { config->log_level = Y_LOG_LEVEL_WARNING; } else if (0 == strncmp("INFO", cur_log_level, strlen("INFO"))) { config->log_level = Y_LOG_LEVEL_INFO; } else if (0 == strncmp("DEBUG", cur_log_level, strlen("DEBUG"))) { config->log_level = Y_LOG_LEVEL_DEBUG; } } } if (!y_init_logs(GARETH_LOG_NAME, config->log_mode, config->log_level, config->log_file, "Starting Gareth alert and messenger service")) { fprintf(stderr, "Error initializing logs\n"); exit_server(&config, GARETH_ERROR); } root = config_root_setting(&cfg); database = config_setting_get_member(root, "database"); if (database != NULL) { if (config_setting_lookup_string(database, "type", &db_type) == CONFIG_TRUE) { if (0 == strncmp(db_type, "sqlite3", strlen("sqlite3"))) { if (config_setting_lookup_string(database, "path", &db_sqlite_path) == CONFIG_TRUE) { config->conn = h_connect_sqlite(db_sqlite_path); if (config->conn == NULL) { config_destroy(&cfg); fprintf(stderr, "Error opening sqlite database %s\n", db_sqlite_path); return 0; } } else { config_destroy(&cfg); fprintf(stderr, "Error, no sqlite database specified\n"); return 0; } } else if (0 == strncmp(db_type, "mariadb", strlen("mariadb"))) { config_setting_lookup_string(database, "host", &db_mariadb_host); config_setting_lookup_string(database, "user", &db_mariadb_user); config_setting_lookup_string(database, "password", &db_mariadb_password); config_setting_lookup_string(database, "dbname", &db_mariadb_dbname); config_setting_lookup_int(database, "port", &db_mariadb_port); config->conn = h_connect_mariadb(db_mariadb_host, db_mariadb_user, db_mariadb_password, db_mariadb_dbname, db_mariadb_port, NULL); if (config->conn == NULL) { fprintf(stderr, "Error opening mariadb database %s\n", db_mariadb_dbname); config_destroy(&cfg); return 0; } } else { config_destroy(&cfg); fprintf(stderr, "Error, database type unknown\n"); return 0; } } else { config_destroy(&cfg); fprintf(stderr, "Error, no database type found\n"); return 0; } } else { config_destroy(&cfg); fprintf(stderr, "Error, no database setting found\n"); return 0; } config_destroy(&cfg); return 1; }
int iteliec_soap_register (char *username, char *password) { herror_t err; SoapCtx *request; SoapCtx *response; config_t cfg; config_setting_t *setting, *root; const char *cfile; cfile = iteliec_config_file (); config_init (&cfg); /* Read the file. If there is an error, report it and exit. */ if (!config_read_file (&cfg, cfile)) { iteliec_log (ITELIEC_ERR, "Please ensure configuration file %s exists and is valid", cfile); //printf ("\n%s:%d - %s", config_error_file (&cfg), config_error_line (&cfg), config_error_text (&cfg)); config_destroy (&cfg); return; } root = config_root_setting (&cfg); /* Init */ err = soap_client_init_args (0, NULL); if (err != H_OK) { err_soap (err); return ITELIEC_ERR; } err = soap_ctx_new_with_method ("", "sendAuth", &request); if (err != H_OK) { err_soap (err); soap_client_destroy (); return ITELIEC_ERR; } /* Add login details */ soap_env_add_item (request->env, "xsd:string", "username", username); soap_env_add_item (request->env, "xsd:string", "password", password); /* Trade for response */ err = soap_client_invoke (request, &response, "http://api.iteliec.com/register/auth/ws/", ""); if (err != H_OK) { err_soap (err); soap_ctx_free (request); soap_client_destroy (); return ITELIEC_ERR; } /* Parse response */ auth_type* ret = parse_auth_response (response); if (ret->auth == true) { printf ("Success\n"); setting = config_setting_get_member (root, "api"); if(!setting) { setting = config_setting_add (root, "api", CONFIG_TYPE_GROUP); } setting = config_setting_add (setting, "hash", CONFIG_TYPE_STRING); config_setting_set_string (setting, ret->token); /* Write out the updated configuration. */ if(! config_write_file(&cfg, cfile)) { fprintf (stderr, "Error while writing file.\n"); config_destroy (&cfg); return (ITELIEC_OK); } printf("\nConfiguration file updated. Server is ready to run.\n"); } else { printf("Auth failed\n"); } /* Destroy */ soap_ctx_free (request); soap_ctx_free (response); soap_client_destroy (); config_destroy (&cfg); return ITELIEC_OK; }
/** * Print the Malheur configuration to a file. * @param f pointer to file stream * @param cfg configuration */ void config_fprint(FILE *f, config_t *cfg) { config_setting_fprint(f, config_root_setting(cfg), 0); }
static const char *test_libconfig_setting_get(void) { struct config_t config; struct config_setting_t *t = NULL; double f; const char *str; const char *input = "/* Test File */\n" "Setting_Int: 1;\n" "Setting_Int64: 1L;\n" "Setting_Float: 1.0;\n" "Setting_Bool: true;\n" "Setting_String: \"1\";\n" "Setting_Array: [ ];\n" "Setting_Group: { };\n" "Setting_List: ( );\n" "/* End test file */\n"; if (libconfig->read_string(&config, input) == CONFIG_FALSE) { libconfig->destroy(&config); return "Unable to parse configuration."; } if ((t = libconfig->lookup(&config, "Setting_Int")) == NULL || libconfig->setting_get_int(t) != 1) { libconfig->destroy(&config); return "libconfig->setting_get_int failed."; } if ((t = libconfig->lookup(&config, "Setting_Int64")) == NULL || libconfig->setting_get_int64(t) != 1) { libconfig->destroy(&config); return "libconfig->lookup_int64 failed."; } if ((t = libconfig->lookup(&config, "Setting_Float")) == NULL || (f = libconfig->setting_get_float(t)) < 1.0 - 0.1 || f > 1.0 + 0.1) { libconfig->destroy(&config); return "libconfig->lookup_float failed."; } if ((t = libconfig->lookup(&config, "Setting_Bool")) == NULL || libconfig->setting_get_bool(t) != 1) { libconfig->destroy(&config); return "libconfig->lookup_bool failed."; } if ((t = libconfig->lookup(&config, "Setting_String")) == NULL || (str = libconfig->setting_get_string(t)) == NULL || str[0] != '1' || str[1] != '\0') { libconfig->destroy(&config); return "libconfig->lookup_string failed."; } t = config_root_setting(&config); if (libconfig->setting_get_int_elem(t, 0) != 1) { libconfig->destroy(&config); return "libconfig->setting_get_int_elem failed."; } if (libconfig->setting_get_int64_elem(t, 1) != 1) { libconfig->destroy(&config); return "libconfig->setting_get_int64_elem failed."; } if ((f = libconfig->setting_get_float_elem(t, 2)) < 1.0 - 0.1 || f > 1.0 + 0.1) { libconfig->destroy(&config); return "libconfig->setting_get_float_elem failed."; } if (libconfig->setting_get_bool_elem(t, 3) != 1) { libconfig->destroy(&config); return "libconfig->setting_get_bool_elem failed."; } if ((str = libconfig->setting_get_string_elem(t, 4)) == NULL || str[0] != '1' || str[1] != '\0') { libconfig->destroy(&config); return "libconfig->setting_get_string_elem failed."; } if ((t = libconfig->setting_get_elem(config.root, 0)) == NULL || libconfig->setting_get_int(t) != 1) { libconfig->destroy(&config); return "libconfig->setting_get_elem failed."; } if ((t = libconfig->setting_get_member(config.root, "Setting_Int")) == NULL || libconfig->setting_get_int(t) != 1 || strcmp(config_setting_name(t), "Setting_Int") != 0) { libconfig->destroy(&config); return "libconfig->setting_get_member failed."; } if ((t = libconfig->setting_get_elem(config.root, 0)) == NULL || strcmp(config_setting_name(t), "Setting_Int") != 0) { libconfig->destroy(&config); return "config_setting_name failed."; } if ((t = libconfig->setting_get_member(config.root, "Setting_Int")) == NULL || libconfig->setting_index(t) != 0) { libconfig->destroy(&config); return "libconfig->setting_index failed."; } if (libconfig->setting_length(config.root) != 8) { libconfig->destroy(&config); return "libconfig->setting_length failed."; } libconfig->destroy(&config); return NULL; }
/********************* return NULL on error *********************/ static char * __copy_group(const char * src_table, const char * src_file, const char * dst_table, const char * dst_file, const char * group_name, va_list ap) { const config_t * src_config = NULL; const config_t * dst_config = NULL; config_setting_t * src_setting = NULL; config_setting_t * dst_setting = NULL; char * path; char * full_path; char * new_group_name = NULL; const char * group_name_used = NULL; path = get_path(ap); full_path = add_entry_to_path(path, (char *)group_name); if(full_path == NULL) { return NULL; } SDL_LockMutex(entry_mutex); src_config = get_config(src_table,src_file); if(src_config==NULL) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } src_setting = config_lookup (src_config, full_path); if( src_setting == NULL ) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } dst_config = get_config(dst_table,dst_file); if(dst_config==NULL) { SDL_UnlockMutex(entry_mutex); free(path); free(full_path); return NULL; } dst_setting = config_lookup(dst_config, full_path); free(full_path); /* if the setting does not exist, create it */ if( dst_setting == NULL ) { group_name_used = group_name; } /* else find a new name for it */ else { new_group_name = __get_unused_group_on_path(dst_table, dst_file, path); group_name_used = new_group_name; } if( path != NULL ) { dst_setting = config_lookup(dst_config, path); } else { dst_setting = config_root_setting(dst_config); } dst_setting = config_setting_add(dst_setting,group_name_used,CONFIG_TYPE_GROUP); free(path); if(!entry_copy_config(src_setting,dst_setting)) { SDL_UnlockMutex(entry_mutex); return NULL; } SDL_UnlockMutex(entry_mutex); if(new_group_name != NULL ) { return new_group_name; } return strdup(group_name); }