-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_utils.c
More file actions
61 lines (56 loc) · 1.67 KB
/
map_utils.c
File metadata and controls
61 lines (56 loc) · 1.67 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ltombell <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 17:12:59 by ltombell #+# #+# */
/* Updated: 2022/11/09 17:38:59 by ltombell ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <string.h>
int count_lines(char *file)
{
int i;
int fd;
int readcount;
char c;
readcount = 1;
i = 0;
fd = open(file, O_RDONLY);
while (readcount > 0)
{
readcount = read(fd, &c, 1);
if (c == '\n')
i++;
}
close(fd);
return (i - 1);
}
void mapreader(char *file, t_program *program)
{
int b;
int count;
int fd;
b = 0;
count = count_lines(file);
fd = open(file, O_RDONLY);
if (fd < 0)
{
ft_printf("file non esistente, esco \n");
exit(0);
}
program->map = (char **) malloc (sizeof(char *) * (count + 1));
program->mp2 = (char **) malloc (sizeof (char *) * (count + 1));
while (b < count)
{
program->map[b] = get_next_line(fd);
program->mp2[b] = strdup(program->map[b]);
b++;
}
program->map[b] = NULL;
program->mp2[b] = NULL;
close(fd);
}