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