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