-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line_bonus.c
115 lines (104 loc) · 2.83 KB
/
get_next_line_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tuchikaw <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/14 12:50:24 by tuchikaw #+# #+# */
/* Updated: 2024/05/08 01:13:30 by tuchikaw ### ########.fr */
/* */
/* ************************************************************************** */
#include "./get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != 0)
i++;
return (i);
}
char *get_new_line(char *static_char)
{
char *newline_address;
int new_char_len;
char *new_char;
newline_address = ft_strchr(static_char, '\n');
if (!newline_address)
{
free(static_char);
return (NULL);
}
new_char_len = ft_strlen(newline_address + 1);
new_char = (char *)malloc(sizeof(char) * (new_char_len + 1));
if (!new_char)
{
free(static_char);
return (NULL);
}
ft_memcpy(new_char, newline_address + 1, (new_char_len));
free(static_char);
new_char[new_char_len] = '\0';
return (new_char);
}
char *get_to_next_newline(int fd, char *static_char)
{
char *buffer;
int read_bytes;
buffer = malloc(sizeof(char) * (BUFFER_SIZE + 1));
if (!buffer)
return (NULL);
read_bytes = 1;
while (!ft_strchr(static_char, '\n') && read_bytes != 0)
{
read_bytes = read(fd, buffer, BUFFER_SIZE);
if (read_bytes == -1)
{
free(buffer);
return (NULL);
}
buffer[read_bytes] = '\0';
static_char = ft_strjoin(static_char, buffer);
if (!static_char)
{
free(buffer);
return (NULL);
}
}
free(buffer);
return (static_char);
}
char *get_next_line(int fd)
{
char *line;
static char *static_char[4096];
if (fd < 0 || BUFFER_SIZE <= 0)
return (NULL);
static_char[fd] = get_to_next_newline(fd, static_char[fd]);
if (!static_char[fd])
return (NULL);
line = get_to_nr(static_char[fd]);
static_char[fd] = get_new_line(static_char[fd]);
if (!line || line[0] == '\0')
return (free(static_char[fd]), NULL);
if (!static_char[fd] && (!line || line[0] == '\0'))
return (NULL);
return (line);
}
// int main(void)
// {
// int fd;
// char *line;
// fd = open("tests/empty.txt", O_RDONLY);
// line = get_next_line(fd);
// line = get_next_line(fd);
// printf("%s", line);
// // while (line)
// // {
// // printf("%s", line);
// // line = get_next_line(fd);
// // }
// free(line);
// close(fd);
// return (0);
// }