-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
122 lines (112 loc) · 2.89 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pcrosnie <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/13 14:25:50 by pcrosnie #+# #+# */
/* Updated: 2016/01/26 14:51:13 by pcrosnie ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void ft_fill_tab(char *buf, t_line *ptr)
{
int i;
int j;
int k;
i = ptr->lasti;
j = ptr->lastj;
k = 0;
while (buf[k])
{
if (buf[k] == '\n' && buf[k++])
{
i++;
j = 0;
if (!(ptr->read[i]))
ptr->read[i] = (char *)malloc(sizeof(char) * 10000000);
}
else if (buf[k])
{
if (buf[k + 1] == '\0')
ptr->read[i + 1] = (char *)malloc(sizeof(char) * 10000000);
ptr->read[i][j++] = buf[k++];
}
}
ptr->lasti = i;
ptr->lastj = j;
}
t_line *ft_create_elem(int fd, char *buf)
{
t_line *ptr;
ptr = (t_line *)malloc(sizeof(t_line));
ptr->read = (char **)malloc(sizeof(char *) * 10000000);
ptr->read[0] = (char *)malloc(sizeof(char) * 10000000);
ptr->read[0] = ft_memset(ptr->read[0], '\0', 10000000);
ptr->fd = fd;
ptr->next = NULL;
ptr->line = 0;
ptr->lasti = 0;
ptr->lastj = 0;
ft_fill_tab(buf, ptr);
return (ptr);
}
t_line *ft_fill_fd(int fd, char *buf, t_line **begin)
{
t_line *ptr;
ptr = *begin;
while (ptr != NULL)
{
if (ptr->fd == fd)
{
ft_fill_tab(buf, ptr);
return (ptr);
}
else if (fd != ptr->fd && ptr->next == NULL)
{
ptr->next = ft_create_elem(fd, buf);
return (ptr->next);
}
ptr = ptr->next;
}
return (ptr);
}
t_line *ft_fill(t_line **begin, int fd, char *buf)
{
if (!*begin)
{
*begin = ft_create_elem(fd, buf);
return (*begin);
}
else
return (ft_fill_fd(fd, buf, begin));
return (NULL);
}
int get_next_line(int const fd, char **line)
{
char *buf;
static t_line *begin;
t_line *ptr;
int ret;
if (fd < 0 || !line)
return (-1);
buf = (char *)malloc(sizeof(char) * (BUFF_SIZE + 1));
buf = ft_memset(buf, '\0', BUFF_SIZE + 1);
while (ft_strchr(buf, '\n') == NULL && (ret = read(fd, buf, BUFF_SIZE)))
{
if (ret == -1)
return (-1);
ptr = ft_fill(&begin, fd, buf);
buf = ft_memset(buf, '\0', BUFF_SIZE + 1);
}
ptr = ft_fill(&begin, fd, buf);
if (ptr->read[0][0] != '\0')
*line = ptr->read[ptr->line];
else
*line = NULL;
ptr->line++;
if (ret == 0 && ptr->read[ptr->line] == NULL)
return (0);
return (1);
}