diff options
| author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-17 16:25:11 +0100 | 
|---|---|---|
| committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-02-17 16:25:11 +0100 | 
| commit | 4f207400fd8e6724d321b6f2fa79fba5f16f9d59 (patch) | |
| tree | 3896037d8a5938044bd6b6d49a18ad6305b948f0 /src/ft_get_colors.c | |
| parent | Added libbsd to linux Makefile (diff) | |
| parent | ready to merge (diff) | |
| download | 42-cub3d-4f207400fd8e6724d321b6f2fa79fba5f16f9d59.tar.gz 42-cub3d-4f207400fd8e6724d321b6f2fa79fba5f16f9d59.tar.bz2 42-cub3d-4f207400fd8e6724d321b6f2fa79fba5f16f9d59.tar.xz 42-cub3d-4f207400fd8e6724d321b6f2fa79fba5f16f9d59.tar.zst 42-cub3d-4f207400fd8e6724d321b6f2fa79fba5f16f9d59.zip | |
Merge branch 'better_parse'
Diffstat (limited to 'src/ft_get_colors.c')
| -rw-r--r-- | src/ft_get_colors.c | 86 | 
1 files changed, 68 insertions, 18 deletions
| diff --git a/src/ft_get_colors.c b/src/ft_get_colors.c index 869dd71..f54fabc 100644 --- a/src/ft_get_colors.c +++ b/src/ft_get_colors.c @@ -13,17 +13,57 @@  #include <libft.h>  #include <cub3d.h>  #include <stddef.h> +#include <stdint.h> -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 +73,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 +101,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]); | 
