libft check

This commit is contained in:
Gregory Tertyshny 2017-03-24 20:07:40 +02:00
parent bd7baf18bd
commit 961ea09e1d
3 changed files with 103 additions and 66 deletions

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */ /* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 19:13:25 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -5,14 +5,14 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */ /* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 15:59:21 by gtertysh #+# #+# */ /* Created: 2017/01/28 18:15:15 by gtertysh #+# #+# */
/* Updated: 2016/12/06 19:45:44 by gtertysh ### ########.fr */ /* 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;
@ -27,7 +27,7 @@ static t_fd *new_node(int fd)
return (new); 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) if (!*cur_fd)
return ((*cur_fd = new_node(fd))); return ((*cur_fd = new_node(fd)));
@ -40,6 +40,59 @@ static t_fd *find_fd(int fd, t_fd **cur_fd)
return ((*cur_fd = new_node(fd))); 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) int get_next_line(const int fd, char **line)
{ {
static t_fd *head_fd; static t_fd *head_fd;
@ -50,27 +103,11 @@ int get_next_line(const int fd, char **line)
*line = ft_strnew(0); *line = ft_strnew(0);
c = find_fd(fd, &head_fd); c = find_fd(fd, &head_fd);
c->readed = 0; c->readed = 0;
if (c->n) { if (check_reminder(c, line))
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); return (1);
} if (check_buf(c, line) == -1)
else if ((*line = ft_strjoin(*line, c->n))) return (-1);
{ if ((c->readed == 0) && !ft_strlen(*line))
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 (0);
return (1); return (1);
} }

View file

@ -6,7 +6,7 @@
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */ /* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 12:21:43 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */