-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_file.c
99 lines (90 loc) · 2.48 KB
/
read_file.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* read_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zszeredi <zszeredi@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/14 16:07:03 by zszeredi #+# #+# */
<<<<<<< HEAD
/* Updated: 2020/02/25 14:32:55 by zszeredi ### ########.fr */
=======
/* Updated: 2020/02/22 18:42:38 by aben-azz ### ########.fr */
>>>>>>> 013b98f415153171ca627e0bde26d963b20acdb3
/* */
/* ************************************************************************** */
#include "fillit.h"
int ft_s_last_line(char **str)
{
int j;
int i;
i = 0;
j = 19;
if (str[i][j] == '\n')
return (-1);
return (1);
}
int ft_m_last_line(char **str, int number_tetroes)
{
int j;
int i;
j = 19;
i = 0;
while ((str[i][j] != '\0') && (number_tetroes >= 1))
{
if (str[i][j] != '\n')
return (-1);
number_tetroes--;
if (number_tetroes != 1)
i++;
}
if (number_tetroes == 0)
{
if (str[i][j] == '\n')
return (-1);
str[i][j] = '\0';
}
return (1);
}
int ft_nbt(int number_tetroes, char **str)
{
if (number_tetroes < 1 || number_tetroes > 26)
return (-1);
if (number_tetroes == 1)
if (ft_s_last_line(str) < 0)
return (-1);
if (number_tetroes > 1 && number_tetroes <= 26)
if ((ft_m_last_line(str, number_tetroes)) < 1)
return (-1);
return (1);
}
t_tetra *let_it_go(char *str)
{
ft_strdel(&str);
return (NULL);
}
t_tetra *read_file(const int fd)
{
int i;
int ret;
int number_tetroes;
char buf[BUFFER_SIZE + 1];
char **str;
number_tetroes = 0;
i = 0;
str = ft_create_double_array(27, 21);
if ((fd < 0 || fd >= OPEN_MAX) || read(fd, buf, 0) < 0)
return (let_it_go(str[i]));
while ((ret = read(fd, buf, BUFFER_SIZE)) && number_tetroes <= 26)
{
buf[ret] = '\0';
str[i] = ft_strdup(buf);
if ((check_charachters(str[i])) < 1)
return (let_it_go(str[i]));
number_tetroes++;
i++;
}
if ((ft_nbt(number_tetroes, str)) < 1)
return (let_it_go(str[i]));
return (ft_store_teros(str, number_tetroes + 1));
}