|
|
1.1 root 1: /* Implementation of strtod for systems with atof.
2: Copyright (C) 1991 Free Software Foundation, Inc.
3:
4: This file is part of the libiberty library.
5: Libiberty is free software; you can redistribute it and/or
6: modify it under the terms of the GNU Library General Public
7: License as published by the Free Software Foundation; either
8: version 2 of the License, or (at your option) any later version.
9:
10: Libiberty is distributed in the hope that it will be useful,
11: but WITHOUT ANY WARRANTY; without even the implied warranty of
12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: Library General Public License for more details.
14:
15: You should have received a copy of the GNU Library General Public
16: License along with libiberty; see the file COPYING.LIB. If
17: not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18: Cambridge, MA 02139, USA. */
19:
20: #include <ctype.h>
21:
22: /* Disclaimer: this is currently just used by CHILL in GDB and therefore
23: has not been tested well. It may have been tested for nothing except
24: that it compiles. */
25:
26: double
27: strtod (str, ptr)
28: char *str;
29: char **ptr;
30: {
31: char *p;
32:
33: if (ptr == (char **)0)
34: return atof (str);
35:
36: p = str;
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: *ptr = p + 7;
56: return atof (str);
57: }
58: else
59: {
60: *ptr = p + 3;
61: return atof (str);
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: *ptr = p;
80: return atof (str);
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: *ptr = p + i;
106: return atof (str);
107: }
108: }
109: *ptr = p;
110: return atof (str);
111: }
112: /* Didn't find any digits. Doesn't look like a number. */
113: *ptr = str;
114: return 0.0;
115: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.