Back to index

cell-binutils  2.17cvs20070401
strtod.c
Go to the documentation of this file.
00001 /* Implementation of strtod for systems with atof.
00002    Copyright (C) 1991, 1995, 2002 Free Software Foundation, Inc.
00003 
00004 This file is part of the libiberty library.  This library is free
00005 software; you can redistribute it and/or modify it under the
00006 terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option)
00008 any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with GNU CC; see the file COPYING.  If not, write to
00017 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019 As a special exception, if you link this library with files
00020 compiled with a GNU compiler to produce an executable, this does not cause
00021 the resulting executable to be covered by the GNU General Public License.
00022 This exception does not however invalidate any other reasons why
00023 the executable file might be covered by the GNU General Public License. */
00024 
00025 /*
00026 
00027 @deftypefn Supplemental double strtod (const char *@var{string}, char **@var{endptr})
00028 
00029 This ISO C function converts the initial portion of @var{string} to a
00030 @code{double}.  If @var{endptr} is not @code{NULL}, a pointer to the
00031 character after the last character used in the conversion is stored in
00032 the location referenced by @var{endptr}.  If no conversion is
00033 performed, zero is returned and the value of @var{string} is stored in
00034 the location referenced by @var{endptr}.
00035 
00036 @end deftypefn
00037 
00038 */
00039 
00040 #include "ansidecl.h"
00041 #include "safe-ctype.h"
00042 
00043 extern double atof (const char *);
00044 
00045 /* Disclaimer: this is currently just used by CHILL in GDB and therefore
00046    has not been tested well.  It may have been tested for nothing except
00047    that it compiles.  */
00048 
00049 double
00050 strtod (char *str, char **ptr)
00051 {
00052   char *p;
00053 
00054   if (ptr == (char **)0)
00055     return atof (str);
00056   
00057   p = str;
00058   
00059   while (ISSPACE (*p))
00060     ++p;
00061   
00062   if (*p == '+' || *p == '-')
00063     ++p;
00064 
00065   /* INF or INFINITY.  */
00066   if ((p[0] == 'i' || p[0] == 'I')
00067       && (p[1] == 'n' || p[1] == 'N')
00068       && (p[2] == 'f' || p[2] == 'F'))
00069     {
00070       if ((p[3] == 'i' || p[3] == 'I')
00071          && (p[4] == 'n' || p[4] == 'N')
00072          && (p[5] == 'i' || p[5] == 'I')
00073          && (p[6] == 't' || p[6] == 'T')
00074          && (p[7] == 'y' || p[7] == 'Y'))
00075        {
00076          *ptr = p + 8;
00077          return atof (str);
00078        }
00079       else
00080        {
00081          *ptr = p + 3;
00082          return atof (str);
00083        }
00084     }
00085 
00086   /* NAN or NAN(foo).  */
00087   if ((p[0] == 'n' || p[0] == 'N')
00088       && (p[1] == 'a' || p[1] == 'A')
00089       && (p[2] == 'n' || p[2] == 'N'))
00090     {
00091       p += 3;
00092       if (*p == '(')
00093        {
00094          ++p;
00095          while (*p != '\0' && *p != ')')
00096            ++p;
00097          if (*p == ')')
00098            ++p;
00099        }
00100       *ptr = p;
00101       return atof (str);
00102     }
00103 
00104   /* digits, with 0 or 1 periods in it.  */
00105   if (ISDIGIT (*p) || *p == '.')
00106     {
00107       int got_dot = 0;
00108       while (ISDIGIT (*p) || (!got_dot && *p == '.'))
00109        {
00110          if (*p == '.')
00111            got_dot = 1;
00112          ++p;
00113        }
00114 
00115       /* Exponent.  */
00116       if (*p == 'e' || *p == 'E')
00117        {
00118          int i;
00119          i = 1;
00120          if (p[i] == '+' || p[i] == '-')
00121            ++i;
00122          if (ISDIGIT (p[i]))
00123            {
00124              while (ISDIGIT (p[i]))
00125               ++i;
00126              *ptr = p + i;
00127              return atof (str);
00128            }
00129        }
00130       *ptr = p;
00131       return atof (str);
00132     }
00133   /* Didn't find any digits.  Doesn't look like a number.  */
00134   *ptr = str;
00135   return 0.0;
00136 }