Fixed text file permissions
[snark14.git] / tools / Input / misc.cpp
1 /** @file misc.cpp
2     @package snark14Input
3     @author Bruno M. Carvalho and Deniz Sarioz
4     licensed under (open-source) QPL v1.0
5     which accompanies this distribution in the file QPL
6 */
7
8 #include "variables.h"
9 #include "misc.h"
10 #include <string.h>
11 #include <cstdio>
12 #include <cmath>
13
14 int toint(const char c);
15
16 // Deniz thought this was overly buggy
17 /** Extracts a float from a string
18 @param void
19 @author Bruno M. Carvalho
20 @version 1.0 */
21 // float strtof(const char *str) {
22 //   printf("string `%s' passed to strtof() defined in misc.cpp\n", str);
23 //   float result=0,fraction=0,s=1,sexp=1;
24 //   int i=0,nf=0,exp=0;
25
26 //   result=0;
27 //   errnum=0;
28 //   if(str[i]=='\0') {
29 //     errnum=2;
30 //     return result;
31 //   }
32 //   while(str[i]!='-' && !isdigit(str[i])) {
33 //     i++;
34 //   }
35 //   if(str[i]=='-') {
36 //     s=-1;
37 //     i++;
38 //   }
39 //   while(isdigit(str[i])) {
40 //     result=result*10+toint(str[i]);
41 //     i++;
42 //   }
43 //   if(str[i]=='.') {
44 //     i++;
45 //   }
46 //   if( (str[i]=='e') || (str[i]=='E') )  {
47 //     i++;
48 //     exp=1;
49 //     if(str[i]=='-') {
50 //       sexp=-1;
51 //       i++;
52 //     }
53 //   }
54 //   while(isdigit(str[i])) {
55 //     fraction=fraction*10+toint(str[i]);
56 //     i++;
57 //     nf++;
58 //   }
59 //   while(str[i]) {
60 //     if(str[i]!=32) errnum=1;
61 //     i++;
62 //   }
63 //   if(nf>0)
64 //     if(exp)
65 //       result=s*result*pow(10,sexp*fraction);
66 //     else 
67 //       result=s*(result+fraction/(10*nf));
68 //   else
69 //     result=s*result;
70 //   return result;
71 // }
72
73
74 /* @author deniz
75    @version 3.0
76    will implement:
77    [-]{digit+[.digit*],.digit+}[{E,e}[+,-]digit[digit]]
78    as decided on 2005.08.11 in group meeting
79 */
80 float strtof(const char *str) {
81   //  printf("string `%s' passed to strtof() defined in misc.cpp\n", str);
82   // new parse 'philosophy': get the number as integer
83   // if there is stuff after the period, count them
84   int mainval=0, mainsign=1;
85   int expval=0, expsign=1;
86   int pos=0, numdigitsafterdot=0;
87   errnum=0;
88   while(isspace(str[pos])) {
89     pos++;
90   }
91   if(str[pos]=='-') {  //  [-]
92     mainsign= -1;
93     pos++;
94   }
95   // adjust at the end
96   // now get:  {digit+[.digit*],.digit+}
97   if(isdigit(str[pos])) { // try to parse (digit+[.digit*])
98     while(isdigit(str[pos])) {
99       mainval = mainval*10 + toint(str[pos]);
100       pos++;
101     }
102     if(str[pos]=='.') {
103       pos++;
104       while(isdigit(str[pos])) { // try to parse (digit*)       
105         mainval = mainval*10 + toint(str[pos]);
106         numdigitsafterdot++;  // number of digits after '.'
107         pos++;
108       }
109     } // --period read after digit
110   } // --digit read before (if at all) period
111   else if(str[pos]=='.') { // parse (.digit+)
112     pos++;
113     if(!isdigit(str[pos])) {
114       errnum = 2; // need a digit there
115       return 0.0;
116     }
117     while(isdigit(str[pos])) {
118       mainval = mainval*10 + toint(str[pos]);
119       numdigitsafterdot++;  // number of digits after '.'
120       pos++;
121     }
122   } // --period read before any digit
123   else {   // {digit+[.digit*],.digit+} not found
124     errnum = 1;
125     return 0.0;
126   }
127   if( (str[pos]=='e') || (str[pos]=='E') )  { // try to parse ([+,-]digit[digit])
128     pos++;
129     if(str[pos]=='-') {
130       expsign= -1;
131       pos++;
132     } else if(str[pos]=='+') { 
133       pos++; 
134     }
135     if(!isdigit(str[pos])) { // try to parse (digit[digit])
136       errnum = 4; // no exp digit
137       return 0.0;
138     } else {
139       expval = toint(str[pos]);
140       pos++;
141     }
142     if(isdigit(str[pos])) { // second possible digit
143       expval = expval*10 + toint(str[pos]);
144       pos++;
145     }
146   }
147   while(isspace(str[pos])) {
148     pos++;
149   }
150   if('\0' != str[pos]) {  // there is junk
151     errnum = 5;
152     return 0.0;
153   }
154   float retval = mainsign*mainval * pow(10, (expsign*expval - numdigitsafterdot));
155   //  printf("%8.8f\n", retval);
156   return retval; // (mainsign*mainval * pow(10, (expsign*expval - numdigitsafterdot) ) );
157 } // strtof()
158
159
160 /** Extracts an int from a string
161 @param void
162 @author Bruno M. Carvalho
163 @version 1.0 */
164 int strtoi(const char *str) {
165   int result=0,i=0,t;
166
167   result=0;
168   errnum=0;
169   if(str[i]=='\0') {
170     errnum=2;
171     return result;
172   }
173   t=strcmp(str,"");
174   if(t) {
175     while(!isdigit(str[i])) {
176       i++;
177     }
178     while(isdigit(str[i])) {
179       result=result*10+toint(str[i]);
180       i++;
181     }
182     while(str[i]) {
183       if(str[i]!=32) errnum=1;
184       i++;
185     }
186   }
187   return result;
188 }
189
190 /** checks if there are leading spaces
191 @param void
192 @author Bruno M. Carvalho
193 @version 1.0 */
194 int isintspace(const char *str) {
195   int i=0;
196
197   errnum=0;
198   if(!strcmp(str,""))
199     return 0;
200   while(isdigit(str[i]) || str[i]==' ') {
201     i++;
202   }
203   if(str[i]) return 0;
204   else  return 1;
205 }
206
207 /** Checks if line is a snark14 formatline
208 @param void
209 @author Bruno M. Carvalho
210 @version 1.0 */
211 int isformatline(const char *str) {
212   int i=0;
213
214   errnum=0;
215   while(str[i]!='(')  i++;
216   i++;
217   while(isdigit(str[i]))  i++;
218   if(stolower(str[i])!='f')
219     errnum=1;
220   if(errnum) return 0;
221   else  {
222     i++;
223     while(isdigit(str[i]))  i++;
224     if(str[i]!='.') {
225       errnum=1;
226       return 0;
227     }
228     i++;
229     while(isdigit(str[i]))  i++;
230     if(str[i]!=')') {
231       errnum=1;
232       return 0;
233     }
234     i++;
235   if(str[i]) return 0;
236   else  return 1;
237   }
238 }
239
240 /** Converts a char into an int 
241 @param void
242 @author Bruno M. Carvalho
243 @version 1.0 */
244 int toint(const char c) {
245
246   if(c>='0' && c<= '9') return c - '0';
247   else return 0;
248 }
249
250 /** converts a char into lower case
251 @param void
252 @author Bruno M. Carvalho
253 @version 1.0 */
254 int stolower(char c) {
255
256   if(isupper(c)) return tolower(c);
257   else return c;
258 }