4
0

xstrtol.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 (xstrtoimax, intmax_t)
  40. _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
  41. # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err) \
  42. do \
  43. { \
  44. switch ((Err)) \
  45. { \
  46. case LONGINT_OK: \
  47. abort (); \
  48. \
  49. case LONGINT_INVALID: \
  50. error ((Exit_code), 0, "invalid %s `%s'", \
  51. (Argument_type_string), (Str)); \
  52. break; \
  53. \
  54. case LONGINT_INVALID_SUFFIX_CHAR: \
  55. error ((Exit_code), 0, "invalid character following %s in `%s'", \
  56. (Argument_type_string), (Str)); \
  57. break; \
  58. \
  59. case LONGINT_OVERFLOW: \
  60. error ((Exit_code), 0, "%s `%s' too large", \
  61. (Argument_type_string), (Str)); \
  62. break; \
  63. } \
  64. } \
  65. while (0)
  66. # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err) \
  67. _STRTOL_ERROR (2, Str, Argument_type_string, Err)
  68. # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err) \
  69. _STRTOL_ERROR (0, Str, Argument_type_string, Err)
  70. #endif /* not XSTRTOL_H_ */