mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +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> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
/* 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);
|
fd = open(file, O_RDONLY);
|
||||||
if (fd < 0)
|
if (fd < 0)
|
||||||
return (ft_strlcpy(map->error, "Can't open file", 16), false);
|
return (ft_strlcpy(map->error, "Can't open file", 16), false);
|
||||||
if (!add_textures(fd, map))
|
if (add_textures(fd, map) != 0)
|
||||||
return (close(fd), ft_strlcpy(map->error,
|
return (close(fd), false);
|
||||||
"Map started before all the textures", 37), false);
|
|
||||||
if (map->error[0])
|
if (map->error[0])
|
||||||
return (close(fd), false);
|
return (close(fd), false);
|
||||||
populate_maps(map, fd);
|
populate_maps(map, fd);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */
|
/* 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);
|
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;
|
char **tab;
|
||||||
int i;
|
int retvalue;
|
||||||
|
|
||||||
tab = ft_split(line, ' ');
|
tab = ft_split(line, ' ');
|
||||||
i = 0;
|
|
||||||
if (tab[0][0] == '1')
|
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 (tab[0] && tab[1])
|
||||||
{
|
{
|
||||||
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
|
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))
|
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))
|
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))
|
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))
|
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
|
||||||
map->f_color = set_color(tab[1], map);
|
map->f_color = set_color(tab[1], map);
|
||||||
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
|
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
|
||||||
map->c_color = set_color(tab[1], map);
|
map->c_color = set_color(tab[1], map);
|
||||||
}
|
}
|
||||||
while (tab[i])
|
return (free_tab(tab), retvalue);
|
||||||
free(tab[i++]);
|
|
||||||
return (free(tab), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
char *line;
|
||||||
int set_lines;
|
int set_lines;
|
||||||
|
int result;
|
||||||
|
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
set_lines = 0;
|
set_lines = 0;
|
||||||
while (line && set_lines < 6)
|
while (line && set_lines < 6)
|
||||||
{
|
{
|
||||||
if (line[0] == '\0' || line[0] == '\n')
|
if (line[0] != '\0' && line[0] != '\n')
|
||||||
;
|
{
|
||||||
else if (!set_textures(line, map))
|
result = set_textures(line, map);
|
||||||
return (free(line), gnl_exhaust(fd), false);
|
if (result != 0)
|
||||||
else
|
return (free(line), gnl_exhaust(fd), result);
|
||||||
set_lines++;
|
set_lines++;
|
||||||
|
}
|
||||||
free(line);
|
free(line);
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
return (true);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue