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

    1: #ifdef HAVE_CONFIG_H
    2: #include <config.h>
    3: #endif
    4: #include <errno.h>
    5: #include <string.h>
    6: #include <locale.h>
    7: #ifdef HAVE_UNISTD_H
    8: #include <unistd.h>
    9: #endif
   10: #ifdef HAVE_STDLIB_H
   11: #include <stdlib.h>
   12: #endif
   13: #include "confuse.h"
   14: 
   15: cfg_t *cfg = NULL;
   16: const char *config_filename = "./reread.conf";
   17: 
   18: void read_config(void)
   19: {
   20: 	static cfg_opt_t arg_opts[] = {
   21: 		CFG_STR("value", "default", CFGF_NONE),
   22: 		CFG_END()
   23: 	};
   24: 	cfg_opt_t opts[] = {
   25: 		CFG_INT("delay", 3, CFGF_NONE),
   26: 		CFG_STR("message", "This is a message", CFGF_NONE),
   27: 		CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE),
   28: 		CFG_END()
   29: 	};
   30: 
   31: 	cfg = cfg_init(opts, CFGF_NONE);
   32: 	if (cfg_parse(cfg, config_filename) != CFG_SUCCESS) {
   33: 		fprintf(stderr, "Failed parsing configuration: %s\n", strerror(errno));
   34: 		exit(1);
   35: 	}
   36: }
   37: 
   38: void print_message()
   39: {
   40: 	size_t i;
   41: 
   42: 	printf("Message: %s", cfg_getstr(cfg, "message"));
   43: 	for (i = 0; i < cfg_size(cfg, "argument"); i++) {
   44: 		cfg_t *arg;
   45: 
   46: 		arg = cfg_getnsec(cfg, "argument", i);
   47: 		if (arg)
   48: 			printf(", %s", cfg_getstr(arg, "value"));
   49: 	}
   50: 	printf("\n");
   51: }
   52: 
   53: int main(void)
   54: {
   55: 	cfg_t* sec;
   56: 
   57: 	/* Localize messages & types according to environment, since v2.9 */
   58: #ifdef LC_MESSAGES
   59: 	setlocale(LC_MESSAGES, "");
   60: 	setlocale(LC_CTYPE, "");
   61: #endif
   62: 
   63: 	read_config();
   64: 	print_message();
   65: 
   66: 	/* Add a new section */
   67: 	sec = cfg_addtsec(cfg, "argument", "two");
   68: 	cfg_setstr(sec, "value", "foo");
   69: 	print_message();
   70: 
   71: 	cfg_free(cfg);
   72: 
   73: 	return 0;
   74: 
   75: }

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