-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utilities.c
78 lines (72 loc) · 2.05 KB
/
get_next_line_utilities.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utilities.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nle-roux <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 13:27:27 by nle-roux #+# #+# */
/* Updated: 2023/11/26 16:59:52 by nle-roux ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_gnl_remove_line(char *content)
{
size_t offset;
char *martyr;
offset = 0;
while (content[offset] && content[offset] != '\n')
offset++;
if (content[offset] == '\n')
offset++;
martyr = content;
content = ft_strdup(martyr + offset);
free(martyr);
return (content);
}
char *ft_gnl_extract_line(char *content)
{
char *line;
int size;
int i;
i = 0;
size = 0;
while (content[size] && content[size] != '\n')
size++;
if (content[size] == '\n')
size++;
line = (char *)ft_calloc(size + 1, sizeof(char));
while (i < size && line)
{
line[i] = content[i];
i++;
}
return (line);
}
char *ft_gnl_read(int fd, char *content)
{
char *buffer;
int bytes_read;
char *martyr;
if (!content)
content = (char *)ft_calloc(1, sizeof(char));
buffer = (char *)ft_calloc(BUFFER_SIZE + 1, sizeof(char));
if (!buffer)
return (NULL);
bytes_read = 1;
martyr = NULL;
while (bytes_read > 0)
{
bytes_read = read(fd, buffer, BUFFER_SIZE);
if (bytes_read < 0)
return (NULL);
buffer[bytes_read] = 0;
martyr = content;
content = ft_strjoin(content, buffer);
free(martyr);
if (ft_strchr(content, '\n'))
break ;
}
free(buffer);
return (content);
}