-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors_utils.c
79 lines (71 loc) · 2.49 KB
/
errors_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* errors_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/13 17:50:08 by jbadaire #+# #+# */
/* Updated: 2023/10/13 17:50:08 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <fcntl.h>
int handle_file_error(char *argv[])
{
int fd;
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)
{
ft_printf("Error\n -> Can't open file.\n");
ft_printf("please check permissions or path: %s\n", 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\n");
free_collectibles(game.world.player.collectibles);
return (free_map(&game.world), free_map(&cloned), -2);
}
free_map(&cloned);
return (0);
}
int handle_elements_error(char *path, t_game game)
{
int elements_code;
elements_code = valid_elements(game.world);
if (elements_code < 0)
{
ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C\n");
free_collectibles(game.world.player.collectibles);
free_map(&game.world);
return (-1);
}
return (handle_map_solve(path, game));
}
int handle_map_error(t_game game)
{
if (has_illegal_character(game.world))
{
free_map(&game.world);
return (ft_printf("Error\n -> Invalid Map / illegal character\n"), -1);
}
if (!is_valid_shape(game.world))
{
free_map(&game.world);
return (ft_printf("Error\n -> Invalid shape / not closed\n"), -1);
}
return (0);
}