File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / nested.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 3 months ago) by misho
CVS tags: MAIN, HEAD
Initial revision

    1: /* Example by Thomas Adam */
    2: 
    3: #include <confuse.h>
    4: #include <stdio.h>
    5: #include <string.h>
    6: 
    7: #define CONF "nested.conf"
    8: 
    9: int main(void)
   10: {
   11: 	cfg_opt_t group_opts[] = {
   12: 		CFG_INT("number", 0, CFGF_NONE),
   13: 		CFG_INT("total", 0, CFGF_NONE),
   14: 		CFG_END()
   15: 	};
   16: 	cfg_opt_t groups_opts[] = {
   17: 		CFG_STR("name", "Esmé", CFGF_NONE),
   18: 		CFG_SEC("group", group_opts, CFGF_TITLE | CFGF_MULTI),
   19: 		CFG_END()
   20: 	};
   21: 	cfg_opt_t opts[] = {
   22: 		CFG_SEC("groups", groups_opts, CFGF_NONE),
   23: 		CFG_END()
   24: 	};
   25: 	cfg_t *cfg, *sec;
   26: 	size_t i, j;
   27: 
   28: 	cfg = cfg_init(opts, CFGF_NONE);
   29: 	if (!cfg || cfg_parse(cfg, CONF) == CFG_PARSE_ERROR) {
   30: 		perror("Failed parsing " CONF);
   31: 		return 1;
   32: 	}
   33: 
   34: 	/* Iterate over the sections and print fields from each section. */
   35: 	for (i = 0; i < cfg_size(cfg, "groups"); i++) {
   36: 		sec = cfg_getnsec(cfg, "groups", i);
   37: 
   38: 		for (j = 0; j < cfg_size(sec, "group"); j++) {
   39: 			cfg_t *opt = cfg_getnsec(sec, "group", j);
   40: 
   41: 			printf("group title:  '%s'\n", cfg_title(opt));
   42: 			printf("group number:  %ld\n", cfg_getint(opt, "number"));
   43: 			printf("group total:   %ld\n", cfg_getint(opt, "total"));
   44: 			printf("\n");
   45: 		}
   46: 	}
   47: 
   48: 	cfg_free(cfg);
   49: 
   50: 	return 0;
   51: }
   52: 
   53: /**
   54:  * Local Variables:
   55:  *  indent-tabs-mode: t
   56:  *  c-file-style: "linux"
   57:  * End:
   58:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>