-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
92 lines (83 loc) · 2.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: salomon <salomon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/03/16 16:16:15 by sbeline #+# #+# */
/* Updated: 2016/06/01 18:49:55 by salomon ### ########.fr */
/* */
/* ************************************************************************** */
#include "Includes/libft.h"
static t_fd *new_data_fd(int const fd)
{
t_fd *new;
if ((new = (t_fd *)malloc(sizeof(t_fd))) != NULL)
{
new->fd = fd;
new->nl = 0;
new->pos_nl = 0;
new->ret = 0;
new->buf = NULL;
new->next = NULL;
}
return (new);
}
static t_fd *get_data_fd(t_fd **lst_fd, int const fd)
{
t_fd *tmp;
if (*lst_fd == NULL)
return ((*lst_fd = new_data_fd(fd)));
tmp = *lst_fd;
while (tmp)
{
if (tmp->fd == fd)
return (tmp);
else if (tmp->next == NULL)
return ((tmp->next = new_data_fd(fd)));
tmp = tmp->next;
}
return (NULL);
}
static void ft_read(t_fd *d_fd)
{
char *tmp;
tmp = (char *)malloc(sizeof(char) * (BUF_SIZE + 1));
if ((d_fd->ret = read(d_fd->fd, tmp, BUF_SIZE)) == -1)
return ;
tmp[d_fd->ret] = '\0';
d_fd->buf = ft_stradd(&d_fd->buf, &tmp);
while ((d_fd->pos_nl = ft_strchr(d_fd->buf, '\n')) == 0 && d_fd->ret > 0)
{
tmp = (char *)malloc(sizeof(char) * (BUF_SIZE + 1));
if ((d_fd->ret = read(d_fd->fd, tmp, BUF_SIZE)) == -1)
return ;
tmp[d_fd->ret] = '\0';
d_fd->buf = ft_stradd(&d_fd->buf, &tmp);
}
if (d_fd->ret > 0)
d_fd->ret = 1;
}
int get_next_line(int const fd, char **line)
{
static t_fd *lst_fd = NULL;
t_fd *d_fd;
if (fd < 0 || line == NULL || (d_fd = get_data_fd(&lst_fd, fd)) == NULL)
return (-1);
if (d_fd->buf == NULL)
d_fd->buf = ft_strnew(BUF_SIZE + 1, "");
if ((d_fd->pos_nl = ft_strchr(d_fd->buf, '\n')) == 0)
ft_read(d_fd);
if (d_fd->ret == 0 && d_fd->pos_nl == 0)
d_fd->pos_nl = ft_strlen(d_fd->buf) + 1;
if (d_fd->ret != -1 && d_fd->pos_nl != 0)
{
*line = ft_strsub(d_fd->buf, 0, d_fd->pos_nl - 1);
ft_strmove(&(d_fd->buf), d_fd->pos_nl);
d_fd->pos_nl = 0;
if (*d_fd->buf == '\0' && d_fd->ret == 0)
ft_strdel(&(d_fd->buf));
}
return (d_fd->ret);
}