-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_map.c
More file actions
81 lines (74 loc) · 1.9 KB
/
read_map.c
File metadata and controls
81 lines (74 loc) · 1.9 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abillote <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 15:24:20 by abillote #+# #+# */
/* Updated: 2024/01/25 14:52:23 by abillote ### ########.fr */
/* */
/* ************************************************************************** */
#include "solong.h"
char **store_map(char *file, int nb_lines)
{
char **map;
int i;
int fd;
int j;
i = 0;
j = 0;
fd = open(file, O_RDONLY);
map = malloc(sizeof(char *) * (nb_lines + 1));
if (!map)
{
ft_printf("\n--Error--\n Memory allocation");
exit (EXIT_FAILURE);
}
while (i < nb_lines)
{
map[i] = get_next_line(fd);
i++;
}
close(fd);
return (map);
}
int map_count_lines(int fd)
{
int nb_lines;
char *line;
line = get_next_line(fd);
if (!line)
{
ft_printf("\n--Error--\nMap is empty\n\n");
exit (EXIT_FAILURE);
}
free(line);
nb_lines = 0;
while (line != NULL)
{
line = get_next_line(fd);
free(line);
nb_lines++;
}
return (nb_lines);
}
void read_map(char *file, t_game *game)
{
int fd;
fd = open(file, O_RDONLY);
if (fd == -1)
{
ft_printf("\n--Error--\nMap does not open\n\n");
exit (EXIT_FAILURE);
}
game->nb_lines = map_count_lines(fd);
close(fd);
game->map = NULL;
game->map = store_map(file, game->nb_lines);
if (is_valid_map(game) == 1)
{
free_map(game);
exit (1);
}
}