/
get_next_line.c
122 lines (114 loc) · 3.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jubalest <jubalest@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/03/09 17:59:06 by jubalest #+# #+# */
/* Updated: 2015/03/09 18:11:23 by jubalest ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "libft.h"
#include <unistd.h>
#include <stdlib.h>
static int treat_no_remain(char **line, char **remain, int fd, char *buf)
{
if (ft_strchr(buf, '\n'))
{
if (**line)
*line = ft_strjoin(ft_strdup(*line),
ft_strndup(buf, ft_strchr(buf, '\n') - buf));
else
*line = ft_strndup(buf, ft_strchr(buf, '\n') - buf);
remain[fd] = ft_strdup(ft_strchr(buf, '\n') + 1);
return (*line && remain[fd] ? 1 : -1);
}
if (**line)
*line = ft_strjoin(ft_strdup(*line), buf);
else
*line = ft_strdup(buf);
return (*line && !remain ? 0 : -1);
}
static int endl_in_remain(char **line, char **remain, int fd, char *buf)
{
if (**line)
*line = ft_strjoin(ft_strdup(*line), ft_strndup(remain[fd],
ft_strchr(remain[fd], '\n') - remain[fd]));
else
*line = ft_strndup(remain[fd],
ft_strchr(remain[fd], '\n') - remain[fd]);
remain[fd] = ft_strjoin(ft_strdup(ft_strchr(remain[fd], '\n') + 1),
ft_strdup(buf));
return (*line && remain[fd] ? 1 : -1);
}
static int no_endl_in_remain(char **line, char *remain, char *buf, int ret)
{
if (**line)
{
*line = ft_strjoin(ft_strdup(*line), remain);
if (ret > 0)
remain = ft_strdup(buf);
}
else
{
*line = ft_strdup(remain);
if (ret > 0)
remain = ft_strdup(buf);
}
if (ret < 1)
{
free(remain);
remain = ft_strnew(0);
}
return (**line && remain ? ret : -1);
}
static int stream_to_line(int const fd, char *buf,
char **line, char **remain)
{
int ret;
ret = 0;
if (!line || !remain[fd])
return (-1);
while (*remain[fd] != '\0' || ((int)ft_strlen(buf) < ret ? (0) :
(ret = read(fd, buf, BUFF_SIZE)) > 0))
{
buf[ret] = '\0';
if (*remain[fd] == '\0')
{
if (treat_no_remain(line, remain, fd, buf) == 1)
return (1);
}
else if (ft_strchr(remain[fd], '\n'))
return (endl_in_remain(line, remain, fd, buf));
else
{
if (no_endl_in_remain(line, remain[fd], buf, ret) == -1)
return (-1);
}
}
if (ret > 0 || *remain[fd] != '\0' || **line != '\0')
return (1);
return (ret < 0 ? -1 : 0);
}
int get_next_line(int const fd, char **line)
{
static char *remain[MAX_FD];
int stream_ret;
char buf[BUFF_SIZE];
stream_ret = 0;
if (fd < 0 || !line || BUFF_SIZE < 1 ||
BUFF_SIZE > MAX_BUFF_SIZE)
return (-1);
*line = ft_strnew(0);
if (!remain[fd])
remain[fd] = ft_strnew(0);
stream_ret = stream_to_line(fd, buf, line, remain);
if (stream_ret > 0)
return (1);
if (*remain[fd])
ft_strdel(&remain[fd]);
ft_strdel(line);
return (stream_ret);
}