-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcharstod.c
80 lines (76 loc) · 1.59 KB
/
charstod.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "lib9.h"
/*
* Reads a floating-point number by interpreting successive characters
* returned by (*f)(vp). The last call it makes to f terminates the
* scan, so is not a character in the number. It may therefore be
* necessary to back up the input stream up one byte after calling charstod.
*/
#define ADVANCE *s++ = c; if(s>=e) return NaN(); c = (*f)(vp)
double
charstod(int(*f)(void*), void *vp)
{
char str[400], *s, *e, *start;
int c;
s = str;
e = str + sizeof str - 1;
c = (*f)(vp);
while(c == ' ' || c == '\t')
c = (*f)(vp);
if(c == '-' || c == '+'){
ADVANCE;
}
start = s;
while(c >= '0' && c <= '9'){
ADVANCE;
}
if(c == '.'){
ADVANCE;
while(c >= '0' && c <= '9'){
ADVANCE;
}
}
if(s > start && (c == 'e' || c == 'E')){
ADVANCE;
if(c == '-' || c == '+'){
ADVANCE;
}
while(c >= '0' && c <= '9'){
ADVANCE;
}
}else if(s == start && (c == 'i' || c == 'I')){
ADVANCE;
if(c != 'n' && c != 'N')
return NaN();
ADVANCE;
if(c != 'f' && c != 'F')
return NaN();
ADVANCE;
if(c != 'i' && c != 'I')
return NaN();
ADVANCE;
if(c != 'n' && c != 'N')
return NaN();
ADVANCE;
if(c != 'i' && c != 'I')
return NaN();
ADVANCE;
if(c != 't' && c != 'T')
return NaN();
ADVANCE;
if(c != 'y' && c != 'Y')
return NaN();
ADVANCE; /* so caller can back up uniformly */
USED(c);
}else if(s == str && (c == 'n' || c == 'N')){
ADVANCE;
if(c != 'a' && c != 'A')
return NaN();
ADVANCE;
if(c != 'n' && c != 'N')
return NaN();
ADVANCE; /* so caller can back up uniformly */
USED(c);
}
*s = 0;
return strtod(str, &s);
}