-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_map.c
More file actions
93 lines (84 loc) · 2.03 KB
/
Copy pathcreate_map.c
File metadata and controls
93 lines (84 loc) · 2.03 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cerdelen <cerdelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 17:26:13 by cerdelen #+# #+# */
/* Updated: 2022/03/07 17:26:13 by cerdelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int open_fd(char *file)
{
int fd;
fd = open(file, O_RDONLY);
if (fd < 0)
perror(file);
if (fd < 0)
exit(EXIT_SUCCESS);
return (fd);
}
int get_columns(char *file)
{
char *line;
int columns;
int fd;
columns = 0;
fd = open_fd(file);
line = get_next_line(fd);
if (!line)
{
write(2, "Empty file!\n", 13);
exit(EXIT_SUCCESS);
}
columns = ft_strlen(line) - 1;
free(line);
while (line)
{
line = get_next_line(fd);
free(line);
}
close(fd);
return (columns - 1);
}
int get_rows(char *file)
{
int fd;
int i;
char *line;
fd = open_fd(file);
i = 0;
line = get_next_line(fd);
while (line != NULL)
{
free(line);
line = get_next_line(fd);
i++;
}
free(line);
close(fd);
return (i);
}
char **create_map(char *file, int rows, int columns)
{
char **map;
char *line;
int i;
int j;
int fd;
map = malloc(sizeof(char *) * rows);
fd = open_fd(file);
i = 0;
j = 0;
line = get_next_line(fd);
while (line != NULL)
{
map[i] = line;
i++;
line = get_next_line(fd);
}
check_validity(map, rows, columns);
return (map);
}