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