1 | /* A more useful interface to strtol.
|
---|
2 | Copyright 1995, 1996, 1998, 1999, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
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 2, or (at your option)
|
---|
7 | any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software Foundation,
|
---|
16 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
---|
17 |
|
---|
18 | #ifndef XSTRTOL_H_
|
---|
19 | # define XSTRTOL_H_ 1
|
---|
20 |
|
---|
21 | # if HAVE_INTTYPES_H
|
---|
22 | # include <inttypes.h> /* for uintmax_t */
|
---|
23 | # endif
|
---|
24 |
|
---|
25 | # ifndef PARAMS
|
---|
26 | # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
|
---|
27 | # define PARAMS(Args) Args
|
---|
28 | # else
|
---|
29 | # define PARAMS(Args) ()
|
---|
30 | # endif
|
---|
31 | # endif
|
---|
32 |
|
---|
33 | # ifndef _STRTOL_ERROR
|
---|
34 | enum strtol_error
|
---|
35 | {
|
---|
36 | LONGINT_OK, LONGINT_INVALID, LONGINT_INVALID_SUFFIX_CHAR, LONGINT_OVERFLOW
|
---|
37 | };
|
---|
38 | typedef enum strtol_error strtol_error;
|
---|
39 | # endif
|
---|
40 |
|
---|
41 | # define _DECLARE_XSTRTOL(name, type) \
|
---|
42 | strtol_error \
|
---|
43 | name PARAMS ((const char *s, char **ptr, int base, \
|
---|
44 | type *val, const char *valid_suffixes));
|
---|
45 | _DECLARE_XSTRTOL (xstrtol, long int)
|
---|
46 | _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
|
---|
47 | _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
|
---|
48 | _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
|
---|
49 |
|
---|
50 | # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err) \
|
---|
51 | do \
|
---|
52 | { \
|
---|
53 | switch ((Err)) \
|
---|
54 | { \
|
---|
55 | case LONGINT_OK: \
|
---|
56 | abort (); \
|
---|
57 | \
|
---|
58 | case LONGINT_INVALID: \
|
---|
59 | error ((Exit_code), 0, "invalid %s `%s'", \
|
---|
60 | (Argument_type_string), (Str)); \
|
---|
61 | break; \
|
---|
62 | \
|
---|
63 | case LONGINT_INVALID_SUFFIX_CHAR: \
|
---|
64 | error ((Exit_code), 0, "invalid character following %s in `%s'", \
|
---|
65 | (Argument_type_string), (Str)); \
|
---|
66 | break; \
|
---|
67 | \
|
---|
68 | case LONGINT_OVERFLOW: \
|
---|
69 | error ((Exit_code), 0, "%s `%s' too large", \
|
---|
70 | (Argument_type_string), (Str)); \
|
---|
71 | break; \
|
---|
72 | } \
|
---|
73 | } \
|
---|
74 | while (0)
|
---|
75 |
|
---|
76 | # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err) \
|
---|
77 | _STRTOL_ERROR (2, Str, Argument_type_string, Err)
|
---|
78 |
|
---|
79 | # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err) \
|
---|
80 | _STRTOL_ERROR (0, Str, Argument_type_string, Err)
|
---|
81 |
|
---|
82 | #endif /* not XSTRTOL_H_ */
|
---|