-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
128 lines (119 loc) · 3.21 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlambert <mlambert@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/07 13:34:06 by mlambert #+# #+# */
/* Updated: 2017/04/09 16:19:02 by mlambert ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
#include <unistd.h>
static t_list *ft_fd(t_list **lst, int fd)
{
t_list *tmp;
if ((tmp = *lst) != NULL)
{
while (tmp->next && (int)tmp->content_size != fd)
tmp = tmp->next;
if ((int)tmp->content_size == fd)
return (tmp);
if (!(tmp->next = (t_list *)malloc(sizeof(t_list))))
return (NULL);
tmp = tmp->next;
tmp->content_size = fd;
tmp->content = NULL;
tmp->next = NULL;
}
else
{
if (!(tmp = (t_list *)malloc(sizeof(t_list))))
return (NULL);
tmp->content_size = fd;
tmp->content = NULL;
tmp->next = NULL;
*lst = tmp;
}
return (tmp);
}
static char *cpy(t_list *tmp, char *buff, char *sub)
{
long int n;
char *tmp2;
char *line;
line = NULL;
n = sub - buff;
if (tmp->content == NULL)
{
if (n < BUFF_SIZE)
tmp->content = ft_strsub(buff, n + 1, BUFF_SIZE - n);
return (ft_strndup(buff, n));
}
if (tmp->content != NULL && (sub = ft_strchr(tmp->content, '\n')))
{
n = sub - (char *)tmp->content;
line = ft_strndup(tmp->content, n);
tmp2 = tmp->content;
tmp->content = ft_strsub(tmp2, n + 1, (ft_strlen(tmp2) - 1 - n));
free(tmp2);
return (line);
}
return (line);
}
static char *ft_alljob(t_list *tmp, char *buff, char *sub, int ret)
{
char *line;
long int n;
char *tmp2;
line = NULL;
buff[ret] = '\0';
tmp->content = tmp->content == NULL ? ft_strnew(0) : tmp->content;
if ((sub = ft_strchr(buff, '\n')))
{
n = sub - buff;
tmp2 = ft_strndup(buff, n);
line = ft_strjoin(tmp->content, tmp2);
free(tmp2);
free(tmp->content);
if (n < BUFF_SIZE)
tmp->content = ft_strsub(buff, n + 1, BUFF_SIZE - n);
return (line);
}
else
{
tmp2 = tmp->content;
tmp->content = ft_strjoin(tmp->content, buff);
free(tmp2);
}
return (line);
}
int get_next_line(int fd, char **line)
{
static t_list *lst = NULL;
int ret;
t_list *tmp;
char *sub;
char buff[BUFF_SIZE + 1];
if (fd < 0 || line == NULL || !(tmp = ft_fd(&lst, fd)))
return (-1);
if (tmp->content != NULL && (sub = ft_strchr(tmp->content, '\n')))
{
*line = cpy(tmp, buff, sub);
return (1);
}
while ((ret = read(fd, buff, BUFF_SIZE)) && ret > 0)
{
*line = ft_alljob(tmp, buff, sub, ret);
if (*line != NULL)
return (1);
}
if (tmp->content != NULL && (*line = ft_strdup(tmp->content)))
{
ft_memdel(&tmp->content);
return (**line ? 1 : 0);
}
return (ret);
}