This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
108 lines (97 loc) · 2.52 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: cfatrane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/21 13:51:49 by cfatrane #+# #+# */
/* Updated: 2016/12/27 16:57:00 by cfatrane ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static void ft_list_add_last(t_gnl **save, t_gnl *elem)
{
t_gnl *list;
list = *save;
while (list->next != NULL)
list = list->next;
list->next = elem;
}
static t_gnl *ft_create_list(int fd)
{
t_gnl *list;
if (!(list = (t_gnl*)malloc(sizeof(*list))))
return (NULL);
list->fd = fd;
list->tempo = ft_strnew(0);
list->text = NULL;
list->next = NULL;
return (list);
}
static t_gnl *ft_check_fd(t_gnl *save, int fd)
{
t_gnl *tmp;
t_gnl *d_list;
tmp = NULL;
d_list = save;
while (d_list)
{
if (d_list->fd == fd)
return (d_list);
if (!(d_list->next))
{
tmp = ft_create_list(fd);
ft_list_add_last(&d_list, tmp);
return (tmp);
}
d_list = d_list->next;
}
return (NULL);
}
static int ft_check(char *save, char **line)
{
char *fin;
if (!save)
return (0);
fin = ft_strchr(save, '\n');
if (fin != NULL)
{
*fin = '\0';
*line = ft_strdup(save);
ft_strncpy(save, &fin[1], ft_strlen(&fin[1]) + 1);
return (1);
}
else if (ft_strlen(save) > 0)
{
*line = ft_strdup(save);
*save = '\0';
return (1);
}
return (0);
}
int get_next_line(const int fd, char **line)
{
char buf[BUFF_SIZE + 1];
static t_gnl *save = NULL;
t_gnl *tmp;
int ret;
if (!(save))
save = ft_create_list(fd);
if (fd == -1 || line == NULL || BUFF_SIZE <= 0)
return (-1);
tmp = ft_check_fd(save, fd);
while (!(ft_strchr(tmp->tempo, '\n')))
{
ret = read(fd, buf, BUFF_SIZE);
if (ret == -1)
return (-1);
if (ret == 0)
return (ft_check(tmp->text, line));
buf[ret] = '\0';
tmp->text = ft_strjoin(tmp->tempo, buf);
free(tmp->tempo);
tmp->tempo = tmp->text;
}
return (ft_check(tmp->text, line));
}