From cff05e83256a67b8cb23b16b1e1e6f761ff52f4c Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 11 Jan 2020 12:37:34 +0100 Subject: fixing a few segfault, map border checking --- parse/parse.c | 19 +++++++++---------- parse/parse_check.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 parse/parse_check.c (limited to 'parse') diff --git a/parse/parse.c b/parse/parse.c index 145d5ad..84a8957 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/15 09:29:21 by cacharle #+# #+# */ -/* Updated: 2020/01/11 10:12:10 by cacharle ### ########.fr */ +/* Updated: 2020/01/11 11:29:45 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ t_state *parse(char *filename) return (NULL); if ((lines = get_file_lines(filename)) == NULL) return (state_destroy(state)); + /* for (int i = 0; lines[i]; i++) */ + /* printf("%d [%s]\n", i, lines[i]); */ i = -1; while (lines[++i] != NULL) { @@ -46,16 +48,17 @@ char **get_file_lines(char *filename) { int fd; int ret; - char buf[BUFFER_SIZE]; + char buf[BUFFER_SIZE + 1]; char *file; - fd = open(filename, O_RDONLY); + if ((fd = open(filename, O_RDONLY)) < 0) + return (NULL); if ((file = ft_strdup("")) == NULL) return (NULL); while ((ret = read(fd, buf, BUFFER_SIZE)) > 0) { buf[ret] = '\0'; - if ((file = ft_strjoin_free(file, buf, 1)) == NULL) + if ((file = ft_strjoin(file, buf)) == NULL) return (NULL); } if (ret == -1) @@ -122,12 +125,8 @@ t_cell *create_map_row(char *line) i = 0; while (*line) { - if (*line == '0') - row[i++] = CELL_EMPTY; - else if (*line == '1') - row[i++] = CELL_WALL; - else if (*line == '2') - row[i++] = CELL_ITEM; + if (*line == '0' || *line == '1' || *line == '2') + row[i++] = *line - '0'; else if (*line == 'N') row[i++] = CELL_LOOK_NORTH; else if (*line == 'S') diff --git a/parse/parse_check.c b/parse/parse_check.c new file mode 100644 index 0000000..7bcfc8b --- /dev/null +++ b/parse/parse_check.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/11 10:59:15 by cacharle #+# #+# */ +/* Updated: 2020/01/11 11:15:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +t_state *parse_check(t_state *state) +{ + int i; + int j; + int player_count; + + i = -1; + while (++i < state->map_width) + if (state->map[0][i] != CELL_WALL + || state->map[state->map_height - 1][i] != CELL_WALL) + return (state_destroy(state)); + i = -1; + while (++i < state->map_height) + if (state->map[i][0] != CELL_WALL + || state->map[i][state->map_width - 1] != CELL_WALL) + return (state_destroy(state)); + // maybe not necessary + /* player_count = 0; */ + /* i = -1; */ + /* while (++i < state->map_height) */ + /* { */ + /* j = -1; */ + /* while (++j < state->map_width) */ + /* if (helper_is_player_cell(state->map[i][j])) */ + /* player_count++; */ + /* } */ + /* if (player_count != 1) */ + /* return (state_destroy(state)); */ + return (state); +} -- cgit