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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 (); 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 (str, ptr) 00051 char *str; 00052 char **ptr; 00053 { 00054 char *p; 00055 00056 if (ptr == (char **)0) 00057 return atof (str); 00058 00059 p = str; 00060 00061 while (ISSPACE (*p)) 00062 ++p; 00063 00064 if (*p == '+' || *p == '-') 00065 ++p; 00066 00067 /* INF or INFINITY. */ 00068 if ((p[0] == 'i' || p[0] == 'I') 00069 && (p[1] == 'n' || p[1] == 'N') 00070 && (p[2] == 'f' || p[2] == 'F')) 00071 { 00072 if ((p[3] == 'i' || p[3] == 'I') 00073 && (p[4] == 'n' || p[4] == 'N') 00074 && (p[5] == 'i' || p[5] == 'I') 00075 && (p[6] == 't' || p[6] == 'T') 00076 && (p[7] == 'y' || p[7] == 'Y')) 00077 { 00078 *ptr = p + 8; 00079 return atof (str); 00080 } 00081 else 00082 { 00083 *ptr = p + 3; 00084 return atof (str); 00085 } 00086 } 00087 00088 /* NAN or NAN(foo). */ 00089 if ((p[0] == 'n' || p[0] == 'N') 00090 && (p[1] == 'a' || p[1] == 'A') 00091 && (p[2] == 'n' || p[2] == 'N')) 00092 { 00093 p += 3; 00094 if (*p == '(') 00095 { 00096 ++p; 00097 while (*p != '\0' && *p != ')') 00098 ++p; 00099 if (*p == ')') 00100 ++p; 00101 } 00102 *ptr = p; 00103 return atof (str); 00104 } 00105 00106 /* digits, with 0 or 1 periods in it. */ 00107 if (ISDIGIT (*p) || *p == '.') 00108 { 00109 int got_dot = 0; 00110 while (ISDIGIT (*p) || (!got_dot && *p == '.')) 00111 { 00112 if (*p == '.') 00113 got_dot = 1; 00114 ++p; 00115 } 00116 00117 /* Exponent. */ 00118 if (*p == 'e' || *p == 'E') 00119 { 00120 int i; 00121 i = 1; 00122 if (p[i] == '+' || p[i] == '-') 00123 ++i; 00124 if (ISDIGIT (p[i])) 00125 { 00126 while (ISDIGIT (p[i])) 00127 ++i; 00128 *ptr = p + i; 00129 return atof (str); 00130 } 00131 } 00132 *ptr = p; 00133 return atof (str); 00134 } 00135 /* Didn't find any digits. Doesn't look like a number. */ 00136 *ptr = str; 00137 return 0.0; 00138 }
1.5.6