From f0189fa7c4e5fdbfd7028e64b182b3f8cfafa631 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Mon, 14 Nov 2022 15:53:40 +0100 Subject: [PATCH] bonus fix --- get_next_line_bonus.c | 104 ++++++++++++++++++++++++++++++++++ get_next_line_bonus.h | 110 ++++++------------------------------ get_next_line_utils_bonus.c | 106 ++++++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+), 94 deletions(-) create mode 100644 get_next_line_bonus.c create mode 100644 get_next_line_utils_bonus.c diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c new file mode 100644 index 0000000..d92677c --- /dev/null +++ b/get_next_line_bonus.c @@ -0,0 +1,104 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet +# include +//# define BUFFER_SIZE 42 -char *ft_getstash(int fd) -{ - char *str; - int readed; - - str = ft_calloc(BUFFER_SIZE + 1, sizeof(char)); - if (str == NULL) - return (NULL); - readed = read(fd, str, BUFFER_SIZE); - if (readed < 1) - { - free(str); - return (NULL); - } - return (str); -} - -char *ft_getline(int fd) -{ - char *stash; - char *buf; - - stash = NULL; - buf = NULL; - while (ft_strchr(stash, '\n') == -1) - { - buf = ft_getstash(fd); - if (buf == NULL) - return (stash); - stash = ft_strfjoin(stash, buf); - if (stash == NULL) - return (NULL); - } - return (stash); -} - -char *ft_getreturn(char *str) -{ - int i; - - if (str == NULL) - return (NULL); - i = ft_strchr(str, '\n') + 1; - if (i == 0) - i = ft_strlen(str); - return (ft_strndup(str, i)); -} - -char *ft_getextra(char *str) -{ - int i; - int j; - - if (str == NULL) - return (NULL); - i = ft_strchr(str, '\n') + 1; - if (i == 0) - return (NULL); - j = ft_strlen(str + i); - return (ft_strndup(str + i, j)); -} - -char *get_next_line(int fd) -{ - static char *stash[1024] = NULL; - char *buf1; - char *buf2; - - buf2 = stash[fd]; - if (ft_strchr(stash[fd], '\n') == -1) - { - buf1 = ft_getline(fd); - buf2 = ft_strfjoin(stash[fd], buf1); - if (buf2 == NULL) - { - free(buf1); - return (NULL); - } - } - buf1 = ft_getreturn(buf2); - stash[fd] = ft_getextra(buf2); - free(buf2); - if (buf1 == NULL) - { - free(stash[fd]); - free(buf1); - return (NULL); - } - return (buf1); -} +void *ft_calloc(size_t nmemb, size_t size); +void *ft_realloc(void *ptr, size_t size); +char *ft_strfjoin(char *dst, char *src); +char *ft_strndup(char *src, size_t n); +size_t ft_strlen(char *str); +ssize_t ft_strchr(char *str, char c); +char *get_next_line(int fd); +#endif diff --git a/get_next_line_utils_bonus.c b/get_next_line_utils_bonus.c new file mode 100644 index 0000000..27287a7 --- /dev/null +++ b/get_next_line_utils_bonus.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet