-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
85 lines (77 loc) · 2.08 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marmulle <marmulle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/14 13:14:16 by marmulle #+# #+# */
/* Updated: 2022/12/20 18:54:27 by marmulle ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
char *free_and_null(char *str)
{
if (!str)
return (NULL);
free(str);
str = NULL;
return (NULL);
}
char *ft_strldup(const char *str, size_t size)
{
char *dup;
size_t s_len;
if (!str)
return (NULL);
s_len = ft_strlen(str);
if (s_len > size)
s_len = size;
dup = (char *)malloc(s_len + 1);
if (!dup)
return (NULL);
while (*str && size-- > 0)
*(dup++) = *(str++);
*dup = '\0';
return (dup - s_len);
}
char *handle_eof(char **cache)
{
char *hold;
if (*cache && scan_for_newline(*cache) == 0)
{
if (ft_strlen(*cache) == 0)
{
free (*cache);
*cache = NULL;
return (NULL);
}
hold = append_buffer(*cache, NULL);
if (*hold == '\0')
return (free_and_null(hold));
*cache = NULL;
return (hold);
}
return (*cache);
}
char *get_next_line(int fd)
{
static char *cache[OPEN_MAX];
char *buffer;
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, NULL, 0) < 0)
return (NULL);
if (!cache[fd])
cache[fd] = NULL;
while (1)
{
if (scan_for_newline(cache[fd]))
return (grab_line(&cache[fd]));
buffer = get_buffer(fd);
if (buffer == NULL)
{
free_and_null(buffer);
return (handle_eof(&cache[fd]));
}
cache[fd] = append_buffer(cache[fd], buffer);
}
}