libft/ft_getline.c
Tanguy MAZE 55767c36c6 plop
added ft_getline WIP
2019-03-07 19:06:02 +01:00

77 lines
2.2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_getline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */
/* Updated: 2019/03/07 19:02:51 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *supercat(char **s1, char *s2[BUFF_SIZE + 1])
{
char *tmp;
printf("lengths supercat %zu\n", ft_strlen(*s2));
if ((tmp = ft_strjoin(*s1, *s2)) == NULL)
return (NULL);
ft_strdel(s1);
ft_strclr(*s2);
*s1 = tmp;
return (*s1);
}
int ft_getline(char **line)
{
static char buff[BUFF_SIZE + 1] = "\0";
char *ind;
int ret;
if (line == NULL)
return (-1);
*line = NULL;
ind = NULL;
if (buff[0] == '\0')
ft_bzero(buff, BUFF_SIZE + 1);
while ((ret = read(0, buff, BUFF_SIZE)) == BUFF_SIZE && (ind = ft_strchr(buff, '\n')) == NULL)
{
if (*line == NULL)
{
if ((*line = ft_strdup(buff)) == NULL)
return (-1);
}
else if (*line != NULL)
if (supercat(line, &buff) == NULL)
{
ft_strdel(line);
return (-1);
}
ft_bzero(&buff, BUFF_SIZE + 1);
}
if (ind != NULL || (ind == NULL && (ind = ft_strchr(buff, '\n')) != NULL))
*ind = '\0';
if (*line == NULL)
{
if ((*line = ft_strdup(buff)) == NULL)
return (-1);
}
else if (*line != NULL)
{
printf("lengths %zu %zu\n", ft_strlen(*line), ft_strlen(buff));
if (supercat(line, &buff) == NULL)
{
ft_strdel(line);
return (-1);
}
}
if (ind == NULL)
ft_bzero(&buff, BUFF_SIZE + 1);
if (ind != NULL && ind < &(buff[BUFF_SIZE]))
ft_memmove(buff, ind + 1, ft_strlen(ind + 1));
return (*line != NULL && ret != 0);
}