File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / testcfg.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Fri Nov 26 01:15:03 2021 UTC (2 years, 4 months ago) by misho
Branches: MAIN
CVS tags: cfg9_5, cfg9_4, cfg9_3, cfg9_2, cfg9_1, HEAD, CFG9_4, CFG9_3, CFG9_2, CFG9_1, CFG9_0
version 9.0

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <elwix.h>
#include <aitcfg.h>


int
main(int argc, char **argv)
{
	cfg_root_t cfg;
	char ch, *str, *sec, *attr;
	array_t *arr;
	int i, mode = 0;
	struct tagCfg *av;

	while ((ch = getopt(argc, argv, "rd")) != -1)
		switch (ch) {
			case 'r':
				mode = 42;
				break;
			case 'd':
				mode = 21;
				break;
			default:
				printf("Error:: missing config.\n\ntestcfg [-r] <config> [section/attribute]\n");
				return 1;
		}
	argc -= optind;
	argv += optind;
	if (!argc) {
		printf("Error:: missing config.\n\ntestcfg [-r] <config> [section/attribute]\n");
		return 1;
	} else
		str = argv[1];

	if (cfgLoadConfig(argv[0], &cfg)) {
		ELIBERR(cfg);
		return 1;
	} else
		printf("Opened config\n");

	if (str) {
		attr = strchr(argv[1], '/');
		if (!attr) {
			attr = str;
			sec = NULL;
		} else {
			*attr++ = 0;
			sec = str;
		}
		if (sec && !*sec)
			sec = NULL;
		printf("Section=%s Attribute=%s\n", sec, attr);

		if (attr && *attr)
			printf("%s = %s\n", attr, cfg_getAttribute(&cfg, sec, attr));
		else {
			arr = cfg_getSection(&cfg, sec);
			if (arr) {
				printf("[%s]\n", sec);
				for (i = 0; i < array_Size(arr); i++) {
					av = array(arr, i, struct tagCfg*);
					if (av)
						printf("%s = %s\n", 
								AIT_ADDR(&av->cfg_attr), 
								AIT_GET_STR(&av->cfg_val));
				}
				array_Destroy(&arr);
			}
		}
	} else if (!mode)
		cfgWriteConfig(stdout, &cfg, 1);
	else if (mode == 42)
		cfgWriteConfigRaw(stdout, &cfg, 1);
	else
		cfg_dumpCfg(&cfg);

	cfgUnloadConfig(&cfg);
	printf("Closed config\n");

	printf("Done.\n");
	return 0;
}

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