xglcomp

view 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 source
1 /* generic unified commandline option and config file parsing library */
2 #ifndef LIBOPTCFG_H_
3 #define LIBOPTCFG_H_
5 struct optcfg;
7 struct optcfg_option {
8 char c; /* short (optional): used only for argument parsing */
9 char *s; /* long: used for long options and config files */
10 int opt; /* the corresponding option enumeration */
11 char *desc; /* text description for printing usage information */
12 };
14 typedef int (*optcfg_opt_callback)(struct optcfg *oc, int opt, void *cls);
15 typedef int (*optcfg_arg_callback)(struct optcfg *oc, const char *arg, void *cls);
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
21 /* initialize the optcfg object with a valid option vector terminated by an
22 * entry with an opt value of -1 (other fields ignored for termination purposes)
23 *
24 * Example:
25 * struct optcfg_option options[] = {
26 * {'f', "foo", OPT_FOO, "Makes sure the foo is bar"},
27 * {'h', "help", OPT_HELP, "Print usage information and exit"},
28 * {0, 0, -1, 0}
29 * };
30 * struct optcfg *oc = optcfg_init(options);
31 */
32 struct optcfg *optcfg_init(struct optcfg_option *optv);
33 void optcfg_destroy(struct optcfg *oc);
35 /* The parse_* functions call the option callback for each option.
36 *
37 * The option callback can then call optcfg_next_value to retrieve any
38 * values attached to this option. When optcfg_next_value returns 0, there
39 * are no more values available.
40 * The option callback must return 0 for success, and -1 to abort parsing.
41 */
42 void optcfg_set_opt_callback(struct optcfg *oc, optcfg_opt_callback func, void *cls);
43 /* the argument callback is only called from optcfg_parse_args(), when a non-option
44 * argument is encountered (an argument not starting with a dash)
45 */
46 void optcfg_set_arg_callback(struct optcfg *oc, optcfg_arg_callback func, void *cls);
48 enum { OPTCFG_ERROR_FAIL, OPTCFG_ERROR_IGNORE };
49 void optcfg_set_error_action(struct optcfg *oc, int act);
51 int optcfg_parse_args(struct optcfg *oc, int argc, char **argv);
52 int optcfg_parse_config_file(struct optcfg *oc, const char *fname);
53 int optcfg_parse_config_stream(struct optcfg *oc, FILE *fp);
54 int optcfg_parse_config_line(struct optcfg *oc, const char *line);
55 /* TODO custom I/O callback version of config file parsing */
57 /* special value function which returns if the option is enabled or disabled
58 * For config files it works similar to calling optcfg_next_value, and
59 * optcfg_bool_value in sequence.
60 * For argument parsing however, it doesn't consume further arguments. Merely
61 * the presence of the option makes it enabled, and its presence with a -no-
62 * prefix disables it. (TODO the second part of this)
63 */
64 int optcfg_enabled_value(struct optcfg *oc, int *enabledp);
66 /* call optcfg_next_value in the option callback to retrieve the next value
67 * of the current option. returns 0 if there is no next value.
68 */
69 char *optcfg_next_value(struct optcfg *oc);
71 /* helper function which can be used to print the available options */
72 void optcfg_print_options(struct optcfg *oc);
74 /* helper functions to convert value strings to typed values
75 * returns 0 for success and value is returned through the valret pointer,
76 * otherwise it returns -1 for type mismatch, and valret contents are undefined
77 */
78 int optcfg_bool_value(char *str, int *valret); /* accepts yes/no, true/false, 1/0 */
79 int optcfg_int_value(char *str, int *valret);
80 int optcfg_float_value(char *str, float *valret);
82 #ifdef __cplusplus
83 }
84 #endif
86 #endif /* LIBOPTCFG_H_ */