mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
feat(map parsing): show error if same texture directive is given twice
https://www.notion.so/Logic-error-leak-when-multiple-identical-texture-directives-are-given-233551de06f4806f8552ce4877a7757d?v=233551de06f480718417000cf26e3225&source=copy_link
This commit is contained in:
parent
3e28da1fcf
commit
8d3746dd53
2 changed files with 51 additions and 23 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
||||
/* Updated: 2025/07/21 15:25:16 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/07/21 15:27:15 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -64,9 +64,8 @@ bool check_cubfile(char *file, t_mapdata *map)
|
|||
fd = open(file, O_RDONLY);
|
||||
if (fd < 0)
|
||||
return (ft_strlcpy(map->error, "Can't open file", 16), false);
|
||||
if (!add_textures(fd, map))
|
||||
return (close(fd), ft_strlcpy(map->error,
|
||||
"Map started before all the textures", 37), false);
|
||||
if (add_textures(fd, map) != 0)
|
||||
return (close(fd), false);
|
||||
if (map->error[0])
|
||||
return (close(fd), false);
|
||||
populate_maps(map, fd);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */
|
||||
/* Updated: 2025/07/21 15:22:41 by kcolin ### ########.fr */
|
||||
/* Updated: 2025/07/21 15:28:36 by kcolin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -40,53 +40,82 @@ unsigned long set_color(const char *s, t_mapdata *map)
|
|||
return (finalcolor);
|
||||
}
|
||||
|
||||
bool set_textures(char *line, t_mapdata *map)
|
||||
/*
|
||||
** return values:
|
||||
** 0: ok
|
||||
** 2: duplicated texture directive
|
||||
*/
|
||||
int try_set_texture(t_mapdata *map, char **texture, char *texture_name)
|
||||
{
|
||||
if (*texture != NULL)
|
||||
{
|
||||
ft_strlcpy(map->error, "Duplicated texture directive", 1024);
|
||||
return (2);
|
||||
}
|
||||
*texture = ft_strdup(texture_name);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** return values:
|
||||
** 0: ok
|
||||
** 1: map starts here
|
||||
** 2: duplicated texture directive
|
||||
*/
|
||||
int set_textures(char *line, t_mapdata *map)
|
||||
{
|
||||
char **tab;
|
||||
int i;
|
||||
int retvalue;
|
||||
|
||||
tab = ft_split(line, ' ');
|
||||
i = 0;
|
||||
if (tab[0][0] == '1')
|
||||
return (false);
|
||||
return (ft_strlcpy(map->error,
|
||||
"Map started before all the textures", 1024), 1);
|
||||
retvalue = 0;
|
||||
if (tab[0] && tab[1])
|
||||
{
|
||||
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
|
||||
map->no_texture = ft_strdup(tab[1]);
|
||||
retvalue = try_set_texture(map, &map->no_texture, tab[1]);
|
||||
if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3))
|
||||
map->so_texture = ft_strdup(tab[1]);
|
||||
retvalue = try_set_texture(map, &map->so_texture, tab[1]);
|
||||
if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3))
|
||||
map->we_texture = ft_strdup(tab[1]);
|
||||
retvalue = try_set_texture(map, &map->we_texture, tab[1]);
|
||||
if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
|
||||
map->ea_texture = ft_strdup(tab[1]);
|
||||
retvalue = try_set_texture(map, &map->ea_texture, tab[1]);
|
||||
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
|
||||
map->f_color = set_color(tab[1], map);
|
||||
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
|
||||
map->c_color = set_color(tab[1], map);
|
||||
}
|
||||
while (tab[i])
|
||||
free(tab[i++]);
|
||||
return (free(tab), true);
|
||||
return (free_tab(tab), retvalue);
|
||||
}
|
||||
|
||||
bool add_textures(int fd, t_mapdata *map)
|
||||
/*
|
||||
** return values:
|
||||
** 0: ok
|
||||
** 1: map starts here
|
||||
** 2: duplicated texture directive
|
||||
*/
|
||||
int add_textures(int fd, t_mapdata *map)
|
||||
{
|
||||
char *line;
|
||||
int set_lines;
|
||||
int result;
|
||||
|
||||
line = get_next_line(fd);
|
||||
set_lines = 0;
|
||||
while (line && set_lines < 6)
|
||||
{
|
||||
if (line[0] == '\0' || line[0] == '\n')
|
||||
;
|
||||
else if (!set_textures(line, map))
|
||||
return (free(line), gnl_exhaust(fd), false);
|
||||
else
|
||||
if (line[0] != '\0' && line[0] != '\n')
|
||||
{
|
||||
result = set_textures(line, map);
|
||||
if (result != 0)
|
||||
return (free(line), gnl_exhaust(fd), result);
|
||||
set_lines++;
|
||||
}
|
||||
free(line);
|
||||
line = get_next_line(fd);
|
||||
}
|
||||
free(line);
|
||||
return (true);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue