Renamed from strtoxmax.c, removing the old strtoimax.c.
authorJim Meyering <jim@meyering.net>
Fri, 31 Aug 2001 09:37:58 +0000 (09:37 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 31 Aug 2001 09:37:58 +0000 (09:37 +0000)
Also, make the following further changes to make this file's
configuration more similar to that of strtol.c:
(UNSIGNED): Renamed from STRTOUXMAX_UNSIGNED.  All uses changed.
(strtoumax, uintmax_t, strtoull, strtol): Remove.
(intmax_t, strtoimax, strtol, strtoll): New macros, if UNSIGNED.
(strtoimax): Renamed from strtoumax.  All uses of unsigned values
changed to signed values.

And make the following changes as well:
Fix copyright notice, as 1999 was missing.
(verify): New macro.
(strtoimax): Check sizes at compile-time, not run-time.
Prefer strtol to strtoll if both work.
(main): Remove; it was not that useful and was a pain to maintain.

lib/strtoimax.c

index d3cd027..cda6b44 100644 (file)
@@ -1 +1,98 @@
-#include "strtoxmax.c"
+/* Convert string representation of a number into an intmax_t value.
+   Copyright 1999, 2001 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+/* Written by Paul Eggert. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#if HAVE_INTTYPES_H
+# include <inttypes.h>
+#endif
+
+#if HAVE_STDLIB_H
+# include <stdlib.h>
+#endif
+
+#ifndef PARAMS
+# if defined PROTOTYPES || defined __STDC__
+#  define PARAMS(Args) Args
+# else
+#  define PARAMS(Args) ()
+# endif
+#endif
+
+/* Verify a requirement at compile-time (unlike assert, which is runtime).  */
+#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
+
+#ifdef UNSIGNED
+# ifndef HAVE_DECL_STRTOUL
+"this configure-time declaration test was not run"
+# endif
+# if !HAVE_DECL_STRTOUL
+unsigned long strtoul PARAMS ((char const *, char **, int));
+# endif
+# ifndef HAVE_DECL_STRTOULL
+"this configure-time declaration test was not run"
+# endif
+# if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
+unsigned long long strtoull PARAMS ((char const *, char **, int));
+# endif
+
+#else
+
+# ifndef HAVE_DECL_STRTOL
+"this configure-time declaration test was not run"
+# endif
+# if !HAVE_DECL_STRTOL
+long strtol PARAMS ((char const *, char **, int));
+# endif
+# ifndef HAVE_DECL_STRTOLL
+"this configure-time declaration test was not run"
+# endif
+# if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
+long long strtoll PARAMS ((char const *, char **, int));
+# endif
+#endif
+
+#ifdef UNSIGNED
+# define INT uintmax_t
+# define strtoimax strtoumax
+# define strtol strtoul
+# define strtoll strtoull
+#else
+# define INT intmax_t
+#endif
+
+INT
+strtoimax (char const *ptr, char **endptr, int base)
+{
+#if HAVE_UNSIGNED_LONG_LONG
+  verify (size_is_that_of_long_or_long_long,
+         (sizeof (INT) == sizeof strtol (ptr, endptr, base)
+          || sizeof (INT) == sizeof strtoll (ptr, endptr, base)));
+
+  if (sizeof (INT) != sizeof strtol (ptr, endptr, base))
+    return strtoll (ptr, endptr, base);
+#else
+  verify (size_is_that_of_long,
+         sizeof (INT) == sizeof strtol (ptr, endptr, base));
+#endif
+
+  return strtol (ptr, endptr, base);
+}