-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
79 lines (72 loc) · 2.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tdenion <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/31 18:02:28 by tdenion #+# #+# */
/* Updated: 2016/04/07 21:22:25 by tdenion ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static t_gnl *add_line(t_gnl *tmp)
{
char buff[BUFF_SIZE + 1];
int ret;
int rded;
rded = 0;
ret = read(tmp->fds, buff, BUFF_SIZE);
tmp->rs = ret;
if (tmp->rs < 0)
return (tmp);
buff[ret] = '\0';
tmp->saved_readed = ft_strfjoin(tmp->saved_readed, buff);
while (tmp->saved_readed[rded] != '\n' && tmp->saved_readed[rded] != '\0')
rded++;
tmp->readed_c = rded;
if (tmp->saved_readed[rded] != '\n' && ret)
return (add_line(tmp));
return (tmp);
}
static t_gnl *lst_chr(const int fd)
{
static t_gnl *l = NULL;
t_gnl *tmp;
tmp = l;
while (tmp)
{
if (tmp->fds == fd)
return (tmp);
tmp = tmp->next;
}
tmp = (t_gnl *)malloc(sizeof(t_gnl));
tmp->fds = fd;
tmp->saved_readed = NULL;
tmp->next = l;
l = tmp;
return (tmp);
}
int get_next_line(const int fd, char **line)
{
t_gnl *tmp;
char *t;
t = NULL;
if (fd < 0 || BUFF_SIZE < 1 || !line)
return (-1);
tmp = add_line(lst_chr(fd));
if (tmp->rs < 0)
return (-1);
*line = ft_strsub(tmp->saved_readed, 0, tmp->readed_c);
if (tmp->saved_readed[0] == '\0')
return (0);
t = tmp->saved_readed;
if (tmp->saved_readed[tmp->readed_c] == '\n')
{
tmp->saved_readed = ft_strdup(&(tmp->saved_readed)[tmp->readed_c + 1]);
free(t);
}
else
tmp->saved_readed[0] = '\0';
return (1);
}