changed to wrappers for strtol(), etc.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1687 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
a358152c0a
commit
1c7200419b
@ -1,86 +1,54 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2001, Travis Geiselbrecht. All rights reserved.
|
* Copyright (c) 2002, OpenBeOS Project.
|
||||||
** Distributed under the terms of the NewOS License.
|
* All rights reserved.
|
||||||
*/
|
* Distributed under the terms of the OpenBeOS license.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* atoi.c:
|
||||||
|
* implements the standard C library functions:
|
||||||
|
* atoi, atoui, atol, atoul, atoll
|
||||||
|
* (these are all just wrappers around calls to the strto[u]l[l] functions)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Author(s):
|
||||||
|
* Daniel Reinhold (danielre@users.sf.net)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
static int hexval(char c)
|
|
||||||
|
int
|
||||||
|
atoi(const char *num)
|
||||||
{
|
{
|
||||||
if (c >= '0' && c <= '9')
|
return (int) strtol(num, NULL, 10);
|
||||||
return c - '0';
|
|
||||||
else if (c >= 'a' && c <= 'f')
|
|
||||||
return c - 'a' + 10;
|
|
||||||
else if (c >= 'A' && c <= 'F')
|
|
||||||
return c - 'A' + 10;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int atoi(const char *num)
|
|
||||||
{
|
|
||||||
int value = 0;
|
|
||||||
if (num[0] == '0' && num[1] == 'x') {
|
|
||||||
// hex
|
|
||||||
num += 2;
|
|
||||||
while (*num && isxdigit(*num))
|
|
||||||
value = value * 16 + hexval(*num++);
|
|
||||||
} else {
|
|
||||||
// decimal
|
|
||||||
while (*num && isdigit(*num))
|
|
||||||
value = value * 10 + *num++ - '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
unsigned int
|
||||||
|
atoui(const char *num)
|
||||||
|
{
|
||||||
|
return (unsigned int) strtoul(num, NULL, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int atoui(const char *num)
|
|
||||||
{
|
|
||||||
int value = 0;
|
|
||||||
if (num[0] == '0' && num[1] == 'x') {
|
|
||||||
// hex
|
|
||||||
num += 2;
|
|
||||||
while (*num && isxdigit(*num))
|
|
||||||
value = value * 16 + hexval(*num++);
|
|
||||||
} else {
|
|
||||||
// decimal
|
|
||||||
while (*num && isdigit(*num))
|
|
||||||
value = value * 10 + *num++ - '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
long
|
||||||
|
atol(const char *num)
|
||||||
|
{
|
||||||
|
return strtol(num, NULL, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
long atol(const char *num)
|
|
||||||
{
|
|
||||||
int value = 0;
|
|
||||||
if (num[0] == '0' && num[1] == 'x') {
|
|
||||||
// hex
|
|
||||||
num += 2;
|
|
||||||
while (*num && isxdigit(*num))
|
|
||||||
value = value * 16 + hexval(*num++);
|
|
||||||
} else {
|
|
||||||
// decimal
|
|
||||||
while (*num && isdigit(*num))
|
|
||||||
value = value * 10 + *num++ - '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
unsigned long
|
||||||
|
atoul(const char *num)
|
||||||
|
{
|
||||||
|
return strtoul(num, NULL, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long atoul(const char *num)
|
|
||||||
{
|
|
||||||
int value = 0;
|
|
||||||
if (num[0] == '0' && num[1] == 'x') {
|
|
||||||
// hex
|
|
||||||
num += 2;
|
|
||||||
while (*num && isxdigit(*num))
|
|
||||||
value = value * 16 + hexval(*num++);
|
|
||||||
} else {
|
|
||||||
// decimal
|
|
||||||
while (*num && isdigit(*num))
|
|
||||||
value = value * 10 + *num++ - '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
long long int
|
||||||
|
atoll(const char *num)
|
||||||
|
{
|
||||||
|
return strtoll(num, NULL, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user