-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
53 lines (48 loc) · 1.61 KB
/
Copy pathget_next_line.c
File metadata and controls
53 lines (48 loc) · 1.61 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ekutlay <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/29 23:48:06 by ekutlay #+# #+# */
/* Updated: 2022/07/31 03:54:31 by ekutlay ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
char *ft_fit_buff(int fd, char *left_str)
{
char *buff;
int rd_bytes;
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
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(left_str);
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_fit_buff(fd, left_str);
if (!left_str)
return (NULL);
line = ft_catch_new_line(left_str);
left_str = ft_new_next_str(left_str);
return (line);
}