-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_as_grid.c
64 lines (57 loc) · 1.59 KB
/
save_as_grid.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* save_as_grid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/12 15:21:19 by zhlim #+# #+# */
/* Updated: 2023/07/24 16:02:54 by zhlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int grid_dup(char **new_grid, t_map *map, int count)
{
int i;
i = 0;
count--;
while (i < count)
{
new_grid[i] = map->grid[i];
i++;
}
return (i);
}
char **create_grid(t_map *map, char *line)
{
char **new_grid;
int i;
new_grid = (char **)malloc(map->rows * sizeof(char *));
if (!new_grid)
return (NULL);
if (map->grid)
{
i = grid_dup(new_grid, map, map->rows);
free(map->grid);
}
else
{
i = 0;
}
new_grid[i] = ft_strdup(line);
return (new_grid);
}
void save_as_grid(int fd, t_map *map)
{
char *line;
line = get_next_line(fd);
while (line)
{
if (*line == '\n')
break ;
map->rows++;
map->grid = create_grid(map, line);
free(line);
line = get_next_line(fd);
}
}