1 | /* Copyright (C) 1991, 1995 Free Software Foundation, Inc.␍␊ |
2 | ␍␊ |
3 | This library is free software; you can redistribute it and/or modify ␍␊ |
4 | it under theterms of the GNU General Public License as published ␍␊ |
5 | by the Free Software Foundation; either version 2, or (at your option)␍␊ |
6 | any later version.␍␊ |
7 | ␍␊ |
8 | This library is distributed in the hope that it will be useful,␍␊ |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of␍␊ |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the␍␊ |
11 | GNU General Public License for more details.␍␊ |
12 | ␍␊ |
13 | You should have received a copy of the GNU General Public License␍␊ |
14 | along with GNU CC; see the file COPYING. If not, write to␍␊ |
15 | the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.␍␊ |
16 | ␍␊ |
17 | As a special exception, if you link this library with files␍␊ |
18 | compiled with a GNU compiler to produce an executable, this does not cause␍␊ |
19 | the resulting executable to be covered by the GNU General Public License.␍␊ |
20 | This exception does not however invalidate any other reasons why␍␊ |
21 | the executable file might be covered by the GNU General Public License. */␍␊ |
22 | ␍␊ |
23 | #include "libsa.h"␍␊ |
24 | ␍␊ |
25 | extern double atof ();␍␊ |
26 | ␍␊ |
27 | double strtod (nptr, enptr)␍␊ |
28 | ␉char *nptr;␍␊ |
29 | ␉char **enptr;␍␊ |
30 | {␍␊ |
31 | ␉char *p;␍␊ |
32 | ␍␊ |
33 | ␉if (enptr == (char **)0)␍␊ |
34 | ␉␉return atof (nptr);␍␊ |
35 | ␍␊ |
36 | ␉p = nptr;␍␊ |
37 | ␍␊ |
38 | ␉while (isspace (*p))␍␊ |
39 | ␉␉++p;␍␊ |
40 | ␍␊ |
41 | ␉if (*p == '+' || *p == '-')␍␊ |
42 | ␉␉++p;␍␊ |
43 | ␍␊ |
44 | ␉/* INF or INFINITY. */␍␊ |
45 | ␉if ((p[0] == 'i' || p[0] == 'I')␍␊ |
46 | ␉␉&& (p[1] == 'n' || p[1] == 'N')␍␊ |
47 | ␉␉&& (p[2] == 'f' || p[2] == 'F'))␍␊ |
48 | ␉{␍␊ |
49 | ␉␉if ((p[3] == 'i' || p[3] == 'I')␍␊ |
50 | ␉␉␉&& (p[4] == 'n' || p[4] == 'N')␍␊ |
51 | ␉␉␉&& (p[5] == 'i' || p[5] == 'I')␍␊ |
52 | ␉␉␉&& (p[6] == 't' || p[6] == 'T')␍␊ |
53 | ␉␉␉&& (p[7] == 'y' || p[7] == 'Y'))␍␊ |
54 | ␉␉{␍␊ |
55 | ␉␉␉*enptr = p + 7;␍␊ |
56 | ␉␉␉return atof (nptr);␍␊ |
57 | ␉␉}␍␊ |
58 | ␉␉else␍␊ |
59 | ␉␉{␍␊ |
60 | ␉␉␉*enptr = p + 3;␍␊ |
61 | ␉␉␉return atof (nptr);␍␊ |
62 | ␉␉}␍␊ |
63 | ␉}␍␊ |
64 | ␍␊ |
65 | ␉/* NAN or NAN(foo). */␍␊ |
66 | ␉if ((p[0] == 'n' || p[0] == 'N')␍␊ |
67 | ␉␉&& (p[1] == 'a' || p[1] == 'A')␍␊ |
68 | ␉␉&& (p[2] == 'n' || p[2] == 'N'))␍␊ |
69 | ␉{␍␊ |
70 | ␉␉p += 3;␍␊ |
71 | ␉␉if (*p == '(')␍␊ |
72 | ␉␉{␍␊ |
73 | ␉␉␉++p;␍␊ |
74 | ␉␉␉while (*p != '\0' && *p != ')')␍␊ |
75 | ␉␉␉␉++p;␍␊ |
76 | ␉␉␉if (*p == ')')␍␊ |
77 | ␉␉␉␉++p;␍␊ |
78 | ␉␉}␍␊ |
79 | ␉␉*enptr = p;␍␊ |
80 | ␉␉return atof (nptr);␍␊ |
81 | ␉}␍␊ |
82 | ␍␊ |
83 | ␉/* digits, with 0 or 1 periods in it. */␍␊ |
84 | ␉if (isdigit (*p) || *p == '.')␍␊ |
85 | ␉{␍␊ |
86 | ␉␉int got_dot = 0;␍␊ |
87 | ␉␉while (isdigit (*p) || (!got_dot && *p == '.'))␍␊ |
88 | ␉␉{␍␊ |
89 | ␉␉␉if (*p == '.')␍␊ |
90 | ␉␉␉␉got_dot = 1;␍␊ |
91 | ␉␉␉++p;␍␊ |
92 | ␉␉}␍␊ |
93 | ␍␊ |
94 | ␉␉/* Exponent. */␍␊ |
95 | ␉␉if (*p == 'e' || *p == 'E')␍␊ |
96 | ␉␉{␍␊ |
97 | ␉␉␉int i;␍␊ |
98 | ␉␉␉i = 1;␍␊ |
99 | ␉␉␉if (p[i] == '+' || p[i] == '-')␍␊ |
100 | ␉␉␉␉++i;␍␊ |
101 | ␉␉␉if (isdigit (p[i]))␍␊ |
102 | ␉␉␉{␍␊ |
103 | ␉␉␉␉while (isdigit (p[i]))␍␊ |
104 | ␉␉␉␉␉++i;␍␊ |
105 | ␉␉␉␉*enptr = p + i;␍␊ |
106 | ␉␉␉␉return atof (nptr);␍␊ |
107 | ␉␉␉}␍␊ |
108 | ␉␉}␍␊ |
109 | ␉␉*enptr = p;␍␊ |
110 | ␉␉return atof (nptr);␍␊ |
111 | ␉}␍␊ |
112 | ␉/* Didn't find any digits. Doesn't look like a number. */␍␊ |
113 | ␉*enptr = nptr;␍␊ |
114 | ␉return 0.0;␍␊ |
115 | }␍␊ |
116 | |