int read_config(const char *filename, section_t &config) { static const char *funcname = "conf::read_config"; section_ptrs ptrs; build_section(&config, ptrs); cfg_t *cfg = cfg_init(ptrs[0].get(), CFGF_NONE); cfg_set_error_function(cfg, cfg_error_fnc); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: throw logging::error(funcname, "Configuration file '%s' cannot be read: %s", filename, strerror(errno)); case CFG_PARSE_ERROR: throw logging::error(funcname, "Errors were encountered during config reading."); default: throw logging::error(funcname, "cfg_parse() returned unexpected value"); case CFG_SUCCESS: break; } std::stringstream errors; read_cfg_section(&config, cfg, errors); cfg_free(cfg); errors.peek(); if (!errors.eof()) { for (std::string line; getline(errors, line); ) logger.log_message(LOG_ERR, funcname, line.c_str()); return 0; } return 1; }
// // E_CreateCfg // // haleyjd 03/21/10: Separated from E_ParseEDF[File|Lump]. Creates and // initializes a libConfuse cfg_t object for use by EDF. All definitions // are now accumulated into this singular cfg_t, as opposed to being // merged from separately allocated ones for secondary EDF sources such // as wad lumps. // static cfg_t *E_CreateCfg(cfg_opt_t *opts) { cfg_t *cfg; E_EDFLogPuts("Creating global cfg_t object\n"); cfg = cfg_init(opts, CFGF_NOCASE); cfg_set_error_function(cfg, edf_error); cfg_set_lexer_callback(cfg, E_CheckRoot); return cfg; }
/** * Parses the config file * * @return a pointer to the configuration data structure, NULL on failure */ static cfg_t * parse_config(void) { cfg_t * cfg = NULL; cfg_opt_t target_opts[] = { CFG_STR("comment", 0, CFGF_NONE), CFG_INT("strip", 0, CFGF_NONE), CFG_STR("rewrite_prefix", 0, CFGF_NONE), CFG_FLOAT("prob", 0, CFGF_NONE), CFG_INT("hash_index", 0, CFGF_NONE), CFG_STR("rewrite_suffix", 0, CFGF_NONE), CFG_INT("status", 1, CFGF_NONE), CFG_INT_LIST("backed_up", NULL, CFGF_NONE), CFG_INT("backup", -1, CFGF_NONE), CFG_END() }; cfg_opt_t prefix_opts[] = { CFG_SEC("target", target_opts, CFGF_MULTI | CFGF_TITLE), CFG_INT("max_targets", -1, CFGF_NONE), CFG_END() }; cfg_opt_t domain_opts[] = { CFG_SEC("prefix", prefix_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("domain", domain_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg = cfg_init(opts, CFGF_NONE); cfg_set_error_function(cfg, conf_error); switch (cfg_parse(cfg, config_file)) { case CFG_FILE_ERROR: LM_ERR("file not found: %s\n", config_file); return NULL; case CFG_PARSE_ERROR: LM_ERR("error while parsing %s in line %i, section %s\n", cfg->filename, cfg->line, cfg->name); return NULL; case CFG_SUCCESS: break; } return cfg; }
gfs_t *G_LoadGFS(const char *filename) { static bool loaded = false; int i; cfg_t *cfg; // only one GFS can be loaded per session if(loaded) return NULL; cfg = cfg_init(gfs_opts, CFGF_NOCASE); cfg_set_error_function(cfg, E_ErrorCB); if(cfg_parse(cfg, filename)) I_Error("G_LoadGFS: failed to parse GFS file\n"); // count number of options gfs.numwads = cfg_size(cfg, SEC_WADFILE); gfs.numdehs = cfg_size(cfg, SEC_DEHFILE); gfs.numcsc = cfg_size(cfg, SEC_CSCFILE); if(gfs.numwads) gfs.wadnames = ecalloc(char **, gfs.numwads, sizeof(char *)); if(gfs.numdehs) gfs.dehnames = ecalloc(char **, gfs.numdehs, sizeof(char *)); if(gfs.numcsc) gfs.cscnames = ecalloc(char **, gfs.numcsc, sizeof(char *)); // load wads, dehs, csc for(i = 0; i < gfs.numwads; i++) { const char *str = cfg_getnstr(cfg, SEC_WADFILE, i); gfs.wadnames[i] = estrdup(str); } for(i = 0; i < gfs.numdehs; i++) { const char *str = cfg_getnstr(cfg, SEC_DEHFILE, i); gfs.dehnames[i] = estrdup(str); } for(i = 0; i < gfs.numcsc; i++) { const char *str = cfg_getnstr(cfg, SEC_CSCFILE, i); gfs.cscnames[i] = estrdup(str); } // haleyjd 07/05/03: support root EDF specification if(cfg_size(cfg, SEC_EDFFILE) >= 1) { const char *str = cfg_getstr(cfg, SEC_EDFFILE); gfs.edf = estrdup(str); gfs.hasEDF = true; } // haleyjd 04/16/03: support iwad specification for end-users // (this is not useful to mod authors, UNLESS their mod happens // to be an IWAD file ;) if(cfg_size(cfg, SEC_IWAD) >= 1) { const char *str = cfg_getstr(cfg, SEC_IWAD); gfs.iwad = estrdup(str); gfs.hasIWAD = true; } // haleyjd 04/16/03: basepath support if(cfg_size(cfg, SEC_BASEPATH) >= 1) { const char *str = cfg_getstr(cfg, SEC_BASEPATH); gfs.filepath = estrdup(str); } else { gfs.filepath = emalloc(char *, strlen(filename) + 1); M_GetFilePath(filename, gfs.filepath, strlen(filename)); } cfg_free(cfg); loaded = true; return &gfs; }