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