-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
70 lines (65 loc) · 2.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cnguyen- <cnguyen-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/30 17:59:36 by cnguyen- #+# #+# */
/* Updated: 2024/06/04 21:53:16 by cnguyen- ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
/*
GET_NEXT_LINE
Reads from the file descriptor to get the next first line. The function
reads BUFFER_SIZE characters at a time and appends the characters to the
return line. It only appends the characters until it meets a newline or the
end of file is reached. The remaining characters stay in the buffer for the
next call of get_next_line. If a newline is already present in the initial
buffer, no additional characters are read.
PARAMETER(S)
File descriptor to read from
RETURN
The function returns the next line read from the file descriptor as a
string. If there is nothing left to be read or if an error occurred, it
returns NULL.
*/
static void *exit_gnl(char *buffer, void *ptr, char *line)
{
while (*buffer)
*buffer++ = '\0';
if (ptr != line)
free(ptr);
if (line && *line != '\0')
return (line);
free(line);
return (NULL);
}
char *get_next_line(int fd)
{
char *line;
int bytes_read;
static char buffer[FOPEN_MAX][BUFFER_SIZE + 1];
if (fd < 0 || fd > FOPEN_MAX)
return (NULL);
line = (char *)malloc(sizeof(char));
if (!line)
return (exit_gnl(buffer[fd], NULL, NULL));
line[0] = '\0';
bytes_read = 1;
while (bytes_read > 0 && !ft_strchr(buffer[fd], '\n'))
{
if (!ft_strsmove(&line, buffer[fd], ft_strchr(buffer[fd], '\0') - 1))
return (exit_gnl(buffer[fd], NULL, NULL));
bytes_read = read(fd, buffer[fd], BUFFER_SIZE);
if (bytes_read == -1)
return (exit_gnl(buffer[fd], line, NULL));
buffer[fd][bytes_read] = '\0';
}
if (bytes_read == 0 || read(fd, 0, 0) < 0)
return (exit_gnl(buffer[fd], line, line));
if (!ft_strsmove(&line, buffer[fd], ft_strchr(buffer[fd], '\n')))
return (exit_gnl(buffer[fd], NULL, NULL));
return (line);
}