reecriture de gnl pour cette histoire de eof
This commit is contained in:
153
get_next_line.c
153
get_next_line.c
@@ -12,58 +12,58 @@
|
||||
|
||||
#include "get_next_line.h"
|
||||
|
||||
/*
|
||||
** //-g -fsanitize=address
|
||||
** //:set noendofline binary
|
||||
** //https://github.com/charMstr/GNL_lover
|
||||
** //https://github.com/mrjvs/42cursus_gnl_tests
|
||||
** //https://github.com/Sherchryst/gnlkiller
|
||||
** //https://github.com/Hellio404/Get_Next_Line_Tester
|
||||
** //https://github.com/TinfoilPancakes/get-next-line-testing-tools
|
||||
**
|
||||
** #include <stdio.h> //for printf
|
||||
** #include <fcntl.h> //for open
|
||||
**
|
||||
** int main(int ac, char **av)
|
||||
** {
|
||||
** int *fd;
|
||||
** int i;
|
||||
** int j;
|
||||
** int ret;
|
||||
** char *line;
|
||||
**
|
||||
** if (ac < 2)
|
||||
** return (0);
|
||||
** line = NULL;
|
||||
** i = 0;
|
||||
** if (!(fd = (int *)malloc(sizeof(int) * ac)))
|
||||
** return (0);
|
||||
** while (++i <= ac - 1)
|
||||
** fd[i - 1] = open(av[i], O_RDONLY);
|
||||
** i = 0;
|
||||
** j = 0;
|
||||
** while (j < ac - 1)
|
||||
** {
|
||||
** if ((ret = get_next_line(fd[i], &line)) > 0)
|
||||
** {
|
||||
** printf("[fd%i-%i] %s\n", fd[i], ret, line);
|
||||
** free(line);
|
||||
** j = 0;
|
||||
** }
|
||||
** else
|
||||
** {
|
||||
** printf("[fd%i-%i]*FINI*\n", fd[i], ret);
|
||||
** j++;
|
||||
** }
|
||||
** i++;
|
||||
** if (i >= ac - 1)
|
||||
** i = 0;
|
||||
** }
|
||||
** free (fd);
|
||||
** //while (1);
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
//-g -fsanitize=address
|
||||
//:set noendofline binary
|
||||
//https://github.com/charMstr/GNL_lover
|
||||
//https://github.com/mrjvs/42cursus_gnl_tests
|
||||
//https://github.com/Sherchryst/gnlkiller
|
||||
//https://github.com/Hellio404/Get_Next_Line_Tester
|
||||
//https://github.com/TinfoilPancakes/get-next-line-testing-tools
|
||||
|
||||
#include <stdio.h> //for printf
|
||||
#include <fcntl.h> //for open
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int *fd;
|
||||
int i;
|
||||
int j;
|
||||
int ret;
|
||||
char *line;
|
||||
|
||||
if (ac < 2)
|
||||
return (0);
|
||||
line = NULL;
|
||||
i = 0;
|
||||
if (!(fd = (int *)malloc(sizeof(int) * ac)))
|
||||
return (0);
|
||||
while (++i <= ac - 1)
|
||||
fd[i - 1] = open(av[i], O_RDONLY);
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (j < ac - 1)
|
||||
{
|
||||
if ((ret = get_next_line(fd[i], &line)) > 0)
|
||||
{
|
||||
printf("[fd%i-%i][%s]\n", fd[i], ret, line);
|
||||
free(line);
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("[fd%i-%i]*FINI*\n", fd[i], ret);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
if (i >= ac - 1)
|
||||
i = 0;
|
||||
}
|
||||
free (fd);
|
||||
//while (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
int multi_fd(int fd, t_gnlist **lst)
|
||||
{
|
||||
@@ -135,6 +135,17 @@ void free_lst(t_gnlist **lst, const int fd)
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
char *ft_strdup_limit(const char *src, int limit)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (!(str = (char*)malloc(sizeof(*str) * (limit + 1))))
|
||||
return (NULL);
|
||||
while (limit-- >= 0)
|
||||
str[limit + 1] = src[limit + 1];
|
||||
return (str);
|
||||
}
|
||||
|
||||
int get_next_line(const int fd, char **line)
|
||||
{
|
||||
char buf[BUFFER_SIZE + 1];
|
||||
@@ -142,24 +153,44 @@ int get_next_line(const int fd, char **line)
|
||||
static t_gnlist *lst = NULL;
|
||||
char *str;
|
||||
|
||||
// ret = read(fd, buf, BUFFER_SIZE);
|
||||
// buf[ret] = '\0';
|
||||
// *line = ft_strdup_limit(buf, BUFFER_SIZE);
|
||||
// return (ret);
|
||||
|
||||
|
||||
ret = 1;
|
||||
if (!(multi_fd(fd, &lst)) || !line || fd < 0 || BUFFER_SIZE < 1)
|
||||
return (-1);
|
||||
while (ret > 0)
|
||||
while (!(str = ft_strchr(lst->string, '\n')) && ret != 0)
|
||||
{
|
||||
if ((str = ft_strchr(lst->string, '\n')))
|
||||
return (hasnewline(str, lst, line));
|
||||
if ((ret = read(fd, buf, BUFFER_SIZE)) < 0)
|
||||
return (-1);
|
||||
buf[ret] = '\0';
|
||||
if (!(lst->string = ft_strjoinfree(lst->string, buf)))
|
||||
return (-1);
|
||||
}
|
||||
if (*(lst->string) && !(*line = ft_strdup(lst->string)))
|
||||
str[0] = '\0';
|
||||
if (!(*line = ft_strdup(lst->string)))
|
||||
return (-1);
|
||||
if (*(lst->string) && ++ret == 1)
|
||||
(lst->string)[0] = '\0';
|
||||
else
|
||||
free_lst(&lst, fd);
|
||||
return (ret);
|
||||
ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1);
|
||||
return (ret > BUFFER_SIZE);
|
||||
|
||||
// while (ret > 0)
|
||||
// {
|
||||
// if ((str = ft_strchr(lst->string, '\n')))
|
||||
// return (hasnewline(str, lst, line));
|
||||
// if ((ret = read(fd, buf, BUFFER_SIZE)) < 0)
|
||||
// return (-1);
|
||||
// buf[ret] = '\0';
|
||||
// if (!(lst->string = ft_strjoinfree(lst->string, buf)))
|
||||
// return (-1);
|
||||
// }
|
||||
// if (*(lst->string) && !(*line = ft_strdup(lst->string)))
|
||||
// return (-1);
|
||||
// if (*(lst->string) && ++ret == 1)
|
||||
// (lst->string)[0] = '\0';
|
||||
// else
|
||||
// free_lst(&lst, fd);
|
||||
// return (ret);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user