-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line.c
119 lines (109 loc) · 2.93 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrios-es <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 18:18:09 by mrios-es #+# #+# */
/* Updated: 2023/11/10 18:18:09 by mrios-es ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
static char *update_line(char *line, char *buffer, int nl_pos)
{
char *updated_line;
char c;
if (!line)
return (NULL);
if (!buffer)
return (free(line), NULL);
if (nl_pos < 0)
return (line);
c = buffer[nl_pos + 1];
buffer[nl_pos + 1] = '\0';
updated_line = ft_strjoin(line, buffer);
buffer[nl_pos + 1] = c;
if (!updated_line)
return (free(line), NULL);
free(line);
line = updated_line;
return (line);
}
static char *update_buffer(char *buffer, int buffer_len, int nl_pos)
{
char *substr;
int len;
if (nl_pos == buffer_len - 1)
{
buffer[0] = '\0';
return (buffer);
}
substr = buffer + nl_pos + 1;
len = buffer_len - nl_pos - 1;
ft_memmove(buffer, substr, len);
buffer[len] = '\0';
return (buffer);
}
static int update_state(char *buffer, char **line)
{
int nl_pos;
int buffer_len;
char *found;
nl_pos = -1;
found = ft_strchr(buffer, '\n');
if (found)
nl_pos = found - buffer;
buffer_len = ft_strlen(buffer);
if (nl_pos >= 0)
{
*line = update_line(*line, buffer, nl_pos);
buffer = update_buffer(buffer, buffer_len, nl_pos);
}
else
{
*line = update_line(*line, buffer, buffer_len - 1);
buffer = update_buffer(buffer, buffer_len, buffer_len - 1);
}
return (nl_pos >= 0);
}
static void init(int fd, char **buffer, char **line)
{
if (fd < 0 || BUFFER_SIZE < 1 || read(fd, buffer, 0) < 0)
return (free_ptr(buffer));
if (!*buffer)
{
*buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!*buffer)
return ;
*buffer[0] = '\0';
}
*line = malloc(1 * sizeof(char));
if (!*line)
return (free_ptr(buffer));
*line[0] = '\0';
}
char *get_next_line(int fd)
{
static char *buffer;
char *line;
int bytes_read;
int has_nl;
init(fd, &buffer, &line);
if (!buffer || !line)
return (NULL);
bytes_read = 1;
while (bytes_read > 0)
{
has_nl = update_state(buffer, &line);
if (!line || has_nl)
break ;
bytes_read = read(fd, buffer, BUFFER_SIZE);
buffer[bytes_read] = '\0';
}
if (!line)
free_ptr(&buffer);
else if (!line[0])
(free_ptr(&buffer), free_ptr(&line));
return (line);
}