-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
113 lines (102 loc) · 2.53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: seshevch <seshevch@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/10 11:35:17 by seshevch #+# #+# */
/* Updated: 2018/11/14 19:48:07 by seshevch ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include "libft.h"
void ft_ss(t_apok *lst, char **line, unsigned int len)
{
char *join;
char *sub;
sub = ft_strsub(lst->data, lst->n_elm, len);
join = ft_strjoin(*line, sub);
free(sub);
free(*line);
*line = join;
}
int ft_write(t_apok *lst, char **line, int nbread)
{
unsigned int i;
char *s;
*line == NULL ? *line = ft_strnew(0) : 0;
if ((s = ft_strchr((lst->data + lst->n_elm), '\n')) != 0)
{
i = s - (lst->data + lst->n_elm);
ft_ss(lst, line, i);
lst->n_elm += i + 1;
lst->nb = nbread;
return (1);
}
else
{
ft_ss(lst, line, nbread - lst->n_elm);
lst->n_elm = 0;
return (0);
}
}
int ft_read(t_apok *lst, char **line)
{
int nbread;
if (ft_write(lst, line, lst->nb) == 1)
return (1);
while ((nbread = read(lst->fd_cp, lst->data, BUFF_SIZE)))
{
lst->data[nbread] = '\0';
if (ft_write(lst, line, nbread) == 1)
return (1);
}
free(lst->data);
lst->data = ft_strnew(0);
if (**line != '\0')
return (1);
else
{
free(*line);
*line = NULL;
return (0);
}
}
void ft_new_lst(t_apok **lst, const int fd)
{
t_apok *s1;
s1 = (t_apok *)malloc(sizeof(t_apok));
if (s1)
{
s1->data = ft_strnew(BUFF_SIZE);
if (s1->data)
{
s1->fd_cp = fd;
s1->n_elm = 0;
s1->nb = 0;
s1->next = *lst;
*lst = s1;
}
else
free(s1);
}
}
int get_next_line(const int fd, char **line)
{
static t_apok *lst = NULL;
t_apok *s1;
if (fd < 0 || fd > 4863 || !line || read(fd, 0, 0) < 0)
return (-1);
if (*line != NULL)
*line = NULL;
s1 = lst;
while (s1)
{
if (s1->fd_cp == fd)
return (ft_read(s1, line));
s1 = s1->next;
}
ft_new_lst(&lst, fd);
return (ft_read(lst, line));
}