1170754Sdelphij/* A more useful interface to strtol.
2170754Sdelphij
3170754Sdelphij   Copyright (C) 1995, 1996, 1998, 1999, 2001, 2002, 2003, 2004 Free
4170754Sdelphij   Software Foundation, Inc.
5170754Sdelphij
6170754Sdelphij   This program is free software; you can redistribute it and/or modify
7170754Sdelphij   it under the terms of the GNU General Public License as published by
8170754Sdelphij   the Free Software Foundation; either version 2, or (at your option)
9170754Sdelphij   any later version.
10170754Sdelphij
11170754Sdelphij   This program is distributed in the hope that it will be useful,
12170754Sdelphij   but WITHOUT ANY WARRANTY; without even the implied warranty of
13170754Sdelphij   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14170754Sdelphij   GNU General Public License for more details.
15170754Sdelphij
16170754Sdelphij   You should have received a copy of the GNU General Public License
17170754Sdelphij   along with this program; if not, write to the Free Software Foundation,
18170754Sdelphij   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19170754Sdelphij
20170754Sdelphij#ifndef XSTRTOL_H_
21170754Sdelphij# define XSTRTOL_H_ 1
22170754Sdelphij
23170754Sdelphij# include "exitfail.h"
24170754Sdelphij
25170754Sdelphij/* Get uintmax_t.  */
26170754Sdelphij# if HAVE_INTTYPES_H
27170754Sdelphij#  include <inttypes.h>
28170754Sdelphij# else
29170754Sdelphij#  if HAVE_STDINT_H
30170754Sdelphij#   include <stdint.h>
31170754Sdelphij#  endif
32170754Sdelphij# endif
33170754Sdelphij
34170754Sdelphij# ifndef _STRTOL_ERROR
35170754Sdelphijenum strtol_error
36170754Sdelphij  {
37170754Sdelphij    LONGINT_OK = 0,
38170754Sdelphij
39170754Sdelphij    /* These two values can be ORed together, to indicate that both
40170754Sdelphij       errors occurred.  */
41170754Sdelphij    LONGINT_OVERFLOW = 1,
42170754Sdelphij    LONGINT_INVALID_SUFFIX_CHAR = 2,
43170754Sdelphij
44170754Sdelphij    LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
45170754Sdelphij						 | LONGINT_OVERFLOW),
46170754Sdelphij    LONGINT_INVALID = 4
47170754Sdelphij  };
48170754Sdelphijtypedef enum strtol_error strtol_error;
49170754Sdelphij# endif
50170754Sdelphij
51170754Sdelphij# define _DECLARE_XSTRTOL(name, type) \
52170754Sdelphij  strtol_error name (const char *, char **, int, type *, const char *);
53170754Sdelphij_DECLARE_XSTRTOL (xstrtol, long int)
54170754Sdelphij_DECLARE_XSTRTOL (xstrtoul, unsigned long int)
55170754Sdelphij_DECLARE_XSTRTOL (xstrtoimax, intmax_t)
56170754Sdelphij_DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
57170754Sdelphij
58170754Sdelphij# define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err)	\
59170754Sdelphij  do									\
60170754Sdelphij    {									\
61170754Sdelphij      switch ((Err))							\
62170754Sdelphij	{								\
63170754Sdelphij	default:							\
64170754Sdelphij	  abort ();							\
65170754Sdelphij									\
66170754Sdelphij	case LONGINT_INVALID:						\
67170754Sdelphij	  error ((Exit_code), 0, "invalid %s `%s'",			\
68170754Sdelphij		 (Argument_type_string), (Str));			\
69170754Sdelphij	  break;							\
70170754Sdelphij									\
71170754Sdelphij	case LONGINT_INVALID_SUFFIX_CHAR:				\
72170754Sdelphij	case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW:		\
73170754Sdelphij	  error ((Exit_code), 0, "invalid character following %s in `%s'", \
74170754Sdelphij		 (Argument_type_string), (Str));			\
75170754Sdelphij	  break;							\
76170754Sdelphij									\
77170754Sdelphij	case LONGINT_OVERFLOW:						\
78170754Sdelphij	  error ((Exit_code), 0, "%s `%s' too large",			\
79170754Sdelphij		 (Argument_type_string), (Str));			\
80170754Sdelphij	  break;							\
81170754Sdelphij	}								\
82170754Sdelphij    }									\
83170754Sdelphij  while (0)
84170754Sdelphij
85170754Sdelphij# define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err)		\
86170754Sdelphij  _STRTOL_ERROR (exit_failure, Str, Argument_type_string, Err)
87170754Sdelphij
88170754Sdelphij# define STRTOL_FAIL_WARN(Str, Argument_type_string, Err)		\
89170754Sdelphij  _STRTOL_ERROR (0, Str, Argument_type_string, Err)
90170754Sdelphij
91170754Sdelphij#endif /* not XSTRTOL_H_ */
92