xglcomp

diff src/optcfg/optcfg.h @ 0:d9b3fba68705

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 21 Jan 2016 08:45:31 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/optcfg/optcfg.h	Thu Jan 21 08:45:31 2016 +0200
     1.3 @@ -0,0 +1,86 @@
     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 +#ifdef __cplusplus
    1.21 +extern "C" {
    1.22 +#endif
    1.23 +
    1.24 +/* initialize the optcfg object with a valid option vector terminated by an
    1.25 + * entry with an opt value of -1 (other fields ignored for termination purposes)
    1.26 + *
    1.27 + * Example:
    1.28 + *   struct optcfg_option options[] = {
    1.29 + *       {'f', "foo", OPT_FOO, "Makes sure the foo is bar"},
    1.30 + *       {'h', "help", OPT_HELP, "Print usage information and exit"},
    1.31 + *       {0, 0, -1, 0}
    1.32 + *   };
    1.33 + *   struct optcfg *oc = optcfg_init(options);
    1.34 + */
    1.35 +struct optcfg *optcfg_init(struct optcfg_option *optv);
    1.36 +void optcfg_destroy(struct optcfg *oc);
    1.37 +
    1.38 +/* The parse_* functions call the option callback for each option.
    1.39 + *
    1.40 + * The option callback can then call optcfg_next_value to retrieve any
    1.41 + * values attached to this option. When optcfg_next_value returns 0, there
    1.42 + * are no more values available.
    1.43 + * The option callback must return 0 for success, and -1 to abort parsing.
    1.44 + */
    1.45 +void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls);
    1.46 +/* the argument callback is only called from optcfg_parse_args(), when a non-option
    1.47 + * argument is encountered (an argument not starting with a dash)
    1.48 + */
    1.49 +void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls);
    1.50 +
    1.51 +enum { OPTCFG_ERROR_FAIL, OPTCFG_ERROR_IGNORE };
    1.52 +void optcfg_set_error_action(struct optcfg *oc, int act);
    1.53 +
    1.54 +int optcfg_parse_args(struct optcfg *oc, int argc, char **argv);
    1.55 +int optcfg_parse_config_file(struct optcfg *oc, const char *fname);
    1.56 +int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp);
    1.57 +int optcfg_parse_config_line(struct optcfg *oc, const char *line);
    1.58 +/* TODO custom I/O callback version of config file parsing */
    1.59 +
    1.60 +/* special value function which returns if the option is enabled or disabled
    1.61 + * For config files it works similar to calling optcfg_next_value, and
    1.62 + * optcfg_bool_value in sequence.
    1.63 + * For argument parsing however, it doesn't consume further arguments. Merely
    1.64 + * the presence of the option makes it enabled, and its presence with a -no-
    1.65 + * prefix disables it. (TODO the second part of this)
    1.66 + */
    1.67 +int optcfg_enabled_value(struct optcfg *oc, int *enabledp);
    1.68 +
    1.69 +/* call optcfg_next_value in the option callback to retrieve the next value
    1.70 + * of the current option. returns 0 if there is no next value.
    1.71 + */
    1.72 +char *optcfg_next_value(struct optcfg *oc);
    1.73 +
    1.74 +/* helper function which can be used to print the available options */
    1.75 +void optcfg_print_options(struct optcfg *oc);
    1.76 +
    1.77 +/* helper functions to convert value strings to typed values
    1.78 + * returns 0 for success and value is returned through the valret pointer,
    1.79 + * otherwise it returns -1 for type mismatch, and valret contents are undefined
    1.80 + */
    1.81 +int optcfg_bool_value(char *str, int *valret);	/* accepts yes/no, true/false, 1/0 */
    1.82 +int optcfg_int_value(char *str, int *valret);
    1.83 +int optcfg_float_value(char *str, float *valret);
    1.84 +
    1.85 +#ifdef __cplusplus
    1.86 +}
    1.87 +#endif
    1.88 +
    1.89 +#endif	/* LIBOPTCFG_H_ */