forked from KrebsCoder/Get_next_line
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line.c
89 lines (80 loc) · 2.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lkrebs-l <lkrebs-l@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 11:09:59 by lkrebs-l #+# #+# */
/* Updated: 2021/10/08 16:17:27 by lkrebs-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
void ft_free(char **ptr)
{
free(*ptr);
*ptr = NULL;
}
char *format_line(char **buffer, char *swap, int new_line_index)
{
char *line_formated;
if (new_line_index < 0)
{
if (!**buffer && !swap)
{
free(*buffer);
*buffer = NULL;
return (NULL);
}
line_formated = ft_strdup(*buffer);
free(*buffer);
*buffer = NULL;
return (line_formated);
}
*buffer = ft_substr(swap, new_line_index + 1, ft_strlen(swap));
swap[new_line_index + 1] = '\0';
line_formated = swap;
swap = NULL;
return (line_formated);
}
char *get_line(int fd, char **buffer, char *read_buffer)
{
int read_bytes;
char *swap;
char *find_new_line;
int new_line_position;
swap = NULL;
find_new_line = ft_strchr(*buffer, '\n');
while (find_new_line == NULL)
{
read_bytes = read(fd, read_buffer, BUFFER_SIZE);
if (read_bytes <= 0)
return (format_line(buffer, swap, -1));
read_buffer[read_bytes] = '\0';
swap = ft_strjoin(*buffer, read_buffer);
ft_free(buffer);
*buffer = swap;
find_new_line = ft_strchr(*buffer, '\n');
}
swap = *buffer;
new_line_position = 0;
while (swap[new_line_position] != '\n')
new_line_position++;
return (format_line(buffer, swap, new_line_position));
}
char *get_next_line(int fd)
{
static char *buffer[MAX_FD];
char *read_buffer;
char *line;
if (fd < 0 || BUFFER_SIZE <= 0 || fd > MAX_FD)
return (NULL);
if (!buffer[fd])
buffer[fd] = ft_strdup("");
read_buffer = malloc(sizeof(char) * BUFFER_SIZE + 1);
if (!read_buffer)
return (NULL);
line = get_line(fd, &buffer[fd], read_buffer);
ft_free(&read_buffer);
return (line);
}