forked from KolibriOS/kolibrios
small polish, samples
git-svn-id: svn://kolibrios.org@6443 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
@@ -4,13 +4,17 @@
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
** itoa(n,s) - Convert n to characters in s
|
||||
** itoa(n,s) - Convert n to characters in s
|
||||
*/
|
||||
char* itoa(int n,char* s)
|
||||
char* __itoa(int n,char* s)
|
||||
{
|
||||
int sign;
|
||||
char *ptr;
|
||||
ptr = s;
|
||||
|
||||
if(n == (int)0x80000000)
|
||||
return strcpy(s, "-2147483648"); // overflowed -n
|
||||
|
||||
if ((sign = n) < 0) n = -n;
|
||||
do {
|
||||
*ptr++ = n % 10 + '0';
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
** itoab(n,s,b) - Convert "unsigned" n to characters in s using base b.
|
||||
** NOTE: This is a non-standard function.
|
||||
*/
|
||||
char* itoab(int n,char* s,int b)
|
||||
char* itoab(unsigned int n, char* s, int b)
|
||||
{
|
||||
char *ptr;
|
||||
int lowbit;
|
||||
@@ -15,7 +15,7 @@ char* itoab(int n,char* s,int b)
|
||||
b >>= 1;
|
||||
do {
|
||||
lowbit = n & 1;
|
||||
n = (n >> 1) & 32767;
|
||||
n = (n >> 1) & 0x7FFFFFFF;
|
||||
*ptr = ((n % b) << 1) + lowbit;
|
||||
if(*ptr < 10) *ptr += '0'; else *ptr += 55;
|
||||
++ptr;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define LONG_MIN (-2147483647L-1)
|
||||
#define LONG_MAX (2147483647L)
|
||||
#define ULONG_MAX (4294967295UL)
|
||||
|
||||
|
||||
int getdigit(char ch, int base)
|
||||
{
|
||||
if (isdigit(ch)) ch-= '0';
|
||||
else
|
||||
if (isalpha(ch) && ch <= 'Z') ch = 10 + ch - 'A';
|
||||
else
|
||||
if (isalpha(ch)) ch = 10 + ch - 'a';
|
||||
else
|
||||
return -1;
|
||||
|
||||
if (ch / base != 0) return -1;
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
long int strtol (const char* str, char** endptr, int base)
|
||||
{
|
||||
long int res = 0;
|
||||
int sign = 1;
|
||||
|
||||
if (base > 36)
|
||||
{
|
||||
errno = EINVAL;
|
||||
goto bye;
|
||||
}
|
||||
|
||||
while (isspace(*str)) str++;
|
||||
|
||||
if (*str == '-') { sign = -1; str++; }
|
||||
else
|
||||
if (*str == '+') str++;
|
||||
|
||||
if (base == 0 || base == 16)
|
||||
{
|
||||
if (*str == '0' && (str[1] == 'x' || str[1] == 'X'))
|
||||
{
|
||||
base = 16;
|
||||
str += 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (base == 0 && *str == '0') base = 8;
|
||||
|
||||
if (base == 0) base = 10;
|
||||
|
||||
|
||||
int digit;
|
||||
while ((digit = getdigit(*str, base)) >= 0)
|
||||
{
|
||||
res = base * res + digit;
|
||||
str++;
|
||||
if (res < 0)
|
||||
{
|
||||
errno = ERANGE;
|
||||
if (sign > 0)
|
||||
res = LONG_MAX;
|
||||
else
|
||||
res = LONG_MIN;
|
||||
}
|
||||
}
|
||||
|
||||
bye:
|
||||
if (endptr)
|
||||
*endptr = (char*)str;
|
||||
|
||||
return res * sign;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user