libft/ft_getline.c
Tanguy MAZE d49f964045 let's test this
added ft_open, ft_close and ft_gets functions not tested
modified ft_getline with read 1 by 1
2019-03-08 14:14:56 +01:00

66 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_getline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */
/* Updated: 2019/03/08 14:11:15 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *supercat(char **s1, char *buff)
{
char *tmp;
if ((tmp = ft_strjoin(*s1, buff)) == NULL)
return (NULL);
ft_strdel(s1);
ft_bzero(buff, BUFF_SIZE + 1);
*s1 = tmp;
return (*s1);
}
static int flush_buff(char **line, char *buff)
{
if (*line == NULL)
{
if ((*line = ft_strdup(buff)) == NULL)
return (-1);
ft_bzero(buff, BUFF_SIZE + 1);
}
else if (*line != NULL)
if (supercat(line, buff) == NULL)
{
ft_strdel(line);
return (-1);
}
return (0);
}
int ft_getline(char **line)
{
char buff[BUFF_SIZE + 1];
int ret;
int i;
if (line == NULL)
return (-1);
*line = NULL;
ft_bzero(buff, BUFF_SIZE + 1);
i = 0;
while ((ret = read(0, &(buff[i]), 1)) == 1 && buff[i] != '\n')
{
if (++i > BUFF_SIZE && flush_buff(line, buff) != 0)
return (-1);
if (i > BUFF_SIZE)
i = 0;
}
if (ret == 0 && i != 0 && flush_buff(line, buff) != 0)
return (-1);
return (line != NULL);
}