strtod.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <limits.h>
  22. #include <stdlib.h>
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/mathematics.h"
  25. static const char *check_nan_suffix(const char *s)
  26. {
  27. const char *start = s;
  28. if (*s++ != '(')
  29. return start;
  30. while ((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
  31. (*s >= '0' && *s <= '9') || *s == '_')
  32. s++;
  33. return *s == ')' ? s + 1 : start;
  34. }
  35. #undef strtod
  36. double strtod(const char *, char **);
  37. double avpriv_strtod(const char *nptr, char **endptr)
  38. {
  39. const char *end;
  40. double res;
  41. /* Skip leading spaces */
  42. while (av_isspace(*nptr))
  43. nptr++;
  44. if (!av_strncasecmp(nptr, "infinity", 8)) {
  45. end = nptr + 8;
  46. res = INFINITY;
  47. } else if (!av_strncasecmp(nptr, "inf", 3)) {
  48. end = nptr + 3;
  49. res = INFINITY;
  50. } else if (!av_strncasecmp(nptr, "+infinity", 9)) {
  51. end = nptr + 9;
  52. res = INFINITY;
  53. } else if (!av_strncasecmp(nptr, "+inf", 4)) {
  54. end = nptr + 4;
  55. res = INFINITY;
  56. } else if (!av_strncasecmp(nptr, "-infinity", 9)) {
  57. end = nptr + 9;
  58. res = -INFINITY;
  59. } else if (!av_strncasecmp(nptr, "-inf", 4)) {
  60. end = nptr + 4;
  61. res = -INFINITY;
  62. } else if (!av_strncasecmp(nptr, "nan", 3)) {
  63. end = check_nan_suffix(nptr + 3);
  64. res = NAN;
  65. } else if (!av_strncasecmp(nptr, "+nan", 4) ||
  66. !av_strncasecmp(nptr, "-nan", 4)) {
  67. end = check_nan_suffix(nptr + 4);
  68. res = NAN;
  69. } else if (!av_strncasecmp(nptr, "0x", 2) ||
  70. !av_strncasecmp(nptr, "-0x", 3) ||
  71. !av_strncasecmp(nptr, "+0x", 3)) {
  72. /* FIXME this doesn't handle exponents, non-integers (float/double)
  73. * and numbers too large for long long */
  74. res = strtoll(nptr, (char **)&end, 16);
  75. } else {
  76. res = strtod(nptr, (char **)&end);
  77. }
  78. if (endptr)
  79. *endptr = (char *)end;
  80. return res;
  81. }