let's test this

added ft_open, ft_close and ft_gets functions not tested
modified ft_getline with read 1 by 1
This commit is contained in:
Tanguy MAZE 2019-03-08 14:14:56 +01:00
parent 13d96a14d8
commit d49f964045
5 changed files with 89 additions and 20 deletions

22
ft_close.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_close.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/08 13:39:05 by tmaze #+# #+# */
/* Updated: 2019/03/08 13:44:25 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_close(t_file *file)
{
int filedes;
filedes = file->fd;
ft_memdel(file);
return (close(filedes) == 0);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 15:12:59 by tmaze #+# #+# */
/* Updated: 2019/03/07 22:06:57 by tmaze ### ########.fr */
/* Updated: 2019/03/08 14:11:15 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,17 +19,18 @@ static char *supercat(char **s1, char *buff)
if ((tmp = ft_strjoin(*s1, buff)) == NULL)
return (NULL);
ft_strdel(s1);
ft_strclr(buff);
ft_bzero(buff, BUFF_SIZE + 1);
*s1 = tmp;
return (*s1);
}
static int update_line(char **line, char *buff)
static int flush_buff(char **line, char *buff)
{
if (*line == NULL)
{
if ((*line = ft_strdup(buff)) == NULL)
return (-1);
ft_bzero(buff, BUFF_SIZE + 1);
}
else if (*line != NULL)
if (supercat(line, buff) == NULL)
@ -42,29 +43,23 @@ static int update_line(char **line, char *buff)
int ft_getline(char **line)
{
static char buff[BUFF_SIZE + 1];
char *ind;
char buff[BUFF_SIZE + 1];
int ret;
int i;
if (line == NULL)
return (-1);
*line = NULL;
ind = NULL;
ft_bzero(buff, BUFF_SIZE + 1);
while ((ret = read(0, buff, BUFF_SIZE)) == BUFF_SIZE
&& (ind = ft_strchr(buff, '\n')) == NULL)
i = 0;
while ((ret = read(0, &(buff[i]), 1)) == 1 && buff[i] != '\n')
{
if (update_line(line, buff) == -1)
if (++i > BUFF_SIZE && flush_buff(line, buff) != 0)
return (-1);
ft_bzero(&buff, BUFF_SIZE + 1);
if (i > BUFF_SIZE)
i = 0;
}
if (ind != NULL || (ind == NULL && (ind = ft_strchr(buff, '\n')) != NULL))
*ind = '\0';
if (update_line(line, buff) == -1)
return (-1);
if (ind == NULL)
ft_bzero(&buff, BUFF_SIZE + 1);
if (ind != NULL && ind < &(buff[BUFF_SIZE]))
ft_memmove(buff, ind + 1, ft_strlen(ind + 1));
return (*line != NULL && ret != 0);
if (ret == 0 && i != 0 && flush_buff(line, buff) != 0)
return (-1);
return (line != NULL);
}

14
ft_gets.c Normal file
View File

@ -0,0 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_gets.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/08 13:44:31 by tmaze #+# #+# */
/* Updated: 2019/03/08 14:11:38 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"

28
ft_open.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_open.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/08 13:27:26 by tmaze #+# #+# */
/* Updated: 2019/03/08 13:38:29 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_file *ft_open(char *filename, oint oflag)
{
t_file *ret;
if ((ret = ft_memalloc(sizeof(t_file))) == NULL)
return (NULL);
if ((ret->fd = open(filename, oflag)) == -1)
{
ft_memdel(&ret);
return (NULL);
}
ft_bzero(ret->buff, BUFF_SIZE + 1);
return (ret);
}

12
libft.h
View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/08 00:12:36 by tmaze #+# #+# */
/* Updated: 2019/03/07 15:38:26 by tmaze ### ########.fr */
/* Updated: 2019/03/08 13:27:16 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -62,6 +62,16 @@ typedef struct s_list
# define BUFF_SIZE 30
/*
** definition type t_file
*/
typedef struct s_file
{
int fd;
char buff[BUFF_SIZE + 1];
} t_file;
enum e_size
{
h,