-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
108 lines (99 loc) · 2.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hmoumani <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/16 16:38:49 by hmoumani #+# #+# */
/* Updated: 2019/11/18 17:09:45 by hmoumani ### ########.fr */
/* */
/* ************************************************************************** */
#include "lib.h"
static int ft_double_free(char **p1, char **p2, int ret)
{
free(*p1);
free(*p2);
return (ret);
}
static int ft_lastline(int ret, char **temp)
{
if (!ret && *temp[0] == '\n')
{
free(*temp);
*temp = NULL;
return (1);
}
free(*temp);
*temp = NULL;
return (0);
}
static int ft_printline(int fd, char **rest, char **line, char *temp)
{
char *p;
char *pfree;
int ret;
while ((ret = read(fd, temp, BUFFER_SIZE)))
{
temp[ret] = '\0';
if ((p = ft_strchr(temp, '\n')) != NULL)
{
*p = '\0';
pfree = *line;
if (!(*line = ft_strjoin(*line, temp)))
return (ft_double_free(&temp, NULL, -1));
free(pfree);
pfree = rest[fd];
if (!(rest[fd] = ft_strdup(p + 1)))
return (ft_double_free(&temp, NULL, -1));
return (ft_double_free(&pfree, &temp, 1));
}
pfree = *line;
if (!(*line = ft_strjoin(*line, temp)))
return (ft_double_free(&temp, NULL, -1));
free(pfree);
}
return (ft_lastline(ret, &temp));
}
static int ft_handlerest(char **rest, char **line, char *temp, int fd)
{
char *pfree;
char *p;
if (!rest[fd])
return (0);
if ((p = ft_strchr(rest[fd], '\n')) != NULL)
{
*p = '\0';
pfree = *line;
if (!(*line = ft_strjoin(*line, rest[fd])))
return (ft_double_free(&temp, NULL, -1));
free(pfree);
pfree = rest[fd];
if (!(rest[fd] = ft_strdup(p + 1)))
return (ft_double_free(&temp, NULL, -1));
return (ft_double_free(&pfree, &temp, 1));
}
pfree = *line;
*line = ft_strjoin(*line, rest[fd]);
free(pfree);
pfree = NULL;
free(rest[fd]);
rest[fd] = NULL;
return (0);
}
int get_next_line(int fd, char **line)
{
static char *rest[OPEN_MAX];
char *temp;
int ret;
if (fd < 0 || line == NULL || BUFFER_SIZE < 0 || read(fd, NULL, 0) < 0)
return (-1);
if (!(temp = malloc((BUFFER_SIZE + 1) * sizeof(char))))
ft_error("malloc failed");
temp[0] = 0;
if (!(*line = ft_strdup("")))
return (ft_double_free(&temp, NULL, -1));
if ((ret = ft_handlerest(rest, line, temp, fd)))
return (ret);
return (ft_printline(fd, rest, line, temp));
}