strtod.c revision 77298
133965Sjdp/* Implementation of strtod for systems with atof.
233965Sjdp   Copyright (C) 1991, 1995 Free Software Foundation, Inc.
333965Sjdp
433965SjdpThis file is part of the libiberty library.  This library is free
533965Sjdpsoftware; you can redistribute it and/or modify it under the
633965Sjdpterms of the GNU General Public License as published by the
733965SjdpFree Software Foundation; either version 2, or (at your option)
833965Sjdpany later version.
933965Sjdp
1033965SjdpThis library is distributed in the hope that it will be useful,
1133965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1233965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1333965SjdpGNU General Public License for more details.
1433965Sjdp
1533965SjdpYou should have received a copy of the GNU General Public License
1633965Sjdpalong with GNU CC; see the file COPYING.  If not, write to
1733965Sjdpthe Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1833965Sjdp
1933965SjdpAs a special exception, if you link this library with files
2033965Sjdpcompiled with a GNU compiler to produce an executable, this does not cause
2133965Sjdpthe resulting executable to be covered by the GNU General Public License.
2233965SjdpThis exception does not however invalidate any other reasons why
2333965Sjdpthe executable file might be covered by the GNU General Public License. */
2433965Sjdp
2577298Sobrien#include "ansidecl.h"
2677298Sobrien#include "safe-ctype.h"
2733965Sjdp
2833965Sjdpextern double atof ();
2933965Sjdp
3033965Sjdp/* Disclaimer: this is currently just used by CHILL in GDB and therefore
3133965Sjdp   has not been tested well.  It may have been tested for nothing except
3233965Sjdp   that it compiles.  */
3333965Sjdp
3433965Sjdpdouble
3533965Sjdpstrtod (str, ptr)
3633965Sjdp     char *str;
3733965Sjdp     char **ptr;
3833965Sjdp{
3933965Sjdp  char *p;
4033965Sjdp
4133965Sjdp  if (ptr == (char **)0)
4233965Sjdp    return atof (str);
4333965Sjdp
4433965Sjdp  p = str;
4533965Sjdp
4677298Sobrien  while (ISSPACE (*p))
4733965Sjdp    ++p;
4833965Sjdp
4933965Sjdp  if (*p == '+' || *p == '-')
5033965Sjdp    ++p;
5133965Sjdp
5233965Sjdp  /* INF or INFINITY.  */
5333965Sjdp  if ((p[0] == 'i' || p[0] == 'I')
5433965Sjdp      && (p[1] == 'n' || p[1] == 'N')
5533965Sjdp      && (p[2] == 'f' || p[2] == 'F'))
5633965Sjdp    {
5733965Sjdp      if ((p[3] == 'i' || p[3] == 'I')
5833965Sjdp	  && (p[4] == 'n' || p[4] == 'N')
5933965Sjdp	  && (p[5] == 'i' || p[5] == 'I')
6033965Sjdp	  && (p[6] == 't' || p[6] == 'T')
6133965Sjdp	  && (p[7] == 'y' || p[7] == 'Y'))
6233965Sjdp	{
6333965Sjdp	  *ptr = p + 7;
6433965Sjdp	  return atof (str);
6533965Sjdp	}
6633965Sjdp      else
6733965Sjdp	{
6833965Sjdp	  *ptr = p + 3;
6933965Sjdp	  return atof (str);
7033965Sjdp	}
7133965Sjdp    }
7233965Sjdp
7333965Sjdp  /* NAN or NAN(foo).  */
7433965Sjdp  if ((p[0] == 'n' || p[0] == 'N')
7533965Sjdp      && (p[1] == 'a' || p[1] == 'A')
7633965Sjdp      && (p[2] == 'n' || p[2] == 'N'))
7733965Sjdp    {
7833965Sjdp      p += 3;
7933965Sjdp      if (*p == '(')
8033965Sjdp	{
8133965Sjdp	  ++p;
8233965Sjdp	  while (*p != '\0' && *p != ')')
8333965Sjdp	    ++p;
8433965Sjdp	  if (*p == ')')
8533965Sjdp	    ++p;
8633965Sjdp	}
8733965Sjdp      *ptr = p;
8833965Sjdp      return atof (str);
8933965Sjdp    }
9033965Sjdp
9133965Sjdp  /* digits, with 0 or 1 periods in it.  */
9277298Sobrien  if (ISDIGIT (*p) || *p == '.')
9333965Sjdp    {
9433965Sjdp      int got_dot = 0;
9577298Sobrien      while (ISDIGIT (*p) || (!got_dot && *p == '.'))
9633965Sjdp	{
9733965Sjdp	  if (*p == '.')
9833965Sjdp	    got_dot = 1;
9933965Sjdp	  ++p;
10033965Sjdp	}
10133965Sjdp
10233965Sjdp      /* Exponent.  */
10333965Sjdp      if (*p == 'e' || *p == 'E')
10433965Sjdp	{
10533965Sjdp	  int i;
10633965Sjdp	  i = 1;
10733965Sjdp	  if (p[i] == '+' || p[i] == '-')
10833965Sjdp	    ++i;
10977298Sobrien	  if (ISDIGIT (p[i]))
11033965Sjdp	    {
11177298Sobrien	      while (ISDIGIT (p[i]))
11233965Sjdp		++i;
11333965Sjdp	      *ptr = p + i;
11433965Sjdp	      return atof (str);
11533965Sjdp	    }
11633965Sjdp	}
11733965Sjdp      *ptr = p;
11833965Sjdp      return atof (str);
11933965Sjdp    }
12033965Sjdp  /* Didn't find any digits.  Doesn't look like a number.  */
12133965Sjdp  *ptr = str;
12233965Sjdp  return 0.0;
12333965Sjdp}
124