-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_map.c
More file actions
65 lines (59 loc) · 1.78 KB
/
read_map.c
File metadata and controls
65 lines (59 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sfelici <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 15:14:18 by sfelici #+# #+# */
/* Updated: 2025/02/21 15:21:46 by sfelici ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static int ft_opener(const char *file_path)
{
int fd;
fd = open(file_path, O_RDONLY);
if (fd < 0)
{
ft_printf("Error\nCan't open file %s\n", file_path);
return (-1);
}
return (fd);
}
static char *validate_tmp_map(char *tmp_map)
{
if (!tmp_map || !*tmp_map)
{
ft_printf("Error\nFile blank or can't read it\n");
free(tmp_map);
return (NULL);
}
return (tmp_map);
}
char **read_map(const char *file_path)
{
int fd;
char *line;
char **map;
char *tmp_map;
char *tmp_join;
fd = ft_opener(file_path);
tmp_map = ft_calloc(1, 1);
line = get_next_line(fd);
while (line != NULL && ft_strncmp(line, "\n", 1))
{
tmp_join = ft_strjoin(tmp_map, line);
free(tmp_map);
tmp_map = tmp_join;
free(line);
line = get_next_line(fd);
}
close(fd);
tmp_map = validate_tmp_map(tmp_map);
if (!tmp_map)
return (NULL);
map = ft_split(tmp_map, '\n');
free(tmp_map);
return (map);
}