-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_errors.c
106 lines (97 loc) · 2.24 KB
/
file_errors.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file_errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/04 18:30:26 by hgrissen #+# #+# */
/* Updated: 2021/11/06 14:45:08 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char *get_file(char **av)
{
int fd;
char *line;
char *str;
fd = open(av[0], O_RDONLY);
str = ft_strdup("");
if (fd < 0)
exit_error(1);
while (get_next_line(fd, &line))
{
str = ft_strjoin(str, line);
str = ft_strjoin(str, ft_strdup("\n"));
}
str = ft_strjoin(str, line);
return (str);
}
void check_elements_count(char *str)
{
int i;
t_elements el;
i = -1;
el.c = 0;
el.p = 0;
el.e = 0;
while (str[++i])
{
if (str[i] == 'C')
el.c++;
if (str[i] == 'E')
el.e++;
if (str[i] == 'P')
el.p++;
}
if (el.c < 1 || el.e < 1 || el.p != 1)
exit_error(2);
}
void check_map_chars(char *str)
{
int i;
int fnnwl;
i = -1;
fnnwl = 0;
while (str[++i])
{
if (str[i] != '0' && str[i] != '1' && str[i] != 'C'
&& str[i] != 'E' && str[i] != 'P' && str[i] != '\n')
exit_error(2);
if (str[i] == '\n')
{
if (fnnwl == 1)
exit_error(2);
fnnwl = 1;
}
else
fnnwl = 0;
}
}
void check_map_closed(char **map)
{
int i;
int j;
int len;
i = 0;
len = ft_strlen(map[i]);
while (map[i])
{
j = 0;
while (map[i][j])
{
if (i == 0 && map[i][j] != '1')
exit_error(2);
if (map[i + 1] == NULL && map[i][j] != '1')
exit_error(2);
if (j == 0 && map[i][j] != '1')
exit_error(2);
if (j == len - 1 && map[i][j] != '1')
exit_error(2);
j++;
}
if (!map[i][j] && j != len)
exit_error(2);
i++;
}
}