-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
52 lines (47 loc) · 1.61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: muhozkan <muhozkan@student.42.tr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/27 14:22:58 by muhozkan #+# #+# */
/* Updated: 2022/02/27 14:23:03 by muhozkan ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_read_to_left_str(int fd, char *left_str)
{
char *buff;
int rd_bytes;
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
return (NULL);
rd_bytes = 1;
while (!ft_strchr(left_str, '\n') && rd_bytes != 0)
{
rd_bytes = read(fd, buff, BUFFER_SIZE);
if (rd_bytes == -1)
{
free(buff);
return (NULL);
}
buff[rd_bytes] = '\0';
left_str = ft_strjoin(left_str, buff);
}
free(buff);
return (left_str);
}
char *get_next_line(int fd)
{
char *line;
static char *left_str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (0);
left_str = ft_read_to_left_str(fd, left_str);
if (!left_str)
return (NULL);
line = ft_get_line(left_str);
left_str = ft_new_left_str(left_str);
return (line);
}