From c554b5d91639432d9b483c5ab179588a753ce098 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 17 Nov 2016 17:05:13 +0100 Subject: [PATCH] new GNL --- libft/includes/get_next_line.h | 23 +++-- .../get_next_line.fr.pdf | Bin libft/src/get_next_line/get_next_line.c | 79 +++++++++++------- 3 files changed, 64 insertions(+), 38 deletions(-) rename libft/{src/get_next_line => pdf}/get_next_line.fr.pdf (100%) diff --git a/libft/includes/get_next_line.h b/libft/includes/get_next_line.h index 9163a6e5..fdeea013 100644 --- a/libft/includes/get_next_line.h +++ b/libft/includes/get_next_line.h @@ -1,20 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* getnextline.h :+: :+: :+: */ +/* get_next_line.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/07 13:21:59 by jhalford #+# #+# */ -/* Updated: 2016/11/07 13:22:13 by jhalford ### ########.fr */ +/* Created: 2016/11/05 12:21:36 by jhalford #+# #+# */ +/* Updated: 2016/11/17 13:18:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H -# define BUFF_SIZE 10 -# include "libft.h" +# define BUFF_SIZE 32 -int get_next_line(int const fd, char **line); +# include "libft/libft.h" +# include +# include + +typedef struct s_save t_save; + +struct s_save +{ + int fd; + char *str; +}; + +int get_next_line(int const fd, char **line); #endif diff --git a/libft/src/get_next_line/get_next_line.fr.pdf b/libft/pdf/get_next_line.fr.pdf similarity index 100% rename from libft/src/get_next_line/get_next_line.fr.pdf rename to libft/pdf/get_next_line.fr.pdf diff --git a/libft/src/get_next_line/get_next_line.c b/libft/src/get_next_line/get_next_line.c index 3263c520..99012019 100644 --- a/libft/src/get_next_line/get_next_line.c +++ b/libft/src/get_next_line/get_next_line.c @@ -3,63 +3,78 @@ /* ::: :::::::: */ /* get_next_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/04 12:45:02 by jhalford #+# #+# */ -/* Updated: 2016/11/04 13:43:29 by jhalford ### ########.fr */ +/* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */ +/* Updated: 2016/11/17 13:12:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" -#define BUFF_SIZE 32 +#include "get_next_line.h" +#include -static char *ft_realloc(char *line, int size) +static int ft_fdcmp(t_save *a, int *b) { - char *str; - - str = (char *)malloc(sizeof(char) * (ft_strlen(line) + size + 1)); - if (str == NULL) - return (NULL); - str = ft_strcpy(str, line); - free(line); - return (str); + return (a->fd - *b); } -static int ft_loop_read(int fd, char **line, char (*save)[]) +static t_list *ft_newfd(t_list **head, int fd) { - char buf[BUFF_SIZE + 1]; - char *pos; - int ret; + t_save new; + + new.fd = fd; + new.str = ft_memalloc((BUFF_SIZE > 0 ? BUFF_SIZE : 0) + 1); + ft_lstadd(head, ft_lstnew(&new, sizeof(t_save))); + return (*head); +} + +static int ft_loop_read(int fd, char **line, char *save) +{ + char buf[BUFF_SIZE + 1]; + char *pos; + char *tmp; + int ret; while ((ret = read(fd, buf, BUFF_SIZE)) > 0) { - buf[ret] = '\0'; + buf[ret] = 0; + tmp = *line; if ((pos = ft_strchr(buf, '\n'))) { - ft_strcpy(*save, pos + 1); - *pos = '\0'; - ft_strcat(*line, buf); - return (1); + ft_strcpy(save, pos + 1); + *pos = 0; } - if ((*line = ft_realloc(*line, ret)) == NULL) + if (!(*line = ft_strjoin(*line, buf))) return (-1); - ft_strcat(*line, buf); + ft_strdel(&tmp); + if (pos) + return (1); } - return (0); + if (ret < 0) + return (-1); + return (**line ? 1 : 0); } int get_next_line(int const fd, char **line) { - static char save[BUFF_SIZE] = ""; - char *pos; + static t_list *head; + t_list *tmp; + char *pos; + char *save; - *line = (char *)malloc(sizeof(char *) * (BUFF_SIZE + ft_strlen(save) + 1)); - *line = ft_strcpy(*line, save); + if (fd < 0 || !line) + return (-1); + if (!(tmp = ft_lst_find(head, (void *)&fd, &ft_fdcmp))) + tmp = ft_newfd(&head, fd); + save = ((t_save*)tmp->content)->str; + if (!(*line = ft_strdup(save))) + return (-1); + ft_bzero(save, BUFF_SIZE + 1); if ((pos = ft_strchr(*line, '\n'))) { ft_strcpy(save, pos + 1); - *pos = '\0'; + *pos = 0; return (1); } - return (ft_loop_read(fd, line, &save)); + return (ft_loop_read(fd, line, save)); }