xref: /freebsd-src/contrib/diff/lib/xstrtol.h (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
1*18fd37a7SXin LI /* A more useful interface to strtol.
2*18fd37a7SXin LI 
3*18fd37a7SXin LI    Copyright (C) 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004 Free
4*18fd37a7SXin LI    Software Foundation, Inc.
5*18fd37a7SXin LI 
6*18fd37a7SXin LI    This program is free software; you can redistribute it and/or modify
7*18fd37a7SXin LI    it under the terms of the GNU General Public License as published by
8*18fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
9*18fd37a7SXin LI    any later version.
10*18fd37a7SXin LI 
11*18fd37a7SXin LI    This program is distributed in the hope that it will be useful,
12*18fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*18fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*18fd37a7SXin LI    GNU General Public License for more details.
15*18fd37a7SXin LI 
16*18fd37a7SXin LI    You should have received a copy of the GNU General Public License
17*18fd37a7SXin LI    along with this program; if not, write to the Free Software Foundation,
18*18fd37a7SXin LI    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19*18fd37a7SXin LI 
20*18fd37a7SXin LI #ifndef XSTRTOL_H_
21*18fd37a7SXin LI # define XSTRTOL_H_ 1
22*18fd37a7SXin LI 
23*18fd37a7SXin LI # include "exitfail.h"
24*18fd37a7SXin LI 
25*18fd37a7SXin LI /* Get uintmax_t.  */
26*18fd37a7SXin LI # if HAVE_INTTYPES_H
27*18fd37a7SXin LI #  include <inttypes.h>
28*18fd37a7SXin LI # else
29*18fd37a7SXin LI #  if HAVE_STDINT_H
30*18fd37a7SXin LI #   include <stdint.h>
31*18fd37a7SXin LI #  endif
32*18fd37a7SXin LI # endif
33*18fd37a7SXin LI 
34*18fd37a7SXin LI # ifndef _STRTOL_ERROR
35*18fd37a7SXin LI enum strtol_error
36*18fd37a7SXin LI   {
37*18fd37a7SXin LI     LONGINT_OK = 0,
38*18fd37a7SXin LI 
39*18fd37a7SXin LI     /* These two values can be ORed together, to indicate that both
40*18fd37a7SXin LI        errors occurred.  */
41*18fd37a7SXin LI     LONGINT_OVERFLOW = 1,
42*18fd37a7SXin LI     LONGINT_INVALID_SUFFIX_CHAR = 2,
43*18fd37a7SXin LI 
44*18fd37a7SXin LI     LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
45*18fd37a7SXin LI 						 | LONGINT_OVERFLOW),
46*18fd37a7SXin LI     LONGINT_INVALID = 4
47*18fd37a7SXin LI   };
48*18fd37a7SXin LI typedef enum strtol_error strtol_error;
49*18fd37a7SXin LI # endif
50*18fd37a7SXin LI 
51*18fd37a7SXin LI # define _DECLARE_XSTRTOL(name, type) \
52*18fd37a7SXin LI   strtol_error name (const char *, char **, int, type *, const char *);
53*18fd37a7SXin LI _DECLARE_XSTRTOL (xstrtol, long int)
54*18fd37a7SXin LI _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
55*18fd37a7SXin LI _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
56*18fd37a7SXin LI _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
57*18fd37a7SXin LI 
58*18fd37a7SXin LI # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err)	\
59*18fd37a7SXin LI   do									\
60*18fd37a7SXin LI     {									\
61*18fd37a7SXin LI       switch ((Err))							\
62*18fd37a7SXin LI 	{								\
63*18fd37a7SXin LI 	default:							\
64*18fd37a7SXin LI 	  abort ();							\
65*18fd37a7SXin LI 									\
66*18fd37a7SXin LI 	case LONGINT_INVALID:						\
67*18fd37a7SXin LI 	  error ((Exit_code), 0, "invalid %s `%s'",			\
68*18fd37a7SXin LI 		 (Argument_type_string), (Str));			\
69*18fd37a7SXin LI 	  break;							\
70*18fd37a7SXin LI 									\
71*18fd37a7SXin LI 	case LONGINT_INVALID_SUFFIX_CHAR:				\
72*18fd37a7SXin LI 	case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW:		\
73*18fd37a7SXin LI 	  error ((Exit_code), 0, "invalid character following %s in `%s'", \
74*18fd37a7SXin LI 		 (Argument_type_string), (Str));			\
75*18fd37a7SXin LI 	  break;							\
76*18fd37a7SXin LI 									\
77*18fd37a7SXin LI 	case LONGINT_OVERFLOW:						\
78*18fd37a7SXin LI 	  error ((Exit_code), 0, "%s `%s' too large",			\
79*18fd37a7SXin LI 		 (Argument_type_string), (Str));			\
80*18fd37a7SXin LI 	  break;							\
81*18fd37a7SXin LI 	}								\
82*18fd37a7SXin LI     }									\
83*18fd37a7SXin LI   while (0)
84*18fd37a7SXin LI 
85*18fd37a7SXin LI # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err)		\
86*18fd37a7SXin LI   _STRTOL_ERROR (exit_failure, Str, Argument_type_string, Err)
87*18fd37a7SXin LI 
88*18fd37a7SXin LI # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err)		\
89*18fd37a7SXin LI   _STRTOL_ERROR (0, Str, Argument_type_string, Err)
90*18fd37a7SXin LI 
91*18fd37a7SXin LI #endif /* not XSTRTOL_H_ */
92