-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
102 lines (93 loc) · 2.63 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
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aceauses <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/09 15:12:47 by aceauses #+# #+# */
/* Updated: 2023/06/09 11:27:40 by aceauses ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
static char *get_text_fd(int fd, char *buffer)
{
char *tmp;
int bytes_readed;
if (!buffer)
buffer = ft_calloc(1, 1);
tmp = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
bytes_readed = 1;
while (bytes_readed > 0)
{
bytes_readed = read(fd, tmp, BUFFER_SIZE);
if (bytes_readed == -1)
return (free(tmp), tmp = NULL, free(buffer), buffer = NULL, NULL);
tmp[bytes_readed] = '\0';
buffer = free_join(buffer, tmp);
if (ft_strchr(tmp, '\n'))
break ;
}
return (free(tmp), buffer);
}
static char *get_line(char *buffer)
{
char *line;
int i;
i = 0;
if (buffer[i] == '\0' || buffer == NULL)
return (NULL);
while (buffer[i] && buffer[i] != '\n')
i++;
line = ft_calloc(i + 2, sizeof(char));
if (line == NULL)
return (free(buffer), free(line), NULL);
i = 0;
while (buffer[i] && buffer[i] != '\n')
{
line[i] = buffer[i];
i++;
}
line[i] = '\n';
if (buffer[i] == '\0')
line[i] = '\0';
return (line);
}
static void *update_buffer(char *buffer)
{
char *new;
int j;
int i;
i = 0;
while (buffer[i] && buffer[i] != '\n')
i++;
if (!buffer[i])
return (free(buffer), NULL);
new = ft_calloc((ft_strlen(buffer) - i + 1), sizeof(char));
i++;
j = 0;
while (buffer[i])
new[j++] = buffer[i++];
return (free(buffer), new);
}
/* [100000] == 'aaaaaa\n
bbbbbb\n
cccccc\n
dddddd'*/
char *get_next_line(int fd)
{
static char *buffer[OPENMAX];
char *line;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
buffer[fd] = get_text_fd(fd, buffer[fd]);
if (buffer[fd] == NULL)
return (NULL);
line = get_line(buffer[fd]);
if (line == NULL)
return (free(buffer[fd]), buffer[fd] = NULL, free(line), NULL);
buffer[fd] = update_buffer(buffer[fd]);
if (buffer[fd] == NULL)
free(buffer[fd]);
return (line);
}