-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
81 lines (73 loc) · 2.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ohachim <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/21 21:49:00 by ohachim #+# #+# */
/* Updated: 2019/01/21 21:49:03 by ohachim ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
int ft_ret_line_read(int ret, char c, size_t len)
{
if (ret == -1)
return (-1);
if (ret == 0 && c == '\0' && !len)
return (0);
return (1);
}
int fill_fill(char **fill, char *buf, const int fd)
{
int ret;
char *temp;
if (*fill == NULL)
*fill = ft_strnew(1);
while ((ret = read(fd, buf, BUFF_SIZE)))
{
buf[ret] = '\0';
temp = *fill;
*fill = ft_strjoin(*fill, buf);
free(temp);
if ((ft_strchr(buf, '\n')))
break ;
}
return (ret);
}
int get_next_line(const int fd, char **line)
{
static char *fill[4094];
char buf[BUFF_SIZE + 1];
char *temp;
int cn;
int ret;
cn = 0;
if (!line || BUFF_SIZE < 0 || read(fd, buf, 0) == -1 || fd < 0)
return (-1);
ret = fill_fill(&fill[fd], buf, fd);
*line = ft_strndup(fill[fd], '\n');
while (fill[fd][cn] != '\n' && fill[fd][cn] != '\0')
cn++;
if (fill[fd][cn] == '\n')
{
cn++;
temp = fill[fd];
fill[fd] = ft_strdup(&fill[fd][cn]);
free(temp);
return (1);
}
return (ft_ret_line_read(ret, *fill[fd], ft_strlen(*line)));
}
// int main(void)
// {
// char *line;
// int fd;
// fd = open("text.txt", O_RDONLY);
// while ((get_next_line(fd, &line)))
// {
// ft_putstr(line);
// ft_putchar('\n');
// ft_strdel(&line); // Must free at every turn.
// }
// }