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; }
void read_config(void) { static cfg_opt_t arg_opts[] = { CFG_STR("value", "default", CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_INT("delay", 3, CFGF_NONE), CFG_STR("message", "This is a message", CFGF_NONE), CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; char *buf = "" \ " delay = 3\n" \ "# message = \"asdfasfasfd tersf\"\n" \ " argument one { value = 1 }\n" \ " argument two { value=foo}\n"; cfg_free(cfg); cfg = cfg_init(opts, 0); cfg_parse_buf(cfg, buf); cfg_parse(cfg, config_filename); }
int main(void) { static cfg_opt_t section_opts[] = { CFG_STR("prop", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("section", section_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; const char *config_data = "section title_one { prop = 'value_one' }\n" "section title_two { prop = 'value_two' }\n"; int rc; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); fail_unless(cfg_addtsec(cfg, "section", "title_three")); fail_unless(cfg_size(cfg, "section") == 3); fail_unless(cfg_title(cfg_gettsec(cfg, "section", "title_three"))); /* attempt to add a pre-existing section should fail */ fail_unless(!cfg_addtsec(cfg, "section", "title_three")); cfg_free(cfg); return 0; }
bool SyncLogger::ParseFile(const char* pszHash) { if (strcmp(m_szCurShare, pszHash) == 0) { // File is currently parsed, there is no need to parse it again. return true; } cfg_opt_t modEntry[] = { // Parses within the group. CFG_STR(FILE_PATH_VARNAME, FILE_PATH_DEFAULT, CFGF_NONE), CFG_STR(MOD_TIME_VARNAME, MOD_TIME_DEFAULT, CFGF_NONE), CFG_STR(MOD_TYPE_VARNAME, MOD_TYPE_DEFAULT, CFGF_NONE), CFG_END() }; cfg_opt_t entries[] = { // Parses the single groups. CFG_SEC(MOD_NUMBER_VARNAME, modEntry, CFGF_TITLE | CFGF_MULTI), CFG_END() }; // Initializes the parser. m_pCFG = cfg_init(entries, CFGF_NONE); // Parses the file. char szLogName[MAX_PATH]; CalcLogFileName(pszHash, szLogName); if (cfg_parse(m_pCFG, szLogName) == CFG_PARSE_ERROR) return false; return true; }
cfg_t *parse_conf(const char *filename) { cfg_opt_t process_opts[] = { CFG_STR("comm", 0, CFGF_NODEFAULT), CFG_STR("args", 0, CFGF_NODEFAULT), CFG_STR("pre", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("process", process_opts, CFGF_MULTI | CFGF_TITLE), CFG_INT("frequency", 10, CFGF_NONE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); //cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port); //cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno)); printf("continuing with default values...\n\n"); case CFG_SUCCESS: break; case CFG_PARSE_ERROR: return 0; } return cfg; }
cfg_t *parse_conf(char *conf) { cfg_opt_t provider_opts[] = { CFG_STR ("username", 0, CFGF_NONE), CFG_STR ("password", 0, CFGF_NONE), CFG_STR_LIST("alias", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_BOOL("syslog", cfg_false, CFGF_NONE), CFG_BOOL("wildcard", cfg_false, CFGF_NONE), CFG_STR ("bind", 0, CFGF_NONE), CFG_INT ("period", 60, CFGF_NONE), CFG_INT ("startup-delay", 0, CFGF_NONE), CFG_INT ("forced-update", 720000, CFGF_NONE), CFG_SEC ("provider", provider_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); switch (cfg_parse(cfg, conf)) { case CFG_FILE_ERROR: fprintf(stderr, "Cannot read configuration file %s: %s\n", conf, strerror(errno)); case CFG_PARSE_ERROR: return NULL; case CFG_SUCCESS: break; } return cfg; }
int main(void) { cfg_opt_t sub_opts[] = { CFG_BOOL("bool", cfg_false, CFGF_NONE), CFG_STR("string", NULL, CFGF_NONE), CFG_INT("int", 0, CFGF_NONE), CFG_FLOAT("float", 0.0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_BOOL_LIST("bool", cfg_false, CFGF_NONE), CFG_STR_LIST("string", NULL, CFGF_NONE), CFG_INT_LIST("int", 0, CFGF_NONE), CFG_FLOAT_LIST("float", "0.0", CFGF_NONE), CFG_SEC("sub", sub_opts, CFGF_MULTI | CFGF_TITLE | CFGF_NO_TITLE_DUPES), CFG_END() }; char *cmd = NULL; const char *reply; int res; int i; cfg = cfg_init(opts, CFGF_NONE); for (;;) { printf("cli> "); fflush(stdout); if (cmd) free(cmd); cmd = input_cmd(); if (!cmd) exit(0); res = split_cmd(cmd); if (res < 0) { printf("Parse error\n"); continue; } if (cmdc == 0) continue; for (i = 0; cmds[i].cmd; ++i) { if (strcmp(cmdv[0], cmds[i].cmd)) continue; reply = cmds[i].handler(cmdc, cmdv); if (!reply) exit(0); printf("%s", reply); break; } if (!cmds[i].cmd) printf("Unknown command\n"); } cfg_free(cfg); return 0; }
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; }
void validate_setup(void) { cfg_opt_t *opt = 0; static cfg_opt_t action_opts[] = { CFG_INT("speed", 0, CFGF_NONE), CFG_STR("name", 0, CFGF_NONE), CFG_INT("xspeed", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t multi_opts[] = { CFG_INT_LIST("speeds", 0, CFGF_NONE), CFG_SEC("options", action_opts, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR_LIST("ip-address", 0, CFGF_NONE), CFG_INT_CB("action", ACTION_NONE, CFGF_NONE, parse_action), CFG_SEC("options", action_opts, CFGF_NONE), CFG_SEC("multi_options", multi_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, 0); cfg_set_validate_func(cfg, "ip-address", validate_ip); fail_unless(cfg_set_validate_func(cfg, "ip-address", validate_ip) == validate_ip); opt = cfg_getopt(cfg, "ip-address"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_ip); cfg_set_validate_func(cfg, "options", validate_action); fail_unless(cfg_set_validate_func(cfg, "options", validate_action) == validate_action); opt = cfg_getopt(cfg, "options"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_action); cfg_set_validate_func(cfg, "options|speed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "options|speed", validate_speed) == validate_speed); opt = cfg_getopt(cfg, "options|speed"); fail_unless(opt != 0); fail_unless(opt->validcb == validate_speed); cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|speeds", validate_speed) == validate_speed); cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed); fail_unless(cfg_set_validate_func(cfg, "multi_options|options|xspeed", validate_speed) == validate_speed); /* Validate callbacks for *set*() functions, i.e. not when parsing file content */ cfg_set_validate_func2(cfg, "multi_options|speed", validate_speed2); cfg_set_validate_func2(cfg, "multi_options|options|name", validate_name2); }
static cfg_t *create_config(void) { static cfg_opt_t sec_opts[] = { CFG_INT("a", 1, CFGF_NONE), CFG_INT("b", 2, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("sec", sec_opts, CFGF_MULTI | CFGF_TITLE), CFG_END() }; return cfg_init(opts, 0); }
int main(void) { char *comment; char *expect = "Now, is it this comment that goes with the option?"; cfg_t *cfg; cfg_opt_t *opt; cfg_opt_t section_opts[] = { CFG_INT("key", 0, CFGF_NONE), CFG_BOOL("bool", 0, CFGF_NONE), CFG_STR("option", NULL, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_STR("option", NULL, CFGF_NONE), CFG_SEC("section", section_opts, CFGF_MULTI), CFG_END() }; cfg = cfg_init(opts, CFGF_COMMENTS); fail_unless(cfg != NULL); fail_unless(cfg_parse(cfg, SRC_DIR "/annotate.conf") == CFG_SUCCESS); /* Verify the parser read the correct comment for this tricky option */ opt = cfg_getopt(cfg, "section|option"); fail_unless(opt != NULL); comment = cfg_opt_getcomment(opt); fail_unless(comment != NULL); fail_unless(strcmp(comment, expect) == 0); expect = "But what's the worst poetry in the universe?"; fail_unless(cfg_opt_setcomment(opt, expect) == CFG_SUCCESS); cfg_opt_setnstr(opt, "Paula Nancy Millstone Jennings was a poet who wrote the worst poetry in " "the universe. In fact, her poetry is still considered to be the worst in " "the Galaxy, closely followed by that of the Azgoths of Kria and the " "Vogons, in that order.", 0); /* Verify that the comment is not reset when changing option value */ comment = cfg_opt_getcomment(opt); fail_unless(comment != NULL); fail_unless(strcmp(comment, expect) == 0); cfg_print(cfg, stdout); fail_unless(cfg_free(cfg) == CFG_SUCCESS); return 0; }
cfg_opt_t * build_section(section_t *section, section_ptrs &ptrs) { static const char *funcname = "conf::build_section"; cfg_opt_t *ptr = new cfg_opt_t[section->size() + 1]; ptrs.push_back(std::unique_ptr<cfg_opt_t []>(ptr)); for (auto &entry : *section) { char *name = const_cast<char *>(entry.first.c_str()); switch (entry.second.what_type()) { case val_type::integer: *ptr = CFG_INT(name, 0, CFGF_NODEFAULT); break; case val_type::string: *ptr = CFG_STR(name, 0, CFGF_NODEFAULT); break; case val_type::multistring: *ptr = CFG_STR_LIST(name, 0, CFGF_NODEFAULT); break; case val_type::section: *ptr = CFG_SEC(name, build_section(entry.second.get<conf::section_t *>(), ptrs), CFGF_NONE); break; case val_type::unknown: throw logging::error(funcname, "Val with unknown type in section: %s", name); } ptr++; } *ptr = CFG_END(); return ptrs.back().get(); }
BOOL saveConfFile(user_input_data *user_dat) { FILE *U2Mconf_file = fopen(cfg_filename, "w"); if (!U2Mconf_file) return FALSE; cfg_opt_t email_opts[] = { CFG_STR("From", user_dat->FROM, CFGF_NONE), CFG_STR("To", user_dat->TO, CFGF_NONE), CFG_STR("Cc", user_dat->CC, CFGF_NONE), CFG_STR("Subject", user_dat->SUBJECT, CFGF_NONE), CFG_STR("Body", user_dat->BODY, CFGF_NONE), CFG_STR("Password", user_dat->pass, CFGF_NONE), CFG_STR("SMTP_server", user_dat->SMTP_SERVER, CFGF_NONE), CFG_INT("Port_number", (int)user_dat->PORT, CFGF_NONE), CFG_END() }; cfg_t *U2MConf = cfg_init(email_opts, CFGF_NONE); cfg_print(U2MConf, U2Mconf_file); fclose(U2Mconf_file); cfg_free(U2MConf); return TRUE; }
/* Function to parse the server config file. @param Config file location in the file system. @param Struct to write results into. @return -1 on failure, 0 on success. */ int parse_config(char * config_file, struct config_struct * running_config) { print_to_log("Parsing config file", LOG_INFO); cfg_opt_t opts[] = { CFG_STR("domain", "", CFGF_NONE), CFG_INT("connection_timeout_in_seconds", 0, CFGF_NONE), CFG_INT("max_connections", 0, CFGF_NONE), CFG_END() }; cfg_t *cfg; cfg = cfg_init(opts, CFGF_NONE); if(cfg_parse(cfg, config_file) == CFG_PARSE_ERROR) { printf("Reading config %s has failed\n", config_file); return -1; } if (strcmp(cfg_getstr(cfg, "domain"),"")!=0) { //Load domain into struct here. } if (cfg_getint(cfg, "connection_timeout_in_seconds")<=60) { //load connection_timeout_in_seconds into struct here. running_config->connection_timeout_in_seconds = cfg_getint(cfg, "connection_timeout_in_seconds"); } if (cfg_getint(cfg, "max_connections")<=1000) { //load connection_timeout_in_seconds into struct here. running_config->max_connections = cfg_getint(cfg, "max_connections"); } return 0; }
/** * 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; }
int main(void) { static cfg_opt_t section_opts[] = { CFG_STR("prop", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("section", section_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; const char *config_data = "section title_one { prop = 'value_one' }\n" "section title_two { prop = 'value_two' }\n" "section title_one { prop = 'value_one' }\n"; int rc; cfg_t *cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); cfg_rmtsec(cfg, "section", "title_two"); fail_unless(cfg_size(cfg, "section") == 1); fail_unless(strcmp(cfg_title(cfg_getnsec(cfg, "section", 0)), "title_one") == 0); cfg_free(cfg); cfg = cfg_init(opts, CFGF_NONE); fail_unless(cfg); rc = cfg_parse_buf(cfg, config_data); fail_unless(rc == CFG_SUCCESS); cfg_rmsec(cfg, "section"); fail_unless(cfg_size(cfg, "section") == 1); fail_unless(strcmp(cfg_title(cfg_getnsec(cfg, "section", 0)), "title_two") == 0); cfg_free(cfg); return 0; }
int main(void) { cfg_opt_t group_opts[] = { CFG_INT("number", 0, CFGF_NONE), CFG_INT("total", 0, CFGF_NONE), CFG_END() }; cfg_opt_t groups_opts[] = { CFG_STR("name", "Esmé", CFGF_NONE), CFG_SEC("group", group_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("groups", groups_opts, CFGF_NONE), CFG_END() }; cfg_t *cfg, *sec; size_t i, j; cfg = cfg_init(opts, CFGF_NONE); if (!cfg || cfg_parse(cfg, CONF) == CFG_PARSE_ERROR) { perror("Failed parsing " CONF); return 1; } /* Iterate over the sections and print fields from each section. */ for (i = 0; i < cfg_size(cfg, "groups"); i++) { sec = cfg_getnsec(cfg, "groups", i); for (j = 0; j < cfg_size(sec, "group"); j++) { cfg_t *opt = cfg_getnsec(sec, "group", j); printf("group title: '%s'\n", cfg_title(opt)); printf("group number: %ld\n", cfg_getint(opt, "number")); printf("group total: %ld\n", cfg_getint(opt, "total")); printf("\n"); } } cfg_free(cfg); return 0; }
cfg_opt_t *filters_get_cfg_opts(void) { cfg_opt_t *opts = malloc(sizeof(cfg_opt_t) * (NUM_FILTER_OPTIONS + 1)); for (int i = 0; i < NUM_FILTER_OPTIONS; ++i) memcpy(&opts[i], &filter_options[i].confuse_opt, sizeof(cfg_opt_t)); cfg_opt_t end = CFG_END(); memcpy(&opts[NUM_FILTER_OPTIONS], &end, sizeof(cfg_opt_t)); return opts; }
cfg_t *parse_conf(const char *filename) { cfg_opt_t bookmark_opts[] = { CFG_STR("host", 0, CFGF_NODEFAULT), CFG_INT("port", 21, CFGF_NONE), CFG_STR("login", "anonymous", CFGF_NONE), CFG_STR("password", "anonymous@", CFGF_NONE), CFG_STR("directory", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE), CFG_BOOL("reverse-dns", cfg_true, CFGF_NONE), CFG_BOOL("passive-mode", cfg_false, CFGF_NONE), CFG_BOOL("remote-completion", cfg_true, CFGF_NONE), CFG_FUNC("alias", conf_alias), CFG_STR_LIST("xterm-terminals", "{xterm, rxvt}", CFGF_NONE), CFG_INT_CB("auto-create-bookmark", ACB_YES, CFGF_NONE, conf_parse_acb), CFG_FUNC("include-file", cfg_include), CFG_END() }; cfg_t *cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "bookmark|port", conf_validate_port); cfg_set_validate_func(cfg, "bookmark", conf_validate_bookmark); switch(cfg_parse(cfg, filename)) { case CFG_FILE_ERROR: printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno)); printf("continuing with default values...\n\n"); case CFG_SUCCESS: break; case CFG_PARSE_ERROR: return 0; } return cfg; }
int main(void) { cfg_opt_t greet_opts[] = { CFG_STR_LIST("targets", "{World}", CFGF_NONE), CFG_INT("repeat", 1, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("greeting", greet_opts, CFGF_TITLE | CFGF_MULTI), CFG_END() }; cfg_t *cfg, *cfg_greet; int repeat; int i, j; cfg = cfg_init(opts, CFGF_NONE); cfg_set_validate_func(cfg, "greeting|repeat", validate_unsigned_int); if(cfg_parse(cfg, "hello.conf") == CFG_PARSE_ERROR) return 1; for(j = 0; j < cfg_size(cfg, "greeting"); j++) { cfg_greet = cfg_getnsec(cfg, "greeting", j); repeat = cfg_getint(cfg_greet, "repeat"); while(repeat--) { printf("%s", cfg_title(cfg_greet)); for(i = 0; i < cfg_size(cfg_greet, "targets"); i++) printf(", %s", cfg_getnstr(cfg_greet, "targets", i)); printf("!\n"); } } cfg_free(cfg); return 0; }
void load_config(god_t* god){ check_dirs(); char *home_dir = getenv("HOME"); char *conf_file = "/.local/share/crunchball/crunchball.cfg"; char tmp_path[500] = {0}; cp_str(tmp_path, home_dir); cat_to_str(tmp_path, conf_file); long int video_mode; long int audio_level; long int fullscreen; long int high_score; cfg_opt_t opts[] = { CFG_SIMPLE_INT("video_mode", &video_mode), CFG_SIMPLE_INT("audio_level", &audio_level), CFG_SIMPLE_INT("fullscreen", &fullscreen), CFG_SIMPLE_INT("high_score", &high_score), CFG_END() }; cfg_t *cfg; cfg = cfg_init(opts, 0); int rc = cfg_parse(cfg, tmp_path); if ( rc != 0 ){ video_mode = 2; audio_level = 5; fullscreen = 0; high_score = 0; } if (video_mode == 0) video_mode = 2; god->scalar.scale = video_mode; god->state.settings_volume = audio_level; god->state.high_score = high_score; god->scalar.fs = fullscreen; FILE *fp = fopen(tmp_path, "w"); cfg_print(cfg, fp); fclose(fp); cfg_free(cfg); }
/** 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; }
cfg_opt_t * get_opt(){ static cfg_opt_t opts[] = { CFG_STR("user", "root", CFGF_NONE), CFG_STR("password", "", CFGF_NONE), CFG_STR("dbname", "", CFGF_NONE), CFG_STR("ip", "127.0.0.1", CFGF_NONE), CFG_INT("port", 5432, CFGF_NONE), CFG_STR("db_query", "", CFGF_NONE), CFG_INT("svm_type",0, CFGF_NONE), CFG_INT("kernel_type",0,CFGF_NONE), CFG_FLOAT("gamma",0,CFGF_NONE), CFG_FLOAT("C",1,CFGF_NONE), CFG_STR("model_filename", "file.model", CFGF_NONE), CFG_STR("model_list_path","",CFGF_NONE), CFG_STR_LIST("model_list","{}",CFGF_NONE), CFG_STR_LIST("query_list","{}",CFGF_NONE), CFG_STR_LIST("model_label_list","{}",CFGF_NONE), CFG_END() }; return opts; }
int readConfigurationfile() { cfg_opt_t opts[] = { CFG_SIMPLE_INT("button_enabled", &confres.button_enabled), CFG_SIMPLE_INT("gamepad1_enabled", &confres.gamepad1_enabled), CFG_SIMPLE_STR("gamepad1_type", &confres.gamepad1_type), CFG_SIMPLE_INT("gamepad2_enabled", &confres.gamepad2_enabled), CFG_SIMPLE_STR("gamepad2_type", &confres.gamepad2_type), CFG_SIMPLE_STR("adapter_version", &confres.adapter_version), CFG_END() }; cfg = cfg_init(opts, 0); if ( access(CFGFILENAME, F_OK) == -1 ) { printf("[SNESDev-Rpi] Error: Cannot find /etc/snesdev.cfg\n"); return 1; } if (cfg_parse(cfg, CFGFILENAME) == CFG_PARSE_ERROR) { printf("[SNESDev-Rpi] Error: Cannot parse /etc/snesdev.cfg\n"); return 1; } return 0; }
static void adm5120_parse_configure (adm5120_t * adm5120) { vm_instance_t *vm = adm5120->vm; cfg_opt_t opts[] = { COMMON_CONFIG_OPTION CFG_END () }; cfg_t *cfg; cfg = cfg_init (opts, 0); cfg_parse (cfg, vm->configure_filename); cfg_free (cfg); VALID_COMMON_CONFIG_OPTION; if (vm->boot_method == BOOT_BINARY) { ASSERT (vm->boot_from == 1, "boot_from must be 1(NOR Flash)\n ADM5120 only can boot from NOR Flash.\n"); } /*Print the configure information */ printf_configure (adm5120); }
/* general section structure */ static cfg_opt_t sec_general[] = { CFG_STR("uid", "nobody", CFGF_NONE), CFG_STR("admin_password", NULL, CFGF_NONE), CFG_STR("logfile", STATEDIR "/log/" PACKAGE ".log", CFGF_NONE), CFG_STR("db_path", STATEDIR "/cache/" PACKAGE "/songs3.db", CFGF_NONE), CFG_INT("db_pragma_cache_size", -1, CFGF_NONE), CFG_STR("db_pragma_journal_mode", NULL, CFGF_NONE), CFG_INT("db_pragma_synchronous", -1, CFGF_NONE), CFG_INT_CB("loglevel", E_LOG, CFGF_NONE, &cb_loglevel), CFG_BOOL("ipv6", cfg_true, CFGF_NONE), CFG_STR("cache_path", STATEDIR "/cache/" PACKAGE "/cache.db", CFGF_NONE), CFG_INT("cache_daap_threshold", 1000, CFGF_NONE), CFG_END() }; /* library section structure */ static cfg_opt_t sec_library[] = { CFG_STR("name", "My Music on %h", CFGF_NONE), CFG_INT("port", 3689, CFGF_NONE), CFG_STR("password", NULL, CFGF_NONE), CFG_STR_LIST("directories", NULL, CFGF_NONE), CFG_STR_LIST("podcasts", NULL, CFGF_NONE), CFG_STR_LIST("audiobooks", NULL, CFGF_NONE), CFG_STR_LIST("compilations", NULL, CFGF_NONE), CFG_STR("compilation_artist", NULL, CFGF_NONE), CFG_BOOL("hide_singles", cfg_false, CFGF_NONE), CFG_BOOL("radio_playlists", cfg_false, CFGF_NONE),
int handle_lispd_config_file() { cfg_t *cfg = 0; unsigned int i = 0; unsigned n = 0; int ret = 0; static cfg_opt_t map_server_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("key-type", 0, CFGF_NONE), CFG_STR("key", 0, CFGF_NONE), CFG_BOOL("proxy-reply", cfg_false, CFGF_NONE), CFG_BOOL("verify", cfg_false, CFGF_NONE), CFG_END() }; static cfg_opt_t db_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", -1, CFGF_NONE), CFG_STR("interface", 0, CFGF_NONE), CFG_INT("priority_v4", 0, CFGF_NONE), CFG_INT("weight_v4", 0, CFGF_NONE), CFG_INT("priority_v6", 0, CFGF_NONE), CFG_INT("weight_v6", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t mc_mapping_opts[] = { CFG_STR("eid-prefix", 0, CFGF_NONE), CFG_INT("iid", 0, CFGF_NONE), CFG_STR("rloc", 0, CFGF_NONE), CFG_INT("priority", 0, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; static cfg_opt_t petr_mapping_opts[] = { CFG_STR("address", 0, CFGF_NONE), CFG_INT("priority", 255, CFGF_NONE), CFG_INT("weight", 0, CFGF_NONE), CFG_END() }; cfg_opt_t opts[] = { CFG_SEC("database-mapping", db_mapping_opts, CFGF_MULTI), CFG_SEC("static-map-cache", mc_mapping_opts, CFGF_MULTI), CFG_SEC("map-server", map_server_opts, CFGF_MULTI), CFG_SEC("proxy-etr", petr_mapping_opts, CFGF_MULTI), CFG_INT("map-request-retries", 0, CFGF_NONE), CFG_INT("control-port", 0, CFGF_NONE), CFG_BOOL("debug", cfg_false, CFGF_NONE), CFG_STR("map-resolver", 0, CFGF_NONE), CFG_STR_LIST("proxy-itrs", 0, CFGF_NONE), CFG_END() }; /* * parse config_file */ cfg = cfg_init(opts, CFGF_NOCASE); ret = cfg_parse(cfg, config_file); if (ret == CFG_FILE_ERROR) { syslog(LOG_DAEMON, "Couldn't find config file %s, exiting...", config_file); exit(EXIT_FAILURE); } else if(ret == CFG_PARSE_ERROR) { syslog(LOG_DAEMON, "NOTE: Version 0.2.4 changed the format of the 'proxy-etr' element."); syslog(LOG_DAEMON, " Check the 'lispd.conf.example' file for an example entry in"); syslog(LOG_DAEMON, " the new format."); syslog(LOG_DAEMON, "Parse error in file %s, exiting...", config_file); exit(EXIT_FAILURE); } /* * lispd config options */ ret = cfg_getint(cfg, "map-request-retries"); if (ret != 0) map_request_retries = ret; cfg_getbool(cfg, "debug") ? (debug = 1) : (debug = 0); /* * LISP config options */ /* * handle map-resolver config */ map_resolver = cfg_getstr(cfg, "map-resolver"); if (!add_server(map_resolver, &map_resolvers)) return(0); #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to map-resolver list", map_resolver); #endif /* * handle proxy-etr config */ n = cfg_size(cfg, "proxy-etr"); for(i = 0; i < n; i++) { cfg_t *petr = cfg_getnsec(cfg, "proxy-etr", i); if (!add_proxy_etr_entry(petr, &proxy_etrs)) { syslog(LOG_DAEMON, "Can't add proxy-etr %d (%s)", i, cfg_getstr(petr, "address")); } } if (!proxy_etrs){ syslog(LOG_DAEMON, "WARNING: No Proxy-ETR defined. Packets to non-LISP destinations will be forwarded natively (no LISP encapsulation). This may prevent mobility in some scenarios."); sleep(3); } /* * handle proxy-itr config */ n = cfg_size(cfg, "proxy-itrs"); for(i = 0; i < n; i++) { if ((proxy_itr = cfg_getnstr(cfg, "proxy-itrs", i)) != NULL) { if (!add_server(proxy_itr, &proxy_itrs)) continue; #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to proxy-itr list", proxy_itr); #endif } } /* * handle database-mapping config */ n = cfg_size(cfg, "database-mapping"); for(i = 0; i < n; i++) { cfg_t *dm = cfg_getnsec(cfg, "database-mapping", i); if (!add_database_mapping(dm)) { syslog(LOG_DAEMON, "Can't add database-mapping %d (%s->%s)", i, cfg_getstr(dm, "eid-prefix"), cfg_getstr(dm, "interface")); } } /* * handle map-server config */ n = cfg_size(cfg, "map-server"); for(i = 0; i < n; i++) { cfg_t *ms = cfg_getnsec(cfg, "map-server", i); if (!add_map_server(cfg_getstr(ms, "address"), cfg_getint(ms, "key-type"), cfg_getstr(ms, "key"), (cfg_getbool(ms, "proxy-reply") ? 1:0), (cfg_getbool(ms, "verify") ? 1:0))) return(0); #ifdef DEBUG syslog(LOG_DAEMON, "Added %s to map-server list", cfg_getstr(ms, "address")); #endif } /* * handle static-map-cache config */ n = cfg_size(cfg, "static-map-cache"); for(i = 0; i < n; i++) { cfg_t *smc = cfg_getnsec(cfg, "static-map-cache", i); if (!add_static_map_cache_entry(smc)) { syslog(LOG_DAEMON,"Can't add static-map-cache %d (EID:%s -> RLOC:%s)", i, cfg_getstr(smc, "eid-prefix"), cfg_getstr(smc, "rloc")); } } #if (DEBUG > 3) dump_tree(AF_INET,AF4_database); dump_tree(AF_INET6,AF6_database); dump_database(); dump_map_servers(); dump_servers(map_resolvers, "map-resolvers"); dump_servers(proxy_etrs, "proxy-etrs"); dump_servers(proxy_itrs, "proxy-itrs"); dump_map_cache(); #endif cfg_free(cfg); return(0); }
#include <time.h> #include "peer.h" #include <chunk.h> #include <mon.h> #include <ul.h> #define MAX_PACKET_SIZE 2048 #define INITIAL_PKTBUFFER_NUM 10 /** Configuration options for the Source */ cfg_opt_t cfg_source[] = { CFG_STR("stream", NULL, CFGF_NONE), CFG_FLOAT("chunk_duration", 1.0, CFGF_NONE), CFG_END() }; /** This function gets called periodically and arranges the publishing of a * MeasurementRecord with SrcLag = 0.0 */ void periodicPublishSrcLag(HANDLE h, void *arg) { MeasurementRecord mr; memset(&mr, 0, sizeof(mr)); mr.published_name = "SrcLag"; mr.value = 0.0; mr.originator = mr.targetA = mr.targetB = LocalPeerID; mr.channel = channel; publishMeasurementRecord(&mr, "SrcLag"); }
void gimmix_config_save (Conf *conf) { FILE *fp; cfg_t *cfg; cfg_opt_t *sopts; cfg_opt_t opts[] = { CFG_SIMPLE_STR ("mpd_hostname", NULL), CFG_SIMPLE_INT ("mpd_port", 0), CFG_SIMPLE_STR ("mpd_password", NULL), CFG_SIMPLE_BOOL ("enable_systray", false), CFG_SIMPLE_BOOL ("enable_notify", false), CFG_END() }; cfg = cfg_init(opts, 0); char *rcfile = cfg_tilde_expand ("~/.gimmixrc"); if((fp = fopen(rcfile, "w"))) { fprintf (fp, "# Gimmix configuration\n"); fprintf (fp, "\n# MPD hostname (default: localhost)\n"); if (conf->hostname) cfg_setstr(cfg, "mpd_hostname", conf->hostname); sopts = cfg_getopt (cfg, "mpd_hostname"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD port (default: 6600)\n"); if (conf->port > 0) cfg_setint(cfg, "mpd_port", conf->port); else cfg_setint(cfg, "mpd_port", 0); sopts = cfg_getopt (cfg, "mpd_port"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# MPD password (leave blank for no password) \n"); if (conf->password) cfg_setstr(cfg, "mpd_password", conf->password); sopts = cfg_getopt (cfg, "mpd_password"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable systray icon (Enable = true, Disable = false)\n"); if (conf->systray_enable == 1) cfg_setbool(cfg, "enable_systray", true); else cfg_setbool(cfg, "enable_systray", false); sopts = cfg_getopt (cfg, "enable_systray"); cfg_opt_print (sopts, fp); fprintf (fp, "\n# Enable/Disable system tray notifications (Enable = true, Disable = false) \n"); if (conf->notify_enable == 1) cfg_setbool(cfg, "enable_notify", true); else cfg_setbool(cfg, "enable_notify", false); sopts = cfg_getopt (cfg, "enable_notify"); cfg_opt_print (sopts, fp); free (rcfile); fclose (fp); } else { fprintf (stderr, "Error while saving config.\n"); } cfg_free_value (opts); cfg_free (cfg); return; }
Conf * gimmix_config_init (void) { cfg_t *cfg = NULL; char *rcfile; Conf *conf; int ret; int port = 1; cfg_bool_t systray_enable = true; cfg_bool_t notify_enable = true; conf = (Conf*)malloc(sizeof(Conf)); char *host = NULL; char *pass = NULL; cfg_opt_t opts[] = { CFG_SIMPLE_STR ("mpd_hostname", &host), CFG_SIMPLE_INT ("mpd_port", &port), CFG_SIMPLE_STR ("mpd_password", &pass), CFG_SIMPLE_BOOL ("enable_systray", &systray_enable), CFG_SIMPLE_BOOL ("enable_notify", ¬ify_enable), CFG_END() }; cfg = cfg_init (opts, 0); rcfile = cfg_tilde_expand ("~/.gimmixrc"); ret = cfg_parse (cfg, rcfile); free (rcfile); if (ret == CFG_PARSE_ERROR) { free (conf); return NULL; } if (host != NULL) strncpy (conf->hostname, host, 255); if (pass != NULL) strncpy (conf->password, pass, 255); conf->port = port; if (systray_enable == true) { conf->systray_enable = 1; } else { conf->systray_enable = 0; } if (notify_enable == true) { conf->notify_enable = 1; } else { conf->notify_enable = 0; } /* Free the memory */ cfg_free_value (opts); if (cfg) cfg_free (cfg); return conf; }