42-archive/traceroute/libft/srcs/str/ft_atoi.c
2017-10-08 17:57:08 +02:00

51 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/08/03 16:17:21 by jhalford #+# #+# */
/* Updated: 2017/03/22 22:21:21 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_iswhitespace(char c)
{
if (c == ' ' || c == '\t' || c == '\n')
return (1);
else if (c == '\v' || c == '\f' || c == '\r')
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
int res;
int sign;
if (!str || !*str)
return (0);
i = 0;
res = 0;
sign = 1;
while (ft_iswhitespace(str[i]))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i + 1] >= '0' && str[i + 1] <= '9')
{
sign = (str[i] == '+') ? 1 : -1;
i++;
}
else
return (0);
}
while (str[i] >= '0' && str[i] <= '9')
res = res * 10 + str[i++] - '0';
res *= sign;
return (res);
}