-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.c
More file actions
64 lines (58 loc) · 1.17 KB
/
maps.c
File metadata and controls
64 lines (58 loc) · 1.17 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
#include "so_long.h"
#include <stdio.h>
static char *read_file(char *filename)
{
int fd, bytes;
char buf[1024 + 1];
char *content, *tmp;
content = ft_strdup("");
fd = open(filename, O_RDONLY);
if (fd < 0)
exit(write(2, "Error\nCannot open map\n", 22));
while ((bytes = read(fd, buf, 1024)) > 0)
{
buf[bytes] = '\0';
tmp = content;
content = ft_strjoin(tmp, buf);
free(tmp);
}
close(fd);
return (content);
}
static int count_rows(char *str)
{
int count = 0;
while (*str)
if (*str++ == '\n')
count++;
return (count + 1);
}
char **load_map(char *filename, t_game *game)
{
char *file;
char **lines;
int i;
file = read_file(filename);
game->height = count_rows(file);
lines = ft_split(file, '\n');
free(file);
if (!lines || !lines[0])
exit(write(2, "Error\nEmpty map\n", 16));
game->width = ft_strlen(lines[0]);
i = 0;
while (lines[i])
{
if ((int)ft_strlen(lines[i]) != game->width)
exit(write(2, "Error\nMap not rectangular\n", 26));
i++;
}
game->map = lines;
return (lines);
}
void free_map(char **map)
{
int i = 0;
while (map && map[i])
free(map[i++]);
free(map);
}