-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
163 lines (148 loc) · 3.56 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaume <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/29 03:48:00 by lgaume #+# #+# */
/* Updated: 2023/11/01 11:17:07 by lgaume ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *get_next_line(int fd)
{
static t_list *stash;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, &line, 0) < 0)
{
free_stash(stash);
stash = NULL;
return (NULL);
}
read_and_stash(fd, &stash);
if (!stash)
return (NULL);
extract_line(stash, &line);
clean_stash(&stash);
if (!line[0])
{
free_stash(stash);
stash = NULL;
free(line);
return (NULL);
}
return (line);
}
/* Uses read() to add characteres to the stash */
void read_and_stash(int fd, t_list **stash)
{
int readed;
char *buff;
readed = 1;
while (!found_newline(*stash) && readed != 0)
{
buff = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buff)
return ;
readed = (int)read(fd, buff, BUFFER_SIZE);
if (readed == -1)
{
free_stash(*stash);
*stash = NULL;
return ;
}
if ((!*stash && !readed) || readed == -1)
{
free(buff);
return ;
}
buff[readed] = '\0';
add_to_stash(stash, buff, readed);
free(buff);
}
}
/* Adds the content of the buffer to the end of the stash */
void add_to_stash(t_list **stash, char *buff, int readed)
{
int i;
t_list *last;
t_list *new;
new = malloc(sizeof(t_list));
if (!new)
return ;
new->next = NULL;
new->content = malloc(sizeof(char) * (readed + 1));
if (!new->content)
return ;
i = 0;
while (buff[i] && i < readed)
{
new->content[i] = buff[i];
i++;
}
new->content[i] = '\0';
if (!*stash)
*stash = new;
else
{
last = ft_lst_get_last(*stash);
last->next = new;
}
}
/* Extracts all characters from our stash and stores them in out line.
* stopping after the first \n it encounters */
void extract_line(t_list *stash, char **line)
{
int i;
int j;
if (!stash)
return ;
generate_line(line, stash);
if (!*line)
return ;
j = 0;
while (stash)
{
i = 0;
while (stash->content[i])
{
if (stash->content[i] == '\n')
{
(*line)[j++] = stash->content[i];
break ;
}
(*line)[j++] = stash->content[i++];
}
stash = stash->next;
}
(*line)[j] = '\0';
}
/* This function clears the stash so only the characters that have not
* been returned at the end of get_next_line() remain in the STATIC stash */
void clean_stash(t_list **stash)
{
t_list *last;
t_list *clean;
int i;
int j;
clean = malloc(sizeof(t_list));
if (!clean)
return ;
clean->next = NULL;
last = ft_lst_get_last(*stash);
i = 0;
while (last->content[i] && last->content[i] != '\n')
i++;
if (last->content && last->content[i] == '\n')
i++;
clean->content = malloc(sizeof(char) * (ft_strlen(last->content) - i + 1));
if (!clean->content)
return ;
j = 0;
while (last->content[i])
clean->content[j++] = last->content[i++];
clean->content[j] = '\0';
free_stash(*stash);
*stash = clean;
}