-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_one_line.c
135 lines (124 loc) · 3.21 KB
/
get_one_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
123
124
125
126
127
128
129
130
131
132
133
134
135
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_one_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schaaban <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/23 19:09:00 by schaaban #+# #+# */
/* Updated: 2017/11/30 16:04:44 by schaaban ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "get_one_line.h"
#include <stdlib.h>
#include <unistd.h>
static int gnl_add(t_line **remain, int fd, char *str)
{
t_line *new;
if ((new = (t_line*)malloc(sizeof(t_line))) == NULL)
return (0);
new->content = str;
new->fd = fd;
new->next = NULL;
if (*remain == NULL)
{
(*remain) = new;
return (1);
}
if (new != NULL)
{
new->next = *remain;
*remain = new;
}
return (1);
}
static t_line *gnl_remain(t_line **remain, int fd)
{
t_line *cpy;
cpy = (*remain);
while (cpy != NULL)
{
if (cpy->fd == fd)
{
return (cpy);
}
cpy = cpy->next;
}
if (gnl_add(remain, fd, NULL) == 0)
return (NULL);
return (*remain);
}
static char *gnl_split(char *str, char **remain, int *end)
{
char *line;
int i;
i = -1;
while (str[++i] != '\0')
{
if (str[i] == '\n')
{
*end = 1;
if ((line = ft_strnew(i + 1)) == NULL ||
(*remain = ft_strdup(str + i + 1)) == NULL)
return (NULL);
if (ft_strlen(*remain) == 0)
{
free(*remain);
*remain = NULL;
}
line = ft_strncpy(line, str, i + 1);
return (line);
}
}
if ((line = ft_strdup(str)) == NULL)
return (NULL);
return (line);
}
static int gnl_read(char **line, t_line **remain, int fd, char *buff)
{
char *to_add;
char *prev_line;
char *prev_buff;
int end;
end = 0;
if ((prev_buff = ft_strdup(buff)) == NULL)
return (1);
if (gnl_remain(remain, fd)->content != NULL)
{
free(gnl_remain(remain, fd)->content);
gnl_remain(remain, fd)->content = NULL;
}
to_add = gnl_split(prev_buff, &(gnl_remain(remain, fd)->content), &end);
free(prev_buff);
prev_line = *line;
if ((*line = ft_strjoin(prev_line, to_add)) == NULL)
return (1);
free(to_add);
if (prev_line != NULL)
free(prev_line);
return (end);
}
int get_one_line(const int fd, char **line)
{
static t_line *remain;
char *buff;
int bytes_read;
int end;
end = 0;
bytes_read = 0;
if (line == NULL || fd < 0 || (buff = ft_strnew(BUFF_SIZE)) == NULL)
return (-1);
*line = NULL;
if (gnl_remain(&remain, fd)->content != NULL)
end = gnl_read(line, &remain, fd, gnl_remain(&remain, fd)->content);
while (!end && (bytes_read = read(fd, buff, BUFF_SIZE)) > 0)
{
end = gnl_read(line, &remain, fd, buff);
ft_strclr(buff);
}
free(buff);
if ((bytes_read == 0 && *line == NULL) || bytes_read < 0)
return ((bytes_read < 0) ? -1 : 0);
return (1);
}