]> mj.ucw.cz Git - libucw.git/blob - lib/conf-test.c
Added bit_array_assign(), replaced BIT_ARRAY_ALLOC by functions.
[libucw.git] / lib / conf-test.c
1 /* Test for configuration parser */
2
3 #include "lib/lib.h"
4 #include "lib/conf.h"
5
6 #include <stdio.h>
7 #include <string.h>
8
9 static u64 robert=14;
10 static int spalek=-3;
11 static char *heslo="prazdne";
12 static int nastaveni1=0,nastaveni2=1;
13 static double decker = 0;               /* The famous London buses :-) */
14
15 static byte *set_nastaveni(struct cfitem *item, byte *value)
16 {
17         int id;
18         if(!strcasecmp(value,"one"))
19                 id=1;
20         else if(!strcasecmp(value,"two"))
21                 id=2;
22         else if(!strcasecmp(value,"three"))
23                 id=3;
24         else if(!strcasecmp(value,"four"))
25                 id=4;
26         else
27                 return "Invalid value of nastaveni";
28         if(!strcasecmp(item->name,"nastaveni1"))
29                 nastaveni1=id;
30         else if(!strcasecmp(item->name,"nastaveni2"))
31                 nastaveni2=id;
32         else
33                 return "Internal error of nastaveni";
34         return NULL;
35 }
36
37 static struct cfitem jmeno[]={
38         {"jmeno",       CT_SECTION,     NULL},
39         {"robert",      CT_U64,         &robert},
40         {"spalek",      CT_INT,         &spalek},
41         {"heslo",       CT_STRING,      &heslo},
42         {"nastaveni1",  CT_FUNCTION,    &set_nastaveni},
43         {"nastaveni2",  CT_FUNCTION,    &set_nastaveni},
44         {"decker",      CT_DOUBLE,      &decker},
45         {NULL,          CT_STOP,        NULL}
46 };
47
48 static int vek=22;
49 static int vyska=178;
50 static int vaha=66;
51
52 static struct cfitem telo[]={
53         {"telo",        CT_SECTION,     NULL},
54         {"vek",         CT_INT,         &vek},
55         {"vyska",       CT_INT,         &vyska},
56         {"vaha",        CT_INT,         &vaha},
57         {NULL,          CT_STOP,        NULL}
58 };
59
60 static byte shortopts[] = CF_SHORT_OPTS "abcp:q:r::";
61 static struct option longopts[] =
62 {
63         CF_LONG_OPTS
64         {"ahoj",        0, 0, 'a'},
65         {"bida",        0, 0, 'b'},
66         {"citron",      0, 0, 'c'},
67         {"pivo",        1, 0, 'p'},
68         {"qwerty",      1, 0, 'q'},
69         {"rada",        2, 0, 'r'},
70         {NULL,          0, 0, 0}
71 };
72
73 int main(int argc, char *argv[])
74 {
75         int c;
76
77         log_init(argv[0]);
78
79         cf_register(jmeno);
80         cf_register(telo);
81
82         while(1){
83                 c=cf_getopt(argc,argv,shortopts,longopts,NULL);
84                 if(c==-1)
85                         break;
86                 else switch(c){
87                         case 'a':
88                         case 'b':
89                         case 'c':
90                                 printf("option %c\n",c);
91                                 break;
92
93                         case 'p':
94                         case 'q':
95                                 printf("option %c with parameter %s\n",c,optarg);
96                                 break;
97                         case 'r':
98                                 if(optarg)
99                                         printf("option r with optional parameter %s\n",optarg);
100                                 else
101                                         printf("option r without optional parameter\n");
102                                 break;
103                         case '?':
104                                 //printf("invalid parameter %d: %s\n",optind,argv[optind]);
105                                 break;
106                         case ':':
107                                 //printf("missing parameter for %d: %s\n",optind,argv[optind]);
108                                 break;
109                         default:
110                                 printf("getopt is confused, it returns %c\n",c);
111                                 break;
112                 }
113         }
114
115         if (optind < argc)
116         {
117                 printf ("non-option ARGV-elements: ");
118                 while (optind < argc)
119                         printf ("%s ", argv[optind++]);
120                 printf ("\n");
121         }
122
123         printf("robert=%Ld, spalek=%d, heslo=%s, nastaveni1/2=%d/%d decker=%f\n",
124                         (long long)robert,spalek,heslo,nastaveni1,nastaveni2,decker);
125         printf("vek=%d, vyska=%d, vaha=%d\n",
126                         vek,vyska,vaha);
127
128         return 0;
129 }