]> mj.ucw.cz Git - libucw.git/blob - lib/conf-test.c
added tester for conf.c, already tested and it works
[libucw.git] / lib / conf-test.c
1 /* Test for configuration parser */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <getopt.h>
6
7 #include "lib/lib.h"
8 #include "lib/conf.h"
9
10 static int robert=14;
11 static int spalek=-3;
12 static char *heslo="prazdne";
13 static int nastaveni1=0,nastaveni2=1;
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         {"robert",      ct_int, &robert},
39         {"spalek",      ct_int, &spalek},
40         {"heslo",       ct_string,      &heslo},
41         {"nastaveni1",  ct_function,    &set_nastaveni},
42         {"nastaveni2",  ct_function,    &set_nastaveni},
43         {NULL,          0,      NULL}
44 };
45
46 static int vek=22;
47 static int vyska=178;
48 static int vaha=66;
49
50 static struct cfitem telo[]={
51         {"vek",         ct_int, &vek},
52         {"vyska",       ct_int, &vyska},
53         {"vaha",        ct_int, &vaha},
54         {NULL,          0,      NULL}
55 };
56
57 static byte shortopts[] = "abcp:q:r::";
58 static struct option longopts[] =
59 {
60         {"ahoj",        0, 0, 'a'},
61         {"bida",        0, 0, 'b'},
62         {"citron",      0, 0, 'c'},
63         {"pivo",        1, 0, 'p'},
64         {"qwerty",      1, 0, 'q'},
65         {"rada",        2, 0, 'r'},
66         {NULL,          0, 0, 0}
67 };
68
69 int main(int argc, char *argv[])
70 {
71         int c;
72
73         cf_register("jmeno",jmeno);
74         cf_register("telo",telo);
75         cf_register_opts(shortopts,longopts);
76
77         while(1){
78                 c=cf_getopt(argc,argv,NULL);
79                 if(c==-1)
80                         break;
81                 else switch(c){
82                         case 'a':
83                         case 'b':
84                         case 'c':
85                                 printf("option %c\n",c);
86                                 break;
87
88                         case 'p':
89                         case 'q':
90                                 printf("option %c with parameter %s\n",c,optarg);
91                                 break;
92                         case 'r':
93                                 if(optarg)
94                                         printf("option r with optional parameter %s\n",optarg);
95                                 else
96                                         printf("option r without optional parameter\n");
97                                 break;
98                         case '?':
99                                 //printf("invalid parameter %d: %s\n",optind,argv[optind]);
100                                 break;
101                         case ':':
102                                 //printf("missing parameter for %d: %s\n",optind,argv[optind]);
103                                 break;
104                         default:
105                                 printf("getopt is confused, it returns %c\n",c);
106                                 break;
107                 }
108         }
109
110         if (optind < argc)
111         {
112                 printf ("non-option ARGV-elements: ");
113                 while (optind < argc)
114                         printf ("%s ", argv[optind++]);
115                 printf ("\n");
116         }
117
118         printf("robert=%d, spalek=%d, heslo=%s, nastaveni1/2=%d/%d\n",
119                         robert,spalek,heslo,nastaveni1,nastaveni2);
120         printf("vek=%d, vyska=%d, vaha=%d\n",
121                         vek,vyska,vaha);
122
123         return 0;
124 }