From 2fd013d7583e0388d50ff9895c5e474e2d76f79f Mon Sep 17 00:00:00 2001 From: Rudy Bousset Date: Mon, 17 Feb 2020 15:31:11 +0100 Subject: Better parse for textures and colors --- src/ft_get_colors.c | 87 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 18 deletions(-) (limited to 'src/ft_get_colors.c') diff --git a/src/ft_get_colors.c b/src/ft_get_colors.c index 869dd71..3d34284 100644 --- a/src/ft_get_colors.c +++ b/src/ft_get_colors.c @@ -13,17 +13,58 @@ #include #include #include +#include -static int - ft_check_digits(const char *word) +static int8_t +ft_check_color_digits(char **num, t_cub *clist) { size_t i; + uint8_t j; i = 0; - while (ft_isdigit(word[i])) - i++; - if (i != ft_strlen(word)) + j = 0; + while (j < 3) + { + while (ft_isdigit(num[j][i])) + i++; + if (i != ft_strlen(num[j])) + { + ft_free_words(num); + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ALPHA, + ft_strlen(FT_ERR_COLOR_ALPHA) + 1); + + return (-1); + } + i = 0; + j++; + } + return (0); +} + +static int8_t + ft_check_max_int(char **num, t_cub *clist) +{ + if (ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255 + || ft_atoi(num[2]) > 255) + { + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_MAX, + ft_strlen(FT_ERR_COLOR_MAX) + 1); + ft_free_words(num); return (-1); + } + return (0); +} + +static int8_t + ft_check_nums_amount(char **num, t_cub *clist) +{ + if (!num[0] || !num[1] || !num[2] || num[3]) + { + ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ARGS, + ft_strlen(FT_ERR_COLOR_ARGS) + 1); + ft_free_words(num); + return (-1); + } return (0); } @@ -33,17 +74,22 @@ int8_t char **num; if (!(*words) || !words[1] || words[2]) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } if (!(num = ft_split(words[1], ','))) - return (-1); - 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); + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); } + if (ft_check_nums_amount(num, clist) < 0) + return (-1); + if (ft_check_color_digits(num, clist) < 0) + return (-1); + if (ft_check_max_int(num, clist) < 0) + return (-1); clist->f_rgb.r = ft_atoi(num[0]); clist->f_rgb.g = ft_atoi(num[1]); clist->f_rgb.b = ft_atoi(num[2]); @@ -56,18 +102,23 @@ int8_t { char **num; - if (!(*words) || ft_strcmp(*words, "C") || !words[1] || words[2]) + if (!(*words) || !words[1] || words[2]) + { + ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); return (-1); + } if (!(num = ft_split(words[1], ','))) - return (-1); - 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); + ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, + ft_strlen(FT_ERR_ALLOCATE) + 1); return (-1); } + if (ft_check_nums_amount(num, clist) < 0) + return (-1); + if (ft_check_color_digits(num, clist) < 0) + return (-1); + if (ft_check_max_int(num, clist) < 0) + return (-1); clist->c_rgb.r = ft_atoi(num[0]); clist->c_rgb.g = ft_atoi(num[1]); clist->c_rgb.b = ft_atoi(num[2]); -- cgit v1.2.3