]> mj.ucw.cz Git - libucw.git/blob - lib/conf2-test.c
macros don't end by a comma, and CF_ITEMS/CF_END reintroduced
[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 <time.h>
13
14 struct sub_sect_1 {
15   byte *name;
16   byte *level;
17   int confidence[2];
18   double *list;
19 };
20
21 static byte *
22 init_sec_1(struct sub_sect_1 *s)
23 {
24   s->name = "unknown";
25   s->level = "default";
26   s->confidence[0] = 5;
27   s->confidence[1] = 6;
28   return NULL;
29 }
30
31 static byte *
32 commit_sec_1(struct sub_sect_1 *s)
33 {
34   if (s->confidence[0] < 0 || s->confidence[0] > 10)
35     return "Well, this can't be";
36   return NULL;
37 }
38
39 static struct cf_section cf_sec_1 = {
40   CF_TYPE(struct sub_sect_1),
41   CF_INIT(init_sec_1),
42   CF_COMMIT(commit_sec_1),
43 #define F(x)    PTR_TO(struct sub_sect_1, x)
44   CF_ITEMS {
45     CF_STRING("name", F(name)),
46     CF_STRING("level", F(level)),
47     CF_INT_ARY("confidence", F(confidence[0]), 2),              // XXX: the [0] is needed for the sake of type checking
48     CF_DOUBLE_DYN("list", F(list), 100),
49     CF_END
50   }
51 #undef F
52 };
53
54 static int nr1 = 15;
55 static int *nrs1 = DYN_ALLOC(int, 5, 5, 4, 3, 2, 1);
56 static int nrs2[5];
57 static byte *str1 = "no worries";
58 static byte **str2 = DYN_ALLOC(byte *, 2, "Alice", "Bob");
59 static u64 u1 = 0xCafeBeefDeadC00ll;
60 static double d1 = -1.1;
61 static struct sub_sect_1 sec1 = { "Charlie", "WBAFC", { 0, -1} };
62 static struct clist secs;
63 static time_t t1, t2;
64
65 static byte *
66 commit_top(void *ptr UNUSED)
67 {
68   if (nr1 != 15)
69     return "Don't touch my variable!";
70   return NULL;
71 }
72
73 static byte *
74 time_parser(uns number, byte **pars, time_t *ptr)
75 {
76   *ptr = number ? atoi(pars[0]) : time(NULL);
77   return NULL;
78 }
79
80 static struct cf_section cf_top UNUSED = {
81   CF_COMMIT(commit_top),
82   CF_ITEMS {
83     CF_INT("nr1", &nr1),
84     CF_INT_DYN("nrs1", &nrs1, 1000),
85     CF_INT_ARY("nrs2", nrs2, 5),
86     CF_STRING("str1", &str1),
87     CF_STRING_DYN("str2", &str2, 2),
88     CF_U64("u1", &u1),
89     CF_DOUBLE("d1", &d1),
90     CF_PARSER("FirstTime", &t1, time_parser, -1),
91     CF_PARSER("SecondTime", &t2, time_parser, 1),
92     CF_SECTION("master", &sec1, &cf_sec_1),
93     CF_LIST("slaves", &secs, &cf_sec_1),
94     CF_END
95   }
96 };
97
98 int
99 main(void)
100 {
101   return 0;
102 }