-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
90 lines (83 loc) · 2.22 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ibeliaie <ibeliaie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/19 12:31:20 by ibeliaie #+# #+# */
/* Updated: 2023/06/19 16:58:43 by ibeliaie ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/* extract line with '\n' from buffer */
char *ft_extract_line(char *rest)
{
int i;
char *str;
i = 0;
if (!rest[i])
return (NULL);
while (rest[i] && rest[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (rest[i] && rest[i] != '\n')
{
str[i] = rest[i];
i++;
}
if (rest[i] == '\n')
{
str[i] = rest[i];
i++;
}
str[i] = '\0';
return (str);
}
/* read from fd until '\n' and put contents into rest*/
char *ft_read_till_nl(int fd, char *rest)
{
char *buff;
int bytes_read;
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buff)
return (NULL);
bytes_read = 1;
while (!ft_strchr(rest, '\n') && bytes_read != 0)
{
bytes_read = read(fd, buff, BUFFER_SIZE);
if (bytes_read == -1)
{
free (buff);
return (NULL);
}
buff[bytes_read] = '\0';
rest = ft_strjoin(rest, buff);
}
free (buff);
return (rest);
}
/* read line from a file specified by fd and return it */
char *get_next_line(int fd)
{
char *line;
static char *rest[4096];
if (fd < 0 || BUFFER_SIZE <= 0 || read(fd, 0, 0) < 0)
{
if (rest[fd])
{
free(rest[fd]);
rest[fd] = NULL;
}
return (NULL);
}
rest[fd] = ft_read_till_nl(fd, rest[fd]);
if (!rest[fd])
return (NULL);
line = ft_extract_line(rest[fd]);
rest[fd] = ft_new_rest(rest[fd]);
return (line);
}