From d81a696d0ed3fafc4865461ae3446cf62e6dab70 Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Thu, 7 Jun 2018 16:56:52 +0200 Subject: [PATCH] adding get_next_line --- Makefile | 4 +- get_next_line.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 10 ++++- 3 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 get_next_line.c diff --git a/Makefile b/Makefile index 4c3fd4d..11aa14c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: tmaze +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2018/04/07 12:47:06 by tmaze #+# #+# # -# Updated: 2018/05/16 12:22:34 by tmaze ### ########.fr # +# Updated: 2018/06/07 16:54:14 by tmaze ### ########.fr # # # #******************************************************************************# @@ -19,7 +19,7 @@ NAME = libft.a SRCS = ft_memset.c ft_bzero.c ft_memcpy.c ft_memccpy.c ft_memmove.c ft_memchr.c ft_memcmp.c ft_strlen.c ft_strdup.c ft_strcpy.c ft_strncpy.c ft_strcat.c ft_strncat.c ft_strlcat.c ft_strchr.c ft_strrchr.c ft_strstr.c ft_strnstr.c ft_strcmp.c ft_strncmp.c ft_atoi.c ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c ft_toupper.c ft_tolower.c \ ft_memalloc.c ft_memdel.c ft_strnew.c ft_strdel.c ft_strclr.c ft_striter.c ft_striteri.c ft_strmap.c ft_strmapi.c ft_strequ.c ft_strnequ.c ft_strsub.c ft_strjoin.c ft_strtrim.c ft_strsplit.c ft_itoa.c ft_putchar.c ft_putstr.c ft_putendl.c ft_putnbr.c ft_putchar_fd.c ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c\ ft_lstnew.c ft_lstdelone.c ft_lstdel.c ft_lstadd.c ft_lstiter.c ft_lstmap.c \ - ft_isupper.c ft_islower.c ft_str_is_alpha.c ft_str_is_lowercase.c ft_str_is_numeric.c ft_str_is_printable.c ft_str_is_uppercase.c ft_strcapitalize.c ft_strlcpy.c ft_strlowcase.c ft_strupcase.c ft_lstsize.c ft_lstgetat.c ft_lstgetlast.c ft_sort_params.c ft_print_words_tables.c ft_lstaddend.c ft_strndup.c ft_abs.c ft_strnchr.c ft_strrnchr.c + ft_isupper.c ft_islower.c ft_str_is_alpha.c ft_str_is_lowercase.c ft_str_is_numeric.c ft_str_is_printable.c ft_str_is_uppercase.c ft_strcapitalize.c ft_strlcpy.c ft_strlowcase.c ft_strupcase.c ft_lstsize.c ft_lstgetat.c ft_lstgetlast.c ft_sort_params.c ft_print_words_tables.c ft_lstaddend.c ft_strndup.c ft_abs.c ft_strnchr.c ft_strrnchr.c get_next_line.c OBJS = $(SRCS:.c=.o) INCLS = -I. diff --git a/get_next_line.c b/get_next_line.c new file mode 100644 index 0000000..fda7155 --- /dev/null +++ b/get_next_line.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2018/04/24 18:08:15 by tmaze #+# #+# */ +/* Updated: 2018/06/07 16:55:21 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static char **get_buff(const int fd, t_list **lst) +{ + t_list *tmp; + + tmp = *lst; + while (tmp) + { + if (tmp->content_size == (size_t)fd) + return ((char**)&tmp->content); + tmp = tmp->next; + } + if ((tmp = ft_lstnew("\0", fd)) == NULL) + return (NULL); + ft_lstadd(lst, tmp); + return (get_buff(fd, lst)); +} + +static char *supercat(char **s1, char **s2) +{ + char *tmp; + + if ((tmp = ft_strjoin(*s1, *s2)) == NULL) + return (NULL); + ft_strdel(s1); + ft_strclr(*s2); + *s1 = tmp; + return (*s1); +} + +static int check_buff(char **buff, char **line) +{ + int ret; + + ret = 1; + if (buff != NULL && (*buff == NULL || ft_strlen(*buff) == 0)) + ret = 0; + else if ((*line = ft_strdup(*buff)) == NULL) + ret = -1; + ft_strclr(*buff); + return (ret); +} + +static int read_gnl(const int fd, char **buff, char **line) +{ + char *tmp; + int ret; + + if ((tmp = ft_strnew(BUFF_SIZE)) == NULL) + return (-1); + while (ft_strrnchr(*buff, '\n', BUFF_SIZE) == NULL &&\ + (ret = read(fd, tmp, BUFF_SIZE)) > 0) + if (supercat(buff, &tmp) == NULL) + { + ft_strdel(&tmp); + return (-1); + } + ft_strdel(&tmp); + if (buff != NULL && *buff != NULL &&\ + ft_strrnchr(*buff, '\n', BUFF_SIZE) != NULL) + return (get_next_line(fd, line)); + else + return (check_buff(buff, line)); +} + +int get_next_line(const int fd, char **line) +{ + static t_list *lst = NULL; + char **buff; + char *tmp; + + tmp = NULL; + if (fd < 0 || read(fd, tmp, 0) == -1 || line == NULL) + return (-1); + if ((buff = get_buff(fd, &lst)) == NULL) + return (-1); + tmp = ft_strchr(*buff, '\n'); + if (tmp != NULL) + { + *line = ft_strndup(*buff, tmp - *buff); + ft_memmove(*buff, &tmp[1], ft_strlen(&tmp[1]) + 1); + return (1); + } + return (read_gnl(fd, buff, line)); +} diff --git a/libft.h b/libft.h index 1889bf9..09f95cf 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */ -/* Updated: 2018/05/16 12:19:51 by tmaze ### ########.fr */ +/* Updated: 2018/06/07 16:54:31 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,12 @@ typedef struct s_list struct s_list *next; } t_list; +/* +** definition macro BUFF_SIZE +*/ + +# define BUFF_SIZE 30 + /* ** fonctions obligatoires */ @@ -131,4 +137,6 @@ char *ft_strndup(const char *s1, size_t n); unsigned int ft_abs(int nb); char *ft_strnchr(const char *s, int c, int n); char *ft_strrnchr(const char *s, int c, int n); + +int get_next_line(const int fd, char **line); #endif