solved leaks still reachable because gnl wasn't going to the end, and close fd

This commit is contained in:
Hugo LAMY
2022-05-04 22:00:36 +02:00
parent d4e5f298fa
commit c89c107e09
9 changed files with 47 additions and 48 deletions

View File

@@ -6,13 +6,13 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:03:50 by pblagoje #+# #+# */
/* Updated: 2022/05/04 15:32:54 by pblagoje ### ########.fr */
/* Updated: 2022/05/04 21:57:24 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3d.h"
int size_map(t_map *map, int fd)
static int size_map(t_map *map, int fd)
{
char *line;
@@ -41,7 +41,7 @@ int size_map(t_map *map, int fd)
return (EXIT_SUCCESS);
}
int find_map(t_map *map, char *file)
static int find_map(t_map *map, char *file)
{
int fd;
int count;
@@ -61,11 +61,13 @@ int find_map(t_map *map, char *file)
}
if (map->tmp_str[0] != '\n' && map->tmp_str[0] != '\0')
count++;
if (count < TOTAL_ELEMENTS + 1)
mb_free(map->tmp_str);
}
return (fd);
}
void fill_row(char *row, char *line, int width)
static void fill_row(char *row, char *line, int width)
{
int i;
@@ -83,53 +85,48 @@ void fill_row(char *row, char *line, int width)
row[i] = '\0';
}
int fill_map(t_map *map, int fd)
static void fill_map(t_map *map, int fd)
{
int i;
int i;
int ret;
ret = 1;
map->content = (char **)mb_alloc((map->size_y + 1) * sizeof(char *));
if (!map->content)
mb_exit("Error\nCouldn't allocate memory for map.\n", EXIT_FAILURE);
i = -1;
while (++i < map->size_y)
{
if (i > 0)
{
get_next_line(fd, &map->tmp_str);
ret = get_next_line(fd, &map->tmp_str);
mb_add(map->tmp_str);
}
map->content[i] = (char *)mb_alloc((map->size_x + 1) * sizeof(char));
if (!map->content[i])
mb_exit("Error\nCouldn't allocate memory for row.\n", EXIT_FAILURE);
fill_row(map->content[i], map->tmp_str, map->size_x);
mb_free(map->tmp_str);
map->tmp_str = NULL;
}
map->content[i] = NULL;
return (EXIT_SUCCESS);
while (ret)
{
ret = get_next_line(fd, &map->tmp_str);
if (ret)
mb_add(map->tmp_str);
}
}
int init_parsing(t_game *game, char *file)
{
int fd;
fd = check_elements(game, file);
if (fd == -1)
game->fd = check_elements(game, file);
if (game->fd == -1)
mb_exit("Error\nInvalid .cub file.\n", EXIT_FAILURE);
if (size_map(&(game->map), fd))
{
close(fd);
if (size_map(&(game->map), game->fd))
mb_exit("Error\nInvalid map format.\n", EXIT_FAILURE);
}
close(fd);
fd = find_map(&(game->map), file);
if (fd == -1)
close(game->fd);
game->fd = -1;
game->fd = find_map(&(game->map), file);
if (game->fd == -1)
mb_exit("Error\nCouldn't locate the map.\n", EXIT_FAILURE);
if (fill_map(&(game->map), fd) == EXIT_FAILURE)
{
close(fd);
mb_exit("Error\nCouldn't save the map.\n", EXIT_FAILURE);
}
close(fd);
fill_map(&(game->map), game->fd);
close(game->fd);
game->fd = -1;
return (EXIT_SUCCESS);
}