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