-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
54 lines (49 loc) · 1.56 KB
/
parse.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_tetrimino_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rgaia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/11 19:14:14 by rgaia #+# #+# */
/* Updated: 2017/11/07 11:49:45 by sadamant ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static int check_valid_format(char *buf)
{
int i;
int row_len;
int column_len;
i = -1;
row_len = 0;
column_len = 0;
while (buf[++i])
{
if (row_len < 4 && VALID_CHAR(buf[i]))
row_len++;
else if (row_len == 4 && buf[i] == '\n')
{
row_len = 0;
column_len++;
if (column_len == 4 && buf[i + 1] == '\n')
{
column_len = 0;
i++;
}
}
else
return (0);
}
return (column_len == 4 && buf[i - 1] != buf[i - 2]);
}
int parse_tetrimino_file(int fd, char *buf)
{
int buf_size;
buf_size = read(fd, buf, BUF_SIZE);
close(fd);
if (buf_size > 545 || buf_size < 20)
return (0);
buf[buf_size] = '\0';
return (check_valid_format(buf));
}