-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_map.c
More file actions
63 lines (57 loc) · 1.8 KB
/
Copy pathread_map.c
File metadata and controls
63 lines (57 loc) · 1.8 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vfiszbin <vfiszbin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 15:32:34 by vfiszbin #+# #+# */
/* Updated: 2022/06/02 11:35:36 by vfiszbin ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void map_error(char *error_msg, char *line, char *new_line)
{
ft_putendl_fd(error_msg, 2);
if (line != NULL)
free(line);
if (new_line != NULL)
free(new_line);
exit(1);
}
void join_lines(char **line, char **new_line)
{
char *tmp;
tmp = *line;
*line = ft_strjoin(*line, *new_line);
if (*line == NULL)
map_error("Error\nft_strjoin failed", tmp, *new_line);
free(tmp);
free(*new_line);
}
/*Read the map contained in filename. Return
a string representing the map*/
char *read_map(char *filename)
{
char *line;
char *new_line;
int fd;
int read;
fd = open(filename, O_RDONLY);
if (!fd)
map_error("Error\nCould not open file", NULL, NULL);
line = get_next_line(fd);
if (!line)
map_error("Error\nget_next_line failed", NULL, NULL);
read = 1;
while (read != 0)
{
new_line = get_next_line(fd);
if (!new_line)
read = 0;
else
join_lines(&line, &new_line);
}
close(fd);
return (line);
}