shithub: libini

ref: 6c63e71880f8bcdf597548e8bbaba165c4fe7d38
dir: /test.c/

View raw version
#include <u.h>
#include <libc.h>
#include "ini.h"

int verbose = 0;

void
parseline(char *section, char *key, char *value, int)
{
	if (verbose)
		print("section: '%s', key: '%s', value: '%s'\n", section, key, value);
}

void
main(int, char **argv)
{
	char *file = argv[1];
	char *status = nil;
	char *err = "error";
	
	if (!parseini(file, parseline, 0, 0)) {
		fprint(2, "error: %r\n");
		status = err;
	}
	
	if (!parseini(file, parseline, 1, 0)) {
		fprint(2, "error: %r\n");
		status = err;
	}
	
	int conf = iniconfig(file, 0);
	if (conf < 0)
		sysfatal("error: %r");
	
	char* v = getinivalue(conf, "Category A", "keyB");
	if (strcmp(v, "valueB") != 0)
		fprint(2, "fail: expected 'valueB', got '%s'\n", v);
	
	int num;
	char** values = getiniarray(conf, "Category C", "keyF", &num);
	if (num != 2)
		fprint(2, "fail: expected 2 entries, got %d\n", num);
	if (values) {
		for (int i = 0; i < num; i++) {
			if (i == 0 && strcmp(values[i], "abc") != 0)
				fprint(2, "fail: expected 'abc', got '%s'\n", values[i]);
			if (i == 1 && strcmp(values[i], "def") != 0)
				fprint(2, "fail: expected 'def', got '%s'\n", values[i]);
		}
	}
	
	v = getinivalue(conf, nil, "nocat");
	if (strcmp(v, "novalue") != 0)
		fprint(2, "fail: expected 'novalue', got '%s'\n", v);
	
	values = getiniarray(conf, nil, "nocatarr", &num);
	if (num != 2)
		fprint(2, "fail: expected 2 entries, got %d\n", num);
	if (values) {
		for (int i = 0; i < num; i++) {
			if (i == 0 && strcmp(values[i], "valA") != 0)
				fprint(2, "fail: expected 'valA', got '%s'\n", values[i]);
			if (i == 1 && strcmp(values[i], "valB") != 0)
				fprint(2, "fail: expected 'valB', got '%s'\n", values[i]);
		}
	}
	
	int r = freeini(conf);
	if (!r)
		fprint(2, "error freeing: %r\n");
	
	sleep(1000);
	
	exits(status);
}