gnl
This commit is contained in:
73
get_next_line.c
Normal file
73
get_next_line.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* get_next_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/15 18:37:44 by aortigos #+# #+# */
|
||||
/* Updated: 2024/03/15 19:31:56 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
char *ft_read_to_left_str(int fd, char *left_str)
|
||||
{
|
||||
char *buff;
|
||||
int rd_bytes;
|
||||
|
||||
buff = malloc((BUFFER_SIZE + 1) * sizeof(char));
|
||||
if (!buff)
|
||||
return (NULL);
|
||||
rd_bytes = 1;
|
||||
while (!ft_strchr(left_str, '\n') && rd_bytes != 0)
|
||||
{
|
||||
rd_bytes = read(fd, buff, BUFFER_SIZE);
|
||||
if (rd_bytes == -1)
|
||||
{
|
||||
free(left_str);
|
||||
free(buff);
|
||||
return (NULL);
|
||||
}
|
||||
buff[rd_bytes] = '\0';
|
||||
left_str = ft_strjoin(left_str, buff);
|
||||
}
|
||||
free(buff);
|
||||
return (left_str);
|
||||
}
|
||||
|
||||
char *get_next_line(int fd)
|
||||
{
|
||||
char *line;
|
||||
static char *left_str;
|
||||
|
||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
left_str = ft_read_to_left_str(fd, left_str);
|
||||
if (!left_str)
|
||||
{
|
||||
free(left_str);
|
||||
return (NULL);
|
||||
}
|
||||
line = ft_get_line(left_str);
|
||||
left_str = ft_new_left_str(left_str);
|
||||
return (line);
|
||||
}
|
||||
|
||||
/*
|
||||
int main()
|
||||
{
|
||||
int fd = open("test.txt", O_RDONLY);
|
||||
char *line;
|
||||
|
||||
while((line = get_next_line(fd)) != NULL)
|
||||
{
|
||||
printf("Linea leida: %s\n", line);
|
||||
free(line);
|
||||
}
|
||||
close(fd);
|
||||
return (0);
|
||||
}*/
|
||||
Reference in New Issue
Block a user