Files
42_INT_02_gnl/get_next_line.c
2019-12-16 14:22:26 +01:00

155 lines
3.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/11 00:27:09 by hulamy #+# #+# */
/* Updated: 2019/12/16 14:21:00 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
/*
** #include <stdio.h> //for printf
** #include <fcntl.h> //for open
**
** int main(int ac, char **av)
** {
** int *fd;
** int i;
** int j;
** 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 (get_next_line(fd[i], &line) > 0)
** {
** printf("fd%i| %s\n", fd[i], line);
** free(line);
** j = 0;
** }
** else
** {
** printf("fd%i|*FINI*\n", fd[i]);
** j++;
** }
** i++;
** if (i >= ac - 1)
** i = 0;
** }
** free (fd);
** // while (1);
** return (0);
** }
*/
int multi_fd(int fd, t_gnlist **lst)
{
t_gnlist *tmp;
tmp = *lst;
while (*lst && (*lst)->lfd != fd && (*lst)->next != tmp)
*lst = (*lst)->next;
if (!tmp || ((*lst)->next == tmp && (*lst)->lfd != fd))
{
if (!(tmp = (t_gnlist*)malloc(sizeof(*tmp))))
return (0);
tmp->lfd = fd;
if (!(tmp->string = ft_strdup("")))
return (0);
if (*lst)
{
tmp->next = (*lst)->next;
(*lst)->next = tmp;
}
else
tmp->next = tmp;
*lst = tmp;
}
return (1);
}
char *ft_strjoinfree(char const *s1, char const *s2)
{
char *str;
int len;
int j;
if (!s1 || !s2)
return (NULL);
len = ft_strlen(s1) + ft_strlen(s2);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
len = 0;
j = 0;
while (s1[j] != '\0')
str[len++] = s1[j++];
j = 0;
while (s2[j] != '\0')
str[len++] = s2[j++];
str[len] = '\0';
free((char*)s1);
return (str);
}
int hasnewline(char *str, t_gnlist *lst, char **line)
{
str[0] = '\0';
if (!(*line = ft_strdup(lst->string)))
return (-1);
ft_memmove(lst->string, str + 1, ft_strlen(str + 1) + 1);
return (1);
}
void free_lst(t_gnlist **lst, const int fd)
{
t_gnlist *tmp;
tmp = *lst;
while ((*lst)->next && (*lst)->next->lfd != fd)
(*lst) = (*lst)->next;
free(tmp->string);
free(tmp);
(*lst)->next = (*lst)->next->next;
}
int get_next_line(const int fd, char **line)
{
char buf[BUFFER_SIZE + 1];
int ret;
static t_gnlist *lst = NULL;
char *str;
if (!(multi_fd(fd, &lst)) || !(ret = 1) || !line || fd < 0)
return (-1);
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';
free_lst(&lst, fd);
return (ret);
}