strtod.c revision 33965
1164022Sdds/* Implementation of strtod for systems with atof.
2164022Sdds   Copyright (C) 1991, 1995 Free Software Foundation, Inc.
3164022Sdds
4164022SddsThis file is part of the libiberty library.  This library is free
5164022Sddssoftware; you can redistribute it and/or modify it under the
6164022Sddsterms of the GNU General Public License as published by the
7164022SddsFree Software Foundation; either version 2, or (at your option)
8164022Sddsany later version.
9164022Sdds
10164022SddsThis library is distributed in the hope that it will be useful,
11164022Sddsbut WITHOUT ANY WARRANTY; without even the implied warranty of
12164022SddsMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13164022SddsGNU General Public License for more details.
14164022Sdds
15164022SddsYou should have received a copy of the GNU General Public License
16164022Sddsalong with GNU CC; see the file COPYING.  If not, write to
17164022Sddsthe Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18164022Sdds
19164022SddsAs a special exception, if you link this library with files
20164022Sddscompiled with a GNU compiler to produce an executable, this does not cause
21164022Sddsthe resulting executable to be covered by the GNU General Public License.
22164022SddsThis exception does not however invalidate any other reasons why
23164022Sddsthe executable file might be covered by the GNU General Public License. */
24164022Sdds
25164022Sdds#include <ctype.h>
26164022Sdds
27164022Sddsextern double atof ();
28164022Sdds
29164022Sdds/* Disclaimer: this is currently just used by CHILL in GDB and therefore
30164022Sdds   has not been tested well.  It may have been tested for nothing except
31164022Sdds   that it compiles.  */
32164022Sdds
33164022Sddsdouble
34164022Sddsstrtod (str, ptr)
35164022Sdds     char *str;
36164022Sdds     char **ptr;
37164022Sdds{
38164022Sdds  char *p;
39164022Sdds
40164022Sdds  if (ptr == (char **)0)
41164022Sdds    return atof (str);
42164022Sdds
43164022Sdds  p = str;
44164022Sdds
45164022Sdds  while (isspace (*p))
46164022Sdds    ++p;
47164022Sdds
48164022Sdds  if (*p == '+' || *p == '-')
49164022Sdds    ++p;
50164022Sdds
51164040Sdds  /* INF or INFINITY.  */
52164022Sdds  if ((p[0] == 'i' || p[0] == 'I')
53164022Sdds      && (p[1] == 'n' || p[1] == 'N')
54164022Sdds      && (p[2] == 'f' || p[2] == 'F'))
55164022Sdds    {
56164022Sdds      if ((p[3] == 'i' || p[3] == 'I')
57164022Sdds	  && (p[4] == 'n' || p[4] == 'N')
58164022Sdds	  && (p[5] == 'i' || p[5] == 'I')
59164022Sdds	  && (p[6] == 't' || p[6] == 'T')
60164022Sdds	  && (p[7] == 'y' || p[7] == 'Y'))
61164022Sdds	{
62164022Sdds	  *ptr = p + 7;
63164022Sdds	  return atof (str);
64164022Sdds	}
65164022Sdds      else
66164022Sdds	{
67164022Sdds	  *ptr = p + 3;
68164022Sdds	  return atof (str);
69164022Sdds	}
70164022Sdds    }
71164022Sdds
72164022Sdds  /* NAN or NAN(foo).  */
73164022Sdds  if ((p[0] == 'n' || p[0] == 'N')
74164022Sdds      && (p[1] == 'a' || p[1] == 'A')
75164022Sdds      && (p[2] == 'n' || p[2] == 'N'))
76164022Sdds    {
77164022Sdds      p += 3;
78164022Sdds      if (*p == '(')
79164022Sdds	{
80164022Sdds	  ++p;
81164022Sdds	  while (*p != '\0' && *p != ')')
82164022Sdds	    ++p;
83164022Sdds	  if (*p == ')')
84164022Sdds	    ++p;
85164022Sdds	}
86164022Sdds      *ptr = p;
87164022Sdds      return atof (str);
88164022Sdds    }
89164022Sdds
90164022Sdds  /* digits, with 0 or 1 periods in it.  */
91164022Sdds  if (isdigit (*p) || *p == '.')
92164022Sdds    {
93164022Sdds      int got_dot = 0;
94164022Sdds      while (isdigit (*p) || (!got_dot && *p == '.'))
95164022Sdds	{
96164022Sdds	  if (*p == '.')
97164022Sdds	    got_dot = 1;
98164022Sdds	  ++p;
99164022Sdds	}
100164022Sdds
101164022Sdds      /* Exponent.  */
102      if (*p == 'e' || *p == 'E')
103	{
104	  int i;
105	  i = 1;
106	  if (p[i] == '+' || p[i] == '-')
107	    ++i;
108	  if (isdigit (p[i]))
109	    {
110	      while (isdigit (p[i]))
111		++i;
112	      *ptr = p + i;
113	      return atof (str);
114	    }
115	}
116      *ptr = p;
117      return atof (str);
118    }
119  /* Didn't find any digits.  Doesn't look like a number.  */
120  *ptr = str;
121  return 0.0;
122}
123