-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
131 lines (120 loc) · 3.29 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
121
122
123
124
125
126
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elanna <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/27 17:01:40 by elanna #+# #+# */
/* Updated: 2021/04/05 23:21:47 by elanna ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void ft_delcont(int fd, t_content **content)
{
t_content *fd_content;
t_content *prev_content;
prev_content = *content;
if (prev_content->fd == fd && !prev_content->next)
{
free(prev_content);
*content = NULL;
return ;
}
else if (prev_content->fd == fd && prev_content->next)
{
*content = prev_content->next;
free(prev_content);
return ;
}
while (prev_content->next != NULL && (prev_content->next)->fd != fd)
{
prev_content = prev_content->next;
}
fd_content = prev_content->next;
prev_content->next = fd_content->next;
free(fd_content);
return ;
}
t_content *ft_lstchr_fd(t_content **lst, int fd)
{
t_content *list;
list = *lst;
while (list)
{
if (fd == list->fd)
return (list);
list = list->next;
}
return (NULL);
}
int get_content_line(int fd, char **line, ssize_t rd, t_content **cont)
{
t_content *fd_content;
char *ptr;
char *tmp;
if (*cont && !(fd_content = ft_lstchr_fd(cont, fd)))
return (-1);
if (rd == 0 && (!*cont || !(fd_content->buffer)))
{
*line = ft_strdup("");
return (0);
}
ptr = ft_strchr(fd_content->buffer, '\n');
if (ptr != NULL)
{
*ptr = '\0';
*line = ft_strdup(fd_content->buffer);
tmp = ft_strdup(++ptr);
free(fd_content->buffer);
fd_content->buffer = tmp;
return (1);
}
*line = fd_content->buffer;
fd_content->buffer = NULL;
ft_delcont(fd, cont);
return (0);
}
void fill_content_struct(t_content **content, char *buffer, int fd)
{
char *tmp_buffer;
t_content *fd_content;
if (!*content)
*content = ft_lstnew(buffer, fd);
else
{
fd_content = ft_lstchr_fd(content, fd);
if (fd_content != NULL)
{
tmp_buffer = ft_strjoin(fd_content->buffer, buffer);
free(fd_content->buffer);
fd_content->buffer = tmp_buffer;
}
else
ft_lstadd_back(content, ft_lstnew(buffer, fd));
}
}
int get_next_line(int fd, char **line)
{
static t_content *content;
char buffer[BUFFER_SIZE + 1];
t_content *fd_content;
ssize_t size_read;
char *new_line;
if (fd < 0 || line == NULL || BUFFER_SIZE <= 0)
return (-1);
while ((size_read = read(fd, buffer, BUFFER_SIZE)) > 0)
{
buffer[size_read] = '\0';
fill_content_struct(&content, buffer, fd);
fd_content = ft_lstchr_fd(&content, fd);
if (!fd_content)
return (-1);
new_line = ft_strchr(fd_content->buffer, '\n');
if (new_line != NULL)
break ;
}
if (size_read < 0)
return (-1);
return (get_content_line(fd, line, size_read, &content));
}