-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
110 lines (99 loc) · 2.71 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rymuller <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/07 13:08:21 by rymuller #+# #+# */
/* Updated: 2019/02/03 16:11:19 by rymuller ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static t_gnl *get_list(t_gnl **alst, int fd)
{
t_gnl *temp;
temp = *alst;
while (temp)
{
if (temp->fd == fd)
return (temp);
temp = temp->next;
}
if (!(temp = (t_gnl *)malloc(sizeof(t_gnl))))
return (NULL);
if (!(temp->content = ft_strdup("")))
{
free(temp);
return (NULL);
}
else
temp->fd = fd;
temp->next = *alst;
*alst = temp;
return (temp);
}
static void del_list(t_gnl **alst, int fd)
{
t_gnl *victim;
t_gnl **buffer;
buffer = alst;
while (*buffer)
{
if ((*buffer)->fd == fd)
{
victim = *buffer;
*buffer = victim->next;
if (victim->content)
free(victim->content);
free(victim);
}
else
buffer = &((*buffer)->next);
}
}
static char *join_buff_to_list(t_gnl *list, char *buff, t_gnl **alst, int fd)
{
char *temp;
temp = list->content;
if (!(list->content = ft_strjoin(temp, buff)))
del_list(alst, fd);
free(temp);
return (list->content);
}
static char *copy_tail_to_list(t_gnl *list, t_gnl **alst, int fd)
{
char *temp;
if (ft_strchr(list->content, '\n'))
{
temp = list->content;
if (!(list->content = ft_strdup(ft_strchr(temp, '\n') + 1)))
del_list(alst, fd);
free(temp);
}
return (list->content);
}
int get_next_line(const int fd, char **line)
{
static t_gnl *alst;
t_gnl *list;
int num_bytes;
char buff[BUFF_SIZE + 1];
CHERROR(fd, line, read(fd, buff, 0));
CHECK((list = get_list(&alst, fd)));
while (((num_bytes = read(fd, buff, BUFF_SIZE)) > 0))
{
buff[num_bytes] = '\0';
CHECK((list->content = join_buff_to_list(list, buff, &alst, fd)));
if ((ft_strchr(buff, '\n')))
break ;
}
if (num_bytes < BUFF_SIZE && ft_strlen(list->content) == 0)
{
del_list(&alst, fd);
return (0);
}
CHECK((*line = ft_strchrdup(list->content, '\n')));
CHECK((list->content = copy_tail_to_list(list, &alst, fd)));
return (1);
}