-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
76 lines (69 loc) · 1.93 KB
/
get_next_line.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelphias <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/30 14:25:00 by aelphias #+# #+# */
/* Updated: 2019/10/28 20:57:22 by aelphias ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_check(char **line, int fd, char **left)
{
char *pos;
char *tmp;
if (!(pos = ft_strchr(left[fd], '\n')))
return (0);
tmp = left[fd];
*pos = '\0';
*line = ft_strdup(tmp);
pos++;
if (*pos == '\0')
{
ft_strdel(&left[fd]);
return (1);
}
left[fd] = ft_strdup(pos);
ft_strdel(&tmp);
return (1);
}
int ft_read_line(char **line, int fd, char **left)
{
int ret;
char b[BUFF_SIZE + 1];
char *tmp;
while ((ret = read(fd, b, BUFF_SIZE)))
{
b[ret] = '\0';
if (!left[fd])
left[fd] = ft_strdup(b);
else
{
tmp = left[fd];
left[fd] = ft_strjoin(tmp, b);
ft_strdel(&tmp);
}
if (ft_check(line, fd, left))
return (1);
}
return (0);
}
int get_next_line(const int fd, char **line)
{
static char *left[11000];
if (!line || fd < 0 || fd > 11000 || read(fd, NULL, 0) < 0)
return (-1);
if (left[fd] && ft_strchr(left[fd], '\n'))
return (ft_check(line, fd, left));
if (ft_read_line(line, fd, left))
return (1);
if (left[fd])
{
*line = ft_strdup(left[fd]);
ft_strdel(&(left[fd]));
return (1);
}
return (0);
}