/
get_next_line.c
87 lines (76 loc) · 2.21 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qduperon <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/20 13:13:05 by qduperon #+# #+# */
/* Updated: 2016/02/01 16:46:12 by qduperon ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int end_of_line(char *tmp)
{
int c;
c = 0;
while (tmp[c])
{
if (tmp[c] == '\n')
return (c);
c++;
}
return (c);
}
static int fill_the_line(const int fd, char *tmp[fd], char **line)
{
int end;
char *temp;
end = end_of_line(tmp[fd]);
*line = ft_strsub(tmp[fd], 0, end);
if (!(temp = (char *)malloc(sizeof(temp) * (ft_strlen(tmp[fd]) + 1))))
return (-1);
ft_strcpy(temp, &tmp[fd][end + 1]);
ft_strclr(tmp[fd]);
tmp[fd] = ft_strcpy(tmp[fd], temp);
free(temp);
return (1);
}
static char *strjoin_free(char *tmp, char *buff)
{
char *temp;
if (!(temp = ft_strjoin(tmp, buff)))
return (NULL);
free(tmp);
tmp = NULL;
return (temp);
}
static int fill_the_array(const int fd, char *tmp[fd])
{
char buff[BUFF_SIZE + 1];
int ret;
while (!(ft_strchr(tmp[fd], '\n')) && (ret = read(fd, buff, BUFF_SIZE)) > 0)
{
buff[ret] = '\0';
tmp[fd] = strjoin_free(tmp[fd], buff);
}
return (ret);
}
int get_next_line(const int fd, char **line)
{
static char *tmp[MAX_FD];
if (fd < 0 || fd == 1 || fd == 2 || fd > MAX_FD || !line || BUFF_SIZE < 0)
return (-1);
if (!tmp[fd] && (!(tmp[fd] = ft_strnew(2))))
return (-1);
if (fill_the_array(fd, &(*tmp)) < 0)
return (-1);
if (tmp[fd][0] == '\0')
{
*line = NULL;
return (0);
}
if (fill_the_line(fd, tmp, line) < 0)
return (-1);
return (1);
}