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
14 int toint(const char c);
16 // Deniz thought this was overly buggy
17 /** Extracts a float from a string
19 @author Bruno M. Carvalho
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;
32 // while(str[i]!='-' && !isdigit(str[i])) {
39 // while(isdigit(str[i])) {
40 // result=result*10+toint(str[i]);
46 // if( (str[i]=='e') || (str[i]=='E') ) {
54 // while(isdigit(str[i])) {
55 // fraction=fraction*10+toint(str[i]);
60 // if(str[i]!=32) errnum=1;
65 // result=s*result*pow(10,sexp*fraction);
67 // result=s*(result+fraction/(10*nf));
77 [-]{digit+[.digit*],.digit+}[{E,e}[+,-]digit[digit]]
78 as decided on 2005.08.11 in group meeting
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;
88 while(isspace(str[pos])) {
91 if(str[pos]=='-') { // [-]
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]);
104 while(isdigit(str[pos])) { // try to parse (digit*)
105 mainval = mainval*10 + toint(str[pos]);
106 numdigitsafterdot++; // number of digits after '.'
109 } // --period read after digit
110 } // --digit read before (if at all) period
111 else if(str[pos]=='.') { // parse (.digit+)
113 if(!isdigit(str[pos])) {
114 errnum = 2; // need a digit there
117 while(isdigit(str[pos])) {
118 mainval = mainval*10 + toint(str[pos]);
119 numdigitsafterdot++; // number of digits after '.'
122 } // --period read before any digit
123 else { // {digit+[.digit*],.digit+} not found
127 if( (str[pos]=='e') || (str[pos]=='E') ) { // try to parse ([+,-]digit[digit])
132 } else if(str[pos]=='+') {
135 if(!isdigit(str[pos])) { // try to parse (digit[digit])
136 errnum = 4; // no exp digit
139 expval = toint(str[pos]);
142 if(isdigit(str[pos])) { // second possible digit
143 expval = expval*10 + toint(str[pos]);
147 while(isspace(str[pos])) {
150 if('\0' != str[pos]) { // there is junk
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) ) );
160 /** Extracts an int from a string
162 @author Bruno M. Carvalho
164 int strtoi(const char *str) {
175 while(!isdigit(str[i])) {
178 while(isdigit(str[i])) {
179 result=result*10+toint(str[i]);
183 if(str[i]!=32) errnum=1;
190 /** checks if there are leading spaces
192 @author Bruno M. Carvalho
194 int isintspace(const char *str) {
200 while(isdigit(str[i]) || str[i]==' ') {
207 /** Checks if line is a snark14 formatline
209 @author Bruno M. Carvalho
211 int isformatline(const char *str) {
215 while(str[i]!='(') i++;
217 while(isdigit(str[i])) i++;
218 if(stolower(str[i])!='f')
223 while(isdigit(str[i])) i++;
229 while(isdigit(str[i])) i++;
240 /** Converts a char into an int
242 @author Bruno M. Carvalho
244 int toint(const char c) {
246 if(c>='0' && c<= '9') return c - '0';
250 /** converts a char into lower case
252 @author Bruno M. Carvalho
254 int stolower(char c) {
256 if(isupper(c)) return tolower(c);