-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
133 lines (121 loc) · 3.09 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: acaceres <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 07:52:22 by acaceres #+# #+# */
/* Updated: 2023/09/11 06:34:56 by acaceres ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static void create_list(int fd, t_list **lst);
static char *set_line(t_list **lst, char *line);
char *get_next_line(int fd)
{
static t_list *lst;
char *line;
char *tmp;
line = NULL;
tmp = NULL;
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, 0, 0) == -1)
return (ft_lstclear(&lst, free), NULL);
if (BUFFER_SIZE == 1)
return (buff_size_1(fd, line, tmp));
create_list(fd, &lst);
line = set_line(&lst, line);
if (!line)
return (NULL);
return (line);
}
static void create_list(int fd, t_list **lst)
{
t_list *node;
char *buff;
int _r;
_r = 1;
node = NULL;
if (*lst && (*lst)->nl != -1)
return ;
buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return (ft_lstclear(lst, free));
while (_r > 0)
{
_r = read(fd, buff, BUFFER_SIZE);
if (_r == -1)
return (free(buff), ft_lstclear(lst, free));
buff[_r] = '\0';
node = create_node(buff);
if (!node)
return (free(buff), ft_lstclear(lst, free));
ft_lstadd_back(lst, node);
if (node->nl != -1)
return (free(buff));
}
free(buff);
}
static ssize_t line_len(t_list **lst)
{
t_list *tmp;
ssize_t len;
len = 0;
if (!*lst)
return (0);
tmp = *lst;
while (tmp)
{
if (tmp->nl == -1)
len += tmp->len;
else
return (len += tmp->nl + 1, len);
tmp = tmp->next;
}
return (len);
}
static char
*fill_line(t_list **lst, t_list *tmp, t_list *new_lst, char *line)
{
int i;
int j;
i = -1;
j = 0;
while (tmp)
{
i = -1;
while (tmp->content[++i])
{
if (tmp->content[i] == '\n')
{
line[j++] = tmp->content[i];
line[j] = '\0';
new_lst = create_node(&tmp->content[++i]);
if (!new_lst)
return (NULL);
return (ft_lstclear(lst, free),
ft_lstadd_back(lst, new_lst), line);
}
line[j++] = tmp->content[i];
}
tmp = tmp->next;
}
return (ft_lstclear(lst, free), line[j] = '\0', line);
}
static char *set_line(t_list **lst, char *line)
{
t_list *new_lst;
t_list *tmp;
tmp = NULL;
new_lst = NULL;
if (!*lst)
return (NULL);
tmp = *lst;
line = malloc(sizeof(char) * (line_len(lst) + 1));
if (!line)
return (ft_lstclear(lst, free), NULL);
line = fill_line(lst, tmp, new_lst, line);
if (!line || line[0] == '\0')
return (free(line), line = NULL, NULL);
return (line);
}