-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing_map.c
73 lines (67 loc) · 1.9 KB
/
parsing_map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbiodies <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/21 18:19:42 by rbiodies #+# #+# */
/* Updated: 2021/11/25 16:55:33 by rbiodies ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void map_error(void)
{
write(1, "Error\nInvalid Map\n", 18);
exit(EXIT_FAILURE);
}
static int check_map(t_map *map, char *filename)
{
int fd;
char *line;
size_t width;
int height;
fd = open(filename, O_DIRECTORY);
if (fd > 0)
map_error();
fd = open(filename, O_RDONLY);
if (fd < 0 || ft_strcmp(".ber", &filename[ft_strlen(filename) - 4]))
map_error();
line = get_next_line(fd);
width = ft_strlen(line);
height = 0;
while (line)
{
if (width != ft_strlen(line))
map_error();
free(line);
line = get_next_line(fd);
height++;
}
close(fd);
map->height = height;
return (width);
}
void parsing_map(t_map *map, char *filename)
{
int fd;
char *line;
int i;
map->width = check_map(map, filename);
fd = open(filename, O_RDONLY);
if (fd < 0)
map_error();
map->map = (char **)malloc(sizeof(char *) * map->height);
line = get_next_line(fd);
i = 0;
while (line)
{
map->map[i] = line;
line = get_next_line(fd);
i++;
}
free(line);
close(fd);
map->step = 0;
valid_map(map);
}