Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / routines / check_option.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger Last modified: Aug 19, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "tisean_cec.h"
26
27 extern void check_alloc(void*);
28 /* possible types are
29    'd'  (long) integer
30    'u'  unsigned (long)
31    '1'  one or two unsigned (long) numbers, separated by comma, if two
32    '2'  two unsigned (long) numbers separated by a comma
33    '3' three unsigned (long) numbers separated by commas
34    'f'  float
35    's'  string
36    'o'  optional string (must only begin with a minus if there is no space)
37    'n'  no parameter
38    */
39
40 void check_unsigned(char *tocheck,int which)
41 {
42   int i,n;
43   char ok=1;
44
45   n=strlen(tocheck);
46   for (i=0;i<n;i++)
47     if (!isdigit((unsigned int)tocheck[i]))
48       ok=0;
49
50   if (!ok) {
51     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be an "
52             "unsigned integer\n",which);
53     exit(CHECK_OPTION_NOT_UNSIGNED);
54   }
55 }
56
57 void check_integer(char *tocheck,int which)
58 {
59   int i,n;
60   char ok=1;
61
62   n=strlen(tocheck);
63   ok=(tocheck[0] == '-') || isdigit((unsigned int)tocheck[0]);
64   if (ok)
65     for (i=1;i<n;i++)
66       if (!isdigit((unsigned int)tocheck[i]))
67         ok=0;
68   
69   if (!ok) {
70     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be an "
71             "integer\n",which);
72     exit(CHECK_OPTION_NOT_INTEGER);
73   }
74 }
75
76 void check_float(char *tocheck,int which)
77 {
78   double dummy;
79   int found;
80   char *rest;
81   
82   check_alloc(rest=(char*)calloc(strlen(tocheck)+1,(size_t)1));
83   found=sscanf(tocheck,"%lf%s",&dummy,rest);
84   if (found != 1) {
85     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be a "
86             "float\n",which);
87     exit(CHECK_OPTION_NOT_FLOAT);
88   }
89   free(rest);
90 }
91
92 void check_two(char *tocheck,int which)
93 {
94   int i,j;
95   unsigned int len;
96
97   len=(unsigned int)strlen(tocheck);
98   for (i=0;i<len;i++)
99     if (tocheck[i] == ',')
100       break;
101   if (i >= (len-1)) {
102     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
103             " unsigned,unsigned\n",which);
104     exit(CHECK_OPTION_NOT_TWO);
105   }
106   for (j=0;j<i;j++)
107     if (!isdigit((unsigned int)tocheck[j])) {
108       fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
109               " unsigned,unsigned\n",which);
110       exit(CHECK_OPTION_NOT_TWO);
111     }
112   for (j=i+1;j<len;j++)
113     if (!isdigit((unsigned int)tocheck[j])) {
114       fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
115               " unsigned,unsigned\n",which);
116       exit(CHECK_OPTION_NOT_TWO);
117     }
118 }
119
120 void check_three(char *tocheck,int which)
121 {
122   int i,j,k;
123   unsigned int len;
124
125   len=(unsigned int)strlen(tocheck);
126   for (i=0;i<len;i++)
127     if (tocheck[i] == ',')
128       break;
129
130   if (i >= (len-1)) {
131     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
132             " unsigned,unsigned,unsigned\n",which);
133     exit(CHECK_OPTION_NOT_THREE);
134   }
135
136   for (j=i+1;j<len;j++)
137     if (tocheck[j] == ',')
138       break;
139
140   if (j >= (len-1)) {
141     fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
142             " unsigned,unsigned,unsigned\n",which);
143     exit(CHECK_OPTION_NOT_THREE);
144   }
145
146   for (k=0;k<i;k++)
147     if (!isdigit((unsigned int)tocheck[k])) {
148       fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
149               " unsigned,unsigned,unsigned\n",which);
150       exit(CHECK_OPTION_NOT_THREE);
151     }
152   for (k=i+1;k<j;k++)
153     if (!isdigit((unsigned int)tocheck[k])) {
154       fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
155               " unsigned,unsigned,unsigned\n",which);
156       exit(CHECK_OPTION_NOT_THREE);
157     }
158   for (k=j+1;k<len;k++)
159     if (!isdigit((unsigned int)tocheck[k])) {
160       fprintf(stderr,"Wrong type of parameter for flag -%c. Has to be"
161               " unsigned,unsigned,unsigned\n",which);
162       exit(CHECK_OPTION_NOT_THREE);
163     }
164 }
165
166 char check_optional(char *tocheck,int which)
167 {
168   if (tocheck[0] == '-') {
169     fprintf(stderr,"If you want to give the -%c flag a parameter starting"
170             " with a - don't put a space. Ignoring it.\n",which);
171     return 0;
172   }
173   return 1;
174 }
175
176 char* check_option(char **in,int n,int which,int type)
177 {
178   char test,*ret=NULL,wasfound=0,ok=1;
179   int i;
180   
181   for (i=1;i<n;i++) {
182     if (in[i] != NULL) {
183       test= (in[i][0] == '-') && (in[i][1] == which);
184       if (test) {
185         wasfound=1;
186         if (type != 'n') {
187           if (strlen(in[i]) > 2) {
188             switch(type) {
189             case 'u': check_unsigned(in[i]+2,which);break;
190             case 'd': check_integer(in[i]+2,which);break;
191             case 'f': check_float(in[i]+2,which);break;
192             case '2': check_two(in[i]+2,which);break;
193             case '3': check_three(in[i]+2,which);break;
194             }
195             if (ret != NULL)
196               free(ret);
197             check_alloc(ret=(char*)calloc(strlen(in[i]+2)+1,(size_t)1));
198             strcpy(ret,in[i]+2);
199             in[i]=NULL;
200           }
201           else {
202             in[i]=NULL;
203             i++;
204             if (i < n) {
205               if (in[i] != NULL) {
206                 switch(type) {
207                 case 'u': check_unsigned(in[i],which);break;
208                 case 'd': check_integer(in[i],which);break;
209                 case 'f': check_float(in[i],which);break;
210                 case '2': check_two(in[i],which);break;
211                 case '3': check_three(in[i]+2,which);break;
212                 case 'o': ok=check_optional(in[i],which);break;
213                 }
214                 if (ok) {
215                   if (ret != NULL)
216                     free(ret);
217                   check_alloc(ret=(char*)calloc(strlen(in[i])+1,(size_t)1));
218                   strcpy(ret,in[i]);
219                   in[i]=NULL;
220                 }
221                 else {
222                   i--;
223                   if (ret != NULL)
224                     free(ret);
225                   ret=NULL;
226                 }
227               }
228             }
229             else {
230               if (ret != NULL) {
231                 free(ret);
232                 ret=NULL;
233               }
234             }
235           }
236         }
237         else {
238           in[i]=NULL;
239         }
240       }
241     }
242   }
243   
244   if (((type == 'o') || (type == 'n')) && (ret == NULL) && wasfound)
245     return "";
246
247   if (wasfound && (ret == NULL)) {
248     fprintf(stderr,"The option -%c needs some value. Exiting!\n",which);
249     exit(CHECK_OPTION_C_NO_VALUE);
250   }
251   return ret;
252 }