95 lines
1.9 KiB
C
95 lines
1.9 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_atoi_base.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/07/19 14:48:20 by cchauvet #+# #+# */
|
|
/* Updated: 2022/07/25 18:52:34 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
int ft_baser(char *str)
|
|
{
|
|
int i;
|
|
int j;
|
|
int count;
|
|
|
|
i = -1;
|
|
while (str[++i] != 0)
|
|
{
|
|
count = 0;
|
|
j = 0;
|
|
while (str[j] != 0)
|
|
if (str[i] == str[j++])
|
|
count++;
|
|
if (count > 1 || str[i] == '+' || str[i] == '-')
|
|
return (0);
|
|
}
|
|
if (i == 1)
|
|
return (0);
|
|
return (i);
|
|
}
|
|
|
|
int ft_find(char *str, char to_find)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (str[i] != 0)
|
|
{
|
|
if (str[i] == to_find)
|
|
return (i);
|
|
i++;
|
|
}
|
|
return (-1);
|
|
}
|
|
|
|
int ft_whitespaces(char *str)
|
|
{
|
|
char *space;
|
|
int j;
|
|
int i;
|
|
|
|
space = "\n\r\v\t\n ";
|
|
i = 0;
|
|
j = 0;
|
|
while (space[i] != 0)
|
|
{
|
|
while (str[j] == space[i])
|
|
{
|
|
if (str[j] == 0)
|
|
return (j);
|
|
i = 0;
|
|
j++;
|
|
}
|
|
i++;
|
|
}
|
|
return (j);
|
|
}
|
|
|
|
int ft_atoi_base(char *str, char *base)
|
|
{
|
|
int base_size;
|
|
int sign;
|
|
int nbr;
|
|
int i;
|
|
|
|
nbr = 0;
|
|
sign = 1;
|
|
base_size = ft_baser(base);
|
|
if (base == 0)
|
|
return (0);
|
|
i = ft_whitespaces(str);
|
|
while (str[i] == '+' || str[i] == '-')
|
|
if (str[i++] == '-')
|
|
sign = sign * -1;
|
|
while (ft_find(base, str[i]) != -1)
|
|
{
|
|
nbr = nbr * base_size + ft_find(base, str[i]);
|
|
str++;
|
|
}
|
|
return (nbr * sign);
|
|
}
|