xstrtol.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* A more useful interface to strtol.
  2. Copyright 1995, 1996, 1998, 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. #ifndef XSTRTOL_H_
  15. # define XSTRTOL_H_ 1
  16. # if HAVE_INTTYPES_H
  17. # include <inttypes.h> /* for uintmax_t */
  18. # endif
  19. # ifndef PARAMS
  20. # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
  21. # define PARAMS(Args) Args
  22. # else
  23. # define PARAMS(Args) ()
  24. # endif
  25. # endif
  26. # ifndef _STRTOL_ERROR
  27. enum strtol_error
  28. {
  29. LONGINT_OK, LONGINT_INVALID, LONGINT_INVALID_SUFFIX_CHAR, LONGINT_OVERFLOW
  30. };
  31. typedef enum strtol_error strtol_error;
  32. # endif
  33. # define _DECLARE_XSTRTOL(name, type) \
  34. strtol_error \
  35. name PARAMS ((const char *s, char **ptr, int base, \
  36. type *val, const char *valid_suffixes));
  37. _DECLARE_XSTRTOL (xstrtol, long int)
  38. _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
  39. _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
  40. # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err) \
  41. do \
  42. { \
  43. switch ((Err)) \
  44. { \
  45. case LONGINT_OK: \
  46. abort (); \
  47. \
  48. case LONGINT_INVALID: \
  49. error ((Exit_code), 0, "invalid %s `%s'", \
  50. (Argument_type_string), (Str)); \
  51. break; \
  52. \
  53. case LONGINT_INVALID_SUFFIX_CHAR: \
  54. error ((Exit_code), 0, "invalid character following %s in `%s'", \
  55. (Argument_type_string), (Str)); \
  56. break; \
  57. \
  58. case LONGINT_OVERFLOW: \
  59. error ((Exit_code), 0, "%s `%s' too large", \
  60. (Argument_type_string), (Str)); \
  61. break; \
  62. } \
  63. } \
  64. while (0)
  65. # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err) \
  66. _STRTOL_ERROR (2, Str, Argument_type_string, Err)
  67. # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err) \
  68. _STRTOL_ERROR (0, Str, Argument_type_string, Err)
  69. #endif /* not XSTRTOL_H_ */