-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_tetri.c
62 lines (57 loc) · 1.57 KB
/
read_tetri.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_tetri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tduval <tduval@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/15 21:13:29 by tduval #+# #+# */
/* Updated: 2018/11/22 11:26:01 by tduval ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "fillit.h"
static char *big_read(const int fd, int *ret, int *i)
{
char *tmp;
char *str;
char buf[22];
str = 0;
while ((*ret = read(fd, buf, 21)))
{
buf[*ret] = '\0';
if (!str)
str = ft_strdup(buf);
else
{
if (!(tmp = ft_strjoin(str, buf)))
{
free(str);
free(tmp);
return (0);
}
free(str);
str = tmp;
}
(*i)++;
}
return (str);
}
char ***read_tetri(const int fd)
{
char *str;
int ret;
int i;
i = 0;
ret = 0;
if (!(str = big_read(fd, &ret, &i)))
return (0);
if ((ret == 0 && i == 0) || ret == -1 || !check_buf(str, i))
{
free(str);
return (0);
}
return (create_tetri(str, i));
}