-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
73 lines (66 loc) · 1.98 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atilegen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/31 12:38:39 by atilegen #+# #+# */
/* Updated: 2018/04/05 15:13:38 by atilegen ### ########.fr */
/* */
/* ************************************************************************** */
#include "./libft/libft.h"
#include "get_next_line.h"
int to_line(char **heap, char **line)
{
char *check;
char *temp;
check = ft_strchr(*heap, '\n');
if (check)
{
temp = *heap;
*line = ft_strndup(temp, check - temp);
*heap = ft_strsub(temp, check - temp + 1,
ft_strlen(temp) - (check - temp) + 1);
free(temp);
return (1);
}
return (0);
}
int to_stack(char **heap, char **line, char *buf, int ret)
{
char *str;
buf[ret] = '\0';
if (*heap == 0)
*heap = ft_strdup(buf);
else
{
str = *heap;
*heap = ft_strjoin(*heap, buf);
free(str);
}
if (to_line(heap, line))
return (1);
return (0);
}
int get_next_line(const int fd, char **line)
{
static char *heap[11966];
int ret;
char buf[BUFF_SIZE + 1];
if (fd < 0 || read(fd, 0, 0) < 0 || fd > 11966)
return (-1);
if (heap[fd] != NULL)
if (to_line(&heap[fd], line))
return (1);
while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
if (to_stack(&heap[fd], line, buf, ret))
return (1);
if (heap[fd] == NULL)
return (0);
if (*heap[fd] == '\0')
return (0);
*line = ft_strdup(heap[fd]);
ft_memdel((void **)&heap[fd]);
return (1);
}