From a4c9a91209a92aa251bb3d5f2ff7bea4ba09d99f Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Mon, 24 Nov 2025 18:21:13 +0100 Subject: [PATCH] bonus started --- get_next_line_bonus.c | 49 +++++++++++++++++++++++ get_next_line_bonus.h | 29 ++++++++++++++ get_next_line_utils_bonus.c | 77 +++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 get_next_line_bonus.c create mode 100644 get_next_line_bonus.h create mode 100644 get_next_line_utils_bonus.c diff --git a/get_next_line_bonus.c b/get_next_line_bonus.c new file mode 100644 index 0000000..c26e795 --- /dev/null +++ b/get_next_line_bonus.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/24 15:55:25 by dgaillet #+# #+# */ +/* Updated: 2025/11/24 17:22:10 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "get_next_line_bonus.h" + +static char *get_lines(int fd, char *str) +{ + char buf[BUFFER_SIZE]; + int res; + + res = read(fd, buf, BUFFER_SIZE); + while (res > 0) + { + if (!str) + str = ft_strdup(buf); + else + str = ft_strjoin_new(str, buf, res); + if (!str) + return (NULL); + ft_bzero(buf, BUFFER_SIZE); + res = read(fd, buf, BUFFER_SIZE); + } + if (res < 0) + { + if (str) + free(str); + return (NULL); + } + return (str); +} + +char *get_next_line(int fd) +{ + static char *strs[1024]; + + if (!strs[fd]) + strs[fd] = get_lines(fd, strs[fd]); + +} diff --git a/get_next_line_bonus.h b/get_next_line_bonus.h new file mode 100644 index 0000000..adf67bf --- /dev/null +++ b/get_next_line_bonus.h @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/24 15:56:45 by dgaillet #+# #+# */ +/* Updated: 2025/11/24 17:22:18 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_BONUS_H +# define GET_NEXT_LINE_BONUS_H + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif + +# include + +char *get_next_line(int fd); + +size_t ft_strlen(const char *s); +char *ft_strjoin_new(char const *s1, char const *s2, size_t limit); +char *ft_strdup(const char *s); +void ft_bzero(void *s, size_t n); + +#endif diff --git a/get_next_line_utils_bonus.c b/get_next_line_utils_bonus.c new file mode 100644 index 0000000..c55af9b --- /dev/null +++ b/get_next_line_utils_bonus.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/24 16:04:39 by dgaillet #+# #+# */ +/* Updated: 2025/11/24 16:12:37 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i]) + i++; + return (i); +} + +char *ft_strjoin_new(char const *s1, char const *s2, size_t limit) +{ + char *str; + size_t i; + size_t j; + + str = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1)); + if (!str) + return (NULL); + i = 0; + j = 0; + while (s1[i]) + { + str[i] = s1[i]; + i++; + } + while (j <= limit && s2[j]) + { + str[i + j] = s2[j]; + j++; + } + str[i + j] = '\0'; + free((void *) s1); + return (str); +} + +char *ft_strdup(const char *s) +{ + char *dest; + int i; + + dest = malloc(sizeof(char) *(ft_strlen(s) + 1)); + if (!dest) + return (NULL); + i = 0; + while (s[i]) + { + dest[i] = s[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} + +void ft_bzero(void *s, size_t n) +{ + while (n > 0) + { + *((unsigned char *) s) = '\0'; + s++; + n--; + } +}