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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,4 +23,4 @@ int ft_lst_len(t_list *lst)
len++; len++;
} }
return (len); return (len);
} }

View file

@ -5,72 +5,109 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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;
new = NULL; new = NULL;
if ((new = (t_fd *)malloc(sizeof(t_fd)))) if ((new = (t_fd *)malloc(sizeof(t_fd))))
{ {
new->fd = fd; new->fd = fd;
new->n = NULL; new->n = NULL;
new->next = NULL; new->next = NULL;
new->t = 0; new->t = 0;
} }
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)));
while ((*cur_fd)) while ((*cur_fd))
{ {
if ((*cur_fd)->fd == fd) if ((*cur_fd)->fd == fd)
return (*cur_fd); return (*cur_fd);
cur_fd = &((*cur_fd)->next); cur_fd = &((*cur_fd)->next);
} }
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;
t_fd *c; t_fd *c;
if (fd < 0 || !line) if (fd < 0 || !line)
return (-1); return (-1);
*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')) && return (1);
(*line = ft_strjoin(*line, ft_strsub(c->n, 0, (c->t - c->n)))) && if (check_buf(c, line) == -1)
(c->n = ++(c->t))) return (-1);
{ if ((c->readed == 0) && !ft_strlen(*line))
return (1); return (0);
} 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);
}

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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,15 +19,15 @@
# define BUFF_SIZE 640000 # define BUFF_SIZE 640000
typedef struct s_fd typedef struct s_fd
{ {
int fd; int fd;
char b[BUFF_SIZE + 1]; char b[BUFF_SIZE + 1];
char *n; char *n;
struct s_fd *next; struct s_fd *next;
char *t; char *t;
int readed; int readed;
} t_fd; } t_fd;
typedef struct s_list typedef struct s_list
{ {