strtod.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * C99-compatible strtod() implementation
  3. * Copyright (c) 2012 Ronald S. Bultje <rsbultje@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <ctype.h>
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/mathematics.h"
  26. static char *check_nan_suffix(char *s)
  27. {
  28. char *start = s;
  29. if (*s++ != '(')
  30. return start;
  31. while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
  32. (*s >= '0' && *s <= '9') || *s == '_')
  33. s++;
  34. return *s == ')' ? s + 1 : start;
  35. }
  36. #undef strtod
  37. double strtod(const char *, char **);
  38. double avpriv_strtod(const char *nptr, char **endptr)
  39. {
  40. char *end;
  41. double res;
  42. /* Skip leading spaces */
  43. while (isspace(*nptr))
  44. nptr++;
  45. if (!av_strncasecmp(nptr, "infinity", 8)) {
  46. end = nptr + 8;
  47. res = INFINITY;
  48. } else if (!av_strncasecmp(nptr, "inf", 3)) {
  49. end = nptr + 3;
  50. res = INFINITY;
  51. } else if (!av_strncasecmp(nptr, "+infinity", 9)) {
  52. end = nptr + 9;
  53. res = INFINITY;
  54. } else if (!av_strncasecmp(nptr, "+inf", 4)) {
  55. end = nptr + 4;
  56. res = INFINITY;
  57. } else if (!av_strncasecmp(nptr, "-infinity", 9)) {
  58. end = nptr + 9;
  59. res = -INFINITY;
  60. } else if (!av_strncasecmp(nptr, "-inf", 4)) {
  61. end = nptr + 4;
  62. res = -INFINITY;
  63. } else if (!av_strncasecmp(nptr, "nan", 3)) {
  64. end = check_nan_suffix(nptr + 3);
  65. res = NAN;
  66. } else if (!av_strncasecmp(nptr, "+nan", 4) ||
  67. !av_strncasecmp(nptr, "-nan", 4)) {
  68. end = check_nan_suffix(nptr + 4);
  69. res = NAN;
  70. } else if (!av_strncasecmp(nptr, "0x", 2) ||
  71. !av_strncasecmp(nptr, "-0x", 3) ||
  72. !av_strncasecmp(nptr, "+0x", 3)) {
  73. /* FIXME this doesn't handle exponents, non-integers (float/double)
  74. * and numbers too large for long long */
  75. res = strtoll(nptr, &end, 16);
  76. } else {
  77. res = strtod(nptr, &end);
  78. }
  79. if (endptr)
  80. *endptr = end;
  81. return res;
  82. }