From 961ea09e1da0a8abc2f0edf5d3f4a4500b0f6e88 Mon Sep 17 00:00:00 2001 From: Gregory Tertyshny Date: Fri, 24 Mar 2017 20:07:40 +0200 Subject: [PATCH] libft check --- libft/ft_lst_len.c | 4 +- libft/get_next_line.c | 147 ++++++++++++++++++++++++++--------------- libft/includes/libft.h | 18 ++--- 3 files changed, 103 insertions(+), 66 deletions(-) diff --git a/libft/ft_lst_len.c b/libft/ft_lst_len.c index 26ffd30..0c0c3a7 100644 --- a/libft/ft_lst_len.c +++ b/libft/ft_lst_len.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/04 19:13:25 by gtertysh #+# #+# */ -/* Updated: 2016/12/06 19:47:50 by gtertysh ### ########.fr */ +/* Updated: 2017/03/24 20:06:06 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,4 +23,4 @@ int ft_lst_len(t_list *lst) len++; } return (len); -} \ No newline at end of file +} diff --git a/libft/get_next_line.c b/libft/get_next_line.c index 2f23cbf..c58f273 100644 --- a/libft/get_next_line.c +++ b/libft/get_next_line.c @@ -5,72 +5,109 @@ /* +:+ +:+ +:+ */ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/12/04 15:59:21 by gtertysh #+# #+# */ -/* Updated: 2016/12/06 19:45:44 by gtertysh ### ########.fr */ +/* Created: 2017/01/28 18:15:15 by gtertysh #+# #+# */ +/* Updated: 2017/01/28 18:15:17 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "get_next_line.h" -static t_fd *new_node(int fd) +t_fd *new_node(int fd) { - t_fd *new; + t_fd *new; - new = NULL; - if ((new = (t_fd *)malloc(sizeof(t_fd)))) - { - new->fd = fd; - new->n = NULL; - new->next = NULL; - new->t = 0; - } - return (new); + new = NULL; + if ((new = (t_fd *)malloc(sizeof(t_fd)))) + { + new->fd = fd; + new->n = NULL; + new->next = NULL; + new->t = 0; + } + return (new); } -static t_fd *find_fd(int fd, t_fd **cur_fd) +t_fd *find_fd(int fd, t_fd **cur_fd) { - if (!*cur_fd) - return ((*cur_fd = new_node(fd))); - while ((*cur_fd)) - { - if ((*cur_fd)->fd == fd) - return (*cur_fd); - cur_fd = &((*cur_fd)->next); - } - return ((*cur_fd = new_node(fd))); + if (!*cur_fd) + return ((*cur_fd = new_node(fd))); + while ((*cur_fd)) + { + if ((*cur_fd)->fd == fd) + return (*cur_fd); + cur_fd = &((*cur_fd)->next); + } + return ((*cur_fd = new_node(fd))); +} + +int check_reminder(t_fd *c, char **line) +{ + char *substr; + char *old_line; + + old_line = *line; + if (c->n) + { + if ((c->t = ft_strchr(c->n, '\n'))) + { + substr = ft_strsub(c->n, 0, c->t - c->n); + *line = ft_strjoin(*line, substr); + free(substr); + c->n = ++(c->t); + return (1); + } + else + { + *line = ft_strjoin(*line, c->n); + c->n = 0; + } + free(old_line); + } + return (0); +} + +int check_buf(t_fd *c, char **line) +{ + char *substr; + char *old_line; + + old_line = *line; + while ((c->readed = read(c->fd, c->b, BUFF_SIZE))) + { + if (c->readed == -1) + return (-1); + c->b[c->readed] = 0; + if ((c->n = ft_strchr(c->b, '\n'))) + { + substr = ft_strsub(c->b, 0, c->n++ - c->b); + old_line = *line; + *line = ft_strjoin(*line, substr); + free(substr); + free(old_line); + break ; + } + old_line = *line; + *line = ft_strjoin(*line, c->b); + free(old_line); + } + return (1); } int get_next_line(const int fd, char **line) { - static t_fd *head_fd; - t_fd *c; + static t_fd *head_fd; + t_fd *c; - if (fd < 0 || !line) - return (-1); - *line = ft_strnew(0); - c = find_fd(fd, &head_fd); - c->readed = 0; - if (c->n) { - if ((c->t = strchr(c->n, '\n')) && - (*line = ft_strjoin(*line, ft_strsub(c->n, 0, (c->t - c->n)))) && - (c->n = ++(c->t))) - { - return (1); - } - else if ((*line = ft_strjoin(*line, c->n))) - { - c->n = 0; - } - } - while ((c->readed = read(c->fd, c->b, BUFF_SIZE))) - { - c->b[c->readed] = 0; - if ((c->n = strchr(c->b, '\n')) && - (*line = ft_strjoin(*line, ft_strsub(c->b, 0, (c->n++ - c->b))))) - break; - *line = ft_strjoin(*line, c->b); - } - if (!c->readed && !ft_strlen(*line)) - return (0); - return (1); -} \ No newline at end of file + if (fd < 0 || !line) + return (-1); + *line = ft_strnew(0); + c = find_fd(fd, &head_fd); + c->readed = 0; + if (check_reminder(c, line)) + return (1); + if (check_buf(c, line) == -1) + return (-1); + if ((c->readed == 0) && !ft_strlen(*line)) + return (0); + return (1); +} diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 1aa6147..de708ac 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 12:21:43 by gtertysh #+# #+# */ -/* Updated: 2016/12/27 18:02:00 by gtertysh ### ########.fr */ +/* Updated: 2017/03/24 20:05:11 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,15 +19,15 @@ # define BUFF_SIZE 640000 -typedef struct s_fd +typedef struct s_fd { - int fd; - char b[BUFF_SIZE + 1]; - char *n; - struct s_fd *next; - char *t; - int readed; -} t_fd; + int fd; + char b[BUFF_SIZE + 1]; + char *n; + struct s_fd *next; + char *t; + int readed; +} t_fd; typedef struct s_list {