-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
102 lines (93 loc) · 2.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anhigo-s <anhigo-s@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/28 00:39:01 by anhigo-s #+# #+# */
/* Updated: 2023/02/23 17:39:54 by anhigo-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
char *gnl_strjoinfree(char *s1, char *s2)
{
char *new_string;
size_t i;
size_t a;
new_string = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
i = 0;
a = 0;
if (!new_string)
return (NULL);
while (s1[i] != '\0')
{
new_string[i] = s1[i];
i++;
}
while (s2[a] != '\0')
{
new_string[i + a] = s2[a];
a++;
}
new_string[i + a] = '\0';
free(s1);
s1 = NULL;
return (new_string);
}
static char *get_line(char **buffer_backup, size_t i)
{
char *line;
char *temp;
while (((*buffer_backup)[i] != '\n') && ((*buffer_backup)[i] != '\0'))
i++;
if ((*buffer_backup)[i] == 0)
{
line = ft_strdup(*buffer_backup);
free(*buffer_backup);
*buffer_backup = NULL;
}
else
{
line = ft_substr(*buffer_backup, 0, i + 1);
temp = ft_strdup(&(*buffer_backup)[i + 1]);
free(*buffer_backup);
*buffer_backup = NULL;
if (*temp)
*buffer_backup = ft_strdup(temp);
free(temp);
temp = NULL;
}
return (line);
}
static char *get_next(int fd, ssize_t bytes_read, char *temp_buff)
{
static char *s_buffer;
char *temp;
while (bytes_read > 0)
{
temp_buff[bytes_read] = '\0';
if (s_buffer == 0)
s_buffer = ft_strdup("");
temp = ft_strdup(s_buffer);
free(s_buffer);
s_buffer = gnl_strjoinfree(temp, temp_buff);
if (ft_strchr(s_buffer, '\n'))
break ;
bytes_read = read(fd, temp_buff, BUFFER_SIZE);
}
if (bytes_read == 0 && s_buffer == 0)
return (NULL);
return (get_line(&s_buffer, 0));
}
char *get_next_line(int fd)
{
char temp_buff[BUFFER_SIZE + 1];
ssize_t bytes_read;
if ((fd < 0) || (BUFFER_SIZE < 1) || (read(fd, temp_buff, 0) < 0))
return (NULL);
bytes_read = read(fd, temp_buff, BUFFER_SIZE);
if (bytes_read < 0)
return (NULL);
return (get_next(fd, bytes_read, temp_buff));
}