-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_file.c
96 lines (89 loc) · 2.17 KB
/
read_file.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schaaban <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/29 12:59:22 by schaaban #+# #+# */
/* Updated: 2017/12/18 13:11:36 by msabwat ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "get_one_line.h"
#include "libft/libft.h"
#include <stdlib.h>
static int rf_read_file(int fd, char **str)
{
char *line;
char *tmp;
*str = NULL;
while ((get_one_line(fd, &line)) == 1)
{
if (ft_strlen(line) != 1 && ft_strlen(line) != 5)
{
free(line);
return (0);
}
tmp = *str;
if (ft_strlen(line) == 5)
{
line[4] = '\0';
*str = ft_strjoin(*str, line);
}
else if (ft_strlen(line) == 1)
*str = ft_strjoin(*str, "O");
if (tmp != NULL)
free(tmp);
free(line);
}
return (1);
}
static int rf_check_tetriminos(char *str)
{
int i;
int count_sha;
int count_dot;
i = -1;
count_dot = 0;
count_sha = 0;
while (str[++i] != '\0')
{
if (str[i] == 'O')
{
if (count_sha != 4 || count_dot != 12)
return (0);
count_dot = 0;
count_sha = 0;
}
else if (str[i] == '.')
count_dot++;
else if (str[i] == '#')
count_sha++;
else
return (0);
}
return ((count_sha != 4 || count_dot != 12) ? 0 : 1);
}
int check_file_content(int fd, char ***tab)
{
char *str;
if (!rf_read_file(fd, &str))
{
if (str != NULL)
free(str);
return (0);
}
if (str == NULL)
return (0);
if (!rf_check_tetriminos(str))
{
if (str != NULL)
free(str);
return (0);
}
*tab = ft_strsplit(str, 'O');
if (str != NULL)
free(str);
return (1);
}