]> mj.ucw.cz Git - libucw.git/blob - lib/conf2-test.c
designed the interface of the new configuration reader. no code has been
[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(void *ptr, struct cf_section *sec UNUSED)
22 {
23   struct sub_sect_1 *s = ptr;
24   s->name = "unknown";
25   s->level = "default";
26   s->confidence = 5;
27   return NULL;
28 }
29
30 static byte *
31 commit_sec_1(void *ptr, struct cf_section *sec UNUSED)
32 {
33   struct sub_sect_1 *s = ptr;
34   if (s->confidence < 0 || s->confidence > 10)
35     return "Well, this can't be";
36   return NULL;
37 }
38
39 static struct cf_section cf_sec_1 = {
40   .size = sizeof(struct sub_sect_1),
41   .init = init_sec_1,
42   .commit = commit_sec_1,
43   .cfg = (struct cf_item[]) {
44 #define F(x)    CF_FIELD(struct sub_sect_1, x)
45     CF_STRING("name", F(name)),
46     CF_STRING("level", F(level)),
47     CF_INT("confidence", F(confidence)),
48     CF_END
49 #undef F
50   }
51 };
52
53 static int nr1 = 15;
54 static int *nrs1 = DEFAULT_ARRAY(int, 5, 5, 4, 3, 2, 1);
55 static int *nrs2;
56 static byte *str1 = "no worries";
57 static byte **str2 = DEFAULT_ARRAY(byte *, 2, "Alice", "Bob");
58 static u64 u1 = 0xCafeBeefDeadC00ll;
59 static double d1 = -1.1;
60 static struct sub_sect_1 sec_1 = { "Charlie", "WBAFC", 0 };
61 static struct cnode secs;
62 static time_t t1, t2;
63
64 static byte *
65 commit_top(void *ptr UNUSED, struct cf_section *sec UNUSED)
66 {
67   return NULL;
68 }
69
70 static byte *
71 time_parser(uns nr_pars, byte **pars, void *sec_ptr, struct cf_section *sec, uns index)
72 {
73   if (nr_pars != 0 && nr_pars != 1)
74     return "Either now or 1 parameter!";
75   ASSERT(!sec_ptr);
76   time_t t = nr_pars ? atoi(pars[0]) : time(NULL);
77   if (sec->cfg[index].name[0] == 'F')
78     t1 = t;
79   else
80     t2 = t;
81   return NULL;
82 }
83
84 static struct cf_section cf_top = {
85   .commit = commit_top,
86   .cfg = (struct cf_item []) {
87     CF_INT("nr1", &nr1),
88     CF_INT_AR("nrs1", &nrs1, 5),
89     CF_INT_AR("nrs2", &nrs2, -1000),
90     CF_STRING("str1", &str1),
91     CF_STRING_AR("str2", &str2, 2),
92     CF_U64("u1", &u1),
93     CF_DOUBLE("d1", &d1),
94     CF_FUNCTION("FirstTime", time_parser),
95     CF_FUNCTION("SecondTime", time_parser),
96     CF_SUB_SECTION("master", &sec_1, &cf_sec_1),
97     CF_LINK_LIST("slaves", &secs, &cf_sec_1),
98     CF_END
99   }
100 };
101
102 int
103 main(void)
104 {
105   return 0;
106 }