2 * Insane tester of reading configuration files
4 * (c) 2006 Robert Spalek <robert@ucw.cz>
9 #include "lib/clists.h"
10 #include "lib/fastbuf.h"
28 init_sec_1(struct sub_sect_1 *s)
34 // leave s->list==NULL
39 commit_sec_1(struct sub_sect_1 *s)
41 if (s->confidence[0] < 0 || s->confidence[0] > 10)
42 return "Well, this can't be";
46 static struct cf_section cf_sec_1 = {
47 CF_TYPE(struct sub_sect_1),
49 CF_COMMIT(commit_sec_1),
50 #define F(x) PTR_TO(struct sub_sect_1, x)
52 CF_STRING("name", F(name)),
53 CF_STRING("level", F(level)),
54 CF_INT_ARY("confidence", F(confidence[0]), 2), // XXX: the [0] is needed for the sake of type checking
55 CF_DOUBLE_DYN("list", F(list), 100),
62 static int *nrs1 = DYN_ALLOC(int, 5, 5, 4, 3, 2, 1);
64 static byte *str1 = "no worries";
65 static byte **str2 = DYN_ALLOC(byte *, 2, "Alice", "Bob");
66 static u64 u1 = 0xCafeBeefDeadC00ll;
67 static double d1 = -1.1;
68 static struct sub_sect_1 sec1 = { {}, "Charlie", "WBAFC", { 0, -1} };
69 static struct clist secs;
74 init_top(void *ptr UNUSED)
76 for (uns i=0; i<5; i++)
78 struct sub_sect_1 *s = cf_malloc(sizeof(struct sub_sect_1));
79 cf_init_section("slaves", &cf_sec_1, s);
81 clist_add_tail(&secs, &s->n);
87 commit_top(void *ptr UNUSED)
90 return "Don't touch my variable!";
95 time_parser(uns number, byte **pars, time_t *ptr)
97 *ptr = number ? atoi(pars[0]) : time(NULL);
101 static struct cf_section cf_top = {
103 CF_COMMIT(commit_top),
106 CF_INT_DYN("nrs1", &nrs1, 1000),
107 CF_INT_ARY("nrs2", nrs2, 5),
108 CF_STRING("str1", &str1),
109 CF_STRING_DYN("str2", &str2, 20),
111 CF_DOUBLE("d1", &d1),
112 CF_PARSER("FirstTime", &t1, time_parser, -1),
113 CF_PARSER("SecondTime", &t2, time_parser, 1),
114 CF_SECTION("master", &sec1, &cf_sec_1),
115 CF_LIST("slaves", &secs, &cf_sec_1),
121 static byte short_opts[] = CF_SHORT_OPTS "v";
122 static struct option long_opts[] = {
124 {"verbose", 0, 0, 'v'},
128 static char *help = "\
129 Usage: conf2-test <options>\n\
144 main(int argc, char *argv[])
147 cf_declare_section("top", &cf_top, 0);
148 cf_def_file = "lib/conf2.t";
151 while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0)
153 case 'v': verbose++; break;
159 struct fastbuf *out = bfdopen(1, 1<<14);
160 cf_dump_sections(out);