-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
92 lines (84 loc) · 2.02 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asibille <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/28 15:20:50 by asibille #+# #+# */
/* Updated: 2022/01/31 11:26:38 by asibille ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *get_next_line(int fd)
{
static char *buf[256];
char *str;
int ret;
if (BUFFER_SIZE < 0 || (fd < 0) || (fd > 255))
return (NULL);
str = NULL;
if (buf[fd])
str = ft_buf_to_str_newbuf(&buf[fd]);
while (!str)
{
buf[fd] = ft_strjoin_buffer(buf[fd]);
ret = read(fd, &buf[fd][ft_strlen(buf[fd])], BUFFER_SIZE);
if (ret == -1)
return (ft_no_read_case(&buf[fd]));
else if (ret == 0)
{
str = ft_nul_return(&buf[fd]);
return (str);
}
else
str = ft_buf_to_str_newbuf(&buf[fd]);
}
return (str);
}
char *ft_buf_to_str_newbuf(char **buf)
{
char *str;
char *buf_cpy;
int i;
i = 0;
buf_cpy = *buf;
while (*buf_cpy)
{
if (*buf_cpy == '\n')
{
str = *buf;
buf_cpy = ft_strdup(buf_cpy + 1);
str[i + 1] = 0;
*buf = buf_cpy;
return (str);
}
++(buf_cpy);
++i;
}
return (NULL);
}
char *ft_nul_return(char **buf)
{
char *buf_cpy;
char *str;
buf_cpy = *buf;
if (ft_strlen(buf_cpy))
{
str = buf_cpy;
*buf = NULL;
return (str);
}
else
{
free(buf_cpy);
*buf = NULL;
return (NULL);
}
}
char *ft_no_read_case(char **buf)
{
free(*buf);
*buf = NULL;
return (NULL);
}