-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
141 lines (129 loc) · 3.49 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
132
133
134
135
136
137
138
139
140
141
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: younhwan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/26 13:54:57 by younhwan #+# #+# */
/* Updated: 2022/08/13 13:30:43 by younhwan ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_next_line(int fd);
int ft_read_fd(int fd, t_list *fd_list);
char *ft_get_line_from_fd_list(int fd, t_list **fd_list);
int ft_remove_line_from_fd_list(t_list **cur_list, t_list **fd_list);
int ft_keep_reading(t_list *cur_list);
char *get_next_line(int fd)
{
char *line;
static t_list *fd_list;
t_list *to_del;
if (read(fd, 0, 0) < 0 || BUFFER_SIZE <= 0)
return (0);
if (!fd_list)
{
fd_list = ft_get_fd_list(fd, fd_list);
if (!fd_list)
return (0);
}
if (!ft_read_fd(fd, fd_list))
{
to_del = ft_get_fd_list(fd, fd_list);
while (to_del)
ft_remove_line_from_fd_list(&to_del, &fd_list);
return (0);
}
line = ft_get_line_from_fd_list(fd, &fd_list);
return (line);
}
int ft_read_fd(int fd, t_list *fd_list)
{
t_list *list_tmp;
t_node *node_tmp;
int read_bytes;
list_tmp = ft_get_fd_list(fd, fd_list);
if (!list_tmp)
return (0);
read_bytes = 1;
while (ft_keep_reading(list_tmp) && read_bytes)
{
node_tmp = ft_insert_node_to_cur_list(list_tmp);
if (!node_tmp)
return (0);
read_bytes = read(fd, node_tmp->buff, BUFFER_SIZE - 1);
if (read_bytes == -1)
return (0);
node_tmp->buff[read_bytes] = '\0';
}
return (1);
}
char *ft_get_line_from_fd_list(int fd, t_list **fd_list)
{
char *ret;
size_t last_idx;
t_list *list_tmp;
t_node *node_tmp;
ret = 0;
list_tmp = ft_get_fd_list(fd, *fd_list);
node_tmp = list_tmp->head;
last_idx = 1;
while (node_tmp && (!ret || (ret && ret[last_idx - 1] != '\n')))
{
if (node_tmp->buff[node_tmp->idx])
last_idx = ft_strlcat(&ret, node_tmp);
if (last_idx == SIZE_MAX)
while (list_tmp)
if (ft_remove_line_from_fd_list(&list_tmp, fd_list))
return (0);
if (!node_tmp->buff[node_tmp->idx])
{
node_tmp = node_tmp->next;
ft_remove_line_from_fd_list(&list_tmp, fd_list);
}
}
return (ret);
}
int ft_remove_line_from_fd_list(t_list **cur_list, t_list **fd_list)
{
t_node *node_to_del;
t_list *list_prev;
node_to_del = (*cur_list)->head;
(*cur_list)->head = (*cur_list)->head->next;
free(node_to_del);
if ((*cur_list)->head)
return (0);
if ((*cur_list) == (*fd_list))
{
*fd_list = (*fd_list)->next;
free(*cur_list);
(*cur_list) = 0;
return (1);
}
list_prev = *fd_list;
while (list_prev->next->fd != (*cur_list)->fd)
list_prev = list_prev->next;
list_prev->next = (*cur_list)->next;
free((*cur_list));
(*cur_list) = 0;
return (1);
}
int ft_keep_reading(t_list *cur_list)
{
t_node *tmp;
size_t i;
tmp = cur_list->tail;
if (!tmp)
return (1);
if (!tmp->buff[0])
return (0);
i = tmp->idx;
while (tmp->buff[i])
{
if (tmp->buff[i] == '\n')
return (0);
i++;
}
return (1);
}