strtoimax.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Convert string representation of a number into an intmax_t value.
  2. Copyright (C) 1999, 2001-2004, 2006, 2009-2015 Free Software Foundation,
  3. Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by Paul Eggert. */
  15. #include <config.h>
  16. /* Verify interface. */
  17. #include <inttypes.h>
  18. #include <stdlib.h>
  19. #include "verify.h"
  20. #ifdef UNSIGNED
  21. # if HAVE_UNSIGNED_LONG_LONG_INT
  22. # ifndef HAVE_DECL_STRTOULL
  23. "this configure-time declaration test was not run"
  24. # endif
  25. # if !HAVE_DECL_STRTOULL
  26. unsigned long long int strtoull (char const *, char **, int);
  27. # endif
  28. # endif
  29. #else
  30. # if HAVE_LONG_LONG_INT
  31. # ifndef HAVE_DECL_STRTOLL
  32. "this configure-time declaration test was not run"
  33. # endif
  34. # if !HAVE_DECL_STRTOLL
  35. long long int strtoll (char const *, char **, int);
  36. # endif
  37. # endif
  38. #endif
  39. #ifdef UNSIGNED
  40. # define Have_long_long HAVE_UNSIGNED_LONG_LONG_INT
  41. # define Int uintmax_t
  42. # define Strtoimax strtoumax
  43. # define Strtol strtoul
  44. # define Strtoll strtoull
  45. # define Unsigned unsigned
  46. #else
  47. # define Have_long_long HAVE_LONG_LONG_INT
  48. # define Int intmax_t
  49. # define Strtoimax strtoimax
  50. # define Strtol strtol
  51. # define Strtoll strtoll
  52. # define Unsigned
  53. #endif
  54. Int
  55. Strtoimax (char const *ptr, char **endptr, int base)
  56. {
  57. #if Have_long_long
  58. verify (sizeof (Int) == sizeof (Unsigned long int)
  59. || sizeof (Int) == sizeof (Unsigned long long int));
  60. if (sizeof (Int) != sizeof (Unsigned long int))
  61. return Strtoll (ptr, endptr, base);
  62. #else
  63. verify (sizeof (Int) == sizeof (Unsigned long int));
  64. #endif
  65. return Strtol (ptr, endptr, base);
  66. }