-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_buf.c
110 lines (101 loc) · 2.21 KB
/
check_buf.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
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_buf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tduval <tduval@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/20 03:58:37 by tduval #+# #+# */
/* Updated: 2018/11/22 11:25:18 by tduval ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "fillit.h"
static int count_pattern(const char *str)
{
int i;
int k;
i = 0;
k = 0;
while (i < 20)
{
if (str[i] == '#')
{
if (i > 0)
if (str[i - 1] == '#')
k++;
if (i < 19)
if (str[i + 1] == '#')
k++;
if (i < 15)
if (str[i + 5] == '#')
k++;
if (i > 4)
if (str[i - 5] == '#')
k++;
}
i++;
}
return (k);
}
static int check_pattern(char *buf)
{
char *tmp;
int i;
int k;
i = 0;
k = 0;
if (!(tmp = (char *)malloc(sizeof(char) * (20))))
return (0);
while (i < 20 && buf[i])
{
tmp[i] = buf[i];
i++;
}
i = 0;
k = count_pattern(tmp);
free(tmp);
return (k == 6 || k == 8);
}
static int check_diezes(const char *str, int *j)
{
int d;
d = 0;
while (*j < 20)
{
if (str[*j] == '#')
d++;
if (str[*j] != '#' && str[*j] != '.' && ((*j + 1) % 5) != 0)
return (0);
if (str[*j] != '\n' && !((*j + 1) % 5))
return (0);
(*j)++;
}
return (d);
}
int check_buf(char *str, int r)
{
char *tmp;
char *free_tmp;
int d;
int j;
int i;
tmp = ft_strdup(str);
free_tmp = tmp;
i = 0;
while (i < r)
{
j = 0;
d = check_diezes(tmp, &j);
if (d != 4 || (tmp[j] != '\n' && i + 1 < r) || (tmp[j] == '\n' && \
!tmp[j + 1]) || !check_pattern(tmp))
{
free(free_tmp);
return (0);
}
tmp += 21;
i++;
}
free(free_tmp);
return (1);
}