]> mj.ucw.cz Git - libucw.git/blob - lib/conf2-test.c
conf2: fill in the skeleton of the tester
[libucw.git] / lib / conf2-test.c
1 /*
2  *      Insane tester of reading configuration files
3  *
4  *      (c) 2006 Robert Spalek <robert@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/conf2.h"
9 #include "lib/clists.h"
10
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <getopt.h>
15
16 static int verbose;
17
18 struct sub_sect_1 {
19   struct cnode n;
20   byte *name;
21   byte *level;
22   int confidence[2];
23   double *list;
24 };
25
26 static byte *
27 init_sec_1(struct sub_sect_1 *s)
28 {
29   s->name = "unknown";
30   s->level = "default";
31   s->confidence[0] = 5;
32   s->confidence[1] = 6;
33   // leave s->list==NULL
34   return NULL;
35 }
36
37 static byte *
38 commit_sec_1(struct sub_sect_1 *s)
39 {
40   if (s->confidence[0] < 0 || s->confidence[0] > 10)
41     return "Well, this can't be";
42   return NULL;
43 }
44
45 static struct cf_section cf_sec_1 = {
46   CF_TYPE(struct sub_sect_1),
47   CF_INIT(init_sec_1),
48   CF_COMMIT(commit_sec_1),
49 #define F(x)    PTR_TO(struct sub_sect_1, x)
50   CF_ITEMS {
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),
55     CF_END
56   }
57 #undef F
58 };
59
60 static uns nr1 = 15;
61 static int *nrs1 = DYN_ALLOC(int, 5, 5, 4, 3, 2, 1);
62 static int nrs2[5];
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;
69 static time_t t1, t2;
70
71 static byte *
72 init_top(void *ptr UNUSED)
73 {
74   for (uns i=0; i<5; i++)
75   {
76     struct sub_sect_1 *s = cf_malloc(sizeof(struct sub_sect_1));
77     cf_init_section("slaves", &cf_sec_1, s);
78     s->confidence[1] = i;
79     clist_add_tail(&secs, &s->n);
80   }
81   return NULL;
82 }
83
84 static byte *
85 commit_top(void *ptr UNUSED)
86 {
87   if (nr1 != 15)
88     return "Don't touch my variable!";
89   return NULL;
90 }
91
92 static byte *
93 time_parser(uns number, byte **pars, time_t *ptr)
94 {
95   *ptr = number ? atoi(pars[0]) : time(NULL);
96   return NULL;
97 }
98
99 static struct cf_section cf_top = {
100   CF_COMMIT(init_top),
101   CF_COMMIT(commit_top),
102   CF_ITEMS {
103     CF_UNS("nr1", &nr1),
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, 2),
108     CF_U64("u1", &u1),
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),
114     CF_END
115   }
116 };
117
118 static byte short_opts[] = CF_SHORT_OPTS "v";
119 static struct option long_opts[] = {
120         CF_LONG_OPTS
121         {"verbose",     0, 0, 'v'},
122         {NULL,          0, 0, 0}
123 };
124
125 static char *help = "\
126 Usage: conf2-test <options>\n\
127 \n\
128 Options:\n"
129 CF_USAGE
130 "-v\t\tBe verbose\n\
131 ";
132
133 static void NONRET
134 usage(void)
135 {
136         fputs(help, stderr);
137         exit(1);
138 }
139
140 int
141 main(int argc, char *argv[])
142 {
143   log_init(argv[0]);
144   cf_declare_section("top", &cf_top, 0);
145
146   int opt;
147   while ((opt = cf_get_opt(argc, argv, short_opts, long_opts, NULL)) >= 0)
148     switch (opt) {
149       case 'v': verbose++; break;
150       default: usage();
151     }
152   if (optind < argc)
153     usage();
154
155   return 0;
156 }