Skip to content

Commit

Permalink
fix:(vSKAH): Fix norm errors
Browse files Browse the repository at this point in the history
add:(vSKAH): file extension are now checked
  • Loading branch information
vSKAH committed Oct 14, 2023
1 parent f7d913f commit 4a5d757
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
38 changes: 25 additions & 13 deletions errors_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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));
}
3 changes: 1 addition & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'), \
Expand Down
28 changes: 18 additions & 10 deletions maps_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,37 @@
#include "so_long.h"
#include <fcntl.h>

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)
{
free(tmp);
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)
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions so_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -16,8 +16,6 @@
# include "libft/libft.h"
# include "minilibx-linux/mlx.h"

# include <stdlib.h>

typedef struct s_location {
int x;
int y;
Expand Down Expand Up @@ -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[]);
Expand All @@ -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);
Expand Down
25 changes: 24 additions & 1 deletion utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
}

0 comments on commit 4a5d757

Please sign in to comment.