1 #include <lib9.h> 2 3 /* 4 * Reads a floating-point number by interpreting successive characters 5 * returned by (*f)(vp). The last call it makes to f terminates the 6 * scan, so is not a character in the number. It may therefore be 7 * necessary to back up the input stream up one byte after calling charstod. 8 */ 9 10 double 11 charstod(int(*f)(void*), void *vp) 12 { 13 double num, dem; 14 int neg, eneg, dig, exp, c; 15 16 num = 0; 17 neg = 0; 18 dig = 0; 19 exp = 0; 20 eneg = 0; 21 22 c = (*f)(vp); 23 while(c == ' ' || c == '\t') 24 c = (*f)(vp); 25 if(c == '-' || c == '+'){ 26 if(c == '-') 27 neg = 1; 28 c = (*f)(vp); 29 } 30 while(c >= '0' && c <= '9'){ 31 num = num*10 + c-'0'; 32 c = (*f)(vp); 33 } 34 if(c == '.') 35 c = (*f)(vp); 36 while(c >= '0' && c <= '9'){ 37 num = num*10 + c-'0'; 38 dig++; 39 c = (*f)(vp); 40 } 41 if(c == 'e' || c == 'E'){ 42 c = (*f)(vp); 43 if(c == '-' || c == '+'){ 44 if(c == '-'){ 45 dig = -dig; 46 eneg = 1; 47 } 48 c = (*f)(vp); 49 } 50 while(c >= '0' && c <= '9'){ 51 exp = exp*10 + c-'0'; 52 c = (*f)(vp); 53 } 54 } 55 exp -= dig; 56 if(exp < 0){ 57 exp = -exp; 58 eneg = !eneg; 59 } 60 dem = pow10(exp); 61 if(eneg) 62 num /= dem; 63 else 64 num *= dem; 65 if(neg) 66 return -num; 67 return num; 68 } 69