Factored to work both for unsigned and signed types, ...
[gnulib.git] / lib / strtoumax.c
1 /* Convert string representation of a number into an uintmax_t value.
2    Copyright 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 /* 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 #endif
27
28 #if HAVE_STDLIB_H
29 # include <stdlib.h>
30 #endif
31
32 #ifndef PARAMS
33 # if defined PROTOTYPES || defined __STDC__
34 #  define PARAMS(Args) Args
35 # else
36 #  define PARAMS(Args) ()
37 # endif
38 #endif
39
40 #ifdef STRTOUXMAX_UNSIGNED
41 # ifndef HAVE_DECL_STRTOUL
42 "this configure-time declaration test was not run"
43 # endif
44 # if !HAVE_DECL_STRTOUL
45 unsigned long strtoul PARAMS ((char const *, char **, int));
46 # endif
47 # ifndef HAVE_DECL_STRTOULL
48 "this configure-time declaration test was not run"
49 # endif
50 # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
51 unsigned long long strtoull PARAMS ((char const *, char **, int));
52 # endif
53
54 #else
55
56 # ifndef HAVE_DECL_STRTOL
57 "this configure-time declaration test was not run"
58 # endif
59 # if !HAVE_DECL_STRTOL
60 long strtol PARAMS ((char const *, char **, int));
61 # endif
62 # ifndef HAVE_DECL_STRTOLL
63 "this configure-time declaration test was not run"
64 # endif
65 # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
66 long long strtoll PARAMS ((char const *, char **, int));
67 # endif
68 #endif
69
70 #ifndef STRTOUXMAX_UNSIGNED
71 # define strtoumax strtoimax
72 # define uintmax_t intmax_t
73 # define strtoull strtoll
74 # define strtoul strtol
75 #endif
76
77 uintmax_t
78 strtoumax (char const *ptr, char **endptr, int base)
79 {
80 #define USE_IF_EQUIVALENT(function) \
81     if (sizeof (uintmax_t) == sizeof function (ptr, endptr, base)) \
82       return function (ptr, endptr, base);
83
84 #if HAVE_UNSIGNED_LONG_LONG
85     USE_IF_EQUIVALENT (strtoull)
86 #endif
87
88   USE_IF_EQUIVALENT (strtoul)
89
90   abort ();
91 }
92
93 #ifdef TESTING
94 # include <stdio.h>
95 int
96 main ()
97 {
98   char *p, *endptr;
99   printf ("sizeof xintmax_t: %d\n", sizeof (uintmax_t));
100   printf ("sizeof strtoxll(): %d\n", sizeof strtoull(p, &endptr, 10));
101   printf ("sizeof strtoxl(): %d\n", sizeof strtoul(p, &endptr, 10));
102   exit (0);
103 }
104 #endif