-
Notifications
You must be signed in to change notification settings - Fork 0
/
sl_getmap.c
106 lines (97 loc) · 2.35 KB
/
sl_getmap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sl_getmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julmuntz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/18 16:06:59 by julmuntz #+# #+# */
/* Updated: 2022/09/18 11:45:50 by julmuntz ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int sl_locate(t_data *data)
{
int line;
int i;
line = -1;
data->direction = 0;
while (data->map[++line])
{
i = -1;
while (data->map[line][++i])
{
if (data->map[line][i] == 'E')
{
data->e_line = line;
data->e_char = i;
}
if (data->map[line][i] == 'P')
{
data->p_line = line;
data->p_char = i;
}
}
}
return (0);
}
void sl_count_line(t_data *data, int fd, char *filename)
{
char *line;
fd = open(filename, O_RDONLY);
if (fd == -1)
return ;
data->nb_line = 0;
while (TRUE)
{
line = get_next_line(fd);
if (line == NULL)
break ;
data->nb_line++;
data->nb_char = ft_linelen(line);
free(line);
}
close(fd);
}
char **sl_getmap(t_data *data, char *filename)
{
int fd;
fd = 0;
sl_count_line(data, fd, filename);
if (!data->nb_line)
{
ft_printf("Error\nMap not found.\n");
exit(EXIT_SUCCESS);
}
fd = open(filename, O_RDONLY);
data->map = malloc(sizeof(char *) * (data->nb_line + 1));
if (data->map == NULL)
return (NULL);
data->x = 0;
while (TRUE)
{
data->map[data->x] = get_next_line(fd);
if (data->map[data->x] == NULL)
break ;
data->x++;
}
close(fd);
return (data->map);
}
char **sl_mapcpy(t_data *data)
{
char **map_copy;
int i;
i = -1;
map_copy = malloc((data->nb_line + 1) * sizeof(char *));
if (!map_copy)
{
data->map_copy = 0;
return (0);
}
while (data->map[++i])
map_copy[i] = ft_strdup(data->map[i]);
map_copy[i] = 0;
data->map_copy = map_copy;
return (map_copy);
}