diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-01 00:39:52 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-03-01 00:39:52 +0100 |
commit | 640b02490953858c3d872c8c1a8d517ffd79ab84 (patch) | |
tree | bc8d06703556a5e2f53996d8bd654b74e20d27cd /libft | |
parent | No leak (diff) | |
download | 42-cub3d-640b02490953858c3d872c8c1a8d517ffd79ab84.tar.gz 42-cub3d-640b02490953858c3d872c8c1a8d517ffd79ab84.tar.bz2 42-cub3d-640b02490953858c3d872c8c1a8d517ffd79ab84.tar.xz 42-cub3d-640b02490953858c3d872c8c1a8d517ffd79ab84.tar.zst 42-cub3d-640b02490953858c3d872c8c1a8d517ffd79ab84.zip |
gnl
Diffstat (limited to 'libft')
-rw-r--r-- | libft/Makefile | 1 | ||||
-rw-r--r-- | libft/inc/libft.h | 20 | ||||
-rw-r--r-- | libft/src/get_next_line.c | 135 | ||||
-rw-r--r-- | libft/src/get_next_line_utils.c | 118 |
4 files changed, 200 insertions, 74 deletions
diff --git a/libft/Makefile b/libft/Makefile index d9ae259..35c2b88 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -83,6 +83,7 @@ SRCS_NAME += ft_nstr.c SRCS_NAME += ft_memdel.c SRCS_NAME += ft_kernel_panic.c SRCS_NAME += get_next_line.c +SRCS_NAME += get_next_line_utils.c SRCS_NAME += ft_printf.c SRCS_NAME += ft_dprintf.c SRCS_NAME += ft_sprintf.c diff --git a/libft/inc/libft.h b/libft/inc/libft.h index ccc1c65..e3dae43 100644 --- a/libft/inc/libft.h +++ b/libft/inc/libft.h @@ -30,6 +30,14 @@ typedef struct s_list struct s_list *next; } t_list; + +typedef struct s_gnl +{ + int fd; + char *rest; + struct s_gnl *next; +} t_gnl; + typedef struct s_printflist { int putlen; @@ -128,7 +136,6 @@ int ft_putendl(const char *s); int ft_putchar_fd(char c, int fd); int ft_putstr_fd(char *s, int fd); int ft_strcmp(const char *s1, const char *s2); -int get_next_line(int fd, char **line); long ft_memlchr(const void *s, int c, size_t n); long ft_strlchr(const char *s, int c); size_t ft_strlen(const char *s); @@ -146,6 +153,17 @@ t_list *ft_lstlast(t_list *lst); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); /* +** get_next_line +*/ + +char *ft_strchr_gnl(const char *s, int c); +char *ft_swap_gnl(char *s1, char *free_ft); +int get_next_line(int fd, char **line); +int ft_free_gnl(int fd, t_gnl **list); +size_t ft_strlen_gnl(const char *s, char c); +t_gnl *ft_find_fd(int fd, t_gnl **list); + +/* ** ft_printf */ diff --git a/libft/src/get_next_line.c b/libft/src/get_next_line.c index 4ace77a..ae64125 100644 --- a/libft/src/get_next_line.c +++ b/libft/src/get_next_line.c @@ -12,100 +12,89 @@ #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[i] != '\0') - { - 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); - ft_memdel((void**)&tmp); - } - if (ret < 0) - { - ft_memdel((void**)&str); - ft_memdel((void**)&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) { - ft_memdel((void**)&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; - char *tmp; - size_t i; - int ret; + static t_gnl *list; + t_gnl *curr; + char *end; + int size; - if (ft_errchck(fd, line, &str) == -1) + if (fd < 0 || BUFFER_SIZE <= 0) return (-1); - ret = ft_linedup(fd, &str); - if (ret < 0) + if (!(*line = (char *)malloc(1 * sizeof(char)))) return (-1); - i = ft_linelen(str); - *line = ft_substr(str, 0, i); - tmp = str; - if (tmp[0] != '\0' && tmp) - str = ft_strdup(tmp + i + (tmp[i] == '\n')); - if (ret == 0 && ((str == NULL || str[0] == '\0') || !ft_linecheck(str))) + **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]; - if (i == '\n') - ft_memdel((void**)&tmp); - 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; } - ft_memdel((void**)&tmp); - return (1); + if (size > 0) + return (1); + ft_free_gnl(fd, &list); + return ((size == -1) ? -1 : 0); } diff --git a/libft/src/get_next_line_utils.c b/libft/src/get_next_line_utils.c new file mode 100644 index 0000000..42d5d3a --- /dev/null +++ b/libft/src/get_next_line_utils.c @@ -0,0 +1,118 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.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> + +size_t + ft_strlen_gnl(const char *s, char c) +{ + size_t i; + + i = 0; + if (!s) + return (0); + while (s[i] != c && s[i] != 0) + i++; + return (i); +} + +int + ft_free_gnl(int fd, t_gnl **list) +{ + t_gnl *scout; + t_gnl *prev; + + prev = *list; + if (prev->fd == fd) + { + scout = prev->next; + ft_memdel((void**)&prev->rest); + ft_memdel((void**)&prev); + *list = scout; + return (0); + } + scout = prev->next; + while (scout->fd != fd) + { + prev = prev->next; + scout = scout->next; + } + prev->next = scout->next; + ft_memdel((void**)&scout->rest); + ft_memdel((void**)&scout); + return (0); +} + +t_gnl + *ft_find_fd(int fd, t_gnl **list) +{ + t_gnl *curr; + t_gnl *new; + + if (!(new = malloc(sizeof(*new)))) + return (NULL); + new->fd = fd; + new->rest = 0; + new->next = NULL; + if (!*list) + { + *list = new; + return (*list); + } + curr = *list; + if (fd != curr->fd) + { + while (curr->next != NULL && fd != curr->fd) + curr = curr->next; + if (curr->next == NULL) + return (curr->next = new); + } + ft_memdel((void**)&new); + return (curr); +} + +char + *ft_strchr_gnl(const char *s, int c) +{ + unsigned int i; + + i = 0; + c = (char)c; + if (!s) + return (NULL); + while (s[i] != 0 && s[i] != c) + i++; + if (s[i] != c) + return (NULL); + return ((char *)s + i); +} + +char + *ft_swap_gnl(char *s1, char *free_ft) +{ + char *dst; + int i; + + if (!(dst = malloc(ft_strlen_gnl(s1, 0) + 1))) + return (NULL); + i = 0; + while (s1[i]) + { + dst[i] = s1[i]; + i++; + } + dst[i] = 0; + ft_memdel((void**)&free_ft); + return (dst); +} |