-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_next_line.c
70 lines (63 loc) · 2.01 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mimeyer <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/29 08:54:19 by mimeyer #+# #+# */
/* Updated: 2019/06/24 10:12:57 by mimeyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdio.h>
void set_text(char *text[], char *temp, const int fd)
{
ft_strdel(&text[fd]);
text[fd] = ft_strdup(temp);
ft_strdel(&temp);
}
int read_file(char *text[], const int fd, char *temp)
{
char buf[BUFF_SIZE + 1];
int red;
if ((red = read(fd, buf, BUFF_SIZE)) == 0)
return (0);
buf[red] = '\0';
temp = ft_strjoin(text[fd], buf);
set_text(text, temp, fd);
return (1);
}
int single_line(char *text[], char **line, const int fd)
{
*line = ft_strdup(text[fd]);
ft_strclr(text[fd]);
return (1);
}
int get_next_line(const int fd, char **line)
{
char buf[BUFF_SIZE + 1];
static char *text[1024];
char *temp;
char *ptr;
temp = NULL;
if (fd < 0 || line == NULL || read(fd, buf, 0) < 0)
return (-1);
if (!text[fd])
text[fd] = ft_strnew(0);
while ((ptr = ft_strchr(text[fd], '\n')) == NULL)
if ((read_file(text, fd, temp)) == 0)
break ;
if (ft_strlen(text[fd]) != 0)
{
if (!(ft_strchr(text[fd], '\n')))
return (single_line(text, &*line, fd));
*ptr = '\0';
temp = ft_strdup(ptr + 1);
*line = ft_strdup(text[fd]);
set_text(text, temp, fd);
}
else
return (0);
return (1);
}