mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
Removed invalid reads and writes
This commit is contained in:
parent
8bce796bcf
commit
f64508b093
4 changed files with 41 additions and 22 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
|
||||
/* Updated: 2025/06/19 17:48:19 by tchampio ### ########.fr */
|
||||
/* Updated: 2025/06/20 12:43:33 by tchampio ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -137,21 +137,37 @@ bool check_filename(t_mapdata *map, char *file)
|
|||
return (true);
|
||||
}
|
||||
|
||||
void free_tab(char **tab)
|
||||
void free_tab_length(char **tab, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!tab)
|
||||
return ;
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
while (i < length)
|
||||
{
|
||||
free(tab[i]);
|
||||
i++;
|
||||
++i;
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
|
||||
int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!map->map)
|
||||
return (0);
|
||||
while (i < length)
|
||||
{
|
||||
newmap[i] = ft_strdup(map->map[i]);
|
||||
newmapflood[i] = ft_strdup(map->mapflood[i]);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
void add_map_line(const char *line, t_mapdata *map)
|
||||
{
|
||||
static int length = 0;
|
||||
|
|
@ -159,24 +175,18 @@ void add_map_line(const char *line, t_mapdata *map)
|
|||
char **newmapflood;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
newmap = malloc(sizeof(char *) * (length + 1));
|
||||
newmap = ft_calloc(sizeof(char *), (length + 1));
|
||||
if (!newmap)
|
||||
return ;
|
||||
newmapflood = malloc(sizeof(char *) * (length + 1));
|
||||
newmapflood = ft_calloc(sizeof(char *), (length + 1));
|
||||
if (!newmapflood)
|
||||
return ;
|
||||
while (map->map && map->map[i] && i < length)
|
||||
{
|
||||
newmap[i] = ft_strdup(map->map[i]);
|
||||
newmapflood[i] = ft_strdup(map->mapflood[i]);
|
||||
i++;
|
||||
}
|
||||
i = copy_old_map(map, newmap, newmapflood, length);
|
||||
newmap[i] = ft_strdup(line);
|
||||
newmapflood[i] = ft_strdup(line);
|
||||
length++;
|
||||
free_tab(map->map);
|
||||
free_tab(map->mapflood);
|
||||
free_tab_length(map->map, length);
|
||||
free_tab_length(map->mapflood, length);
|
||||
map->mapheight = ++length;
|
||||
map->map = newmap;
|
||||
map->mapflood = newmapflood;
|
||||
}
|
||||
|
|
@ -188,7 +198,6 @@ void populate_maps(t_mapdata *map, int fd)
|
|||
line = get_next_line(fd);
|
||||
while (line)
|
||||
{
|
||||
ft_printf("%s", line);
|
||||
if (line[0] != '\n')
|
||||
add_map_line(line, map);
|
||||
free(line);
|
||||
|
|
@ -197,10 +206,16 @@ void populate_maps(t_mapdata *map, int fd)
|
|||
free(line);
|
||||
}
|
||||
|
||||
bool check_walls(int fd, t_mapdata *map)
|
||||
bool check_walls(t_mapdata *map)
|
||||
{
|
||||
(void)fd;
|
||||
ft_printf("%d\n", map->mapheight);
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < map->mapheight)
|
||||
{
|
||||
ft_printf("line: %s", map->map[i]);
|
||||
i++;
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
|
|
@ -214,6 +229,7 @@ void print_map(char **map)
|
|||
ft_printf("line2: %s", map[i]);
|
||||
i++;
|
||||
}
|
||||
ft_printf("(%p) %s", map[i], map[i]);
|
||||
}
|
||||
|
||||
bool check_cubfile(char *file, t_mapdata *map)
|
||||
|
|
@ -231,8 +247,11 @@ bool check_cubfile(char *file, t_mapdata *map)
|
|||
if (map->error[0])
|
||||
return (close(fd), false);
|
||||
populate_maps(map, fd);
|
||||
//if (!check_walls(fd, map))
|
||||
if (!check_walls(map))
|
||||
return (close(fd), ft_strlcpy(map->error,
|
||||
"Map is malformed (invalid chars or missing walls)", 51), false);
|
||||
//if (!flood_fill(map->mapflood))
|
||||
// return (close(fd), ft_strlcpy(map->error,
|
||||
// "Map is malformed (invalid chars or missing walls)", 51), false);
|
||||
// "Map is not possible (flood fill failed)", 41), false);
|
||||
return (true);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue