55 lines
1.7 KiB
C
55 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* get_next_line_bonus.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/11/18 17:12:02 by rparodi #+# #+# */
|
|
/* Updated: 2026/01/29 14:25:47 by rparodi ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "get_next_line_bonus.h"
|
|
|
|
char *ft_check(char *memory, int fd)
|
|
{
|
|
char *buffer;
|
|
int bytescopy;
|
|
|
|
bytescopy = 1;
|
|
buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1));
|
|
if (!buffer)
|
|
return (NULL);
|
|
while (ft_strchr_index(memory, '\n') <= 0 && bytescopy)
|
|
{
|
|
bytescopy = read(fd, buffer, BUFFER_SIZE);
|
|
if (bytescopy == -1)
|
|
return (ft_free(buffer));
|
|
buffer[bytescopy] = '\0';
|
|
memory = ft_strjoin(memory, buffer, 0, -1);
|
|
}
|
|
free(buffer);
|
|
return (memory);
|
|
}
|
|
|
|
char *ft_free(char *str)
|
|
{
|
|
free(str);
|
|
return (NULL);
|
|
}
|
|
|
|
char *get_next_line(int fd)
|
|
{
|
|
static char *memory[OPEN_MAX] = { 0 };
|
|
char *line;
|
|
|
|
if (fd < 0 || fd >= OPEN_MAX || BUFFER_SIZE <= 0)
|
|
return (NULL);
|
|
memory[fd] = ft_check(memory[fd], fd);
|
|
if (!memory[fd])
|
|
return (ft_free(memory[fd]));
|
|
line = ft_get_line(memory[fd]);
|
|
memory[fd] = ft_get_next(memory[fd]);
|
|
return (line);
|
|
}
|