-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
61 lines (56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nahmed-m <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/27 16:20:44 by nahmed-m #+# #+# */
/* Updated: 2016/01/06 16:04:39 by nahmed-m ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static int get_line(int const fd, char *buffer, char *save[fd])
{
int ret;
char *c;
char *tmp;
while ((c = ft_strchr(buffer, '\n')) == NULL &&
(ret = read(fd, buffer, BUFF_SIZE)) > 0)
{
buffer[ret] = '\0';
tmp = save[fd];
save[fd] = ft_strjoin(tmp, buffer);
ft_strdel(&tmp);
}
ft_strdel(&buffer);
if (ret == -1)
return (-1);
return (1);
}
int get_next_line(int const fd, char **line)
{
static char *save[MAX_FD];
char *buffer;
int ret;
char *str;
char *tmp;
buffer = ft_strnew(BUFF_SIZE);
if (fd < 0 || line == NULL || buffer == NULL || BUFF_SIZE < 1)
return (-1);
if (save[fd] == NULL)
save[fd] = ft_strnew(1);
if ((ret = get_line(fd, buffer, save)) == -1)
return (-1);
if ((str = ft_strchr(save[fd], '\n')) != NULL)
{
*line = ft_strsub(save[fd], 0, str - save[fd]);
tmp = save[fd];
save[fd] = ft_strdup(str + 1);
ft_strdel(&tmp);
return (1);
}
*line = ft_strdup(save[fd]);
save[fd] = NULL;
return (ft_strlen(*line) > 0 ? 1 : 0);
}