diff --git a/Makefile b/Makefile index 9d6a583..e507ce0 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ ft_strlcat.c ft_toupper.c ft_tolower.c ft_strchr.c ft_strrchr.c ft_strncmp.c \ ft_memchr.c ft_memcmp.c ft_strnstr.c ft_atoi.c ft_calloc.c ft_strdup.c \ ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \ ft_striteri.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ -ft_get_size.c ft_power.c +ft_get_size.c ft_power.c ft_atoi_check.c OBJS = ${SRCS:.c=.o} BONUS_SRCS = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c \ ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c diff --git a/ft_atoi_check.c b/ft_atoi_check.c new file mode 100644 index 0000000..b549055 --- /dev/null +++ b/ft_atoi_check.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi_check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/07/21 08:21:05 by erey-bet #+# #+# */ +/* Updated: 2022/12/08 16:37:19 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_isspace(char c) +{ + if (c == ' ' || c == '\f' + ||c == '\n' || c == '\r' || c == '\t' || c == '\v') + return (1); + return (0); +} + +int ft_atoi_check(const char *nptr) +{ + long result; + int sign; + + while (ft_isspace(*nptr)) + nptr++; + sign = 1; + if (*nptr == '+' || *nptr == '-') + { + if (*nptr == '-') + sign = -1; + nptr++; + } + result = 0; + while (*nptr >= '0' && *nptr <= '9') + { + result = result * 10 + *nptr++ - '0'; + if ((result > 2147483647 && sign == 1) + || (result > 2147483648 && sign == -1)) + return (0); + } + if (*nptr--) + return (0); + if (*nptr == '-' || *nptr == '+') + return (0); + return (1); +} diff --git a/libft.h b/libft.h index aceddd6..7546f2e 100644 --- a/libft.h +++ b/libft.h @@ -66,5 +66,6 @@ void ft_lstdelone(t_list *lst, void (*del)(void*)); void ft_lstclear(t_list **lst, void (*del)(void*)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +int ft_atoi_check(const char *nptr); #endif