-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
125 lines (115 loc) · 3.11 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oohnivch <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 14:59:31 by oohnivch #+# #+# */
/* Updated: 2024/04/29 19:44:23 by oohnivch ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
char *ft_buffjoin(char **buffer, char **content)
{
size_t size;
char *ptr;
char *c_ptr;
size_t i;
if (!*buffer && !*content)
return (NULL);
size = ft_strlen(*buffer) + ft_strlen(*content);
ptr = ft_calloc((size + 1), sizeof(char));
if (ptr == NULL)
return (ft_free(buffer), ft_free(content), NULL);
c_ptr = ptr;
i = 0;
while (*buffer && (*buffer)[i])
*c_ptr++ = (*buffer)[i++];
i = 0;
while (*content && (*content)[i])
*c_ptr++ = (*content)[i++];
ft_free(buffer);
return (ptr);
}
char *ft_move_buffer(char **old, size_t start, size_t len)
{
size_t i;
char *new;
if (!old || !*old)
return (NULL);
if (start > ft_strlen(*old))
len = 0;
else if (start + len > ft_strlen(*old))
len = ft_strlen(*old) - start;
new = ft_calloc((len + 1), sizeof(char));
if (new == NULL)
return (ft_free(old), NULL);
if (!len)
{
ft_free(old);
return (new);
}
i = 0;
while (i < len && (*old)[i + start])
{
new[i] = (unsigned char)(*old)[i + start];
i++;
}
ft_free(old);
return (new);
}
char *ft_parseline(char **buffer)
{
size_t i;
size_t len;
unsigned char *cont;
if (!(*buffer) || !(*buffer[0]))
return (NULL);
len = ft_linelen(*buffer);
cont = ft_calloc(len + 1, sizeof(char));
if (!cont)
return (ft_free(buffer), NULL);
i = 0;
while (i < len)
{
cont[i] = (unsigned char)(*buffer)[i];
i++;
}
*buffer = ft_move_buffer(buffer, len, BUFFER_SIZE);
if (!(*buffer))
return (ft_free((char **)&cont), ft_free(buffer), NULL);
return ((char *)cont);
}
char *end_check(char **buffer, char *content, ssize_t bytes)
{
if (bytes == 0)
ft_free(buffer);
return (content);
}
char *get_next_line(int fd)
{
char *content;
ssize_t bytes;
static char *buffer;
bytes = 1;
if (fd < 0)
return (NULL);
content = ft_calloc((BUFFER_SIZE + 1), sizeof(char));
if (!content)
return (ft_free(&buffer), NULL);
while (!ft_nlcheck(buffer) && bytes != 0)
{
bytes = read(fd, content, BUFFER_SIZE);
if (bytes == -1)
return (ft_free(&buffer), ft_free(&content), NULL);
content[bytes] = 0;
if (bytes != 0)
buffer = ft_buffjoin(&buffer, &content);
if (!buffer)
return (ft_free(&content), NULL);
}
ft_free(&content);
content = ft_parseline(&buffer);
return (end_check(&buffer, content, bytes));
}