From 4a5d757a46403e348a4424f45b04243941934d04 Mon Sep 17 00:00:00 2001 From: vSKAH Date: Sat, 14 Oct 2023 17:50:17 +0200 Subject: [PATCH] fix:(vSKAH): Fix norm errors add:(vSKAH): file extension are now checked --- errors_utils.c | 38 +++++++++++++++++++++++++------------- main.c | 3 +-- maps_utils.c | 28 ++++++++++++++++++---------- so_long.h | 8 ++++---- utils.c | 25 ++++++++++++++++++++++++- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/errors_utils.c b/errors_utils.c index 5cc86aa..5b103a0 100644 --- a/errors_utils.c +++ b/errors_utils.c @@ -19,16 +19,38 @@ int handle_file_error(char *argv[]) if (!argv[1]) return (ft_printf("Error\n -> Invalid file path.\n"), -2); + if (!ft_endwith(argv[1], ".ber")) + return (ft_printf("Error\n -> Invalid file format.\n"), -2); fd = open(argv[1], O_RDONLY); if (fd == -1) - return (ft_printf("Error\n -> Invalid file path.\n"), -2); + { + ft_printf("Error\n -> Can't open file.\n"); + ft_printf("please check permissions or path: %s", argv[1]); + return (-2); + } return (fd); } +t_boolean handle_map_solve(char *path, t_game game) +{ + t_world cloned; + + load_map(open(path, O_RDONLY), path, &cloned); + is_solvable(cloned.map, game.world.player.location.x, \ + game.world.player.location.y, game.world.length_y); + if (count_element(cloned, 'C') > 0 || count_element(cloned, 'E') > 0) + { + ft_printf("Error\n -> Sorry, your map is not resolvable"); + free_collectibles(game.world.player.collectibles); + return (free_map(&game.world), free_map(&cloned), -2); + } + free_map(&cloned); + return (0); +} + int handle_map_error(char *path, t_game game) { int elements_code; - t_world cloned; if (has_illegal_character(game.world)) { @@ -48,15 +70,5 @@ int handle_map_error(char *path, t_game game) ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C"); return (-1); } - load_map(open(path, O_RDONLY), path, &cloned); - is_solvable(cloned.map, game.world.player.location.x, \ - game.world.player.location.y, game.world.length_y); - if (count_element(cloned, 'C') > 0 || count_element(cloned, 'E') > 0) - { - ft_printf("Error\n -> Sorry, your map is not resolvable"); - free_collectibles(game.world.player.collectibles); - return (free_map(&game.world), free_map(&cloned), -2); - } - free_map(&cloned); - return (0); + return (handle_map_solve(path, game)); } diff --git a/main.c b/main.c index 2459c7f..1f3a22a 100644 --- a/main.c +++ b/main.c @@ -18,9 +18,8 @@ int main(int argc, char *argv[]) t_game game; int value; - (void) argc; fd = handle_file_error(argv); - if (fd < 0) + if (argc < 1 || fd < 0) return (0); load_map(fd, argv[1], &game.world); game.world.player = init_player(find_element(game.world, 'P'), \ diff --git a/maps_utils.c b/maps_utils.c index 1c181d8..7e9ea06 100644 --- a/maps_utils.c +++ b/maps_utils.c @@ -12,15 +12,12 @@ #include "so_long.h" #include -t_world *load_map(int fd, char *path, t_world *world) +int map_size(int fd) { - int index; - int y; - char *s; char *tmp; + int index; index = 0; - y = 0; tmp = get_next_line(fd); while (tmp != NULL) { @@ -28,13 +25,24 @@ t_world *load_map(int fd, char *path, t_world *world) tmp = get_next_line(fd); index++; } - world->map = malloc((index + 1) * sizeof(char *)); + close(fd); + return (index); +} + +t_world *load_map(int fd, char *path, t_world *world) +{ + int y; + int y_index; + char *s; + + y = 0; + y_index = map_size(fd); + world->map = malloc((y_index + 1) * sizeof(char *)); if (!world->map) return (world); - world->map[index] = NULL; - close(fd); + world->map[y_index] = NULL; fd = open(path, O_RDONLY); - while (y <= index) + while (y <= y_index) { s = get_next_line(fd); if (s != NULL) @@ -44,7 +52,7 @@ t_world *load_map(int fd, char *path, t_world *world) } y++; } - return (world->length_y = index, world); + return (world->length_y = y_index, world); } int valid_elements(t_world world) diff --git a/so_long.h b/so_long.h index 2965826..df855e9 100644 --- a/so_long.h +++ b/so_long.h @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/02 17:59:52 by jbadaire #+# #+# */ -/* Updated: 2023/10/12 20:15:13 by jbadaire ### ########.fr */ +/* Updated: 2023/10/14 16:22:10 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,6 @@ # include "libft/libft.h" # include "minilibx-linux/mlx.h" -# include - typedef struct s_location { int x; int y; @@ -72,7 +70,7 @@ typedef struct s_game // ** UTILS ** // t_boolean loc_equals(t_location loc_1, t_location loc_2); t_boolean is_inside_world(int y, int x, t_world world); -int handle_launch_error(char *argv[], t_world *world); +t_boolean ft_endwith(char *src, char *pattern); // ** ERRORS **// int handle_file_error(char *argv[]); @@ -85,6 +83,8 @@ t_boolean has_good_shape(t_world world); t_boolean is_closed(t_world world); t_boolean has_illegal_character(t_world world); t_location find_element(t_world world, char type); +t_boolean handle_map_solve(char *path, t_game game); +int map_size(int fd); int count_element(t_world world, char type); int valid_elements(t_world world); void is_solvable(char **map, int x, int y, int length_y); diff --git a/utils.c b/utils.c index 702ffbf..691c798 100644 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: jbadaire +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/03 10:24:01 by jbadaire #+# #+# */ -/* Updated: 2023/10/05 15:02:04 by jbadaire ### ########.fr */ +/* Updated: 2023/10/14 16:21:39 by jbadaire ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,3 +62,26 @@ int count_element(t_world world, char type) } return (count); } + +t_boolean ft_endwith(char *src, char *pattern) +{ + int pattern_size; + int src_size; + + if (!pattern || !src) + return (_false); + pattern_size = (int) ft_strlen(pattern); + src_size = (int) ft_strlen(src); + if (pattern_size > src_size) + return (_false); + --pattern_size; + --src_size; + while (src_size > 0 && pattern_size > 0) + { + if (src[src_size] != pattern[pattern_size]) + return (_false); + pattern_size--; + src_size--; + } + return (_true); +}