-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_bonus.c
77 lines (71 loc) · 2.16 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_bonus.c :+: :+: */
/* +:+ */
/* By: cwesseli <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/18 14:43:00 by cwesseli #+# #+# */
/* Updated: 2022/11/18 14:45:55 by cwesseli ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void free_str(char *str)
{
if (str)
free (str);
}
char *read_and_join(int fd, char *passed_line)
{
ssize_t bytes_read;
char buffer[BUFFER_SIZE + 1];
bytes_read = 1;
while (bytes_read != 0 && ft_findchr(passed_line, '\n') == 0)
{
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read == -1)
{
free_str (passed_line);
return (NULL);
}
if (bytes_read != 0)
{
buffer[bytes_read] = '\0';
passed_line = ft_strjoin_gnl(passed_line, buffer);
if (passed_line == NULL)
{
free(passed_line);
passed_line = NULL;
break ;
}
}
}
return (passed_line);
}
char *get_next_line(int fd)
{
static char *passed_line[MAX_OPEN];
char *return_line;
return_line = NULL;
if (fd < 0 || BUFFER_SIZE < 1 || fd > (MAX_OPEN - 1))
return (NULL);
if (ft_findchr(passed_line[fd], '\n') == 0)
{
passed_line[fd] = read_and_join(fd, passed_line[fd]);
if (passed_line[fd] == NULL || passed_line[fd][0] == '\0')
{
free_str (passed_line[fd]);
passed_line[fd] = NULL;
return (NULL);
}
}
return_line = get_new_line(passed_line[fd]);
if (!return_line || return_line[0] == '\0')
{
free_str(passed_line[fd]);
passed_line[fd] = NULL;
return (NULL);
}
passed_line[fd] = get_new_string(passed_line[fd]);
return (return_line);
}