cub3d/src/map/populate_map.c

85 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* populate_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */
/* Updated: 2025/07/23 13:34:33 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../libft/includes/libft.h"
#include "../utils/frees.h"
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;
char **newmap;
char **newmapflood;
int i;
newmap = ft_calloc(sizeof(char *), (length + 2));
if (!newmap)
return ;
newmapflood = ft_calloc(sizeof(char *), (length + 2));
if (!newmapflood)
return ;
i = copy_old_map(map, newmap, newmapflood, length);
newmap[i] = ft_strdup(line);
newmapflood[i] = ft_strdup(line);
free_tab_length(map->map, length);
free_tab_length(map->mapflood, length);
map->mapheight = ++length;
map->map = newmap;
map->mapflood = newmapflood;
}
/*
** return values:
** true: everything is fine
** false: error occured, caller should look at map->error
*/
bool populate_maps(t_mapdata *map, int fd)
{
char *line;
bool end_reached;
bool retvalue;
line = get_next_line(fd);
end_reached = false;
retvalue = true;
while (line)
{
if (line[0] != '\n' && end_reached)
{
ft_strlcpy(map->error, "Trailing chars after map", ERRLEN);
retvalue = false;
}
if (line[0] != '\n')
add_map_line(line, map);
if (line[0] == '\n')
end_reached = true;
free(line);
line = get_next_line(fd);
}
free(line);
return (retvalue);
}