Loading...
Searching...
No Matches
nested.c
/* Example by Thomas Adam */
#include <confuse.h>
#include <stdio.h>
#include <string.h>
#define CONF "nested.conf"
int main(void)
{
cfg_opt_t group_opts[] = {
CFG_INT("number", 0, CFGF_NONE),
CFG_INT("total", 0, CFGF_NONE),
CFG_END()
};
cfg_opt_t groups_opts[] = {
CFG_STR("name", "Esmé", CFGF_NONE),
CFG_SEC("group", group_opts, CFGF_TITLE | CFGF_MULTI),
CFG_END()
};
cfg_opt_t opts[] = {
CFG_SEC("groups", groups_opts, CFGF_NONE),
CFG_END()
};
cfg_t *cfg, *sec;
size_t i, j;
cfg = cfg_init(opts, CFGF_NONE);
if (!cfg || cfg_parse(cfg, CONF) == CFG_PARSE_ERROR) {
perror("Failed parsing " CONF);
return 1;
}
/* Iterate over the sections and print fields from each section. */
for (i = 0; i < cfg_size(cfg, "groups"); i++) {
sec = cfg_getnsec(cfg, "groups", i);
for (j = 0; j < cfg_size(sec, "group"); j++) {
cfg_t *opt = cfg_getnsec(sec, "group", j);
printf("group title: '%s'\n", cfg_title(opt));
printf("group number: %ld\n", cfg_getint(opt, "number"));
printf("group total: %ld\n", cfg_getint(opt, "total"));
printf("\n");
}
}
cfg_free(cfg);
return 0;
}
confuse.h
A configuration file parser library.
CFG_SEC
#define CFG_SEC(_name, _opts, _flags)
Initialize a section.
Definition confuse.h:790
CFGF_TITLE
#define CFGF_TITLE
option has a title (only applies to sections)
Definition confuse.h:86
CFGF_NONE
#define CFGF_NONE
Flags.
Definition confuse.h:82
CFG_END
#define CFG_END()
Terminate list of options.
Definition confuse.h:858
CFGF_MULTI
#define CFGF_MULTI
option may be specified multiple times (only applies to sections)
Definition confuse.h:83
CFG_STR
#define CFG_STR(name, def, flags)
Initialize a string option.
Definition confuse.h:367
CFG_PARSE_ERROR
#define CFG_PARSE_ERROR
Error parsing the configuration.
Definition confuse.h:106
CFG_INT
#define CFG_INT(name, def, flags)
Initialize an integer option.
Definition confuse.h:473
cfg_opt_t
Data structure holding information about an option.
Definition confuse.h:324
cfg_t
Data structure holding information about a "section".
Definition confuse.h:250