File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / testcfg.c
Revision 1.1.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Nov 25 23:44:52 2021 UTC (2 years, 6 months ago) by misho
Branches: cfg8_2
adds cfgWriteConfigRaw function

    1: #include <stdio.h>
    2: #include <unistd.h>
    3: #include <string.h>
    4: #include <elwix.h>
    5: #include <aitcfg.h>
    6: 
    7: 
    8: int
    9: main(int argc, char **argv)
   10: {
   11: 	cfg_root_t cfg;
   12: 	char ch, *str, *sec, *attr;
   13: 	array_t *arr;
   14: 	int i, mode = 0;
   15: 	struct tagCfg *av;
   16: 
   17: 	while ((ch = getopt(argc, argv, "r")) != -1)
   18: 		switch (ch) {
   19: 			case 'r':
   20: 				mode = 42;
   21: 				break;
   22: 			default:
   23: 				printf("Error:: missing config.\n\ntestcfg [-r] <config> [section/attribute]\n");
   24: 				return 1;
   25: 		}
   26: 	argc -= optind;
   27: 	argv += optind;
   28: 	if (!argc) {
   29: 		printf("Error:: missing config.\n\ntestcfg [-r] <config> [section/attribute]\n");
   30: 		return 1;
   31: 	} else
   32: 		str = argv[1];
   33: 
   34: 	if (cfgLoadConfig(argv[0], &cfg)) {
   35: 		ELIBERR(cfg);
   36: 		return 1;
   37: 	} else
   38: 		printf("Opened config\n");
   39: 
   40: 	if (str) {
   41: 		attr = strchr(argv[1], '/');
   42: 		if (!attr) {
   43: 			attr = str;
   44: 			sec = NULL;
   45: 		} else {
   46: 			*attr++ = 0;
   47: 			sec = str;
   48: 		}
   49: 		if (sec && !*sec)
   50: 			sec = NULL;
   51: 		printf("Section=%s Attribute=%s\n", sec, attr);
   52: 
   53: 		if (attr && *attr)
   54: 			printf("%s = %s\n", attr, cfg_getAttribute(&cfg, sec, attr));
   55: 		else {
   56: 			arr = cfg_getSection(&cfg, sec);
   57: 			if (arr) {
   58: 				printf("[%s]\n", sec);
   59: 				for (i = 0; i < array_Size(arr); i++) {
   60: 					av = array(arr, i, struct tagCfg*);
   61: 					if (av)
   62: 						printf("%s = %s\n", 
   63: 								AIT_ADDR(&av->cfg_attr), 
   64: 								AIT_GET_STR(&av->cfg_val));
   65: 				}
   66: 				array_Destroy(&arr);
   67: 			}
   68: 		}
   69: 	} else if (!mode)
   70: 		cfgWriteConfig(stdout, &cfg, 1);
   71: 	else
   72: 		cfgWriteConfigRaw(stdout, &cfg, 1);
   73: 
   74: 	cfgUnloadConfig(&cfg);
   75: 	printf("Closed config\n");
   76: 
   77: 	printf("Done.\n");
   78: 	return 0;
   79: }

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