diff --git a/Makefile b/Makefile index 203eaf9..a3f6647 100644 --- a/Makefile +++ b/Makefile @@ -12,22 +12,23 @@ ASAN_FLAGS = -fsanitize=address,undefined -fno-omit-frame-pointer SRC = main.c \ debug/main.c \ parsing/parse_prep.c \ + parsing/check_order.c \ parsing/validation.c \ parsing/config/headers.c \ parsing/config/headers_values.c \ - parsing/map/validation.c \ - win.c \ - render_canvas.c \ - utils.c \ - draw.c \ - mini_map.c \ - player.c \ - move_player.c \ - keys_actions.c \ - init_graphics.c \ - rotate_player.c \ - mini_player.c \ - utils_minimap.c + parsing/map/validation.c \ + win.c \ + render_canvas.c \ + utils.c \ + draw.c \ + mini_map.c \ + player.c \ + move_player.c \ + keys_actions.c \ + init_graphics.c \ + rotate_player.c \ + mini_player.c \ + utils_minimap.c OBJ = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) diff --git a/Makefile.gen b/Makefile.gen deleted file mode 100644 index 1bb18dd..0000000 --- a/Makefile.gen +++ /dev/null @@ -1 +0,0 @@ -INC=/usr/include diff --git a/assets/textures/est.xpm b/assets/test/east.xpm similarity index 100% rename from assets/textures/est.xpm rename to assets/test/east.xpm diff --git a/assets/textures/north.xpm b/assets/test/north.xpm similarity index 100% rename from assets/textures/north.xpm rename to assets/test/north.xpm diff --git a/assets/textures/south.xpm b/assets/test/south.xpm similarity index 100% rename from assets/textures/south.xpm rename to assets/test/south.xpm diff --git a/assets/textures/west.xpm b/assets/test/west.xpm similarity index 100% rename from assets/textures/west.xpm rename to assets/test/west.xpm diff --git a/maps/invalid/valid_file_bad_content.cub b/assets/test/x similarity index 100% rename from maps/invalid/valid_file_bad_content.cub rename to assets/test/x diff --git a/inc/parsing.h b/inc/parsing.h index b7ba945..f783ae8 100644 --- a/inc/parsing.h +++ b/inc/parsing.h @@ -6,7 +6,7 @@ /* By: chguerre +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/11/14 15:53:07 by rpinheir #+# #+# */ +/* Updated: 2026/11/14 16:30:03 by rpinheir ### ########.ch */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +bool check_map_last(char *line) +{ + int i; + + i = 0; + while (line[i] == '\n' || line[i] == ' ' || line[i] == '\t') + i++; + if ((is_identifier(line + i)) < 6) + { + ft_putendl_fd("Error\nFound map before header", 2); + return (1); + } + return (0); +} + +int skip_empty(char **line, int fd, char ***buffer, t_game *game) +{ + while (1) + { + *line = get_next_line(fd); + if (!*line) + return (0); + if (buffer == &game->map && check_map_last(*line)) + return (free(*line), *line = NULL, -1); + if ((*line)[0] != '\n') + return (1); + free(*line); + if (buffer == &game->map) + return (ft_putendl_fd("Error\nFound newline on map", 2), -1); + } +} + +static bool find_start_map(char *line) +{ + int i; + + i = -1; + while (line[++i] == '\t' || line[i] == ' ') + ; + return (line[i] == '1'); +} + +int dup_buffer(t_game *game, int fd) +{ + char *line; + char ***buffer; + int count_len; + + buffer = &game->config; + count_len = -1; + while (++count_len >= 0) + { + if (skip_empty(&line, fd, buffer, game) == -1) + return (1); + if (!line) + break ; + if (find_start_map(line) && buffer == &game->config) + { + buffer = &game->map; + count_len = 0; + } + *buffer = expand_map(*buffer, count_len, -1, line); + if (!*buffer) + return (ft_strfree(*buffer), free(line), close(fd), -1); + } + if (!game->config || !game->map) + return (ft_putendl_fd("Error\nempty map or header", 2), 1); + return (0); +} diff --git a/src/parsing/config/headers.c b/src/parsing/config/headers.c index a199518..ff9a943 100644 --- a/src/parsing/config/headers.c +++ b/src/parsing/config/headers.c @@ -6,13 +6,13 @@ /* By: rpinheir +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/10 17:46:48 by rpinheir #+# #+# */ -/* Updated: 2026/11/11 11:09:27 by rpinheir ### ########.ch */ +/* Updated: 2026/11/14 17:10:46 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ #include "main.h" -static int is_identifier(char *line) +int is_identifier(char *line) { if (line[0] == 'C' && (line[1] == ' ' || line[1] == '\t')) return (HEADER_C); @@ -68,19 +68,24 @@ static char *get_header_name(int i) static int verify_registerd_headers(bool *seen) { int i; + int missing; - i = 0; - while (i < 6) + missing = 0; + i = -1; + while (++i < 6) { if (!seen[i]) { - ft_putstr_fd(FG_BOLD_RED "Error\nmissing header ", 2); + if (missing == 0) + ft_putstr_fd(FG_BOLD_RED "Error\nmissing header : ", 2); ft_putstr_fd(get_header_name(i), 2); - ft_putendl_fd(FG_DEFAULT, 2); - return (1); + ft_putstr_fd(" ", 2); + missing++; } - i++; } + ft_putendl_fd(FG_DEFAULT, 2); + if (missing > 0) + return (1); return (0); } diff --git a/src/parsing/config/headers_values.c b/src/parsing/config/headers_values.c index aafffda..7c00a01 100644 --- a/src/parsing/config/headers_values.c +++ b/src/parsing/config/headers_values.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "main.h" +#include int check_value(char *line, int *i, int *result, int j) { @@ -59,15 +60,34 @@ int validate_rgb(t_game *game, char *line, int id) return (0); } +static int check_extension(char *str) +{ + int len; + + len = ft_strlen(str); + if (len < 4) + return (false); + if (str[len - 1] == 'm' && str[len - 2] == 'p' && str[len - 3] == 'x' + && str[len - 4] == '.') + return (true); + return (false); +} + static int check_path(t_img *img) { - int fd; + int fd; + struct stat info; if (!img->path || !img->path[0]) return (ft_putendl_fd("Error\n couldn't setup wall textures", 2), 1); fd = open(img->path, O_RDONLY); if (fd == -1) - return (ft_putendl_fd("Error\ncouldn't open texture", 2), 1); + return (close(fd), ft_putendl_fd("Error\ncouldn't open texture", 2), 1); + fstat(fd, &info); + if (!S_ISREG(info.st_mode)) + return (close (fd), ft_putendl_fd("Error\nPath is a directory", 2), 1); + if (!check_extension(img->path)) + return (close(fd), ft_putendl_fd("Error\nBad file extension", 2), 1); close(fd); return (0); } @@ -92,7 +112,7 @@ int validate_texture_path(t_game *game, char *line, int id) else return (1); img->path = ft_strtrim(line + i, " \t"); - if (check_path(img)) + if (check_path(img) != 0) return (1); return (0); } diff --git a/src/parsing/map/validation.c b/src/parsing/map/validation.c index ee112ad..9f08924 100644 --- a/src/parsing/map/validation.c +++ b/src/parsing/map/validation.c @@ -6,7 +6,7 @@ /* By: rpinheir +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/12 16:11:16 by rpinheir #+# #+# */ -/* Updated: 2026/11/13 19:39:58 by rpinheir ### ########.ch */ +/* Updated: 2026/11/14 17:15:59 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ @@ -23,7 +23,14 @@ int check_pos(t_game *game, char c, int x, int y) } game->player.pos_x = x + 0.5; game->player.pos_y = y + 0.5; - game->player.orientation = c; + if (c == 'N') + game->player.orientation = ON; + else if (c == 'S') + game->player.orientation = OS; + else if (c == 'W') + game->player.orientation = OW; + else if (c == 'E') + game->player.orientation = OE; } return (0); } @@ -36,7 +43,7 @@ int check_valid_char(char **map, t_game *game) i = -1; while (map[++i]) { - j = 0; + j = -1; while (map[i][++j]) { if (!ft_strchr("01NSWE ", map[i][j])) @@ -67,12 +74,14 @@ int check_map(char **map) if (map[y][x] == '0' || map[y][x] == 'N' || map[y][x] == 'S' || map[y][x] == 'W' || map[y][x] == 'E') { + if (x == 0 || y == 0 || !map[y + 1] || x >= (int)ft_strlen(map[y + - 1]) || x >= (int)ft_strlen(map[y + 1])) + return (ft_putendl_fd("Error\nMap not closed", 2), 1); if ((map[y][x + 1] == '\0' || map[y][x + 1] == ' ') || (map[y + 1][x] == '\0' || map[y + 1][x] == ' ') || (map[y][x - 1] == '\0' || map[y][x - 1] == ' ') || (map[y - 1][x] == '\0' || map[y - 1][x] == ' ')) - return (ft_putendl_fd("Error\nMap not closed by walls", 2), - 1); + return (ft_putendl_fd("Error\nMap not closed", 2), 1); } } } diff --git a/src/parsing/parse_prep.c b/src/parsing/parse_prep.c index 8b6189a..1de02f6 100644 --- a/src/parsing/parse_prep.c +++ b/src/parsing/parse_prep.c @@ -6,7 +6,7 @@ /* By: rpinheir +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/04 17:21:24 by rpinheir #+# #+# */ -/* Updated: 2026/11/13 19:41:35 by rpinheir ### ########.ch */ +/* Updated: 2026/11/14 15:56:41 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ @@ -22,13 +22,14 @@ static bool check_extension(char *str, int len) return (false); } -static char **expand_map(char **map, int count_len, int i, char *line) +char **expand_map(char **map, int count_len, int i, char *line) { char **new; new = malloc(sizeof(char *) * (count_len + 2)); if (!new) - return (ft_putendl_fd("Error: couldn't malloc new map", 2), NULL); + return (ft_strfree(map), + ft_putendl_fd("Error: couldn't malloc new map", 2), NULL); while (++i < count_len) new[i] = map[i]; free(map); @@ -39,45 +40,6 @@ static char **expand_map(char **map, int count_len, int i, char *line) return (new); } -char *skip_empty(char *line, int fd) -{ - while (1) - { - line = get_next_line(fd); - if (!line || line[0] != '\n') - return (line); - free(line); - } -} - -int dup_buffer(t_game *game, int fd) -{ - char *line; - char ***buffer; - int count_len; - - buffer = &game->config; - count_len = -1; - line = NULL; - while (++count_len >= 0) - { - line = skip_empty(line, fd); - if (!line) - break ; - if (line[0] == '1' && buffer == &game->config) - { - buffer = &game->map; - count_len = 0; - } - *buffer = expand_map(*buffer, count_len, -1, line); - if (!*buffer) - return (ft_strfree(*buffer), free(line), close(fd), -1); - } - if (!game->config || !game->map) - return (ft_putendl_fd("Error\nempty map or header", 2), 1); - return (0); -} - int file_check(t_game *game) { int map_fd; diff --git a/src/parsing/validation.c b/src/parsing/validation.c index 0ce9e6e..2121558 100644 --- a/src/parsing/validation.c +++ b/src/parsing/validation.c @@ -6,7 +6,7 @@ /* By: rpinheir +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/05 16:53:57 by rpinheir #+# #+# */ -/* Updated: 2026/11/12 16:13:17 by rpinheir ### ########.ch */ +/* Updated: 2026/11/14 13:10:37 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ @@ -17,8 +17,10 @@ int validate(t_game *game) int errno; errno = validate_headers(game); + if (errno != 0) + return (errno); errno = validate_map(game); - if (errno > 0) + if (errno != 0) return (errno); return (0); }