1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
strtol,strtoul,strtoll,strtoull
18
convert string to long, unsigned long, long long or unsigned long long.
19
strtoxx(char *src,char **ptr,int base)
20
converts the string pointed to by src to an long of appropriate long and
21
returnes it. It skips leading spaces and tabs (but not newlines, formfeeds,
22
backspaces), then it accepts an optional sign and a sequence of digits
23
in the specified radix.
24
If the value of ptr is not (char **)NULL, a pointer to the character
25
terminating the scan is returned in the location pointed to by ptr.
26
Trailing spaces will NOT be skipped.
28
If an error is detected, the result will be LONG_MIN, 0 or LONG_MAX,
29
(or LONGLONG..) and errno will be set to
30
EDOM if there are no digits
31
ERANGE if the result would overflow.
32
the ptr will be set to src.
33
This file is based on the strtol from the the GNU C Library.
34
it can be compiled with the UNSIGNED and/or LONGLONG flag set
38
#if !defined(_global_h) || !defined(_m_string_h)
39
# error Calling file must include 'my_global.h' and 'm_string.h'
40
/* see 'strtoll.c' and 'strtoull.c' for the reasons */
44
#include "my_sys.h" /* defines errno */
52
#define UTYPE_MAX (~(ulonglong) 0)
53
#define TYPE_MIN LONGLONG_MIN
54
#define TYPE_MAX LONGLONG_MAX
55
#define longtype longlong
56
#define ulongtype ulonglong
58
#define function ulongtype strtoull
60
#define function longtype strtoll
63
#define UTYPE_MAX (ulong) ~0L
64
#define TYPE_MIN LONG_MIN
65
#define TYPE_MAX LONG_MAX
67
#define ulongtype unsigned long
69
#define function ulongtype strtoul
71
#define function longtype strtol
76
/* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
77
If BASE is 0 the base is determined by the presence of a leading
78
zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
79
If BASE is < 2 or > 36, it is reset to 10.
80
If ENDPTR is not NULL, a pointer to the character after the last
81
one converted is stored in *ENDPTR. */
84
function (const char *nptr,char **endptr,int base)
87
register ulongtype cutoff;
88
register unsigned int cutlim;
90
register const char *s;
95
if (base < 0 || base == 1 || base > 36)
100
/* Skip white space. */
101
while (my_isspace(&my_charset_latin1, *s))
108
/* Check for a sign. */
121
if (base == 16 && s[0] == '0' && my_toupper (&my_charset_latin1, s[1]) == 'X')
124
/* If BASE is zero, figure it out ourselves. */
129
if (my_toupper (&my_charset_latin1, s[1]) == 'X')
141
/* Save the pointer so we can check later if anything happened. */
144
cutoff = UTYPE_MAX / (unsigned long int) base;
145
cutlim = (uint) (UTYPE_MAX % (unsigned long int) base);
149
for (c = *s; c != '\0'; c = *++s)
151
if (my_isdigit (&my_charset_latin1, c))
153
else if (my_isalpha (&my_charset_latin1, c))
154
c = my_toupper (&my_charset_latin1, c) - 'A' + 10;
159
/* Check for overflow. */
160
if (i > cutoff || (i == cutoff && c > cutlim))
164
i *= (ulongtype) base;
169
/* Check if anything actually happened. */
173
/* Store in ENDPTR the address of one character
174
past the last character we converted. */
176
*endptr = (char *) s;
179
/* Check for a value that is within the range of
180
`unsigned long int', but outside the range of `long int'. */
183
if (i > (ulongtype) TYPE_MIN)
186
else if (i > (ulongtype) TYPE_MAX)
196
return negative ? TYPE_MIN : TYPE_MAX;
200
/* Return the result of the appropriate sign. */
201
return (negative ? -((longtype) i) : (longtype) i);
204
/* There was no number to convert. */
207
*endptr = (char *) nptr;