Files
42_INT_10_cube3d/srcs/parsing/check_rgb.c
2022-04-23 20:12:13 +02:00

104 lines
2.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_rgb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:11:24 by pblagoje #+# #+# */
/* Updated: 2022/04/23 18:40:13 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3d.h"
int check_colors(char **str)
{
int i;
int num;
i = 0;
num = 0;
while (str && str[i])
{
num = ft_atoi(str[i]);
if (num < 0 || num > 255)
return (EXIT_FAILURE);
i++;
}
return (EXIT_SUCCESS);
}
int ft_strlen_2d(char **str)
{
int i;
i = 0;
while (str && str[i])
i++;
return (i);
}
void set_floor_rgb(t_txt *txt, char **rgb)
{
int i;
i = 0;
while (i < 3)
{
txt->rgb_floor[i] = ft_atoi(rgb[i]);
i++;
}
}
void set_ceiling_rgb(t_txt *txt, char **rgb)
{
int i;
i = 0;
while (i < 3)
{
txt->rgb_ceiling[i] = ft_atoi(rgb[i]);
i++;
}
}
void ft_free_2d(char **str)
{
int i;
i = 0;
while (str && str[i])
{
free(str[i]);
i++;
}
free(str);
}
int check_rgb(t_txt *txt, char *element, char identifier)
{
char **rgb;
rgb = ft_split(element, ',');
if (!rgb || ft_strlen_2d(rgb) != 3 || \
element[ft_strlen(element) - 1] == ',')
{
ft_free_2d(rgb);
ft_putstr_fd("Error\nInvalid RGB code.\n", 2);
return (EXIT_FAILURE);
}
if ((identifier != 'F' && identifier != 'C') || check_colors(rgb))
{
ft_free_2d(rgb);
ft_putstr_fd("Error\nInvalid RGB code.\n", 2);
return (EXIT_FAILURE);
}
if (identifier == 'F')
set_floor_rgb(txt, rgb);
else
set_ceiling_rgb(txt, rgb);
ft_free_2d(rgb);
return (EXIT_SUCCESS);
}