More consistent inttypes.h / stdint.h handling.
[gnulib.git] / lib / strtoimax.c
1 /* Convert string representation of a number into an intmax_t value.
2    Copyright 1999, 2001, 2002 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 /* Written by Paul Eggert. */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if HAVE_INTTYPES_H
25 # include <inttypes.h>
26 #else
27 # if HAVE_STDINT_H
28 #  include <stdint.h>
29 # endif
30 #endif
31
32 #if HAVE_STDLIB_H
33 # include <stdlib.h>
34 #endif
35
36 #ifndef PARAMS
37 # if defined PROTOTYPES || defined __STDC__
38 #  define PARAMS(Args) Args
39 # else
40 #  define PARAMS(Args) ()
41 # endif
42 #endif
43
44 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
45 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
46
47 #ifdef UNSIGNED
48 # ifndef HAVE_DECL_STRTOUL
49 "this configure-time declaration test was not run"
50 # endif
51 # if !HAVE_DECL_STRTOUL
52 unsigned long strtoul PARAMS ((char const *, char **, int));
53 # endif
54 # ifndef HAVE_DECL_STRTOULL
55 "this configure-time declaration test was not run"
56 # endif
57 # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
58 unsigned long long strtoull PARAMS ((char const *, char **, int));
59 # endif
60
61 #else
62
63 # ifndef HAVE_DECL_STRTOL
64 "this configure-time declaration test was not run"
65 # endif
66 # if !HAVE_DECL_STRTOL
67 long strtol PARAMS ((char const *, char **, int));
68 # endif
69 # ifndef HAVE_DECL_STRTOLL
70 "this configure-time declaration test was not run"
71 # endif
72 # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
73 long long strtoll PARAMS ((char const *, char **, int));
74 # endif
75 #endif
76
77 #ifdef UNSIGNED
78 # undef HAVE_LONG_LONG
79 # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
80 # define INT uintmax_t
81 # define strtoimax strtoumax
82 # define strtol strtoul
83 # define strtoll strtoull
84 #else
85 # define INT intmax_t
86 #endif
87
88 INT
89 strtoimax (char const *ptr, char **endptr, int base)
90 {
91 #if HAVE_LONG_LONG
92   verify (size_is_that_of_long_or_long_long,
93           (sizeof (INT) == sizeof (long)
94            || sizeof (INT) == sizeof (long long)));
95
96   if (sizeof (INT) != sizeof (long))
97     return strtoll (ptr, endptr, base);
98 #else
99   verify (size_is_that_of_long,
100           sizeof (INT) == sizeof (long));
101 #endif
102
103   return strtol (ptr, endptr, base);
104 }