-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
120 lines (110 loc) · 2.8 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
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mesafi <mesafi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/29 15:31:59 by mesafi #+# #+# */
/* Updated: 2019/11/13 22:29:02 by mesafi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_lst *ft_newnode(int fd, t_lst **node)
{
t_lst *new;
new = (t_lst *)malloc(sizeof(t_lst));
if (!new)
return (NULL);
new->fd = fd;
if (!(new->res = ft_strnew(0)))
return (NULL);
new->next = *node;
*node = new;
return (new);
}
t_lst *ft_lstsearch(int fd, t_lst *node)
{
while (node)
{
if (node->fd == fd)
return (node);
node = node->next;
}
return (NULL);
}
char *ft_secure_join(char *s1, char *s2, int end, int option)
{
char *str;
str = NULL;
if (option == 0)
{
s2[end] = '\0';
if (*s2 == '\0')
return (s1);
if (!(str = ft_strjoin(s1, s2)))
return (NULL);
free(s1);
}
else
{
if (s1 != NULL)
{
if (!(str = ft_strdup(s1 + 1)))
return (NULL);
free(s2);
}
else
free(s2);
}
return (str);
}
int ft_freenode(t_lst **head, int fd)
{
t_lst *tmp;
t_lst *prev;
tmp = *head;
prev = NULL;
while (tmp->fd != fd && tmp->next != NULL)
{
prev = tmp;
tmp = tmp->next;
}
if (tmp->fd == fd)
{
if (prev)
prev->next = tmp->next;
else
*head = tmp->next;
free(tmp->res);
free(tmp);
return (-1);
}
return (0);
}
int get_next_line(const int fd, char **line)
{
static t_lst *head = NULL;
char buf[BUFF_SIZE + 1];
int count;
t_lst *node;
char *snap;
if (!line || fd < 0 || read(fd, buf, 0) < 0)
return (-1);
if (!(node = ft_lstsearch(fd, head)))
if (!(node = ft_newnode(fd, &head)))
return (-1);
while (node->res != NULL && !(snap = ft_strchr(node->res, '\n'))
&& ((count = read(fd, buf, BUFF_SIZE)) > 0))
if (!(node->res = ft_secure_join(node->res, buf, count, 0)))
return (ft_freenode(&head, fd));
if ((node->res == NULL || *(node->res) == '\0') && ft_freenode(&head, fd))
return (0);
if (node->res != NULL && snap)
*snap = '\0';
if (node->res && !(*line = ft_strdup(node->res)))
return (ft_freenode(&head, fd));
if (!(node->res = ft_secure_join(snap, node->res, 0, 1)) && snap)
return (ft_freenode(&head, fd));
return (1);
}