This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
82 lines (74 loc) · 2.08 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jstaunto <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/25 13:51:40 by jstaunto #+# #+# */
/* Updated: 2020/08/25 13:51:40 by jstaunto ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_strjoin_fr(char const *s1, char const *s2)
{
char *res;
res = ft_strjoin(s1, s2);
free((char *)s1);
return (res);
}
void ft_swap_mem(char *str, char **rem)
{
char *tmp;
if (*str)
{
tmp = *rem;
*rem = ft_strdup(str);
free(tmp);
}
}
int ft_rem(char **line, char **rem, char **p_nl)
{
if (*rem && **rem)
{
if ((*p_nl = ft_strchr(*rem, '\n')))
{
**p_nl = '\0';
*line = ft_strjoin_fr(*line, *rem);
ft_swap_mem(++(*p_nl), rem);
if (ft_strequ(*rem, *line))
ft_strclr(*rem);
return (1);
}
else
*line = ft_strjoin_fr(*line, *rem);
if (!ft_strchr(*rem, '\n'))
ft_strclr(*rem);
}
return (0);
}
int get_next_line(const int fd, char **line)
{
char buf[BUFF_SIZE + 1];
char *p_nl;
static char *rem[OPEN_MAX];
int rd;
if (fd < 0 || !line || fd > OPEN_MAX)
return (-1);
*line = ft_strnew(0);
if (ft_rem(line, &rem[fd], &p_nl))
return (1);
while ((rd = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[rd] = '\0';
if ((p_nl = ft_strchr(buf, '\n')))
{
*p_nl = '\0';
ft_swap_mem(++p_nl, &rem[fd]);
*line = ft_strjoin_fr(*line, buf);
return (1);
}
*line = ft_strjoin_fr(*line, buf);
}
return ((**line && !rd) ? 1 : rd);
}