This commit is contained in:
Jack Halford 2016-11-17 17:05:13 +01:00
parent ed8b34bc5d
commit c554b5d916
3 changed files with 64 additions and 38 deletions

View file

@ -1,19 +1,30 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* getnextline.h :+: :+: :+: */ /* get_next_line.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:21:59 by jhalford #+# #+# */ /* Created: 2016/11/05 12:21:36 by jhalford #+# #+# */
/* Updated: 2016/11/07 13:22:13 by jhalford ### ########.fr */ /* Updated: 2016/11/17 13:18:28 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef GET_NEXT_LINE_H #ifndef GET_NEXT_LINE_H
# define GET_NEXT_LINE_H # define GET_NEXT_LINE_H
# define BUFF_SIZE 10 # define BUFF_SIZE 32
# include "libft.h"
# include "libft/libft.h"
# include <sys/types.h>
# include <sys/uio.h>
typedef struct s_save t_save;
struct s_save
{
int fd;
char *str;
};
int get_next_line(int const fd, char **line); int get_next_line(int const fd, char **line);

View file

@ -3,63 +3,78 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */ /* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 12:45:02 by jhalford #+# #+# */ /* Created: 2016/11/15 13:12:06 by jhalford #+# #+# */
/* Updated: 2016/11/04 13:43:29 by jhalford ### ########.fr */ /* Updated: 2016/11/17 13:12:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "get_next_line.h"
#define BUFF_SIZE 32 #include <stdio.h>
static char *ft_realloc(char *line, int size) static int ft_fdcmp(t_save *a, int *b)
{ {
char *str; return (a->fd - *b);
str = (char *)malloc(sizeof(char) * (ft_strlen(line) + size + 1));
if (str == NULL)
return (NULL);
str = ft_strcpy(str, line);
free(line);
return (str);
} }
static int ft_loop_read(int fd, char **line, char (*save)[]) static t_list *ft_newfd(t_list **head, int fd)
{
t_save new;
new.fd = fd;
new.str = ft_memalloc((BUFF_SIZE > 0 ? BUFF_SIZE : 0) + 1);
ft_lstadd(head, ft_lstnew(&new, sizeof(t_save)));
return (*head);
}
static int ft_loop_read(int fd, char **line, char *save)
{ {
char buf[BUFF_SIZE + 1]; char buf[BUFF_SIZE + 1];
char *pos; char *pos;
char *tmp;
int ret; int ret;
while ((ret = read(fd, buf, BUFF_SIZE)) > 0) while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
{ {
buf[ret] = '\0'; buf[ret] = 0;
tmp = *line;
if ((pos = ft_strchr(buf, '\n'))) if ((pos = ft_strchr(buf, '\n')))
{ {
ft_strcpy(*save, pos + 1); ft_strcpy(save, pos + 1);
*pos = '\0'; *pos = 0;
ft_strcat(*line, buf); }
if (!(*line = ft_strjoin(*line, buf)))
return (-1);
ft_strdel(&tmp);
if (pos)
return (1); return (1);
} }
if ((*line = ft_realloc(*line, ret)) == NULL) if (ret < 0)
return (-1); return (-1);
ft_strcat(*line, buf); return (**line ? 1 : 0);
}
return (0);
} }
int get_next_line(int const fd, char **line) int get_next_line(int const fd, char **line)
{ {
static char save[BUFF_SIZE] = ""; static t_list *head;
t_list *tmp;
char *pos; char *pos;
char *save;
*line = (char *)malloc(sizeof(char *) * (BUFF_SIZE + ft_strlen(save) + 1)); if (fd < 0 || !line)
*line = ft_strcpy(*line, save); return (-1);
if (!(tmp = ft_lst_find(head, (void *)&fd, &ft_fdcmp)))
tmp = ft_newfd(&head, fd);
save = ((t_save*)tmp->content)->str;
if (!(*line = ft_strdup(save)))
return (-1);
ft_bzero(save, BUFF_SIZE + 1);
if ((pos = ft_strchr(*line, '\n'))) if ((pos = ft_strchr(*line, '\n')))
{ {
ft_strcpy(save, pos + 1); ft_strcpy(save, pos + 1);
*pos = '\0'; *pos = 0;
return (1); return (1);
} }
return (ft_loop_read(fd, line, &save)); return (ft_loop_read(fd, line, save));
} }