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"
27 static struct sub_sect_1 sec1 = { {}, "Charlie", "WBAFC", { 0, -1}, DARY_ALLOC(double, 3, 1e4, -1e-4, 8) };
30 init_sec_1(struct sub_sect_1 *s)
32 if (s == &sec1) { // this is a static variable; skip clearing
33 DARY_LEN(sec1.list) = 3; // XXX: fix for the bug in DARY_ALLOC()
40 // leave s->list==NULL
45 commit_sec_1(struct sub_sect_1 *s)
47 if (s->confidence[0] < 0 || s->confidence[0] > 10)
48 return "Well, this can't be";
52 static struct cf_section cf_sec_1 = {
53 CF_TYPE(struct sub_sect_1),
55 CF_COMMIT(commit_sec_1),
56 #define F(x) PTR_TO(struct sub_sect_1, x)
58 CF_STRING("name", F(name)),
59 CF_STRING("level", F(level)),
60 CF_INT_ARY("confidence", F(confidence[0]), 2), // XXX: the [0] is needed for the sake of type checking
61 CF_DOUBLE_DYN("list", F(list), 100),
68 static int *nrs1 = DARY_ALLOC(int, 5, 5, 4, 3, 2, 1);
70 static byte *str1 = "no worries";
71 static byte **str2 = DARY_ALLOC(byte *, 2, "Alice", "Bob");
72 static u64 u1 = 0xCafeBeefDeadC00ll;
73 static double d1 = -1.1;
74 static struct clist secs;
79 init_top(void *ptr UNUSED)
81 for (uns i=0; i<5; i++)
83 struct sub_sect_1 *s = xmalloc(sizeof(struct sub_sect_1)); // XXX: cannot by cf_malloc(), because it's deleted when cf_reload()'ed
84 cf_init_section("slaves", &cf_sec_1, s, 1);
86 clist_add_tail(&secs, &s->n);
92 commit_top(void *ptr UNUSED)
95 return "Don't touch my variable!";
100 time_parser(uns number, byte **pars, time_t *ptr)
102 *ptr = number ? atoi(pars[0]) : time(NULL);
106 static struct cf_section cf_top = {
108 CF_COMMIT(commit_top),
111 CF_INT_DYN("nrs1", &nrs1, 1000),
112 CF_INT_ARY("nrs2", nrs2, 5),
113 CF_STRING("str1", &str1),
114 CF_STRING_DYN("str2", &str2, 20),
116 CF_DOUBLE("d1", &d1),
117 CF_PARSER("FirstTime", &t1, time_parser, -1),
118 CF_PARSER("SecondTime", &t2, time_parser, 1),
119 CF_SECTION("master", &sec1, &cf_sec_1),
120 CF_LIST("slaves", &secs, &cf_sec_1),
126 static byte short_opts[] = CF_SHORT_OPTS "v";
127 static struct option long_opts[] = {
129 {"verbose", 0, 0, 'v'},
133 static char *help = "\
134 Usage: conf2-test <options>\n\
138 "-v\t\t\tBe verbose\n\
142 usage(byte *msg, ...)
147 vfprintf(stderr, msg, va);
153 main(int argc, char *argv[])
156 cf_declare_section("top", &cf_top, 0);
157 cf_def_file = "lib/conf2.t";
160 while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0)
162 case 'v': verbose++; break;
163 default: usage("unknown option %c\n", opt);
166 usage("too many parameters (%d more)\n", argc-optind);
168 //cf_reload("non-existent file");
170 struct fastbuf *out = bfdopen(1, 1<<14);
171 cf_dump_sections(out);