-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.c
83 lines (76 loc) · 1.82 KB
/
init.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dyoula <dyoula@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/10 19:18:33 by dyoula #+# #+# */
/* Updated: 2022/01/26 00:29:47 by dyoula ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int error_messages_read_file(int n)
{
if (n == 0)
{
ft_putstr_fd("Error\nEmpty_file\n", 2);
return (0);
}
return (1);
}
void ciao(char *txt)
{
free(txt);
printf("Error empty file.\n");
exit (1);
}
int file_reader(t_vars *g, int fd)
{
char buf[2];
char *txt;
int i;
i = 0;
txt = ft_strdup("");
if (!txt)
return (0);
while (read(fd, buf, 1) > 0)
{
buf[1] = 0;
txt = ft_strjoin(txt, buf);
i++;
}
if (!error_messages_read_file(i))
{
free(txt);
return (0);
}
g->map = ft_split(txt, '\n');
if (!g->map)
ciao(txt);
free(txt);
return (1);
}
int init_parse(t_vars *g)
{
g->fd = open(g->av[1], O_DIRECTORY);
if (g->fd > 0)
{
printf("It's a directory Dummy\n");
close(g->fd);
exit(-1);
}
else
g->fd = open(g->av[1], O_RDONLY);
if (!file_reader(g, g->fd))
{
exit (0);
}
return (1);
}
int parse_and_check_errors(t_vars *g)
{
if (!init_parse(g) || !check_errors(g))
return (0);
return (1);
}