-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
67 lines (62 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hezzahir <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/06 14:43:28 by hezzahir #+# #+# */
/* Updated: 2019/05/14 21:04:22 by hezzahir ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int r_f_line(const int fd, char **str, char **line)
{
int i;
char *tempstr;
i = 0;
while (str[fd][i] != '\n' && str[fd][i])
i++;
if (str[fd][i] == '\n')
{
*line = ft_strsub(str[fd], 0, i);
if (str[fd] != NULL)
{
tempstr = str[fd];
str[fd] = ft_strdup(tempstr + i + 1);
free(tempstr);
}
}
else if (str[fd][i] == '\0')
{
*line = ft_strdup(str[fd]);
ft_memdel((void**)&str[fd]);
}
return (1);
}
int get_next_line(const int fd, char **line)
{
int r;
char buff[BUFF_SIZE + 1];
char *temp;
static char *buffer[4096];
while ((r = read(fd, buff, BUFF_SIZE)) > 0)
{
if (!(buff[r] = '\0') && buffer[fd] != NULL)
{
temp = ft_strdup(buffer[fd]);
free(buffer[fd]);
}
else
temp = ft_strnew(0);
buffer[fd] = ft_strjoin(temp, buff);
free(temp);
if (ft_strchr(buff, '\n'))
break ;
}
if (r == 0 && (buffer[fd] == NULL || buffer[fd][0] == '\0'))
return (0);
else if (r < 0 || fd < 0 || fd > 4096)
return (-1);
return (r_f_line(fd, buffer, line));
}