-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_bonus.c
90 lines (82 loc) · 2.27 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: viferrei <viferrei@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/29 17:44:32 by arbernar #+# #+# */
/* Updated: 2021/10/05 17:48:52 by viferrei ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
void free_ptr(char *ptr)
{
if (ptr)
{
free(ptr);
ptr = NULL;
}
}
char *get_next_line(int fd)
{
char *line;
static char *s_buff[OPEN_MAX];
char *buffer;
if (fd < 0 || BUFFER_SIZE <= 0 || fd > OPEN_MAX)
return (NULL);
buffer = (char *) malloc((BUFFER_SIZE + 1) * sizeof(char));
if (!buffer)
return (NULL);
if (read(fd, buffer, 0) < 0)
{
free(buffer);
return (NULL);
}
if (!s_buff[fd])
s_buff[fd] = ft_strdup("");
if (read_file(fd, &buffer, &s_buff[fd], &line) == 0 && *line == '\0')
{
free_ptr(line);
return (NULL);
}
return (line);
}
int read_file(int fd, char **buffer, char **s_buff, char **line)
{
int bytes_read;
char *temp;
bytes_read = 1;
while (!ft_strchr(*s_buff, '\n') && bytes_read)
{
bytes_read = read(fd, *buffer, BUFFER_SIZE);
(*buffer)[bytes_read] = '\0';
temp = *s_buff;
*s_buff = ft_strjoin(*s_buff, *buffer);
free_ptr(temp);
}
free_ptr(*buffer);
get_line(line, s_buff);
return (bytes_read);
}
char *get_line(char **line, char **s_buff)
{
char *buff_temp;
int buff_nl;
buff_nl = 0;
buff_temp = *s_buff;
while ((*s_buff)[buff_nl] != '\n' && (*s_buff)[buff_nl] != '\0')
buff_nl++;
if (ft_strchr(*s_buff, '\n'))
{
*line = ft_substr(*s_buff, 0, buff_nl + 1);
*s_buff = ft_strdup(*s_buff + buff_nl + 1);
}
else
{
*line = ft_strdup(buff_temp);
*s_buff = NULL;
}
free_ptr(buff_temp);
return (*line);
}