-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
126 lines (114 loc) · 3.23 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abdsalah <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/05 08:53:07 by abdsalah #+# #+# */
/* Updated: 2024/09/05 12:04:44 by abdsalah ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
// free memory inside buffers
static void *ft_free_buffers(char **global_buffer, char *read_buffer)
{
if (global_buffer && *global_buffer)
{
free(*global_buffer);
*global_buffer = NULL;
}
if (read_buffer)
free(read_buffer);
return (NULL);
}
static int ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}
static char *ft_read_into_buffer(char **global_buffer, int fd)
{
int read_value;
char *read_buffer;
char *temp_buffer;
read_buffer = malloc(BUFFER_SIZE + 1);
if (!read_buffer)
return (ft_free_buffers(global_buffer, NULL));
while (!ft_strchr(*global_buffer, '\n'))
{
read_value = read(fd, read_buffer, BUFFER_SIZE);
if (read_value == -1)
return (ft_free_buffers(global_buffer, read_buffer));
if (**global_buffer == '\0' && read_value == 0)
return (ft_free_buffers(NULL, read_buffer));
if (read_value == 0)
break ;
read_buffer[read_value] = '\0';
temp_buffer = ft_strjoin(*global_buffer, read_buffer);
if (!temp_buffer)
return (ft_free_buffers(global_buffer, read_buffer));
free(*global_buffer);
*global_buffer = temp_buffer;
}
free(read_buffer);
return (*global_buffer);
}
static char *ft_extract_line(char **global_buffer)
{
char *pos;
char *str;
int offset;
char *temp_global_buffer;
pos = ft_strchr(*global_buffer, '\n');
if (pos)
offset = pos - *global_buffer + 1;
else
offset = ft_strlen(*global_buffer);
str = malloc(offset + 1);
if (!str)
return (ft_free_buffers(global_buffer, NULL));
ft_strncpy(str, *global_buffer, offset);
str[offset] = '\0';
temp_global_buffer = ft_strdup(*global_buffer + offset);
if (!temp_global_buffer)
{
free(str);
return (ft_free_buffers(global_buffer, NULL));
}
free(*global_buffer);
*global_buffer = temp_global_buffer;
return (str);
}
char *get_next_line(int fd)
{
static char *global_buffer = NULL;
char *str;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
if (!global_buffer)
{
global_buffer = ft_strdup("");
if (!global_buffer)
return (NULL);
}
if (!ft_read_into_buffer(&global_buffer, fd))
return (ft_free_buffers(&global_buffer, NULL));
str = ft_extract_line(&global_buffer);
return (str);
}
// int main(int argc, char **argv)
// {
// char *str;
// int fd;
// (void)argc;
// fd = open(argv[1], O_RDONLY);
// while (str = get_next_line(fd))
// {
// printf("%s", str);
// }
// return (0);
// }