-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
109 lines (99 loc) · 2.65 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
103
104
105
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hmstrx <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 15:21:48 by sojammal #+# #+# */
/* Updated: 2025/01/10 17:16:12 by hmstrx ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
int lm(char *line, int i)
{
if (line[i] == '\n')
return (1);
return (0);
}
char *read_from_fd(int fd, char *buffer)
{
char *tmp_holder;
int reading_bytes;
tmp_holder = malloc(BUFFER_SIZE + 1);
if (!tmp_holder)
return (free(buffer), NULL);
reading_bytes = 1;
while (!ft_strchr(buffer, '\n') && reading_bytes > 0)
{
reading_bytes = read(fd, tmp_holder, BUFFER_SIZE);
if (reading_bytes == -1)
return (free(tmp_holder), free(buffer), NULL);
tmp_holder[reading_bytes] = '\0';
buffer = ft_strjoin(buffer, tmp_holder);
if (!buffer)
return (free(tmp_holder), NULL);
}
free(tmp_holder);
return (buffer);
}
char *extract_line(char *line)
{
int i;
char *str;
if (!line || !*line)
return (NULL);
i = len_at_newline(line, 0);
str = malloc(i + 1 + (line[i] == '\n'));
if (!str)
return (NULL);
i = 0;
while (line[i] && line[i] != '\n')
{
str[i] = line[i];
i++;
}
if (line[i] == '\n')
str[i++] = '\n';
str[i] = '\0';
return (str);
}
char *clean_up_buffer(char *line)
{
int i;
int j;
char *new_buffer;
if (!line || !*line)
return (free(line), NULL);
i = len_at_newline(line, 0);
if (!line[i])
return (free(line), NULL);
new_buffer = malloc(ft_strlen(line) - i);
if (!new_buffer)
return (free(line), NULL);
i++;
j = 0;
while (line[i])
new_buffer[j++] = line[i++];
new_buffer[j] = '\0';
free(line);
return (new_buffer);
}
char *get_next_line(int fd)
{
static char *buffer[OPEN_MAX];
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || BUFFER_SIZE > INT_MAX)
return (NULL);
buffer[fd] = read_from_fd(fd, buffer[fd]);
if (!buffer[fd])
return (NULL);
line = extract_line(buffer[fd]);
buffer[fd] = clean_up_buffer(buffer[fd]);
if (!line && buffer[fd])
{
free(buffer[fd]);
buffer[fd] = NULL;
}
return (line);
}