-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
executable file
·124 lines (113 loc) · 3.46 KB
/
input.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/12 18:56:28 by akharrou #+# #+# */
/* Updated: 2019/03/23 17:36:59 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static t_bool legal_connections(t_char buf[PIECE_SIZE])
{
t_uint8 block_connections;
t_int8 i;
i = -1;
block_connections = 0;
while (PIECE_SIZE > ++i)
if (buf[i] == '#')
{
if ((i + 1) < PIECE_SIZE && buf[i + 1] == '#')
block_connections++;
if ((i - 1) >= 0 && buf[i - 1] == '#')
block_connections++;
if ((i + 5) < PIECE_SIZE && buf[i + 5] == '#')
block_connections++;
if ((i - 5) >= 0 && buf[i - 5] == '#')
block_connections++;
}
return (block_connections == 6 || block_connections == 8);
}
/*
** The checksum starts with the number of times the conditions should
** return true (i.e 1). 4 times for the hash character, 12 times for the
** dot character and 4 times for the return line; not more, not less.
**
** By the end of the process, 'checksum' should be equal to precisely 0.
*/
static t_bool legal_chars(t_char buf[PIECE_SIZE])
{
t_uint8 checksum;
t_int8 row;
t_int8 i;
i = -1;
row = -1;
checksum = (4 * 1) + (12 * 2) + (4 * 3);
while (PIECE_ROWS > ++row)
{
checksum -= (buf[++i] == '#');
checksum -= (buf[i] == '.') ? 2 : 0;
checksum -= (buf[++i] == '#');
checksum -= (buf[i] == '.') ? 2 : 0;
checksum -= (buf[++i] == '#');
checksum -= (buf[i] == '.') ? 2 : 0;
checksum -= (buf[++i] == '#');
checksum -= (buf[i] == '.') ? 2 : 0;
checksum -= (buf[++i] == '\n') ? 3 : 0;
}
return (checksum == 0);
}
#define ROW (0)
#define COL (1)
static t_piece generate_piece(t_char letter, t_char buf[PIECE_SIZE])
{
t_uint8 offsets[2];
t_uint8 block_n;
t_piece piece;
t_int8 i;
i = -1;
block_n = 0;
bzero(offsets, sizeof(offsets));
while (PIECE_SIZE > ++i && BLOCKS_PER_PIECE > block_n)
if (buf[i] == '#')
{
if (block_n == 0)
{
offsets[0] = i / (PIECE_ROWS + 1);
offsets[1] = i % (PIECE_COLS + 1);
}
piece.coord[block_n][0] = (i / (PIECE_ROWS + 1)) - offsets[0];
piece.coord[block_n][1] = (i % (PIECE_COLS + 1)) - offsets[1];
++block_n;
}
piece.mask = get_piece_mask(0, buf);
piece.width = get_piece_width(buf);
piece.height = get_piece_height(buf);
piece.letter = letter;
return (piece);
}
t_uint8 read_pieces(ssize_t fd, t_piece (*piece)[MAX_PIECES],
t_uint8 npieces)
{
t_char buf[PIECE_SIZE];
t_char piece_id;
size_t ret;
t_int8 i;
i = -1;
piece_id = 'A';
while (npieces > ++i)
{
ret = read(fd, buf, PIECE_SIZE);
if (ret == PIECE_SIZE && legal_chars(buf) && legal_connections(buf))
(*piece)[i] = generate_piece(piece_id++, buf);
else
EXIT(INVALID_INPUT);
if (read(fd, buf, 1) == 0)
break ;
else if (buf[0] != '\n')
EXIT(INVALID_INPUT);
}
return (i + 1);
}