-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_opener.c
90 lines (81 loc) · 2.17 KB
/
file_opener.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file_opener.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vroussea <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/06 17:07:20 by vroussea #+# #+# */
/* Updated: 2016/01/26 17:38:38 by vroussea ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
static int file_opener(char *source, char *buf)
{
int fd;
int ret;
if ((fd = open(source, O_RDONLY)) == -1)
return (-1);
ret = read(fd, buf, BUF_SIZE);
if (ret > 545 || ret == -1)
return (-1);
buf[ret] = '\0';
if (close(fd) == -1)
return (-1);
return (ret);
}
static int fill_tetri(t_tetr **start, char *buf)
{
t_tetr *current;
char *tmp;
int nb;
int i;
tmp = buf;
if ((nb = new_list(tmp, start)) == 0)
return (-1);
i = nb;
current = *start;
while (i)
{
if (!check_next(current->tetrimino)
|| !check_size_tetri(current->tetrimino))
return (-1);
current = current->next;
i--;
}
return (nb);
}
static int init_buf(char *source, char *buf)
{
int size;
if ((size = file_opener(source, buf)) == -1)
return (-1);
if (!check_symbols(buf, size))
return (-1);
if (!check_size(buf, size))
return (-1);
return (size);
}
int init_tetri(char *source, t_tetr **start)
{
int size;
char *buf;
int nb;
buf = (char *)ft_memalloc(BUF_SIZE);
if ((size = init_buf(source, buf)) == -1)
{
ft_memdel((void **)&buf);
return (-1);
}
if ((nb = fill_tetri(start, buf)) == -1)
{
ft_memdel((void **)&buf);
return (-1);
}
ft_memdel((void **)&buf);
return (nb);
}