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, int64_t or uint64_t.
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(DRIZZLE_SERVER_GLOBAL_H) || !defined(_m_string_h)
39
# error Calling file must include 'drizzled/global.h' and 'mystrings/m_string.h'
40
/* see 'strtoll.c' and 'strtoull.c' for the reasons */
43
#include "mysys/mysys_priv.h"
50
#define TYPE_MIN LONGLONG_MIN
51
#define TYPE_MAX LONGLONG_MAX
52
#define longtype int64_t
53
#define ulongtype uint64_t
55
#define function ulongtype strtoull
57
#define function longtype strtoll
60
#define TYPE_MIN LONG_MIN
61
#define TYPE_MAX LONG_MAX
63
#define ulongtype unsigned long
65
#define function ulongtype strtoul
67
#define function longtype strtol
72
/* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
73
If BASE is 0 the base is determined by the presence of a leading
74
zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
75
If BASE is < 2 or > 36, it is reset to 10.
76
If ENDPTR is not NULL, a pointer to the character after the last
77
one converted is stored in *ENDPTR. */
80
function (const char *nptr,char **endptr,int base)
83
register ulongtype cutoff;
84
register unsigned int cutlim;
86
register const char *s;
87
register unsigned char c;
91
if (base < 0 || base == 1 || base > 36)
96
/* Skip white space. */
97
while (my_isspace(&my_charset_utf8_general_ci, *s))
104
/* Check for a sign. */
117
if (base == 16 && s[0] == '0' && my_toupper (&my_charset_utf8_general_ci, s[1]) == 'X')
120
/* If BASE is zero, figure it out ourselves. */
125
if (my_toupper (&my_charset_utf8_general_ci, s[1]) == 'X')
137
/* Save the pointer so we can check later if anything happened. */
140
cutoff = UINT64_MAX / (unsigned long int) base;
141
cutlim = (uint32_t) (UINT64_MAX % (unsigned long int) base);
145
for (c = *s; c != '\0'; c = *++s)
147
if (my_isdigit (&my_charset_utf8_general_ci, c))
149
else if (my_isalpha (&my_charset_utf8_general_ci, c))
150
c = my_toupper (&my_charset_utf8_general_ci, c) - 'A' + 10;
155
/* Check for overflow. */
156
if (i > cutoff || (i == cutoff && c > cutlim))
160
i *= (ulongtype) base;
165
/* Check if anything actually happened. */
169
/* Store in ENDPTR the address of one character
170
past the last character we converted. */
172
*endptr = (char *) s;
175
/* Check for a value that is within the range of
176
`unsigned long int', but outside the range of `long int'. */
179
if (i > (ulongtype) TYPE_MIN)
182
else if (i > (ulongtype) TYPE_MAX)
192
return negative ? TYPE_MIN : TYPE_MAX;
196
/* Return the result of the appropriate sign. */
197
return (negative ? -((longtype) i) : (longtype) i);
200
/* There was no number to convert. */
203
*endptr = (char *) nptr;