basic HTTP GET response
This commit is contained in:
50
srcs/ft_itoa.cpp
Normal file
50
srcs/ft_itoa.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
static int eval_is_negative(int *n)
|
||||
{
|
||||
if (*n < 0)
|
||||
{
|
||||
*n = *n * -1;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int eval_digit_nbr(int n)
|
||||
{
|
||||
int digit_nbr;
|
||||
|
||||
if (n == 0)
|
||||
return (1);
|
||||
digit_nbr = 0;
|
||||
while (n != 0)
|
||||
{
|
||||
digit_nbr++;
|
||||
n = n / 10;
|
||||
}
|
||||
return (digit_nbr);
|
||||
}
|
||||
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
int i;
|
||||
char *str;
|
||||
int is_negative;
|
||||
|
||||
if (n == -2147483648)
|
||||
return (strdup("-2147483648"));
|
||||
is_negative = eval_is_negative(&n);
|
||||
i = eval_digit_nbr(n) + is_negative;
|
||||
str = new char[i+1];
|
||||
if (is_negative)
|
||||
str[0] = '-';
|
||||
str[i] = '\0';
|
||||
while (i > 0 + is_negative)
|
||||
{
|
||||
i--;
|
||||
str[i] = (n % 10) + '0';
|
||||
n = n / 10;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
Reference in New Issue
Block a user