-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
executable file
·125 lines (114 loc) · 3 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vroussea <vroussea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/05 22:07:54 by vroussea #+# #+# */
/* Updated: 2016/02/17 17:25:27 by vroussea ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
static int cleaner(t_file *crt, int ret)
{
t_file *tmp;
int strlen;
char *tmpstring;
if (ret != 0)
{
tmpstring = ft_strsub(crt->str,
ft_strchr(crt->str, '\n') - crt->str + 1, ft_strlen(crt->str) -
(ft_strchr(crt->str, '\n') - crt->str));
ft_strdel(&(crt->str));
crt->str = tmpstring;
}
strlen = ft_strlen(crt->str);
if (ret == 0 && crt && crt->prev)
{
tmp = crt->prev;
tmp->next = crt->next;
ft_strdel(&crt->str);
ft_memdel((void **)&crt);
crt = NULL;
}
return (strlen);
}
static t_file *new_node(int const fd)
{
t_file *new;
new = (t_file *)ft_memalloc(sizeof(t_file));
new->fd = fd;
new->str = ft_strnew(BUFF_SIZE);
new->next = NULL;
new->prev = NULL;
return (new);
}
static t_file *test_node(int const fd, t_file *start)
{
t_file *current;
t_file *tmp;
current = start;
tmp = start;
while (current)
{
if (current->fd == fd)
{
return (current);
}
tmp = current;
current = current->next;
}
current = new_node(fd);
tmp->next = current;
if (current->fd != start->fd)
current->prev = tmp;
return (current);
}
static int reader(int ret, t_file *crt, int fd)
{
char *buff;
char *tmp;
if (BUFF_SIZE <= 0 || !(buff = (char *)ft_memalloc(BUFF_SIZE + 1)))
return (-1);
while (!ft_strchr(crt->str, '\n') && ret > 0)
{
ret = read(fd, buff, BUFF_SIZE);
if (ret == -1)
return (-1);
buff[ret] = '\0';
if (ret > 0)
{
tmp = crt->str;
crt->str = ft_strjoin(crt->str, buff);
ft_strdel(&tmp);
}
}
ft_strdel(&buff);
return (ret);
}
int get_next_line(int const fd, char **line)
{
static t_file *start = NULL;
t_file *crt;
int strlen;
int ret;
if (start == NULL)
start = new_node(0);
ret = 1;
crt = test_node(fd, start);
if ((ret = reader(ret, crt, fd)) == -1)
return (-1);
if (crt->str)
*line = (!ft_strchr(crt->str, '\n') ?
ft_strsub(crt->str, 0, ft_strlen(crt->str)) :
ft_strsub(crt->str, 0, ft_strchr(crt->str, '\n') - crt->str));
strlen = cleaner(crt, ret);
if (crt->fd == 0 && ret == 0)
{
ft_strdel(&(crt->str));
crt->str = ft_strnew(BUFF_SIZE);
}
return ((ret > 0) || (strlen > 0));
}