From f2b7255587c5d24999fe5d471f480341f8162733 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 25 Jan 2020 19:51:12 +0100 Subject: Better error management --- src/ft_get_colors.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/ft_get_colors.c b/src/ft_get_colors.c index c2e8b31..2e09fc6 100644 --- a/src/ft_get_colors.c +++ b/src/ft_get_colors.c @@ -2,6 +2,19 @@ #include #include +static int +ft_check_digits(const char *word) +{ + size_t i; + + i = 0; + while (ft_isdigit(word[i])) + i++; + if (i != ft_strlen(word)) + return (-1); + return (0); +} + static int ft_get_f_color(char *line, char **words, t_win *wlist) { @@ -11,7 +24,10 @@ ft_get_f_color(char *line, char **words, t_win *wlist) return (-1); if (!(num = ft_split(words[1], ','))) return (-1); - if (!num[0] || !num[1] || !num[2] || num[3]) + if (!num[0] || !num[1] || !num[2] || num[3] || + ft_check_digits(num[0]) || ft_check_digits(num[1]) || + ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || + ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { ft_free_words(num, NULL); return (-1); @@ -35,7 +51,10 @@ ft_get_c_color(char *line, char **words, t_win *wlist) return (-1); if (!(num = ft_split(words[1], ','))) return (-1); - if (!num[0] || !num[1] || !num[2] || num[3]) + if (!num[0] || !num[1] || !num[2] || num[3] || + ft_check_digits(num[0]) || ft_check_digits(num[1]) || + ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || + ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { ft_free_words(num, NULL); return (-1); @@ -74,7 +93,7 @@ ft_get_colors(int fd, t_win *wlist) if (ft_get_c_color(line, words, wlist) < 0) { ft_free_words(words, line); - return (-1); + return (ft_map_error(9, wlist)); } return (0); } -- cgit v1.2.3