diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-19 22:27:11 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-19 22:27:11 +0200 |
commit | 62afe606a355581c5b48cca361478c43fb6ae4cf (patch) | |
tree | a278fe2fdf44c296892616554743b9d380ff7afa /libft/src/get_next_line.c | |
parent | Added joe-sh_history to gitignore (diff) | |
download | 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.gz 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.bz2 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.xz 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.tar.zst 42-minishell-62afe606a355581c5b48cca361478c43fb6ae4cf.zip |
Well well well that wasn't too bad, now remake everything
Diffstat (limited to 'libft/src/get_next_line.c')
-rw-r--r-- | libft/src/get_next_line.c | 154 |
1 files changed, 71 insertions, 83 deletions
diff --git a/libft/src/get_next_line.c b/libft/src/get_next_line.c index 2b2f084..ae64125 100644 --- a/libft/src/get_next_line.c +++ b/libft/src/get_next_line.c @@ -1,112 +1,100 @@ /* ************************************************************************** */ -/* LE - / */ -/* / */ -/* get_next_line.c .:: .:/ . .:: */ -/* +:+:+ +: +: +:+:+ */ -/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ -/* #+# #+ #+ #+# */ -/* Created: 2019/10/29 00:37:39 by rbousset #+# ## ## #+# */ -/* Updated: 2019/10/29 00:37:41 by rbousset ### #+. /#+ ###.fr */ -/* / */ -/* / */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:07:20 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:07:20 by rbousset ### ########lyon.fr */ +/* */ /* ************************************************************************** */ #include <libft.h> #include <stdlib.h> +#include <stddef.h> +#include <stdint.h> #include <unistd.h> -#include <stdio.h> -static uint8_t - ft_linecheck(const char *str) +static char + *ft_strjoin_gnl(char *s1, char *s2) { size_t i; + size_t j; + char *dst; + size_t size1; + size_t size2; - i = 0; - while (str && str[i]) - { - if (str[i] == '\n') - return (1); - i++; - } - return (0); -} - -static size_t - ft_linelen(const char *str) -{ - size_t i; - - i = 0; - while (str[i] != '\n' && str[i] != '\0') - i++; - return (i); + i = -1; + j = -1; + size1 = ft_strlen_gnl(s1, 10); + size2 = ft_strlen_gnl(s2, 10); + if (!(dst = (char*)malloc((size1 + size2 + 1) * sizeof(char)))) + return (NULL); + while (++i < size1) + dst[i] = s1[i]; + while (++j < size2) + dst[i + j] = s2[j]; + dst[i + j] = '\0'; + ft_memdel((void**)&s1); + return (dst); } static int - ft_linedup(int fd, char **str) + ft_read_gnl(int fd, char **line, t_gnl *curr) { - char *buff; - char *tmp; - int ret; + char *buf; + char *end; + int64_t size; - if (!(buff = (char *)malloc((BUFFER_SIZE + 1) * sizeof(char)))) - return (0); - ret = 1; - while (!ft_linecheck(*str) && (ret = read(fd, buff, BUFFER_SIZE)) > 0) - { - buff[ret] = '\0'; - tmp = *str; - *str = ft_strjoin(tmp, buff); - free(tmp); - } - if (ret < 0) - { - free(*str); - free(buff); + if (!(buf = malloc(BUFFER_SIZE + 1))) return (-1); - } - free(buff); - return (ret); -} - -static int - ft_errchck(int fd, char **line, char **str) -{ - if (*str == NULL) - *str = ft_nstr(0); - if (fd < 0 || !line || BUFFER_SIZE <= 0) + *buf = 0; + *line = ft_strjoin_gnl(*line, curr->rest); + size = BUFFER_SIZE; + while ((end = ft_strchr_gnl(buf, 10)) == NULL && size == BUFFER_SIZE) { - free(*str); - return (-1); + *line = ft_strjoin_gnl(*line, buf); + if ((size = read(fd, buf, BUFFER_SIZE)) == -1) + ft_memdel((void**)&buf); + if (size == -1) + return (-1); + buf[size] = 0; } - return (0); + *line = ft_strjoin_gnl(*line, buf); + if (end) + curr->rest = ft_swap_gnl(end + 1, curr->rest); + ft_memdel((void**)&buf); + if (!end) + return (0); + return (size); } int get_next_line(int fd, char **line) { - static char *str[1024]; - char *tmp; - size_t i; - int ret; + static t_gnl *list; + t_gnl *curr; + char *end; + int size; - if (ft_errchck(fd, line, &str[fd]) == -1) + if (fd < 0 || BUFFER_SIZE <= 0) return (-1); - ret = ft_linedup(fd, &str[fd]); - if (ret < 0) + if (!(*line = (char *)malloc(1 * sizeof(char)))) return (-1); - i = ft_linelen(str[fd]); - *line = ft_substr(str[fd], 0, i); - tmp = str[fd]; - if (tmp[0] != '\0' && tmp) - str[fd] = ft_strdup(tmp + i + (tmp[i] == '\n')); - if (ret == 0 && ((str[fd] == NULL || str[fd][0] == '\0') || !ft_linecheck(str[fd]))) + **line = 0; + curr = ft_find_fd(fd, &list); + if (ft_strchr_gnl(curr->rest, 10) == NULL) + size = ft_read_gnl(fd, line, curr); + else { - i = tmp[i]; - free(tmp); - tmp = NULL; - return (i == '\n'); + end = ft_strchr_gnl(curr->rest, 10); + *line = ft_strjoin_gnl(*line, curr->rest); + curr->rest = ft_swap_gnl(end + 1, curr->rest); + size = 1; } - free(tmp); - return (1); + if (size > 0) + return (1); + ft_free_gnl(fd, &list); + return ((size == -1) ? -1 : 0); } |