-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
111 lines (101 loc) · 2.7 KB
/
get_next_line.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
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelsayed <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/11 23:56:08 by aelsayed #+# #+# */
/* Updated: 2025/01/11 23:56:20 by aelsayed ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *freed(char *store, char *buffer)
{
if (store)
{
free(store);
store = NULL;
}
if (buffer)
{
free(buffer);
buffer = NULL;
}
return (NULL);
}
static char *fetch_segment(char *store, int fd)
{
char *buffer;
char *total;
ssize_t readbytes;
readbytes = 1;
buffer = (char *)malloc(BUFFER_SIZE + 1);
if (!buffer || !store)
return (freed(store, buffer));
while (readbytes > 0 && ft_strchr_index(store, '\n') == -1)
{
readbytes = read(fd, buffer, BUFFER_SIZE);
if (readbytes == -1)
return (freed(store, buffer));
buffer[readbytes] = '\0';
total = ft_strjoin(store, buffer);
if (!total)
return (freed(store, buffer));
free(store);
store = total;
}
free(buffer);
return (store);
}
static char *segment_to_newline(char *store)
{
char *line;
int position;
position = 0;
if (!store || !*store)
return (NULL);
while (store[position] != '\n' && store[position])
position++;
if (!store[1])
line = ft_strndup(store, 1);
else if (!store[position])
line = ft_strndup(store, position);
else
line = ft_strndup(store, position + 1);
return (line);
}
static char *clear_till_newline(char *store)
{
int position;
char *new_store;
position = 0;
while (store[position] != '\n' && store[position])
position++;
if (!store[position])
return (freed(store, NULL));
new_store = ft_substr(store, position + 1, ft_strlen(store));
free(store);
return (new_store);
}
char *get_next_line(int fd)
{
static char *store;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || BUFFER_SIZE >= 0x7FFFFFFF)
return (free(store), NULL);
if (!store)
store = ft_strndup("", 0);
store = fetch_segment(store, fd);
if (!store)
return (NULL);
line = segment_to_newline(store);
if (!line)
{
free(store);
store = NULL;
return (NULL);
}
store = clear_till_newline(store);
return (line);
}