bool readVarConfig(cfg_t **cfg) { cfg_opt_t device_opts[] = { CFG_INT(const_cast<char *>("state"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("stateValue"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC(const_cast<char *>("device"), device_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; FILE *fp = fopen(VAR_CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle if (!fp) { Log::warning("Unable to open var config file, %s", VAR_CONFIG_FILE); return false; } (*cfg) = cfg_init(opts, CFGF_NOCASE); if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) { (*cfg) = 0; fclose(fp); Log::warning("Unable to parse var config file, %s", VAR_CONFIG_FILE); return false; } fclose(fp); return true; }
int main(void) { FILE *fp; char *param; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); /* set a string parameter to a string including a quote character */ cfg_setstr(cfg, "parameter", "text \" with quotes and \\"); /* print the config to a temporary file */ fp = tmpfile(); fail_unless(fp); cfg_print(cfg, fp); cfg_free(cfg); /* read it back, we expect 'parameter' to include a quote character */ rewind(fp); cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); fail_unless(cfg_parse_fp(cfg, fp) == CFG_SUCCESS); fail_unless(fclose(fp) == 0); param = cfg_getstr(cfg, "parameter"); fail_unless(param); fail_unless(strcmp(param, "text \" with quotes and \\") == 0); cfg_free(cfg); return 0; }
DLLIMPORT int cfg_parse(cfg_t *cfg, const char *filename) { int ret; FILE *fp; assert(cfg && filename); if (cfg->path) { char *f; if ((f = cfg_searchpath(cfg->path, filename)) == NULL) return CFG_FILE_ERROR; free(cfg->filename); cfg->filename = f; } else { free(cfg->filename); cfg->filename = cfg_tilde_expand(filename); } if ((fp = fopen(cfg->filename, "r")) == 0) return CFG_FILE_ERROR; ret = cfg_parse_fp(cfg, fp); fclose(fp); return ret; }
bool readConfig(cfg_t **cfg) { // All the const_cast keywords is to remove the compiler warnings generated by the C++-compiler. cfg_opt_t controller_opts[] = { CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE), CFG_STR(const_cast<char *>("name"), const_cast<char *>(""), CFGF_NONE), CFG_INT(const_cast<char *>("type"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("serial"), const_cast<char *>(""), CFGF_NONE), CFG_END() }; cfg_opt_t device_parameter_opts[] = { // Groups CFG_STR(const_cast<char *>("devices"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("house"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("unit"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("code"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("system"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("units"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("fade"), 0, CFGF_NONE), CFG_END() }; cfg_opt_t device_opts[] = { CFG_INT(const_cast<char *>("id"), -1, CFGF_NONE), CFG_STR(const_cast<char *>("name"), const_cast<char *>("Unnamed"), CFGF_NONE), CFG_INT(const_cast<char *>("controller"), 0, CFGF_NONE), CFG_STR(const_cast<char *>("protocol"), const_cast<char *>("arctech"), CFGF_NONE), CFG_STR(const_cast<char *>("model"), const_cast<char *>(""), CFGF_NONE), CFG_SEC(const_cast<char *>("parameters"), device_parameter_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR(const_cast<char *>("user"), const_cast<char *>("nobody"), CFGF_NONE), CFG_STR(const_cast<char *>("group"), const_cast<char *>("plugdev"), CFGF_NONE), CFG_STR(const_cast<char *>("deviceNode"), const_cast<char *>("/dev/tellstick"), CFGF_NONE), CFG_STR(const_cast<char *>("ignoreControllerConfirmation"), const_cast<char *>("false"), CFGF_NONE), CFG_SEC(const_cast<char *>("device"), device_opts, CFGF_MULTI), CFG_SEC(const_cast<char *>("controller"), controller_opts, CFGF_MULTI), CFG_END() }; FILE *fp = fopen(CONFIG_FILE, "re"); // e for setting O_CLOEXEC on the file handle if (!fp) { return false; } (*cfg) = cfg_init(opts, CFGF_NOCASE); if (cfg_parse_fp((*cfg), fp) == CFG_PARSE_ERROR) { (*cfg) = 0; fclose(fp); return false; } fclose(fp); return true; }
/** Parse the configuration file with confuse * * \param config_fp The configuration file stream * \return Return true if parsing succeeded */ static bool parse_configuration_file(FILE *config_fp) { cfg_opt_t opts[] = { CFG_STR("rendering", "render", CFGF_NONE), CFG_STR_LIST("plugins", "{}", CFGF_NONE), CFG_END() }; globalconf.cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse_fp(globalconf.cfg, config_fp) == CFG_PARSE_ERROR) return false; return true; }
DLLIMPORT int cfg_parse(cfg_t *cfg, const char *filename) { int ret; FILE *fp; assert(cfg && filename); free(cfg->filename); cfg->filename = cfg_tilde_expand(filename); fp = fopen(cfg->filename, "r"); if(fp == 0) return CFG_FILE_ERROR; ret = cfg_parse_fp(cfg, fp); fclose(fp); return ret; }