-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
111 lines (102 loc) · 2.73 KB
/
Copy pathget_next_line.c
File metadata and controls
111 lines (102 loc) · 2.73 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ophuong <ophuong@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/02 17:37:50 by ophuong #+# #+# */
/* Updated: 2020/05/24 17:18:54 by Student ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "libft.h"
static t_list *f_lstnew(size_t fd)
{
t_list *fresh;
if (!(fresh = (t_list*)malloc(sizeof(t_list))))
return (NULL);
else
{
fresh->content = NULL;
fresh->content_size = fd;
fresh->next = NULL;
}
return (fresh);
}
static t_list *f_list(const int fd, t_list **head)
{
t_list *buf_long;
buf_long = *head;
if (!(*head))
{
buf_long = f_lstnew((size_t)fd);
buf_long->content = ft_strnew(0);
*head = buf_long;
}
while (buf_long->content_size != (size_t)fd)
{
if (buf_long->next == NULL)
{
buf_long = f_lstnew(fd);
buf_long->content = ft_strnew(0);
ft_lstadd(&*head, buf_long);
break ;
}
buf_long = buf_long->next;
}
return (buf_long);
}
static int f_getline(char **line, t_list **buf_long, char *ptr)
{
ssize_t id;
char *tmp;
id = 0;
tmp = ft_strnew(ft_strlen((*buf_long)->content) + 1);
ft_strcpy(tmp, (*buf_long)->content);
while (tmp[id] != '\n' && tmp[id] != '\0')
id++;
ptr = tmp + id + 1;
free((*buf_long)->content);
if (tmp[0] != '\0')
{
if (tmp[0] == '\n')
*line = ft_strnew(0);
else
{
*line = ft_strnew(id);
ft_strncpy(*line, tmp, id);
}
(*buf_long)->content = ft_strdup(ptr);
free(tmp);
return (1);
}
free(tmp);
return (0);
}
int get_next_line(const int fd, char **line)
{
t_list *buf_long;
char *text;
ssize_t nbread;
char buf[BUFF_SIZE + 1];
static t_list *head;
if (fd < 0 || BUFF_SIZE < 0 || line == NULL || read(fd, NULL, 0) < 0)
return (-1);
buf_long = f_list(fd, &head);
while ((nbread = read(fd, buf, BUFF_SIZE)) > 0)
{
buf[nbread] = '\0';
if (buf_long->content == NULL)
buf_long->content = ft_strdup(buf);
else
{
text = ft_strjoin(buf_long->content, buf);
free(buf_long->content);
buf_long->content = text;
}
if (ft_strchr(buf_long->content, '\n') != NULL)
break ;
}
return (f_getline(line, &buf_long, 0));
}