-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line.c
86 lines (79 loc) · 2.25 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
77
78
79
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vbrazas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/09 04:41:41 by vbrazas #+# #+# */
/* Updated: 2019/02/15 02:55:04 by vbrazas ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#define CSTR(x) ((char *)(x)->content)
#define STR(x) (x)->content
#define FD(x) (x)->content_size
static t_list *get_node(int fd, t_list **head)
{
t_list *node;
if (!*head)
{
*head = ft_memalloc(sizeof(t_list));
FD(*head) = fd;
return (*head);
}
node = *head;
while (node)
{
if ((int)FD(node) == fd)
return (node);
if (!node->next)
break ;
node = node->next;
}
node->next = ft_memalloc(sizeof(t_list));
FD(node->next) = fd;
return (node->next);
}
static int get_line(t_list *l, char **line, char *n_pos)
{
char *tmp;
if (!STR(l) || !*CSTR(l))
return (0);
if (n_pos)
{
*line = ft_strsub(STR(l), 0, n_pos - CSTR(l));
tmp = STR(l);
STR(l) = ft_strdup(n_pos + 1);
free(tmp);
}
else
{
*line = ft_strdup(STR(l));
ft_strdel((char**)&STR(l));
}
return (1);
}
int get_next_line(const int fd, char **line)
{
static t_list *head = NULL;
t_list *l;
char buf[BUFF_SIZE + 1];
char *tmp;
int ret;
if (fd < 0 || line == NULL || BUFF_SIZE < 1 || read(fd, NULL, 0) < 0)
return (-1);
l = get_node(fd, &head);
if (STR(l) && (tmp = ft_strchr(STR(l), '\n')))
return (get_line(l, line, tmp));
while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[ret] = '\0';
tmp = STR(l);
STR(l) = ft_strjoin(STR(l), buf);
ft_strdel(&tmp);
if ((tmp = ft_strchr(STR(l), '\n')))
break ;
}
return (get_line(l, line, tmp));
}