-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_next_line.c
More file actions
111 lines (100 loc) · 2.66 KB
/
get_next_line.c
File metadata and controls
111 lines (100 loc) · 2.66 KB
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: vaguayo- <vaguayo-@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/11 12:32:09 by vaguayo- #+# #+# */
/* Updated: 2025/11/24 11:54:12 by vaguayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *init_storage(char **storage)
{
char *buffer;
if (!*storage)
*storage = ft_calloc(1, 1);
buffer = ft_calloc(BUFFER_SIZE + 1, sizeof(char));
return (buffer);
}
char *line_reader(char **storage, int fd, char *temp)
{
char *buffer;
ssize_t bytes;
buffer = init_storage(storage);
if (!buffer)
return (NULL);
bytes = 1;
while (!ft_strchr(*storage, '\n'))
{
bytes = read(fd, buffer, BUFFER_SIZE);
if (bytes <= 0)
break ;
buffer [bytes] = '\0';
temp = *storage;
*storage = ft_strjoin(temp, buffer);
free(temp);
if (!*storage)
return (free(buffer), NULL);
}
if (bytes < 0)
return (free(*storage), *storage = NULL, free(buffer), NULL);
free(buffer);
if (**storage == '\0')
return (free(*storage), *storage = NULL, NULL);
return (*storage);
}
char *clean_line(char **storage, size_t i)
{
char *line;
char *tempstorage;
line = ft_substr(*storage, 0, i + 1);
tempstorage = ft_substr(*storage, i + 1, ft_strlen(*storage) - (i + 1));
free(*storage);
*storage = NULL;
if (!line || !tempstorage)
{
free(line);
free(tempstorage);
return (NULL);
}
*storage = tempstorage;
return (line);
}
char *line_separator(char **storage)
{
char *line;
size_t i;
if (!*storage)
return (NULL);
i = 0;
while ((*storage)[i] && (*storage)[i] != '\n')
i++;
if ((*storage)[i] == '\n')
line = clean_line(storage, i);
else
{
line = ft_substr(*storage, 0, ft_strlen(*storage));
free(*storage);
*storage = NULL;
}
if (!line)
return (NULL);
return (line);
}
char *get_next_line(int fd)
{
static char *storage;
char *line;
char *temp;
temp = NULL;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (line_reader(&storage, fd, temp) == NULL)
return (NULL);
line = line_separator(&storage);
if (!line)
return (NULL);
return (line);
}