File: print_filter.c

package info (click to toggle)
libconfuse 3.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,192 kB
  • sloc: ansic: 5,532; lex: 451; xml: 439; makefile: 213; sh: 39
file content (70 lines) | stat: -rw-r--r-- 1,618 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "check_confuse.h"
#include <stdio.h>
#include <string.h>

static int no_foo(cfg_t *cfg, cfg_opt_t *opt)
{
	return !strncmp(cfg_opt_name(opt), "foo-", 4);
}

static int no_bar(cfg_t *cfg, cfg_opt_t *opt)
{
	return !strncmp(cfg_opt_name(opt), "bar-", 4);
}

int main(void)
{
	cfg_opt_t sub[] = {
		CFG_INT_LIST("bar-int", "{0,1,2}", CFGF_NONE),
		CFG_INT_LIST("foo-int", NULL, CFGF_NONE),
		CFG_BOOL("bar-bool", cfg_false, CFGF_NONE),
		CFG_FLOAT("foo-float", 2.718, CFGF_NONE),
		CFG_STR("gazonk-string", NULL, CFGF_NONE),
		CFG_END()
	};
	cfg_opt_t opts[] = {
		CFG_INT_LIST("foo-int", "{0,1,2}", CFGF_NONE),
		CFG_INT_LIST("bar-int", NULL, CFGF_NONE),
		CFG_BOOL("foo-bool", cfg_true, CFGF_NONE),
		CFG_FLOAT("bar-float", 3.14, CFGF_NONE),
		CFG_STR("gazonk-string", "foobar", CFGF_NONE),
		CFG_SEC("sub", sub, CFGF_NONE),
		CFG_END()
	};
	char buf[200]; /* should be enough */
	FILE *f;

	cfg_t *cfg = cfg_init(opts, 0);

	cfg_set_print_filter_func(cfg, no_foo);
	f = fmemopen(buf, sizeof(buf), "w+");
	fail_unless(f != NULL);
	cfg_print(cfg, f);
	fclose(f);

	fprintf(stderr, "no_foo filter:\n%s", buf);
	fail_unless(strstr(buf, "foo-") == NULL);
	fail_unless(strstr(buf, "bar-") != NULL);

	cfg_set_print_filter_func(cfg, no_bar);
	f = fmemopen(buf, sizeof(buf), "w+");
	fail_unless(f != NULL);
	cfg_print(cfg, f);
	fclose(f);

	fprintf(stderr, "----\n");
	fprintf(stderr, "no_bar filter:\n%s", buf);
	fail_unless(strstr(buf, "foo-") != NULL);
	fail_unless(strstr(buf, "bar-") == NULL);

	cfg_free(cfg);

	return 0;
}

/**
 * Local Variables:
 *  indent-tabs-mode: t
 *  c-file-style: "linux"
 * End:
 */