2.4. Parsing

After libcfg+ context is created and context flags are set, we can start with configuration parsing. This chapter contains information about particular parsing methods.

#include <cfg+.h>

int cfg_parse(const CFG_CONTEXT con);

int cfg_get_next_opt(const CFG_CONTEXT con);

These functions get created context as its single argument. Depending on context type they parse next configuration file line or command line argument. If found option is in specified options set (see Options set chapter) and if value field in cfg_option of appropriate option is not NULL, that field is filled with parsed argument or arguments.

The main and only one difference between these two parsing functions is, that after option is found on command line or in config file and val structure field is not 0, cfg_get_next_opt() returns that value. Otherwise function continues on next config file line or on next command line argument. This feature is especially usuable for applications with need of more sophisticated command line or config file parsing. Function cfg_parse() allways ignores val value.

These functions also returns error code if error has occured. Note, that all error codes are lower than 0. That is the reason why it is good to keep value of val structure member equal or greater than 0. More information with error codes description are in Error handling chapter.

At the end of parsing 0 is returned.

Following functions provide such additional information when returned options are handled.

#include <cfg+.h>

char *cfg_get_cur_opt(const CFG_CONTEXT con);

char *cfg_get_cur_arg(const CFG_CONTEXT con);

int cfg_get_cur_idx(const CFG_CONTEXT con);

Functions have following behaviour: cfg_get_cur_opt() returns currently processed option name, cfg_get_cur_arg() returns currently processed option argument. Return value of cfg_get_cur_idx() function depends on current type of context. It returns argv index of processed option in command line context and file byte or line position of processed option in config file context.