new function is_in_range.
[swftools.git] / lib / args.h
1 /* args.h
2    Routines to simplify argument handling
3
4    Part of the swftools package.
5
6    Copyright (c) 2001 Matthias Kramm <kramm@quiss.org> 
7
8    This file is distributed under the GPL, see file COPYING for details */
9
10 #ifndef __args_h__
11 #define __args_h__
12
13 extern int args_callback_option(char*,char*);
14 extern int args_callback_longoption(char*,char*);
15 extern int args_callback_command(char*,char*);
16 extern void args_callback_usage(char*name);
17
18 //int argn;
19 //char**argv;
20
21 void processargs(int argn2,char**argv2)
22 {
23     int t;
24     if(argn2==1)
25     {
26         args_callback_usage(argv2[0]);
27         exit(1);
28     }
29     for(t=1;t<argn2;t++)
30     {
31         char*next;
32         if(t<argn2-1) next=argv2[t+1];
33         else        next=0;
34
35         if(argv2[t][0]=='-')
36         {
37             if(argv2[t][1]=='-')
38             {
39                 if(!strcmp(&argv2[t][2],"help")) 
40                 {
41                     args_callback_usage(argv2[0]);
42                     exit(1);
43                 }
44                 t+=args_callback_longoption(&argv2[t][2],next);
45             }
46             else
47             {
48                 if(strchr("?h",argv2[t][1]))
49                 {
50                     args_callback_usage(argv2[0]);
51                     exit(1);
52                 }
53                 if(argv2[t][1]) // this might be something like e.g. -xvf
54                 {
55                     char buf[2];
56                     int s=1;
57                     int ret;
58                     buf[1]=0;
59                     do{
60                         if(argv2[t][s+1]) {
61                           buf[0] = argv2[t][s];
62                           ret = args_callback_option(buf,&argv2[t][s+1]);
63                         }
64                         else {
65                           t+= args_callback_option(&argv2[t][s], next);
66                           break;
67                         }
68                         s++;
69                     } while(!ret);
70                 }
71                 else // - usually means "read stdout"
72                 {
73                     t+=args_callback_option(&argv2[t][1],next);
74                 }
75             }
76         }
77         else
78         {
79             t+=args_callback_command(argv2[t],next);
80         }
81     }
82 }
83
84 struct options_t
85 {
86     char*shortoption;
87     char*longoption;
88 };
89
90 int args_long2shortoption(struct options_t*options, char*name, char*val)
91 {
92     char*equal = strchr(name,'=');
93     if (equal) {
94         *equal = 0;
95         equal++;
96     }
97     while(options->shortoption) {
98         if(!strcmp(options->longoption, name)) {
99                 char*tmp = (char*)malloc(strlen(options->shortoption)
100                         +(equal?strlen(equal)+2:2));
101                 strcpy(tmp, options->shortoption);
102                 if(equal) {
103                     //strcpy(&tmp[strlen(tmp)], equal);
104                     int ret = args_callback_option(tmp, equal);
105                     if(!ret) {
106                         fprintf(stderr, "Warning: Option --%s takes no parameter.\n", name);
107                     }
108                     return 0;
109                 }
110                 return args_callback_option(tmp,val);
111         }
112         options++;
113     }
114     fprintf(stderr, "Unknown option: --%s\n", name);
115     exit(1);
116 }
117
118 /* check whether the value t is in a given range.
119   examples: 3 is in range 1-10: true
120             7 is in range 2-4,6,8-10: false
121             9 is in range 1,2,3-12: true
122 */
123 char is_in_range(int t, char*irange)
124 {
125     char*pos = irange;
126     char*digits;
127     int num;
128     char range = 0;
129     int last=0;
130     char tmp;
131
132     if(!irange)  // no range resembles (-OO,OO)
133         return 1;
134
135     while(*pos)
136     {
137         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
138             pos++;
139
140         digits = pos;
141         while(*digits>='0' && *digits<='9')
142             digits++;
143         if(digits == pos) {
144             fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange);
145             exit(1);
146         }
147         
148         tmp=*digits;*digits=0;
149         num = atoi(pos);
150         *digits=tmp;
151         pos = digits;
152
153         while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t')
154             pos++;
155
156         if(range && last<=t && num>=t)
157             return 1;
158         if(range) {
159             range = 0;
160             if(*pos)
161              pos ++;
162             continue;
163         }
164
165         if(*pos=='-')
166         {
167             if(range) {
168                 fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange);
169                 exit(1);
170             }
171             last = num;
172             range = 1;
173             if(*pos)
174              pos ++;
175             continue;
176         } 
177         else 
178         {
179             /* if it isn't a '-', we assume it is a seperator like
180                ',', ';', ':', whatever. */
181             if(t == num)
182                 return 1;
183             if(*pos)
184              pos ++;
185             continue;
186         }
187     }
188     if(range && last<=t)
189         return 1;
190     return 0;
191 }
192
193 #endif //__args_h__