-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
89 lines (80 loc) · 2.24 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mchamayo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/10 15:51:15 by mchamayo #+# #+# */
/* Updated: 2019/02/13 14:25:55 by mchamayo ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
t_list *ft_fd_check(const int fd, t_list **list)
{
t_list *tmp;
tmp = *list;
while (tmp)
{
if (tmp->content_size == (unsigned int)fd)
return (tmp);
tmp = tmp->next;
}
tmp = ft_lstnew("\0", fd);
ft_lstadd(list, tmp);
tmp = *list;
return (tmp);
}
void ft_copyline(char **line, char *str, char s)
{
int i;
i = 0;
while (str[i] != s && str[i])
i++;
*line = ft_strnew(i + 1);
*line = ft_strncpy(*line, str, i);
}
char *ft_dup(char *str, char s)
{
int len;
int i;
char *tmp;
i = 0;
len = ft_strlen(str);
while ((str[i] != s) && str[i])
i++;
if (i == len)
ft_strclr(str);
else
{
tmp = str;
str = ft_strdup(str + i + 1);
free(tmp);
}
return (str);
}
int get_next_line(const int fd, char **line)
{
t_list *list;
static t_list *newlist = NULL;
char buf[BUFF_SIZE + 1];
int ret;
char *tmp;
if (line == NULL || fd < 0 || (!(list = ft_fd_check(fd, &newlist)))
|| read(list->content_size, buf, BUFF_SIZE < 0))
return (-1);
while ((ret = read(list->content_size, buf, BUFF_SIZE)) > 0)
{
buf[ret] = '\0';
tmp = list->content;
list->content = ft_strjoin(list->content, buf);
free(tmp);
if (ft_strchr(list->content, '\n'))
break ;
}
if (ret <= BUFF_SIZE && !ft_strlen(list->content))
return (0);
ft_copyline(line, list->content, '\n');
list->content = ft_dup(list->content, '\n');
return (1);
}