-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_map.c
More file actions
115 lines (104 loc) · 2.59 KB
/
make_map.c
File metadata and controls
115 lines (104 loc) · 2.59 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
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
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dhuss <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/06 15:28:23 by dhuss #+# #+# */
/* Updated: 2024/09/18 10:12:00 by dhuss ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void get_x_y(char *map_name, t_point *size)
{
int fd;
char *line;
fd = open(map_name, O_RDONLY);
if (fd < 0)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Can't open map\n", 1);
exit(EXIT_FAILURE);
}
size->y = 0;
while (1)
{
line = get_next_line(fd);
if (line == NULL)
break ;
if (size->y == 0)
size->x = ft_strlen(line);
size->y++;
free (line);
}
close (fd);
}
static size_t *for_rectangle(t_game_state *game)
{
size_t *x_len;
x_len = (size_t *)malloc(sizeof(size_t)
* (game->size.y + 1));
if (!x_len)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Allocation failure\n", 1);
exit(EXIT_FAILURE);
}
return (x_len);
}
static size_t ft_strlen_newline(char *str)
{
size_t i;
i = 0;
while (str[i] != '\0' && str[i] != '\n')
i++;
return (i);
}
static char **populate_map(char **area, t_game_state *game, int fd)
{
int i;
char *line;
i = 0;
game->x_len = for_rectangle(game);
while (i < game->size.y)
{
line = get_next_line(fd);
game->x_len[i] = ft_strlen_newline(line);
if (!line)
break ;
area[i] = malloc(sizeof(char) * game->size.x);
if (!area[i])
{
free(line);
free_double_exit(area);
}
ft_strlcpy(area[i], line, game->size.x);
free(line);
i++;
}
area[i] = NULL;
return (area);
}
char **make_area(t_game_state *game, char *map_name)
{
char **area;
int fd;
fd = open(map_name, O_RDONLY);
if (fd < 0)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Can't open map\n", 1);
exit(EXIT_FAILURE);
}
area = (char **)malloc(sizeof(char *) * (game->size.y + 1));
if (!area)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd("Allocation failure\n", 1);
exit(EXIT_FAILURE);
}
area = populate_map(area, game, fd);
close (fd);
return (area);
}