strtod.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Implementation of strtod for systems with atof.
  2. Copyright (C) 1991, 1995, 2002, 2011 Free Software Foundation, Inc.
  3. This file is part of the libiberty library. This library is free
  4. software; you can redistribute it and/or modify it under the
  5. terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU CC; see the file COPYING. If not, write to
  14. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, if you link this library with files
  16. compiled with a GNU compiler to produce an executable, this does not cause
  17. the resulting executable to be covered by the GNU General Public License.
  18. This exception does not however invalidate any other reasons why
  19. the executable file might be covered by the GNU General Public License. */
  20. /*
  21. @deftypefn Supplemental double strtod (const char *@var{string}, @
  22. char **@var{endptr})
  23. This ISO C function converts the initial portion of @var{string} to a
  24. @code{double}. If @var{endptr} is not @code{NULL}, a pointer to the
  25. character after the last character used in the conversion is stored in
  26. the location referenced by @var{endptr}. If no conversion is
  27. performed, zero is returned and the value of @var{string} is stored in
  28. the location referenced by @var{endptr}.
  29. @end deftypefn
  30. */
  31. #include "ansidecl.h"
  32. #include "safe-ctype.h"
  33. extern double atof (const char *);
  34. /* Disclaimer: this is currently just used by CHILL in GDB and therefore
  35. has not been tested well. It may have been tested for nothing except
  36. that it compiles. */
  37. double
  38. strtod (char *str, char **ptr)
  39. {
  40. char *p;
  41. if (ptr == (char **)0)
  42. return atof (str);
  43. p = str;
  44. while (ISSPACE (*p))
  45. ++p;
  46. if (*p == '+' || *p == '-')
  47. ++p;
  48. /* INF or INFINITY. */
  49. if ((p[0] == 'i' || p[0] == 'I')
  50. && (p[1] == 'n' || p[1] == 'N')
  51. && (p[2] == 'f' || p[2] == 'F'))
  52. {
  53. if ((p[3] == 'i' || p[3] == 'I')
  54. && (p[4] == 'n' || p[4] == 'N')
  55. && (p[5] == 'i' || p[5] == 'I')
  56. && (p[6] == 't' || p[6] == 'T')
  57. && (p[7] == 'y' || p[7] == 'Y'))
  58. {
  59. *ptr = p + 8;
  60. return atof (str);
  61. }
  62. else
  63. {
  64. *ptr = p + 3;
  65. return atof (str);
  66. }
  67. }
  68. /* NAN or NAN(foo). */
  69. if ((p[0] == 'n' || p[0] == 'N')
  70. && (p[1] == 'a' || p[1] == 'A')
  71. && (p[2] == 'n' || p[2] == 'N'))
  72. {
  73. p += 3;
  74. if (*p == '(')
  75. {
  76. ++p;
  77. while (*p != '\0' && *p != ')')
  78. ++p;
  79. if (*p == ')')
  80. ++p;
  81. }
  82. *ptr = p;
  83. return atof (str);
  84. }
  85. /* digits, with 0 or 1 periods in it. */
  86. if (ISDIGIT (*p) || *p == '.')
  87. {
  88. int got_dot = 0;
  89. while (ISDIGIT (*p) || (!got_dot && *p == '.'))
  90. {
  91. if (*p == '.')
  92. got_dot = 1;
  93. ++p;
  94. }
  95. /* Exponent. */
  96. if (*p == 'e' || *p == 'E')
  97. {
  98. int i;
  99. i = 1;
  100. if (p[i] == '+' || p[i] == '-')
  101. ++i;
  102. if (ISDIGIT (p[i]))
  103. {
  104. while (ISDIGIT (p[i]))
  105. ++i;
  106. *ptr = p + i;
  107. return atof (str);
  108. }
  109. }
  110. *ptr = p;
  111. return atof (str);
  112. }
  113. /* Didn't find any digits. Doesn't look like a number. */
  114. *ptr = str;
  115. return 0.0;
  116. }