/
get_next_line.c
96 lines (88 loc) · 1.58 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
#include "get_next_line.h"
static t_list *gnl_rest(char *rst)
{
static t_list *rest = NULL;
t_list *tmp;
if (rst != NULL)
{
if (ft_strlen(rst) != 0)
rest = ft_lstnew(rst, ft_strlen(rst));
else
rest = NULL;
}
else if (rest)
{
tmp = ft_memdup(rest, sizeof(t_list));
free(rest);
rest = NULL;
}
else
tmp = NULL;
return (tmp);
}
static char *gnl_lsttochar(t_list **alst)
{
char *str;
int size;
t_list *lst;
size = 0;
lst = *alst;
while (lst)
{
size = size + lst->content_size;
lst = lst->next;
}
lst = *alst;
str = ft_strnew(size);
while (lst)
{
size = size - lst->content_size;
ft_memcpy(str + size, lst->content, lst->content_size);
ft_lstdelone(&lst, del);
}
return (str);
}
static int gnl_act(int fd, char **line, t_list **alst, char *str)
{
int rd;
int i;
rd = 1;
str = ft_strnew(BUFF_SIZE);
while (rd >= 0)
{
if ((i = ft_strclen((*alst)->content, '\n')) >= 0 || rd == 0)
{
if (i >= 0)
{
gnl_rest((*alst)->content + i + 1);
((char*)(*alst)->content)[i] = '\0';
(*alst)->content_size = i;
}
*line = gnl_lsttochar(alst);
free(str);
return (1);
}
if ((rd = read(fd, str, BUFF_SIZE)) < 0)
return (rd);
ft_lstadd(alst, ft_lstnew(str, rd));
}
return (-1);
}
int get_next_line(const int fd, char **line)
{
t_list *lst;
char *str;
int rd;
rd = 1;
if (line == NULL)
return (-1);
if ((lst = gnl_rest(NULL)) == NULL)
{
str = ft_strnew(BUFF_SIZE);
if ((rd = read(fd, str, BUFF_SIZE)) <= 0)
return (rd);
lst = ft_lstnew(str, rd);
free(str);
}
return (gnl_act(fd, line, &lst, str));
}