libft/ft_getline.c
vagrant 13d96a14d8 finally \o/
added ft_printf to libft
finalised ans tested ft_getline
2019-03-07 22:29:34 +00:00

71 lines
2.0 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_getline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */
/* Updated: 2019/03/07 22:06:57 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_strclr(buff);
*s1 = tmp;
return (*s1);
}
static int update_line(char **line, char *buff)
{
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);
}
return (0);
}
int ft_getline(char **line)
{
static char buff[BUFF_SIZE + 1];
char *ind;
int ret;
if (line == NULL)
return (-1);
*line = NULL;
ind = NULL;
ft_bzero(buff, BUFF_SIZE + 1);
while ((ret = read(0, buff, BUFF_SIZE)) == BUFF_SIZE
&& (ind = ft_strchr(buff, '\n')) == NULL)
{
if (update_line(line, buff) == -1)
return (-1);
ft_bzero(&buff, BUFF_SIZE + 1);
}
if (ind != NULL || (ind == NULL && (ind = ft_strchr(buff, '\n')) != NULL))
*ind = '\0';
if (update_line(line, buff) == -1)
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);
}