-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c.old
65 lines (58 loc) · 1.94 KB
/
get_next_line.c.old
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ewilliam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/26 10:26:14 by ewilliam #+# #+# */
/* Updated: 2017/02/07 10:46:59 by ewilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static t_tuple ft_read(const int fd)
{
char buf[BUFF_SIZE];
t_tuple tuple;
tuple.len = read(fd, buf, BUFF_SIZE);
tuple.str = tuple.len > READ_EOF
? ft_strndup(buf, tuple.len)
: NULL;
tuple.pos = 0;
return (tuple);
}
static int find_line(const int fd, char **line, char *full_line)
{
static t_tuple b;
while (1)
{
if (b.str && (b.nl_ptr = ft_strchr(b.str + b.pos, '\n')))
{
ft_strnjoin(&full_line, b.str + b.pos, b.nl_ptr - (b.str + b.pos));
b.pos += 1 + b.nl_ptr - &b.str[b.pos];
return (!!(*line = full_line));
}
if (b.str)
{
ft_strnjoin(&full_line, b.str + b.pos, b.len - b.pos);
b.pos = b.len;
}
if (b.len == b.pos)
{
ft_strdel(&b.str);
b = ft_read(fd);
if (!b.str && full_line && *full_line == '\0')
return (READ_EOF);
if (b.len == 0)
return (!!(*line = full_line));
}
}
}
int get_next_line(const int fd, char **line)
{
char *full_line;
full_line = NULL;
return ((!line || fd < 0 || read(fd, full_line, 0) == READ_ERROR)
? READ_ERROR
: (find_line(fd, line, full_line)));
}