From 7dc605631e697c4de85d8e74fc4adab94c0f0058 Mon Sep 17 00:00:00 2001 From: hugodu69 Date: Sun, 29 Dec 2019 01:25:35 +0100 Subject: [PATCH] ajout de pleins de phrases de debug --- get_next_line.c | 220 +++++++++++++++++++++++++++++++----------------- get_next_line.h | 6 +- 2 files changed, 144 insertions(+), 82 deletions(-) diff --git a/get_next_line.c b/get_next_line.c index 02bbf33..cca6603 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -12,80 +12,134 @@ #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 -int free_lst(t_gnlist **lst, const int fd, int ret) + #include //for printf + #include //for open + + void ft_putchar(char c){ + write(1, &c, 1); + } + + void ft_putstr(char *s) + { + while (s && *s) + ft_putchar(*s++); + } + + void ft_putnbr(int n) + { + long l; + + l = n; + if (l < 0) + { + ft_putchar('-'); + l *= -1; + } + if (l >= 10) + ft_putnbr(l / 10); + ft_putchar((l % 10) + '0'); + } + + 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); + ft_putstr("[fd");ft_putnbr(fd[i]);ft_putchar('-');ft_putnbr(ret);ft_putstr("]\n"); + free(line); + j = 0; + } + else + { + free(line); + //printf("[fd%i-%i]*FINI*\n", fd[i], ret); + ft_putstr("[fd");ft_putnbr(fd[i]);ft_putchar('-');ft_putnbr(ret);ft_putstr("]*FINI*\n"); + j++; + } + i++; + if (i >= ac - 1) + i = 0; + } + free(fd); + //while (1); + return (0); + } + + +int free_lst(t_gnlist **lst, int ret) { t_gnlist *tmp; + int debug; tmp = *lst; - while ((*lst)->next && (*lst)->next->lfd != fd) + debug = tmp->lfd; + +// char *s; +// s = tmp->string; +// if (ret == -1) +// while (lst->lfd != fd) +// { +// free((*lst)->string); +// *lst = (*lst)->next; +// free(tmp); +// tmp = *lst; +// } +// tmp->string = tmp->next->string; +// free(s); + +// write(1, "\nIII\n", 5); + while ((*lst)->next != tmp) (*lst) = (*lst)->next; - free(tmp->string); (*lst)->next = (*lst)->next->next; + free(tmp->str); free(tmp); + + tmp = (*lst)->next; + ft_putstr("\nDEL ");debug>9 ? ft_putstr(" "):ft_putstr(" ");ft_putchar('-'); + while (tmp != *lst) + { + ft_putnbr(tmp->lfd); ft_putchar('-'); tmp = tmp->next; + } + ft_putnbr(tmp->lfd); ft_putchar('-'); tmp = tmp->next; + ft_putchar('('); ft_putnbr(tmp->lfd); ft_putstr(")"); + return (ret); } @@ -101,7 +155,7 @@ int multi_fd(int fd, t_gnlist **lst) if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp)))) return (0); tmp->lfd = fd; - if (!(tmp->string = ft_strdup(""))) + if (!(tmp->str = ft_strdup(""))) return (0); if (*lst) { @@ -112,6 +166,15 @@ int multi_fd(int fd, t_gnlist **lst) tmp->next = tmp; *lst = tmp; } + + tmp = *lst; + ft_putstr("LST ");ft_putnbr(tmp->lfd); ft_putchar('-'); tmp = tmp->next; + while (tmp != *lst) + { + ft_putnbr(tmp->lfd); ft_putchar('-'); tmp = tmp->next; + } + ft_putchar('('); ft_putnbr(tmp->lfd); ft_putstr(")"); + return (1); } @@ -124,22 +187,21 @@ int get_next_line(const int fd, char **line) 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) + return (free_lst(&lst, -1)); + while (!(str = ft_strchr(lst->str, '\n')) && ret != 0) { if ((ret = read(fd, buf, BUFFER_SIZE)) < 0) - return (free_lst(&lst, fd, -1)); + return (free_lst(&lst, -1)); buf[ret] = '\0'; - if (!(lst->string = ft_strjoinfree(lst->string, buf))) - return (free_lst(&lst, fd, -1)); + if (!(lst->str = ft_strjoinfree(lst->str, buf))) + return (free_lst(&lst, -1)); } 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)); + if (!(*line = ft_strdup(lst->str))) + return (free_lst(&lst, -1)); + if (str != NULL) + return(ft_memmove(lst->str, str + 1, ft_strlen(str + 1) + 1) != NULL); +// return (0); + return (free_lst(&lst, 0)); } diff --git a/get_next_line.h b/get_next_line.h index f1ed2d9..3e8cb80 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -20,18 +20,18 @@ typedef struct s_gnlist { int lfd; - char *string; + char *str; struct s_gnlist *next; } t_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 free_lst(t_gnlist **lst, int ret); 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); +char *ft_strjoinfree(char const *s1, char const *s2); #endif