-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
106 lines (97 loc) · 2.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elmaksim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/24 13:23:27 by elmaksim #+# #+# */
/* Updated: 2024/01/24 13:40:37 by elmaksim ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *ft_reserve(char *reserve)
{
int i;
int c;
char *str;
i = 0;
while (reserve[i] && reserve[i] != '\n')
i++;
if (!reserve[i])
{
free(reserve);
return (NULL);
}
str = (char *)malloc(sizeof(char) * (ft_strlen(reserve) - i + 1));
if (!str)
return (NULL);
i++;
c = 0;
while (reserve[i])
str[c++] = reserve[i++];
str[c] = '\0';
free(reserve);
return (str);
}
static char *ft_get_line(char *reserve)
{
int i;
char *str;
i = 0;
if (!reserve[i])
return (NULL);
while (reserve[i] && reserve[i] != '\n')
i++;
str = (char *)malloc(sizeof(char) * (i + 2));
if (!str)
return (NULL);
i = 0;
while (reserve[i] && reserve[i] != '\n')
{
str[i] = reserve[i];
i++;
}
if (reserve[i] == '\n')
{
str[i] = reserve[i];
i++;
}
str[i] = '\0';
return (str);
}
static char *ft_rd_reserv(int fd, char *reserve)
{
ssize_t rd_byte;
char *buf;
buf = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buf)
return (NULL);
rd_byte = 1;
while (!ft_strchr(reserve, '\n') && rd_byte != 0)
{
rd_byte = read(fd, buf, BUFFER_SIZE);
if (rd_byte == -1)
{
free(buf);
return (NULL);
}
buf[rd_byte] = '\0';
reserve = ft_strjoin(reserve, buf);
}
free(buf);
return (reserve);
}
char *get_next_line(int fd)
{
char *line;
static char *reserve;
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
reserve = ft_rd_reserv(fd, reserve);
if (!reserve)
return (NULL);
line = ft_get_line(reserve);
reserve = ft_reserve(reserve);
return (line);
}