mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
fix: redid all the checks for the color
- checks if it has less or more than 3 fields - checks if it's only digits - checks if it's between 0 and 255
This commit is contained in:
parent
ea8d3e085a
commit
c495547a70
1 changed files with 31 additions and 5 deletions
|
|
@ -13,9 +13,35 @@
|
|||
#include "../../libft/includes/libft.h"
|
||||
#include "../utils/frees.h"
|
||||
|
||||
bool perform_color_checks(int *color, char *strcolor, t_mapdata *map)
|
||||
{
|
||||
size_t i;
|
||||
int colorvalue;
|
||||
|
||||
i = 0;
|
||||
while (i < ft_strlen(strcolor) - 1)
|
||||
{
|
||||
if (!ft_isdigit(strcolor[i]))
|
||||
{
|
||||
ft_strlcpy(map->error, "invalid characters in color definition", 1024);
|
||||
return (false);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
colorvalue = ft_atoi(strcolor);
|
||||
if (colorvalue < 0 || colorvalue > 255)
|
||||
{
|
||||
ft_strlcpy(map->error, "invalid value for colors", 1024);
|
||||
return (false);
|
||||
}
|
||||
*color = colorvalue;
|
||||
return (true);
|
||||
}
|
||||
|
||||
unsigned long set_color(const char *s, t_mapdata *map)
|
||||
{
|
||||
int i;
|
||||
bool isok;
|
||||
unsigned long finalcolor;
|
||||
char **tab;
|
||||
int rgb[100];
|
||||
|
|
@ -26,15 +52,15 @@ unsigned long set_color(const char *s, t_mapdata *map)
|
|||
{
|
||||
if (i > 2)
|
||||
ft_strlcpy(map->error, "too many colors", 16);
|
||||
rgb[i] = ft_atoi(tab[i]);
|
||||
isok = perform_color_checks(&rgb[i], tab[i], map);
|
||||
free(tab[i]);
|
||||
i++;
|
||||
}
|
||||
free(tab);
|
||||
if (rgb[0] < 0 || rgb[0] > 255
|
||||
|| rgb[1] < 0 || rgb[1] > 255
|
||||
|| rgb[2] < 0 || rgb[2] > 255)
|
||||
return (ft_strlcpy(map->error, "Invalid color", 14), 0);
|
||||
if (i <= 2)
|
||||
return (ft_strlcpy(map->error, "Not enough colors", 18), 0);
|
||||
if (!isok)
|
||||
return (0);
|
||||
finalcolor = ((rgb[0] & 0xff) << 16)
|
||||
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
|
||||
return (finalcolor);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue