-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
93 lines (85 loc) · 2.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgermain <dgermain@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/12/08 12:42:05 by dgermain #+# #+# */
/* Updated: 2013/12/30 14:13:27 by ccorazza ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pipex.h"
static int ft_check_n(char **rest, char *str, char **line)
{
int c;
c = 0;
*rest = ft_strdup(ft_strchr(str, '\n') + 1);
if (*rest == NULL)
return (-1);
while (str[c] != '\n')
c++;
str[c] = '\0';
*line = str;
return (1);
}
static int ft_read_buf(int const fd, char **buf, int *ret)
{
*buf = ft_strnew(BUFF_SIZE);
if (*buf == NULL)
return (-1);
*ret = read(fd, *buf, BUFF_SIZE);
if (*ret == -1)
return (-1);
(*buf)[*ret] = '\0';
return (*ret);
}
static int ft_test_rest(char **rest, char **str, char **buf)
{
char *temp;
if (*rest == NULL)
{
temp = *str;
*str = ft_strjoin(temp, *buf);
if (*str == NULL)
return (-1);
ft_memdel((void **)buf);
ft_memdel((void **)&temp);
}
else
{
temp = *rest;
ft_memdel((void **)str);
*str = ft_strjoin(temp, *buf);
if (*str == NULL)
return (-1);
ft_memdel((void **)rest);
ft_memdel((void **)buf);
}
return (0);
}
int get_next_line(int const fd, char **line)
{
int ret;
char *buf;
char *str;
static char *rest;
ret = 1;
str = ft_strnew(BUFF_SIZE);
if (str == NULL)
return (-1);
while (ret > 0)
{
if (ft_read_buf(fd, &buf, &ret) == -1)
return (-1);
if (ft_test_rest(&rest, &str, &buf) == -1)
return (-1);
if (ft_strchr(str, '\n') != NULL)
return (ft_check_n(&rest, str, line));
ft_memdel((void **)&buf);
}
*line = str;
if (*line != NULL && **line != '\0')
return (1);
return (0);
}