-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid_read_and_split.c
88 lines (78 loc) · 2.71 KB
/
valid_read_and_split.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_read_and_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eguajard <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/22 21:17:02 by eguajard #+# #+# */
/* Updated: 2017/12/01 16:41:10 by rgaia ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
/*
** Calculates the number of Tetriminos a file is expected to have,
** given it's length
*/
int get_num_tetriminos(size_t len)
{
int num;
num = (((int)len + 1) / (TET_AREA + 1));
return (num);
}
/*
** Does some preliminary file validation after reading into buffer to make sure
** it's not too small (less than a single tetrimino), or not too large
** (larger than MAX_TET (max tetriminos))
*/
char *read_file(int fd)
{
int bytes_read;
char *file_buf;
file_buf = ft_strnew(BUF_SIZE);
bytes_read = read(fd, file_buf, BUF_SIZE);
close(fd);
if (bytes_read > MAX_F_LEN || bytes_read < TET_AREA)
{
ft_strdel(&file_buf);
return (NULL);
}
if (!((ft_strlen(file_buf) == 20) ||
((ft_strlen(file_buf) - 20) % 21 == 0)))
return (0);
file_buf[bytes_read] = '\0';
return (file_buf);
}
/*
** At this point, we know file_buf isn't too big or too small
** (j): variable keeps index of new lines that splits tetriminos.
** if we don't access here and check if it's a new line or other character,
** the character at j's index will be skipped and will not be
** accessible again from the **file_tetriminos
*/
char **split_tetriminos(char *file_buf, int i)
{
int j;
int expected_num_tetriminos;
char *piece;
char **file_tetriminos;
j = 0;
expected_num_tetriminos = get_num_tetriminos(ft_strlen(file_buf));
g_num_tetriminos = expected_num_tetriminos;
file_tetriminos = malloc(sizeof(char*) * expected_num_tetriminos + 1);
file_tetriminos[expected_num_tetriminos] = (char*)0;
while (i < expected_num_tetriminos)
{
piece = ft_strndup((file_buf + (j++) + (i * TET_AREA)), TET_AREA);
if (ft_strlen(piece) != TET_AREA)
{
ft_strdel(&piece);
while (i--)
ft_strdel(&file_tetriminos[i]);
ft_memdel((void**)file_tetriminos);
return ((char **)0);
}
file_tetriminos[i++] = piece;
}
return (file_tetriminos);
}