mirror of
https://github.com/adtools/clib2.git
synced 2025-12-08 14:59:05 +00:00
strtoul() and strtoull() now count how many digits were converted.
If that number is zero, then the conversion is considered to have
failed: 0 is returned, errno is set to ERANGE and the 'ptr' parameter
reference is initialized with a pointer to the string to be converted.
This has two consequences:
1) A floating point number that has no digits in the significand
is no longer converted to zero. For example ".e10" can no longer
result in a successful conversion.
2) You can detect if a conversion is successful by comparing pointers
after the conversion function has returned:
char * str;
char * ptr;
long n;
n = strtol(str,&ptr,10);
if(n == 0 && str == ptr)
printf("conversion has failed");
git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15030 87f5fb63-7c3d-0410-a384-fd976d0f7a62
205 lines
4.2 KiB
C
205 lines
4.2 KiB
C
/*
|
|
* $Id: inttypes_strtoumax.c,v 1.2 2005-09-28 09:28:39 obarthel Exp $
|
|
*
|
|
* :ts=4
|
|
*
|
|
* Portable ISO 'C' (1994) runtime library for the Amiga computer
|
|
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* - Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
*
|
|
* - Neither the name of Olaf Barthel nor the names of contributors
|
|
* may be used to endorse or promote products derived from this
|
|
* software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef _STDLIB_NULL_POINTER_CHECK_H
|
|
#include "stdlib_null_pointer_check.h"
|
|
#endif /* _STDLIB_NULL_POINTER_CHECK_H */
|
|
|
|
/****************************************************************************/
|
|
|
|
#ifndef _INTTYPES_HEADERS_H
|
|
#include "inttypes_headers.h"
|
|
#endif /* _INTTYPES_HEADERS_H */
|
|
|
|
/****************************************************************************/
|
|
|
|
uintmax_t
|
|
strtoumax(const char *str, char **ptr, int base)
|
|
{
|
|
const char * stop = str;
|
|
size_t num_digits_converted = 0;
|
|
BOOL is_negative;
|
|
uintmax_t result = 0;
|
|
uintmax_t new_sum;
|
|
uintmax_t sum;
|
|
char c;
|
|
|
|
ENTER();
|
|
|
|
SHOWSTRING(str);
|
|
SHOWPOINTER(ptr);
|
|
SHOWVALUE(base);
|
|
|
|
assert(str != NULL && base >= 0);
|
|
|
|
#if defined(CHECK_FOR_NULL_POINTERS)
|
|
{
|
|
if(str == NULL)
|
|
{
|
|
SHOWMSG("invalid str parameter");
|
|
|
|
__set_errno(EFAULT);
|
|
goto out;
|
|
}
|
|
}
|
|
#endif /* CHECK_FOR_NULL_POINTERS */
|
|
|
|
if(base < 0)
|
|
{
|
|
SHOWMSG("invalid base parameter");
|
|
|
|
__set_errno(ERANGE);
|
|
goto out;
|
|
}
|
|
|
|
/* Skip all leading blanks. */
|
|
while((c = (*str)) != '\0')
|
|
{
|
|
if(NOT isspace(c))
|
|
break;
|
|
|
|
str++;
|
|
}
|
|
|
|
/* The first character may be a sign. */
|
|
if(c == '-')
|
|
{
|
|
/* It's a negative number. */
|
|
is_negative = TRUE;
|
|
|
|
str++;
|
|
}
|
|
else
|
|
{
|
|
/* It's not going to be negative. */
|
|
is_negative = FALSE;
|
|
|
|
/* But there may be a sign we will choose to
|
|
ignore. */
|
|
if(c == '+')
|
|
str++;
|
|
}
|
|
|
|
c = (*str);
|
|
|
|
/* There may be a leading '0x' to indicate that what
|
|
follows is a hexadecimal number. */
|
|
if(base == 0 || base == 16)
|
|
{
|
|
if((c == '0') && (str[1] == 'x' || str[1] == 'X'))
|
|
{
|
|
base = 16;
|
|
|
|
str += 2;
|
|
|
|
c = (*str);
|
|
}
|
|
}
|
|
|
|
/* If we still don't know what base to use and the
|
|
next letter to follow is a zero then this is
|
|
probably a number in octal notation. */
|
|
if(base == 0)
|
|
{
|
|
if(c == '0')
|
|
base = 8;
|
|
else
|
|
base = 10;
|
|
}
|
|
|
|
sum = 0;
|
|
|
|
if(1 <= base && base <= 36)
|
|
{
|
|
while(c != '\0')
|
|
{
|
|
if('0' <= c && c <= '9')
|
|
c -= '0';
|
|
else if ('a' <= c)
|
|
c -= 'a' - 10;
|
|
else if ('A' <= c)
|
|
c -= 'A' - 10;
|
|
else
|
|
break;
|
|
|
|
/* Ignore invalid numbers. */
|
|
if(c >= base)
|
|
break;
|
|
|
|
new_sum = base * sum + c;
|
|
if(new_sum < sum) /* overflow? */
|
|
{
|
|
__set_errno(ERANGE);
|
|
|
|
result = UINTMAX_MAX;
|
|
|
|
goto out;
|
|
}
|
|
|
|
sum = new_sum;
|
|
|
|
str++;
|
|
|
|
c = (*str);
|
|
|
|
/* Remember where the conversion stopped and
|
|
that we converted something. */
|
|
stop = str;
|
|
|
|
num_digits_converted++;
|
|
}
|
|
}
|
|
|
|
/* Did we convert anything? */
|
|
if(num_digits_converted == 0)
|
|
{
|
|
__set_errno(ERANGE);
|
|
goto out;
|
|
}
|
|
|
|
if(is_negative)
|
|
result = (-sum);
|
|
else
|
|
result = sum;
|
|
|
|
out:
|
|
|
|
/* If desired, remember where we stopped reading the
|
|
number from the buffer. */
|
|
if(ptr != NULL)
|
|
(*ptr) = (char *)stop;
|
|
|
|
RETURN(result);
|
|
return(result);
|
|
}
|