nuclear@0: /* generic unified commandline option and config file parsing library */ nuclear@0: #ifndef LIBOPTCFG_H_ nuclear@0: #define LIBOPTCFG_H_ nuclear@0: nuclear@0: struct optcfg; nuclear@0: nuclear@0: struct optcfg_option { nuclear@0: char c; /* short (optional): used only for argument parsing */ nuclear@0: char *s; /* long: used for long options and config files */ nuclear@0: int opt; /* the corresponding option enumeration */ nuclear@0: char *desc; /* text description for printing usage information */ nuclear@0: }; nuclear@0: nuclear@0: typedef int (*optcfg_opt_callback)(struct optcfg *oc, int opt, void *cls); nuclear@0: typedef int (*optcfg_arg_callback)(struct optcfg *oc, const char *arg, void *cls); nuclear@0: nuclear@0: /* initialize the optcfg object with a valid option vector terminated by an nuclear@0: * entry with an opt value of -1 (other fields ignored for termination purposes) nuclear@0: * nuclear@0: * Example: nuclear@0: * struct optcfg_option options[] = { nuclear@0: * {'f', "foo", OPT_FOO, "Makes sure the foo is bar"}, nuclear@0: * {'h', "help", OPT_HELP, "Print usage information and exit"}, nuclear@0: * {0, 0, -1, 0} nuclear@0: * }; nuclear@0: * struct optcfg *oc = optcfg_init(options); nuclear@0: */ nuclear@0: struct optcfg *optcfg_init(struct optcfg_option *optv); nuclear@0: void optcfg_destroy(struct optcfg *oc); nuclear@0: nuclear@0: /* The parse_* functions call the option callback for each option. nuclear@0: * nuclear@0: * The option callback can then call optcfg_next_value to retrieve any nuclear@0: * values attached to this option. When optcfg_next_value returns 0, there nuclear@0: * are no more values available. nuclear@0: * The option callback must return 0 for success, and -1 to abort parsing. nuclear@0: */ nuclear@0: void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls); nuclear@0: /* the argument callback is only called from optcfg_parse_args(), when a non-option nuclear@0: * argument is encountered (an argument not starting with a dash) nuclear@0: */ nuclear@0: void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls); nuclear@0: nuclear@0: enum { OPTCFG_ERROR_FAIL, OPTCFG_ERROR_IGNORE }; nuclear@0: void optcfg_set_error_action(struct optcfg *oc, int act); nuclear@0: nuclear@0: int optcfg_parse_args(struct optcfg *oc, int argc, char **argv); nuclear@0: int optcfg_parse_config_file(struct optcfg *oc, const char *fname); nuclear@0: int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp); nuclear@0: int optcfg_parse_config_line(struct optcfg *oc, const char *line); nuclear@0: /* TODO custom I/O callback version of config file parsing */ nuclear@0: nuclear@2: /* special value function which returns if the option is enabled or disabled nuclear@2: * For config files it works similar to calling optcfg_next_value, and nuclear@2: * optcfg_bool_value in sequence. nuclear@2: * For argument parsing however, it doesn't consume further arguments. Merely nuclear@2: * the presence of the option makes it enabled, and its presence with a -no- nuclear@2: * prefix disables it. (TODO the second part of this) nuclear@2: */ nuclear@2: int optcfg_enabled_value(struct optcfg *oc, int *enabledp); nuclear@2: nuclear@0: /* call optcfg_next_value in the option callback to retrieve the next value nuclear@0: * of the current option. returns 0 if there is no next value. nuclear@0: */ nuclear@0: char *optcfg_next_value(struct optcfg *oc); nuclear@0: nuclear@0: /* helper function which can be used to print the available options */ nuclear@0: void optcfg_print_options(struct optcfg *oc); nuclear@0: nuclear@0: /* helper functions to convert value strings to typed values nuclear@0: * returns 0 for success and value is returned through the valret pointer, nuclear@0: * otherwise it returns -1 for type mismatch, and valret contents are undefined nuclear@0: */ nuclear@0: int optcfg_bool_value(char *str, int *valret); /* accepts yes/no, true/false, 1/0 */ nuclear@0: int optcfg_int_value(char *str, int *valret); nuclear@0: int optcfg_float_value(char *str, float *valret); nuclear@0: nuclear@0: nuclear@0: #endif /* LIBOPTCFG_H_ */