creation des bonus

This commit is contained in:
Hugo LAMY
2019-12-29 18:11:06 +01:00
parent 6f9d6ee1a2
commit 7a475a6824
5 changed files with 46 additions and 49 deletions

View File

@@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/29 18:07:43 by hulamy #+# #+# */
/* Updated: 2019/12/29 18:08:47 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
/* /*

View File

@@ -1,20 +1,21 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */ /* get_next_line_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/21 00:28:33 by hulamy #+# #+# */ /* Created: 2019/12/29 18:06:33 by hulamy #+# #+# */
/* Updated: 2019/12/21 00:28:39 by hulamy ### ########.fr */ /* Updated: 2019/12/29 18:07:19 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line_bonus.h" #include "get_next_line_bonus.h"
/* /*
** //-g -fsanitize=address
** //:set noendofline binary ** //:set noendofline binary
** //https://github.com/lgrellie/gnl_tester
** //https://github.com/Mazoise/42TESTERS-GNL
** //https://github.com/charMstr/GNL_lover ** //https://github.com/charMstr/GNL_lover
** //https://github.com/mrjvs/42cursus_gnl_tests ** //https://github.com/mrjvs/42cursus_gnl_tests
** //https://github.com/Sherchryst/gnlkiller ** //https://github.com/Sherchryst/gnlkiller
@@ -27,36 +28,20 @@
** int main(int ac, char **av) ** int main(int ac, char **av)
** { ** {
** int *fd; ** int *fd;
** int i; ** int i = 0;
** int j; ** int j = 0;
** int ret; ** int ret = 1;
** char *line; ** char *line = NULL;
** **
** // if (ac < 2) ** fd = (int *)malloc(sizeof(int) * ac);
** // 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) ** while (++i <= ac - 1)
** fd[i - 1] = open(av[i], O_RDONLY); ** fd[i - 1] = open(av[i], O_RDONLY);
** i = 0; ** i = 0;
** j = 0;
** while (j < ac - 1) ** while (j < ac - 1)
** { ** {
** if ((ret = get_next_line(fd[i], &line)) > 0) ** if (ret > 0)
** { ** {
** ret = get_next_line(fd[i], &line);
** printf("[fd%i-%i] %s\n", fd[i], ret, line); ** printf("[fd%i-%i] %s\n", fd[i], ret, line);
** free(line); ** free(line);
** j = 0; ** j = 0;
@@ -70,22 +55,24 @@
** if (i >= ac - 1) ** if (i >= ac - 1)
** i = 0; ** i = 0;
** } ** }
** free (fd); ** free(fd);
** //while (1); ** //while (1);
** return (0); ** return (0);
** } ** }
*/ */
int free_lst(t_gnlist **lst, const int fd, int ret) int free_lst(t_gnlist **lst, int ret)
{ {
t_gnlist *tmp; t_gnlist *tmp;
tmp = *lst; tmp = *lst;
while ((*lst)->next && (*lst)->next->lfd != fd) while ((*lst)->next != tmp)
(*lst) = (*lst)->next; (*lst) = (*lst)->next;
free(tmp->string);
(*lst)->next = (*lst)->next->next; (*lst)->next = (*lst)->next->next;
free(tmp->str);
free(tmp); free(tmp);
tmp = NULL;
*lst = tmp;
return (ret); return (ret);
} }
@@ -101,7 +88,7 @@ int multi_fd(int fd, t_gnlist **lst)
if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp)))) if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp))))
return (0); return (0);
tmp->lfd = fd; tmp->lfd = fd;
if (!(tmp->string = ft_strdup(""))) if (!(tmp->str = ft_strdup("")))
return (0); return (0);
if (*lst) if (*lst)
{ {
@@ -124,22 +111,20 @@ int get_next_line(const int fd, char **line)
ret = 1; ret = 1;
if (!(multi_fd(fd, &lst)) || !line || BUFFER_SIZE < 1) if (!(multi_fd(fd, &lst)) || !line || BUFFER_SIZE < 1)
return (free_lst(&lst, fd, -1)); return (free_lst(&lst, -1));
while (!(str = ft_strchr(lst->string, '\n')) && ret != 0) while (!(str = ft_strchr(lst->str, '\n')) && ret != 0)
{ {
if ((ret = read(fd, buf, BUFFER_SIZE)) < 0) if ((ret = read(fd, buf, BUFFER_SIZE)) < 0)
return (free_lst(&lst, fd, -1)); return (free_lst(&lst, -1));
buf[ret] = '\0'; buf[ret] = '\0';
if (!(lst->string = ft_strjoinfree(lst->string, buf))) if (!(lst->str = ft_strjoinfree(lst->str, buf)))
return (free_lst(&lst, fd, -1)); return (free_lst(&lst, -1));
} }
if (str != NULL) if (str != NULL)
{
str[0] = '\0'; str[0] = '\0';
if (!(*line = ft_strdup(lst->string))) if (!(*line = ft_strdup(lst->str)))
return (free_lst(&lst, fd, -1)); return (free_lst(&lst, -1));
ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1); if (str != NULL)
return (1); return(ft_memmove(lst->str, str + 1, ft_strlen(str + 1) + 1) != NULL);
} return (free_lst(&lst, 0));
return (free_lst(&lst, fd, 0));
} }

View File

@@ -20,18 +20,18 @@
typedef struct s_gnlist typedef struct s_gnlist
{ {
int lfd; int lfd;
char *string; char *str;
struct s_gnlist *next; struct s_gnlist *next;
} t_gnlist; } t_gnlist;
int get_next_line(const int fd, char **line); int get_next_line(const int fd, char **line);
int multi_fd(int fd, t_gnlist **lst); 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); size_t ft_strlen(const char *str);
void *ft_memmove(void *dst, const void *src, size_t len); void *ft_memmove(void *dst, const void *src, size_t len);
char *ft_strchr(const char *s, int c); char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *src); 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 #endif

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */ /* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */
/* Updated: 2019/12/11 00:57:19 by hulamy ### ########.fr */ /* Updated: 2019/12/29 18:07:58 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */ /* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */ /* Created: 2019/12/11 00:26:54 by hulamy #+# #+# */
/* Updated: 2019/12/11 00:57:19 by hulamy ### ########.fr */ /* Updated: 2019/12/29 18:04:55 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */