liboptcfg

diff src/optcfg.h @ 0:10b89befcaa9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 13 Nov 2015 23:48:31 +0200
parents
children 9c73004c7af3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/optcfg.h	Fri Nov 13 23:48:31 2015 +0200
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* generic unified commandline option and config file parsing library */
     1.5 +#ifndef LIBOPTCFG_H_
     1.6 +#define LIBOPTCFG_H_
     1.7 +
     1.8 +struct optcfg;
     1.9 +
    1.10 +struct optcfg_option {
    1.11 +	char c;		/* short (optional): used only for argument parsing */
    1.12 +	char *s;	/* long: used for long options and config files */
    1.13 +	int opt;	/* the corresponding option enumeration */
    1.14 +	char *desc;	/* text description for printing usage information */
    1.15 +};
    1.16 +
    1.17 +typedef int (*optcfg_opt_callback)(struct optcfg *oc, int opt, void *cls);
    1.18 +typedef int (*optcfg_arg_callback)(struct optcfg *oc, const char *arg, void *cls);
    1.19 +
    1.20 +/* initialize the optcfg object with a valid option vector terminated by an
    1.21 + * entry with an opt value of -1 (other fields ignored for termination purposes)
    1.22 + *
    1.23 + * Example:
    1.24 + *   struct optcfg_option options[] = {
    1.25 + *       {'f', "foo", OPT_FOO, "Makes sure the foo is bar"},
    1.26 + *       {'h', "help", OPT_HELP, "Print usage information and exit"},
    1.27 + *       {0, 0, -1, 0}
    1.28 + *   };
    1.29 + *   struct optcfg *oc = optcfg_init(options);
    1.30 + */
    1.31 +struct optcfg *optcfg_init(struct optcfg_option *optv);
    1.32 +void optcfg_destroy(struct optcfg *oc);
    1.33 +
    1.34 +/* The parse_* functions call the option callback for each option.
    1.35 + *
    1.36 + * The option callback can then call optcfg_next_value to retrieve any
    1.37 + * values attached to this option. When optcfg_next_value returns 0, there
    1.38 + * are no more values available.
    1.39 + * The option callback must return 0 for success, and -1 to abort parsing.
    1.40 + */
    1.41 +void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls);
    1.42 +/* the argument callback is only called from optcfg_parse_args(), when a non-option
    1.43 + * argument is encountered (an argument not starting with a dash)
    1.44 + */
    1.45 +void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls);
    1.46 +
    1.47 +enum { OPTCFG_ERROR_FAIL, OPTCFG_ERROR_IGNORE };
    1.48 +void optcfg_set_error_action(struct optcfg *oc, int act);
    1.49 +
    1.50 +int optcfg_parse_args(struct optcfg *oc, int argc, char **argv);
    1.51 +int optcfg_parse_config_file(struct optcfg *oc, const char *fname);
    1.52 +int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp);
    1.53 +int optcfg_parse_config_line(struct optcfg *oc, const char *line);
    1.54 +/* TODO custom I/O callback version of config file parsing */
    1.55 +
    1.56 +/* call optcfg_next_value in the option callback to retrieve the next value
    1.57 + * of the current option. returns 0 if there is no next value.
    1.58 + */
    1.59 +char *optcfg_next_value(struct optcfg *oc);
    1.60 +
    1.61 +/* helper function which can be used to print the available options */
    1.62 +void optcfg_print_options(struct optcfg *oc);
    1.63 +
    1.64 +/* helper functions to convert value strings to typed values
    1.65 + * returns 0 for success and value is returned through the valret pointer,
    1.66 + * otherwise it returns -1 for type mismatch, and valret contents are undefined
    1.67 + */
    1.68 +int optcfg_bool_value(char *str, int *valret);	/* accepts yes/no, true/false, 1/0 */
    1.69 +int optcfg_int_value(char *str, int *valret);
    1.70 +int optcfg_float_value(char *str, float *valret);
    1.71 +
    1.72 +
    1.73 +#endif	/* LIBOPTCFG_H_ */