1 Configuration and command line parser
2 =====================================
4 Libucw contains a parser for configuration files described in
7 The principle is you specify the structure of the configuration file,
8 the section names, variable names and types and your C variables that
9 are assigned to them. Then you run the parser and it fills your
10 variables with the values from the configuration file.
12 It is modular. It means you do not have to write all configuration at
13 the same place, you just declare the parts you need locally and do not
14 care about the other parts.
16 The command line parser has the same interface as unix getopt_long,
17 but handles setting of configuration files and configuration values
21 * <<ex_structure,The structure>>
23 - <<conf_h,ucw/conf.h>>
24 * <<conf_types,Data types>>
25 * <<conf_macros,Convenience macros>>
26 * <<alloc,Memory allocation>>
27 * <<journal,Undo journal>>
28 * <<bparser,Parsers for basic types>>
29 - <<getopt_h,ucw/getopt.h>>
30 * <<conf_load,Safe configuration loading>>
31 * <<conf_direct,Direct access>>
32 * <<conf_dump,Debug dumping>>
33 * <<conf_journal,Journaling control>>
34 * <<conf_getopt,Loading by cf_getopt()>>
41 Let's say you have configuration file with this content and want to
52 First, you declare the structure and let the configuration parser know
58 static char *hw_text = "Hello world";
59 static int hw_count = 1;
60 static int hw_wait_answer = 0;
62 static struct cf_section hw_config = {
64 CF_STRING("Text", &hw_text),
65 CF_INT("Count", &hw_count),
66 CF_INT("WaitAnswer", &hw_wait_answer),
71 static void CONSTRUCTOR hw_init(void) {
72 cf_declare_section("HelloWorld", &hw_config, 0);
75 The variables are used to store the loaded values. Their initial
76 values work as default, if nothing else is loaded. The hw_config()
77 structure assigns the variables to configuration names. The hw_init()
78 function (because of the `CONSTRUCTOR` macro) is run before main()
79 is called and it plugs in the whole section to the parser.
81 You can plug in as many configuration sections as you like, from
82 various places across your code.
87 You need to parse the command line arguments and load the
88 configuration. You can do it in a similar way to this example.
92 #include <ucw/getopt.h>
94 static byte short_opts[] = CF_SHORT_OPTS "v";
95 static struct option long_opts[] = {
97 { "verbose", 0, 0, 'v' },
103 int main(int argc, char *argv[]) {
104 cf_def_file = "default.cf";
106 while((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0)
108 case 'v': verbose = 1; break;
109 default: fprintf("Unknown option %c\n", opt); return 1;
112 The `short_opts` and `long_opts` variables describe the command line
113 arguments. Notice the `CF_SHORT_OPTS` and `CF_LONG_OPTS` macros. They
114 add options for the configuration parser. These options are handled
115 internally by @cf_getopt(). It loads the configuration before it starts
116 giving you your program's options.
118 See documentation of unix getopt_long() function.
124 Use this file if you want define a configuration section, request
125 loading of some variables or create new item type.
133 This header contains routines for parsing command line arguments and
134 loading the configuration.