2 * Insane tester of reading configuration files
4 * (c) 2006 Robert Spalek <robert@ucw.cz>
9 #include "lib/clists.h"
27 init_sec_1(struct sub_sect_1 *s)
33 // leave s->list==NULL
38 commit_sec_1(struct sub_sect_1 *s)
40 if (s->confidence[0] < 0 || s->confidence[0] > 10)
41 return "Well, this can't be";
45 static struct cf_section cf_sec_1 = {
46 CF_TYPE(struct sub_sect_1),
48 CF_COMMIT(commit_sec_1),
49 #define F(x) PTR_TO(struct sub_sect_1, x)
51 CF_STRING("name", F(name)),
52 CF_STRING("level", F(level)),
53 CF_INT_ARY("confidence", F(confidence[0]), 2), // XXX: the [0] is needed for the sake of type checking
54 CF_DOUBLE_DYN("list", F(list), 100),
61 static int *nrs1 = DYN_ALLOC(int, 5, 5, 4, 3, 2, 1);
63 static byte *str1 = "no worries";
64 static byte **str2 = DYN_ALLOC(byte *, 2, "Alice", "Bob");
65 static u64 u1 = 0xCafeBeefDeadC00ll;
66 static double d1 = -1.1;
67 static struct sub_sect_1 sec1 = { {}, "Charlie", "WBAFC", { 0, -1} };
68 static struct clist secs;
72 init_top(void *ptr UNUSED)
74 for (uns i=0; i<5; i++)
76 struct sub_sect_1 *s = cf_malloc(sizeof(struct sub_sect_1));
77 cf_init_section("slaves", &cf_sec_1, s);
79 clist_add_tail(&secs, &s->n);
85 commit_top(void *ptr UNUSED)
88 return "Don't touch my variable!";
93 time_parser(uns number, byte **pars, time_t *ptr)
95 *ptr = number ? atoi(pars[0]) : time(NULL);
99 static struct cf_section cf_top = {
101 CF_COMMIT(commit_top),
104 CF_INT_DYN("nrs1", &nrs1, 1000),
105 CF_INT_ARY("nrs2", nrs2, 5),
106 CF_STRING("str1", &str1),
107 CF_STRING_DYN("str2", &str2, 20),
109 CF_DOUBLE("d1", &d1),
110 CF_PARSER("FirstTime", &t1, time_parser, -1),
111 CF_PARSER("SecondTime", &t2, time_parser, 1),
112 CF_SECTION("master", &sec1, &cf_sec_1),
113 CF_LIST("slaves", &secs, &cf_sec_1),
118 static byte short_opts[] = CF_SHORT_OPTS "v";
119 static struct option long_opts[] = {
121 {"verbose", 0, 0, 'v'},
125 static char *help = "\
126 Usage: conf2-test <options>\n\
141 main(int argc, char *argv[])
144 cf_declare_section("top", &cf_top, 0);
145 cf_def_file = "lib/conf2.t";
148 while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0)
150 case 'v': verbose++; break;