-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_reader.c
119 lines (108 loc) · 2.91 KB
/
map_reader.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: danimart <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/05 14:00:19 by danimart #+# #+# */
/* Updated: 2022/05/10 13:59:30 by danimart ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int map_free(char **map, int map_height, int return_value)
{
int i;
i = 0;
while (i < map_height)
{
free(map[i]);
i++;
}
*map = NULL;
return (return_value);
}
int validate_map_structure(t_map map)
{
int i;
int j;
i = 0;
while (i < map.height)
{
j = 0;
while (j < map.length && (i == 0 || i == map.height - 1))
{
if (map.arr[i][j] != '1')
return (map_free(map.arr, map.height, 9));
j++;
}
if (map.arr[i][0] != '1' || map.arr[i][map.length - 1] != '1')
return (map_free(map.arr, map.height, 9));
i++;
}
return (validate_map_objects(&map, 0));
}
int validate_map_content(t_map map)
{
int i;
int j;
map.length = ft_strlen(map.arr[0], 1);
i = 1;
while (i < map.height)
{
j = 0;
if (ft_strlen(map.arr[i], 1) != map.length)
return (map_free(map.arr, map.height, 7));
while (map.arr[i][j] != '\0' && map.arr[i][j] != '\n')
{
if (map.arr[i][j] != '0' && map.arr[i][j] != '1'
&& map.arr[i][j] != 'C' && map.arr[i][j] != 'E'
&& map.arr[i][j] != 'P')
return (map_free(map.arr, map.height, 4));
j++;
}
i++;
}
if (map.height == 0 || map.length == 0)
return (map_free(map.arr, map.height, 5));
else if (map.length > 1024)
return (map_free(map.arr, map.height, 6));
return (validate_map_structure(map));
}
int read_map_file(char *map_name)
{
int fd;
char *map[1024];
char *current_map_line;
int i;
fd = open(map_name, O_RDONLY);
if (fd < 0)
return (3);
i = 0;
while (i < 1024)
{
current_map_line = get_next_line(fd);
if (current_map_line == NULL)
break ;
map[i] = current_map_line;
i++;
}
current_map_line = get_next_line(fd);
if (current_map_line != NULL)
{
free(current_map_line);
return (map_free(map, i, 6));
}
return (validate_map_content(create_base_map(map, i)));
}
int parse_map_input(char **args)
{
int size;
size = 0;
while (args[1][size] != '\0')
size++;
if (size <= 3 || args[1][size - 1] != 'r' || args[1][size - 2] != 'e'
|| args[1][size - 3] != 'b' || args[1][size - 4] != '.')
return (2);
return (read_map_file(args[1]));
}