Skip to content

Commit

Permalink
fixed build_map function, no more segfaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Aug 16, 2024
1 parent 7a002f6 commit 011ce20
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 27 deletions.
9 changes: 8 additions & 1 deletion includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */
/* Updated: 2024/08/16 14:23:50 by jteissie ### ########.fr */
/* Updated: 2024/08/16 15:31:54 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -52,4 +52,11 @@ typedef enum e_bool
TRUE,
} t_bool;

typedef struct s_data
{
char *map_path;
int map_fd;
char **map;
} t_data;

#endif
7 changes: 5 additions & 2 deletions includes/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 15:00:57 by jteissie #+# #+# */
/* Updated: 2024/08/16 14:22:57 by jteissie ### ########.fr */
/* Updated: 2024/08/16 15:43:22 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PARSER_H
# define PARSER_H
# include "cub3d.h"

typedef enum e_bool t_bool;
typedef struct s_data t_data;

typedef enum e_parse_status
{
MAP_OK,
MAP_ERR,
PANIC_ERR,
} t_parse_status;

int parse_map(int map_fd);
int parse_map(t_data *data);

#endif
16 changes: 8 additions & 8 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */
/* Updated: 2024/08/16 14:35:22 by jteissie ### ########.fr */
/* Updated: 2024/08/16 15:43:57 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

int main(int ac, char *av[])
{
int map_fd;

//check for file
t_data data;

if (ac < 2 || ac > 2)
return (EXIT_FAILURE);
map_fd = open(av[1], O_RDONLY);
if (map_fd < 0)
//check for file extension (.cub);
data.map_path = av[1];
data.map_fd = open(av[1], O_RDONLY);
if (data.map_fd < 0)
return (EXIT_FAILURE);
if (parse_map(map_fd) == PANIC)
if (parse_map(&data) == PANIC)
return (EXIT_FAILURE);
close(map_fd);
close(data.map_fd);
return (0);
}
60 changes: 44 additions & 16 deletions srcs/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:12:00 by tsuchen #+# #+# */
/* Updated: 2024/08/16 14:42:13 by jteissie ### ########.fr */
/* Updated: 2024/08/16 15:46:18 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,31 +27,61 @@ static void panic_free(char **array)
free(array);
}

static char **build_map(int map_fd)
static size_t get_map_size(char *path)
{
int fd;
size_t line_nb;
char *gnl_buff;

fd = open(path, O_RDONLY);
if (fd < 0)
return (0);
gnl_buff = get_next_line(fd);
if (!gnl_buff)
return (0);
line_nb = 1;
while (gnl_buff)
{
free(gnl_buff);
gnl_buff = get_next_line(fd);
line_nb++;
}
close(fd);
return (line_nb);
}

static char **build_map(t_data *data)
{
char **map;
char *line;
size_t size;
uint32_t index;

index = 0;
map = ft_calloc(1, sizeof(char **));
if (!map)
return (NULL);
line = get_next_line(map_fd);
map = NULL;
line = get_next_line(data->map_fd);
if (!line)
return (NULL);
size = get_map_size(data->map_path);
if (size == 0)
{
free(line);
return (NULL);
}
map = ft_calloc(size + 1, sizeof(char *));
if (!map)
return (NULL);
while (line)
{
map[index] = ft_calloc(1, sizeof(char *));
map[index] = ft_strdup(line);
if (!map[index])
{
free(line);
panic_free(map);
return (NULL);
}
map[index] = line;
free(line);
line = get_next_line(map_fd);
line = get_next_line(data->map_fd);
index++;
}
map[index] = NULL;
Expand Down Expand Up @@ -174,17 +204,15 @@ t_parse_status verify_map(char **map)
return (MAP_OK);
}

int parse_map(int map_fd)
int parse_map(t_data *data)
{
char **map;

map = build_map(map_fd);
if (!map)
data->map = build_map(data);
if (!data->map)
return (PANIC);
if (verify_map(map) == MAP_ERR)
if (verify_map(data->map) == MAP_ERR)
{
ft_putstr_fd("Error, implement custom messages here (parse_map)\n", STDERR_FILENO);
return (ft_free_all(map), PANIC);
return (ft_free_all(data->map), PANIC);
}
return (SUCCESS);
}

0 comments on commit 011ce20

Please sign in to comment.