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