-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
127 lines (116 loc) · 2.84 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbaldy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/07 13:41:50 by dbaldy #+# #+# */
/* Updated: 2016/02/17 10:44:59 by dbaldy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
t_lc *list_clear(t_lc **begin, int fd)
{
t_lc *buf;
t_lc *temp;
buf = *begin;
temp = buf->next;
if (buf->fno == fd)
{
*begin = (*begin)->next;
free((buf)->str);
free(buf);
}
while (temp)
{
if (temp->fno == fd)
{
free(temp->str);
buf->next = temp->next;
temp->next = NULL;
free(temp);
}
buf = buf->next;
temp = temp->next;
}
return (NULL);
}
t_lc *scan(int fd, int type)
{
static t_lc *lc_begin;
t_lc *temp;
t_lc *new;
temp = lc_begin;
if (type == 1)
return (list_clear(&lc_begin, fd));
while (temp)
{
if (temp->fno == fd)
return (temp);
temp = temp->next;
}
if ((new = (t_lc*)malloc(sizeof(t_lc))) == NULL)
return (NULL);
new->next = lc_begin;
lc_begin = new;
new->fno = fd;
new->str = ft_strdup("");
return (new);
}
static int ft_check(t_lc *obj, char **res)
{
int size;
char *temp;
temp = ft_strdup(obj->str);
if (ft_strchr(temp, '\n') != NULL)
{
size = ft_strlen(temp) - ft_strlen(ft_strchr(temp, '\n'));
*res = ft_strsub(temp, 0, size);
free(obj->str);
obj->str = ft_strdup(ft_strchr(temp, '\n') + 1);
free(temp);
return (1);
}
*res = obj->str;
free(temp);
return (0);
}
static int read_line(t_lc *obj, char *buf, char **line)
{
int size;
char *temp;
size = BUFF_SIZE;
while (size == BUFF_SIZE)
{
if ((size = read(obj->fno, buf, BUFF_SIZE)) == -1)
return (-1);
buf[BUFF_SIZE] = '\0';
if (size < BUFF_SIZE)
ft_strclr(&buf[size]);
temp = ft_strjoin(obj->str, buf);
free(obj->str);
obj->str = temp;
if (ft_check(obj, line) == 1)
return (1);
}
*line = ft_strdup(obj->str);
scan(obj->fno, 1);
return (0);
}
int get_next_line(int const fd, char **line)
{
t_lc *obj;
char *buf;
int ret;
buf = NULL;
if ((line == NULL) || (obj = scan(fd, 0)) == NULL)
return (-1);
if (ft_check(obj, line) == 1)
return (1);
if ((buf = ft_strnew(BUFF_SIZE + 1)) == NULL)
return (-1);
ret = read_line(obj, buf, line);
free(buf);
return (ret);
}