r6831@lvps87-230-33-50: verhaegs | 2008-02-03 14:08:57 +0100
[tangerine.git] / compiler / clib / strtoll.c
blobffb943b0b36b09bc8bddccb42ced20b42d5ef1c5
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 ANSI C function strtoll().
6 */
8 /* This requires the C99 type long long. */
10 #include <aros/system.h>
11 #if defined(AROS_HAVE_LONG_LONG)
13 #include <ctype.h>
14 #include <errno.h>
15 #ifndef AROS_NO_LIMITS_H
16 # include <limits.h>
17 #else
18 # define LLONG_MAX 0x7fffffffffffffffLL
19 # define LLONG_MIN (-0x7fffffffffffffffLL - 1)
20 #endif
22 /*****************************************************************************
24 NAME */
25 #include <stdlib.h>
27 long long strtoll (
29 /* SYNOPSIS */
30 const char * restrict str,
31 char ** restrict endptr,
32 int base)
34 /* FUNCTION
35 Convert a string of digits into an integer according to the
36 given base.
38 INPUTS
39 str - The string which should be converted. Leading
40 whitespace are ignored. The number may be prefixed
41 by a '+' or '-'. If base is above 10, then the
42 alphabetic characters from 'A' are used to specify
43 digits above 9 (ie. 'A' or 'a' is 10, 'B' or 'b' is
44 11 and so on until 'Z' or 'z' is 35).
45 endptr - If this is non-NULL, then the address of the first
46 character after the number in the string is stored
47 here.
48 base - The base for the number. May be 0 or between 2 and 36,
49 including both. 0 means to autodetect the base. strtoul()
50 selects the base by inspecting the first characters
51 of the string. If they are "0x", then base 16 is
52 assumed. If they are "0", then base 8 is assumed. Any
53 other digit will assume base 10. This is like in C.
55 If you give base 16, then an optional "0x" may
56 precede the number in the string.
58 RESULT
59 The value of the string. The first character after the number
60 is returned in *endptr, if endptr is non-NULL. If no digits can
61 be converted, *endptr contains str (if non-NULL) and 0 is
62 returned.
64 NOTES
66 EXAMPLE
67 // returns 1, ptr points to the 0-Byte
68 strtoll (" \t +0x1", &ptr, 0);
70 // Returns 15. ptr points to the a
71 strtoll ("017a", &ptr, 0);
73 // Returns 215 (5*36 + 35)
74 strtoll ("5z", &ptr, 36);
76 BUGS
78 SEE ALSO
79 strtoull()
81 INTERNALS
83 ******************************************************************************/
85 long long val = 0;
86 char * ptr;
87 char * copy;
89 while (isspace (*str))
90 str ++;
92 copy = (char *)str;
94 if (*str)
96 val = strtoull (str, &ptr, base);
98 if (endptr)
100 if (ptr == str)
101 str = copy;
102 else
103 str = ptr;
106 /* Remember: strtoull() has already done the sign conversion */
107 if (*copy == '-')
109 if ((signed long long)val > 0)
111 errno = ERANGE;
112 val = LLONG_MIN;
115 else
117 if ((signed long long)val < 0)
119 errno = ERANGE;
120 val = LLONG_MAX;
125 if (endptr)
126 *endptr = (char *)str;
128 return val;
129 } /* strtoll */
131 #endif /* AROS_HAVE_LONG_LONG */