-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
120 lines (110 loc) · 2.55 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_v2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fde-los- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/05 22:22:24 by fde-los- #+# #+# */
/* Updated: 2016/11/30 02:40:45 by fde-los- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <fcntl.h>
t_node *create_node(const int fd)
{
t_node *new;
if (!(new = (t_node *)malloc(sizeof(t_node))))
return (NULL);
new->fd = fd;
new->str = ft_strnew(0);
new->next = NULL;
return (new);
}
void add_node(t_node *node, t_node *new_node)
{
while (node)
{
if (node->next == NULL)
{
node->next = new_node;
new_node->next = NULL;
}
node = node->next;
}
}
char *get_line(t_node *node, char **line)
{
char *temp;
char *str;
int i;
temp = NULL;
i = 0;
str = node->str;
while (str[i])
{
if (str[i] == NL)
{
*line = ft_strsub(str, 0, i);
temp = str;
str = ft_strdup(str + (i + 1));
free(temp);
return (str);
}
i++;
}
*line = ft_strdup(str);
ft_strclr(str);
ft_strclr(node->str);
return (str);
}
int read_file(int fd, t_node *node)
{
int ret;
char buffer[BUFF_SIZE + 1];
char *temp;
temp = NULL;
ret = -3;
while (!ft_strchr(node->str, NL))
{
if ((ret = read(fd, buffer, BUFF_SIZE)) < 0)
return (-1);
else
{
buffer[ret] = 0;
temp = node->str;
if (!(node->str = ft_strjoin(node->str, buffer)))
return (-1);
free(temp);
}
if (ret < BUFF_SIZE)
return (ret);
}
return (ret);
}
int get_next_line(const int fd, char **line)
{
static t_node *head;
t_node *temp;
int ret;
if (!head)
head = create_node(fd);
temp = head;
if (fd < 0 || !line)
return (-1);
while (temp)
{
if (temp->fd == fd)
break ;
if (temp->next == NULL)
add_node(temp, create_node(fd));
temp = temp->next;
}
if ((ret = read_file(fd, temp)) == -1)
return (-1);
temp->str = get_line(temp, line);
if (!ft_strlen(temp->str) && !ft_strlen(*line) && !ret)
return (0);
else
return (1);
}