-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.c
104 lines (94 loc) · 2.26 KB
/
parsing.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gbrochar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/05/30 13:40:01 by gbrochar #+# #+# */
/* Updated: 2016/06/22 04:44:36 by gbrochar ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf3d.h"
char **get_map(char *str)
{
int i;
int fd;
char *line;
char **map;
i = 0;
if ((fd = open(str, O_RDONLY)) == -1)
return (NULL);
while (get_next_line(fd, &line) > 0)
{
i++;
free(line);
}
close(fd);
if (!(map = (char **)malloc((i + 1) * sizeof(char *))))
return (NULL);
map[i] = NULL;
i = 0;
if ((fd = open(str, O_RDONLY)) == -1)
return (NULL);
while (get_next_line(fd, &line) > 0)
map[i++] = line;
close(fd);
return (map);
}
int check_player(int **map, t_env *e)
{
int i;
int j;
i = 0;
j = 0;
while (map[i])
++i;
while (map[0][j] != 255)
++j;
if (check_player_2(map, e, j))
return (1);
if (check_border(i, j, map))
return (1);
return (0);
}
void del(char **map_str)
{
int i;
i = 0;
while (map_str[i])
free(map_str[i++]);
free(map_str);
}
int **check_map(char **map_str, t_env *e)
{
int **map;
int tab_len;
int i;
if (!map_str)
return (NULL);
if (ft_strtab_isdigit(map_str) == 0)
return (NULL);
i = 0;
while (map_str[i])
++i;
if (!(map = (int **)malloc((i + 1) * sizeof(int *))))
return (NULL);
map[i] = NULL;
i = -1;
while (map_str[++i])
{
map[i] = ft_atoi_split(map_str[i], ' ', &tab_len);
map[i][tab_len] = 255;
}
del(map_str);
if (check_player(map, e))
return (NULL);
return (map);
}
int parse_map(t_env *e, char *str)
{
if (!(e->map = check_map(get_map(str), e)))
return (1);
return (0);
}