File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / example / testcfg.c
Revision 1.1.2.1: download - view: text, annotated - select for diffs - revision graph
Thu Nov 25 22:31:10 2021 UTC (2 years, 5 months ago) by misho
Branches: cfg8_2
adds new testcfg tool for check parser and configs

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


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

	if (argc < 2) {
		printf("Error:: missing config.\n\ntestcfg <config> [section/attribute]\n");
		return 1;
	} else
		str = argv[2];

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

	if (str) {
		attr = strchr(argv[2], '/');
		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_GET_STR(&av->cfg_attr), 
								AIT_GET_STR(&av->cfg_val));
				}
				array_Destroy(&arr);
			}
		}
	} else
		cfgWriteConfig(stdout, &cfg, 1);

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

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

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