fix: parsing color as a pro

This commit is contained in:
Camille Chauvet 2023-05-05 11:54:25 +00:00
parent a3a2b8d9d2
commit 7b4412b977

View File

@ -1,4 +1,5 @@
#include "./parsing_private.h"
#include <stddef.h>
static int set_texture(t_map *map, int token, const char *key, char *value)
{
@ -11,20 +12,56 @@ static int set_texture(t_map *map, int token, const char *key, char *value)
return (0);
}
static unsigned int get_rgb(unsigned char red, unsigned char green,
unsigned char blue)
{
return (red << 24 | green << 16 | blue << 8 | 255);
}
static long long get_color(const char **tab)
{
long long rgb[3];
size_t i;
i = 0;
while (i < 3)
{
if (ft_atoul_check(tab[i]) == 0)
{
ft_eprintf("map: %s color invalid format", tab[i]);
return (-1);
}
rgb[i] = ft_atoul(tab[i]);
if (rgb[i] > 255)
{
ft_eprintf("map: %s is a to high to be a color", tab[i]);
return (-1);
}
i++;
}
return (get_rgb((unsigned char) rgb[0], (unsigned char) rgb[1], (unsigned char) rgb[2]));
}
static int set_color(long long *color, const char *key, char *value)
{
char **tab;
if (*color != -1)
{
ft_eprintf("redefinition of %s", key);
ft_eprintf("map: %s redefinition", key);
return (1);
}
if (ft_atoul_check(value))
tab = ft_split(value, ',');
if (tab == NULL)
return (1);
if (ft_tablen((const void **) tab) != 3)
{
ft_eprintf("%s is to high", value);
ft_freer_tab_ultimate(1, tab);
ft_eprintf("map: invalid format %s %s", key, value);
return (1);
}
*color = ft_atoul(value);
return (0);
*color = get_color((const char **) tab);
return (*color == -1);
}
int header_is_valid(char ***header, t_map *map)