From 11da29a0202352205f8e17064ada37b72e5db605 Mon Sep 17 00:00:00 2001 From: hugodu69 Date: Fri, 27 Dec 2019 12:36:31 +0100 Subject: [PATCH] copi new gnl en bonus --- get_next_line.c | 225 ++++++++++++++---------------------- get_next_line.h | 3 +- get_next_line_bonus.c | 124 +++++++++----------- get_next_line_bonus.h | 13 +-- get_next_line_utils.c | 23 ++++ get_next_line_utils_bonus.c | 29 ++++- test.txt | 3 + 7 files changed, 203 insertions(+), 217 deletions(-) create mode 100644 test.txt diff --git a/get_next_line.c b/get_next_line.c index d6c9a15..02bbf33 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -12,58 +12,82 @@ #include "get_next_line.h" +/* +** //-g -fsanitize=address +** //:set noendofline binary +** //https://github.com/charMstr/GNL_lover +** //https://github.com/mrjvs/42cursus_gnl_tests +** //https://github.com/Sherchryst/gnlkiller +** //https://github.com/Hellio404/Get_Next_Line_Tester +** //https://github.com/TinfoilPancakes/get-next-line-testing-tools +** +** #include //for printf +** #include //for open +** +** int main(int ac, char **av) +** { +** int *fd; +** int i; +** int j; +** int ret; +** char *line; +** +** // if (ac < 2) +** // return (0); +** +** // (void)i;(void)j; +** // line = NULL; +** // fd[0] = open(av[1], O_RDONLY); +** // ret = 1; +** // while (ret) +** // { +** // ret = get_next_line(fd[0], &line); +** // printf("[%i] %s\n", ret, line); +** // } +** +** line = NULL; +** i = 0; +** if (!(fd = (int *)malloc(sizeof(int) * ac))) +** return (0); +** while (++i <= ac - 1) +** fd[i - 1] = open(av[i], O_RDONLY); +** i = 0; +** j = 0; +** while (j < ac - 1) +** { +** if ((ret = get_next_line(fd[i], &line)) > 0) +** { +** printf("[fd%i-%i] %s\n", fd[i], ret, line); +** free(line); +** j = 0; +** } +** else +** { +** printf("[fd%i-%i]*FINI*\n", fd[i], ret); +** j++; +** } +** i++; +** if (i >= ac - 1) +** i = 0; +** } +** free (fd); +** //while (1); +** return (0); +** } +*/ - //-g -fsanitize=address - //:set noendofline binary - //https://github.com/charMstr/GNL_lover - //https://github.com/mrjvs/42cursus_gnl_tests - //https://github.com/Sherchryst/gnlkiller - //https://github.com/Hellio404/Get_Next_Line_Tester - //https://github.com/TinfoilPancakes/get-next-line-testing-tools - - #include //for printf - #include //for open - - int main(int ac, char **av) - { - int *fd; - int i; - int j; - int ret; - char *line; - - if (ac < 2) - return (0); - line = NULL; - i = 0; - if (!(fd = (int *)malloc(sizeof(int) * ac))) - return (0); - while (++i <= ac - 1) - fd[i - 1] = open(av[i], O_RDONLY); - i = 0; - j = 0; - while (j < ac - 1) - { - if ((ret = get_next_line(fd[i], &line)) > 0) - { - printf("[fd%i-%i][%s]\n", fd[i], ret, line); - free(line); - j = 0; - } - else - { - printf("[fd%i-%i]*FINI*\n", fd[i], ret); - j++; - } - i++; - if (i >= ac - 1) - i = 0; - } - free (fd); - //while (1); - return (0); - } +int free_lst(t_gnlist **lst, const int fd, int ret) +{ + t_gnlist *tmp; + tmp = *lst; + while ((*lst)->next && (*lst)->next->lfd != fd) + (*lst) = (*lst)->next; + free(tmp->string); + (*lst)->next = (*lst)->next->next; + free(tmp); + return (ret); +} int multi_fd(int fd, t_gnlist **lst) { @@ -91,61 +115,6 @@ int multi_fd(int fd, t_gnlist **lst) return (1); } -char *ft_strjoinfree(char const *s1, char const *s2) -{ - char *str; - int len; - int j; - - if (!s1 || !s2) - return (NULL); - len = ft_strlen(s1) + ft_strlen(s2); - if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) - return (NULL); - len = 0; - j = 0; - while (s1[j] != '\0') - str[len++] = s1[j++]; - j = 0; - while (s2[j] != '\0') - str[len++] = s2[j++]; - str[len] = '\0'; - free((char*)s1); - return (str); -} - -int hasnewline(char *str, t_gnlist *lst, char **line) -{ - str[0] = '\0'; - if (!(*line = ft_strdup(lst->string))) - return (-1); - ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); - return (1); -} - -void free_lst(t_gnlist **lst, const int fd) -{ - t_gnlist *tmp; - - tmp = *lst; - while ((*lst)->next && (*lst)->next->lfd != fd) - (*lst) = (*lst)->next; - free(tmp->string); - (*lst)->next = (*lst)->next->next; - free(tmp); -} - -char *ft_strdup_limit(const char *src, int limit) -{ - char *str; - - if (!(str = (char*)malloc(sizeof(*str) * (limit + 1)))) - return (NULL); - while (limit-- >= 0) - str[limit + 1] = src[limit + 1]; - return (str); -} - int get_next_line(const int fd, char **line) { char buf[BUFFER_SIZE + 1]; @@ -153,44 +122,24 @@ int get_next_line(const int fd, char **line) static t_gnlist *lst = NULL; char *str; -// ret = read(fd, buf, BUFFER_SIZE); -// buf[ret] = '\0'; -// *line = ft_strdup_limit(buf, BUFFER_SIZE); -// return (ret); - - ret = 1; - if (!(multi_fd(fd, &lst)) || !line || fd < 0 || BUFFER_SIZE < 1) - return (-1); + if (!(multi_fd(fd, &lst)) || !line || BUFFER_SIZE < 1) + return (free_lst(&lst, fd, -1)); while (!(str = ft_strchr(lst->string, '\n')) && ret != 0) { if ((ret = read(fd, buf, BUFFER_SIZE)) < 0) - return (-1); + return (free_lst(&lst, fd, -1)); buf[ret] = '\0'; if (!(lst->string = ft_strjoinfree(lst->string, buf))) - return (-1); + return (free_lst(&lst, fd, -1)); } - str[0] = '\0'; - if (!(*line = ft_strdup(lst->string))) - return (-1); - ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); - return (ret > BUFFER_SIZE); - -// while (ret > 0) -// { -// if ((str = ft_strchr(lst->string, '\n'))) -// return (hasnewline(str, lst, line)); -// if ((ret = read(fd, buf, BUFFER_SIZE)) < 0) -// return (-1); -// buf[ret] = '\0'; -// if (!(lst->string = ft_strjoinfree(lst->string, buf))) -// return (-1); -// } -// if (*(lst->string) && !(*line = ft_strdup(lst->string))) -// return (-1); -// if (*(lst->string) && ++ret == 1) -// (lst->string)[0] = '\0'; -// else -// free_lst(&lst, fd); -// return (ret); + if (str != NULL) + { + str[0] = '\0'; + if (!(*line = ft_strdup(lst->string))) + return (free_lst(&lst, fd, -1)); + ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); + return (1); + } + return (free_lst(&lst, fd, 0)); } diff --git a/get_next_line.h b/get_next_line.h index 60a540e..f1ed2d9 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -27,12 +27,11 @@ typedef struct s_gnlist int get_next_line(const int fd, char **line); int multi_fd(int fd, t_gnlist **lst); char *ft_strjoinfree(char const *s1, char const *s2); -int hasnewline(char *str, t_gnlist *lst, char **line); -void free_lst(t_gnlist **lst, const int fd); size_t ft_strlen(const char *str); void *ft_memmove(void *dst, const void *src, size_t len); char *ft_strchr(const char *s, int c); char *ft_strdup(const char *src); +int free_lst(t_gnlist **lst, const int fd, int ret); #endif diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c index 5de312f..96f36ef 100644 --- a/get_next_line_bonus.c +++ b/get_next_line_bonus.c @@ -1,18 +1,26 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line_bonus.c :+: :+: :+: */ +/* get_next_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/12/16 14:08:36 by hulamy #+# #+# */ -/* Updated: 2019/12/21 00:29:07 by hulamy ### ########.fr */ +/* Created: 2019/12/21 00:28:33 by hulamy #+# #+# */ +/* Updated: 2019/12/21 00:28:39 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "get_next_line_bonus.h" /* +** //-g -fsanitize=address +** //:set noendofline binary +** //https://github.com/charMstr/GNL_lover +** //https://github.com/mrjvs/42cursus_gnl_tests +** //https://github.com/Sherchryst/gnlkiller +** //https://github.com/Hellio404/Get_Next_Line_Tester +** //https://github.com/TinfoilPancakes/get-next-line-testing-tools +** ** #include //for printf ** #include //for open ** @@ -21,10 +29,22 @@ ** int *fd; ** int i; ** int j; +** int ret; ** char *line; ** -** if (ac < 2) -** return (0); +** // if (ac < 2) +** // return (0); +** +** // (void)i;(void)j; +** // line = NULL; +** // fd[0] = open(av[1], O_RDONLY); +** // ret = 1; +** // while (ret) +** // { +** // ret = get_next_line(fd[0], &line); +** // printf("[%i] %s\n", ret, line); +** // } +** ** line = NULL; ** i = 0; ** if (!(fd = (int *)malloc(sizeof(int) * ac))) @@ -35,15 +55,15 @@ ** j = 0; ** while (j < ac - 1) ** { -** if (get_next_line(fd[i], &line) > 0) +** if ((ret = get_next_line(fd[i], &line)) > 0) ** { -** printf("fd%i| %s\n", fd[i], line); +** printf("[fd%i-%i] %s\n", fd[i], ret, line); ** free(line); ** j = 0; ** } ** else ** { -** printf("fd%i|*FINI*\n", fd[i]); +** printf("[fd%i-%i]*FINI*\n", fd[i], ret); ** j++; ** } ** i++; @@ -51,11 +71,24 @@ ** i = 0; ** } ** free (fd); -** // while (1); +** //while (1); ** return (0); ** } */ +int free_lst(t_gnlist **lst, const int fd, int ret) +{ + t_gnlist *tmp; + + tmp = *lst; + while ((*lst)->next && (*lst)->next->lfd != fd) + (*lst) = (*lst)->next; + free(tmp->string); + (*lst)->next = (*lst)->next->next; + free(tmp); + return (ret); +} + int multi_fd(int fd, t_gnlist **lst) { t_gnlist *tmp; @@ -82,50 +115,6 @@ int multi_fd(int fd, t_gnlist **lst) return (1); } -char *ft_strjoinfree(char const *s1, char const *s2) -{ - char *str; - int len; - int j; - - if (!s1 || !s2) - return (NULL); - len = ft_strlen(s1) + ft_strlen(s2); - if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) - return (NULL); - len = 0; - j = 0; - while (s1[j] != '\0') - str[len++] = s1[j++]; - j = 0; - while (s2[j] != '\0') - str[len++] = s2[j++]; - str[len] = '\0'; - free((char*)s1); - return (str); -} - -int hasnewline(char *str, t_gnlist *lst, char **line) -{ - str[0] = '\0'; - if (!(*line = ft_strdup(lst->string))) - return (-1); - ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); - return (1); -} - -void free_lst(t_gnlist **lst, const int fd) -{ - t_gnlist *tmp; - - tmp = *lst; - while ((*lst)->next && (*lst)->next->lfd != fd) - (*lst) = (*lst)->next; - free(tmp->string); - (*lst)->next = (*lst)->next->next; - free(tmp); -} - int get_next_line(const int fd, char **line) { char buf[BUFFER_SIZE + 1]; @@ -133,23 +122,24 @@ int get_next_line(const int fd, char **line) static t_gnlist *lst = NULL; char *str; - if (!(multi_fd(fd, &lst)) || !(ret = 1) || !line || fd < 0) - return (-1); - while (ret > 0) + ret = 1; + if (!(multi_fd(fd, &lst)) || !line || BUFFER_SIZE < 1) + return (free_lst(&lst, fd, -1)); + while (!(str = ft_strchr(lst->string, '\n')) && ret != 0) { - if ((str = ft_strchr(lst->string, '\n'))) - return (hasnewline(str, lst, line)); if ((ret = read(fd, buf, BUFFER_SIZE)) < 0) - return (-1); + return (free_lst(&lst, fd, -1)); buf[ret] = '\0'; if (!(lst->string = ft_strjoinfree(lst->string, buf))) - return (-1); + return (free_lst(&lst, fd, -1)); } - if (*(lst->string) && !(*line = ft_strdup(lst->string))) - return (-1); - if (*(lst->string) && ++ret == 1) - (lst->string)[0] = '\0'; - else - free_lst(&lst, fd); - return (ret); + if (str != NULL) + { + str[0] = '\0'; + if (!(*line = ft_strdup(lst->string))) + return (free_lst(&lst, fd, -1)); + ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); + return (1); + } + return (free_lst(&lst, fd, 0)); } diff --git a/get_next_line_bonus.h b/get_next_line_bonus.h index e3b6950..f1ed2d9 100644 --- a/get_next_line_bonus.h +++ b/get_next_line_bonus.h @@ -1,17 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line_bonus.h :+: :+: :+: */ +/* get_next_line.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/12/16 14:07:51 by hulamy #+# #+# */ -/* Updated: 2019/12/19 19:38:21 by hulamy ### ########.fr */ +/* Created: 2019/01/29 10:19:37 by hulamy #+# #+# */ +/* Updated: 2019/12/19 19:38:39 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef GET_NEXT_LINE_BONUS_H -# define GET_NEXT_LINE_BONUS_H +#ifndef GET_NEXT_LINE_H +# define GET_NEXT_LINE_H # include # include @@ -27,12 +27,11 @@ typedef struct s_gnlist int get_next_line(const int fd, char **line); int multi_fd(int fd, t_gnlist **lst); char *ft_strjoinfree(char const *s1, char const *s2); -int hasnewline(char *str, t_gnlist *lst, char **line); -void free_lst(t_gnlist **lst, const int fd); size_t ft_strlen(const char *str); void *ft_memmove(void *dst, const void *src, size_t len); char *ft_strchr(const char *s, int c); char *ft_strdup(const char *src); +int free_lst(t_gnlist **lst, const int fd, int ret); #endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 041aed1..4a3190a 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -71,3 +71,26 @@ size_t ft_strlen(const char *str) i++; return (i); } + +char *ft_strjoinfree(char const *s1, char const *s2) +{ + char *str; + int len; + int j; + + if (!s1 || !s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + len = 0; + j = 0; + while (s1[j] != '\0') + str[len++] = s1[j++]; + j = 0; + while (s2[j] != '\0') + str[len++] = s2[j++]; + str[len] = '\0'; + free((char*)s1); + return (str); +} diff --git a/get_next_line_utils_bonus.c b/get_next_line_utils_bonus.c index 3ecc75e..f25e66c 100644 --- a/get_next_line_utils_bonus.c +++ b/get_next_line_utils_bonus.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* get_next_line_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/12/16 14:08:47 by hulamy #+# #+# */ -/* Updated: 2019/12/16 14:10:54 by hulamy ### ########.fr */ +/* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */ +/* Updated: 2019/12/11 00:57:19 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,3 +71,26 @@ size_t ft_strlen(const char *str) i++; return (i); } + +char *ft_strjoinfree(char const *s1, char const *s2) +{ + char *str; + int len; + int j; + + if (!s1 || !s2) + return (NULL); + len = ft_strlen(s1) + ft_strlen(s2); + if (!(str = (char *)malloc(sizeof(char) * (len + 1)))) + return (NULL); + len = 0; + j = 0; + while (s1[j] != '\0') + str[len++] = s1[j++]; + j = 0; + while (s2[j] != '\0') + str[len++] = s2[j++]; + str[len] = '\0'; + free((char*)s1); + return (str); +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..f23c68e --- /dev/null +++ b/test.txt @@ -0,0 +1,3 @@ +sdfolisjfoijef +sdfkjdsflkjk +dsgdfgdsfg \ No newline at end of file