Parsing capabilities of libcfg+ library are accessed via context. It allows you to parse multiple configuration sets. For particular command lines and/or configuration files you can have different contexes. They are implemented like CFG_CONTEXT structure.
Here are methods used for creating and manipulating context:
#include <cfg+.h> |
CFG_CONTEXT cfg_get_context
(struct cfg_option *options);
CFG_CONTEXT cfg_get_cmdline_context
(int argc, char **argv, struct cfg_option options);
CFG_CONTEXT cfg_get_cfgfile_context
(long begin_pos, long end_pos, char *filename, struct cfg_option options);
void cfg_set_cmdline_context
(CFG_CONTEXT con, int argc, char **argv);
void cfg_set_cfgfile_context
(CFG_CONTEXT con, long begin_pos, long end_pos, char *filename);
First function cfg_get_context() initializes context. Although it is initialized, it also needs to be set to one from the command line or config file type. Parameter options defines options set we have mentioned in previous chapter.
For full context initialization there are functions cfg_get_cmdline_context() and cfg_get_cfgfile_context(). There are also functions cfg_set_cmdline_context() and cfg_set_cfgfile_context() for setting up already existing context. They have parameters with the same meanings as their equivalents with one difference thet they are modifying already existing context and nothing returning.
cfg_get_cmdline_function() function initializes context and sets it to command line. Parameter argc tells number of command line arguments. Next parameter argv contain array of strings with these arguments. They can be passed just as they are passed to the main() function.
cfg_get_cfgfile_function() function initializes context and sets it to configuration file. Parameters begin_pos and end_pos tells starting and ending possition in file for parsing. By default they are expected as byte possition, but they can be changes to line position by appropriate context flag. See (TODO) context flags section for more informations. Filename to parse is passed as the filename parameter.
Here are methods used for reseting and freeing context:
There are often situations, that reseting of context is neccessary. In command line context type current position is returned to the beginning of command line arguments. Similary in config file context type, position in file is set to begin_pos position. Context flags remains unchanged. For this purpose is cfg_reset_context() function provided.
After parsing of configuration options is complete, context should be freed. The last function cfg_free_context() frees passed CFG_CONTEXT structure. It will not frees allocated arguments or argument arrays set in any value member of your options set.