4
0

strtoimax.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Convert string representation of a number into an intmax_t value.
  2. Copyright 1999, 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Written by Paul Eggert. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #if HAVE_INTTYPES_H
  19. # include <inttypes.h>
  20. #endif
  21. #if HAVE_STDLIB_H
  22. # include <stdlib.h>
  23. #endif
  24. #ifndef PARAMS
  25. # if defined PROTOTYPES || defined __STDC__
  26. # define PARAMS(Args) Args
  27. # else
  28. # define PARAMS(Args) ()
  29. # endif
  30. #endif
  31. /* Verify a requirement at compile-time (unlike assert, which is runtime). */
  32. #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
  33. #ifdef UNSIGNED
  34. # ifndef HAVE_DECL_STRTOUL
  35. "this configure-time declaration test was not run"
  36. # endif
  37. # if !HAVE_DECL_STRTOUL
  38. unsigned long strtoul PARAMS ((char const *, char **, int));
  39. # endif
  40. # ifndef HAVE_DECL_STRTOULL
  41. "this configure-time declaration test was not run"
  42. # endif
  43. # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
  44. unsigned long long strtoull PARAMS ((char const *, char **, int));
  45. # endif
  46. #else
  47. # ifndef HAVE_DECL_STRTOL
  48. "this configure-time declaration test was not run"
  49. # endif
  50. # if !HAVE_DECL_STRTOL
  51. long strtol PARAMS ((char const *, char **, int));
  52. # endif
  53. # ifndef HAVE_DECL_STRTOLL
  54. "this configure-time declaration test was not run"
  55. # endif
  56. # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
  57. long long strtoll PARAMS ((char const *, char **, int));
  58. # endif
  59. #endif
  60. #ifdef UNSIGNED
  61. # undef HAVE_LONG_LONG
  62. # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
  63. # define INT uintmax_t
  64. # define strtoimax strtoumax
  65. # define strtol strtoul
  66. # define strtoll strtoull
  67. #else
  68. # define INT intmax_t
  69. #endif
  70. INT
  71. strtoimax (char const *ptr, char **endptr, int base)
  72. {
  73. #if HAVE_LONG_LONG
  74. verify (size_is_that_of_long_or_long_long,
  75. (sizeof (INT) == sizeof (long)
  76. || sizeof (INT) == sizeof (long long)));
  77. if (sizeof (INT) != sizeof (long))
  78. return strtoll (ptr, endptr, base);
  79. #else
  80. verify (size_is_that_of_long,
  81. sizeof (INT) == sizeof (long));
  82. #endif
  83. return strtol (ptr, endptr, base);
  84. }