From 2b7dad9c154b8bb0e0a5a819d732717351a2771d Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Fri, 21 Nov 2025 19:07:39 +0100 Subject: [PATCH] GNL with static buf worked. Next step, find a way to catch multiple nl when they're in the same buf --- get_next_line.c | 86 ++++++++++++++++++++++++++++++++++++++++++- get_next_line.h | 13 ++++++- get_next_line_utils.c | 79 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 174 insertions(+), 4 deletions(-) diff --git a/get_next_line.c b/get_next_line.c index 5274e05..4f8bed1 100644 --- a/get_next_line.c +++ b/get_next_line.c @@ -6,11 +6,93 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/11/21 17:20:41 by dgaillet #+# #+# */ -/* Updated: 2025/11/21 17:21:22 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/11/21 19:06:31 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ +#include "get_next_line.h" +#include + +static char *ft_read_line(int fd, char *dest, char buf[BUFFER_SIZE]) +{ + int readed; + int i; + + readed = 1; + while (readed) + { + readed = read(fd, buf, BUFFER_SIZE); + if (readed < 0) + return (NULL); + i = index_of_nl(buf, BUFFER_SIZE); + if (i < 0) + dest = ft_strjoin_new(dest, buf, readed); + else + dest = ft_strjoin_new(dest, buf, i); + if (!dest) + return (NULL); + if (i >= 0) + break ; + } + return (dest); +} + +static char *create_new_line(char buf[BUFFER_SIZE], char *base_str) +{ + char *new_str; + int buf_len; + int nl_i; + + if (!buf) + return (NULL); + buf_len = ft_strlen(buf); + nl_i = index_of_nl(buf, buf_len); + if (nl_i >= 0) + { + new_str = ft_substr(buf, nl_i, buf_len - nl_i); + free(base_str); + return (new_str); + } + return (NULL); +} + char *get_next_line(int fd) { - return (0); + static char buf[BUFFER_SIZE]; + char *nl; + char *temp; + + nl = malloc(sizeof(char)); + if (!nl) + return (NULL); + temp = create_new_line(buf, nl); + if (temp) + nl = ft_read_line(fd, temp, buf); + else + { + nl[0] = '\0'; + nl = ft_read_line(fd, nl, buf); + } + return (nl); +} + +#include +#include + +int main(int argc, char **argv) +{ + char *str; + int fd; + + if (argc > 1) + { + fd = open(argv[1], O_RDONLY); + str = get_next_line(fd); + printf("%s", str); + free(str); + str = get_next_line(fd); + printf("%s", str); + free(str); + close(fd); + } } diff --git a/get_next_line.h b/get_next_line.h index 8eefb02..28858b9 100644 --- a/get_next_line.h +++ b/get_next_line.h @@ -6,13 +6,24 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/11/21 17:22:03 by dgaillet #+# #+# */ -/* Updated: 2025/11/21 17:22:38 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/11/21 18:57:22 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #ifndef GET_NEXT_LINE_H # define GET_NEXT_LINE_H +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif + +# include + char *get_next_line(int fd); +int index_of_nl(char *str, int limit); +char *ft_strjoin_new(char const *s1, char const *s2, size_t limit); +size_t ft_strlen(const char *s); +char *ft_substr(char const *s, unsigned int start, size_t len); + #endif diff --git a/get_next_line_utils.c b/get_next_line_utils.c index 6667ad4..46fa581 100644 --- a/get_next_line_utils.c +++ b/get_next_line_utils.c @@ -6,7 +6,84 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/11/21 17:21:38 by dgaillet #+# #+# */ -/* Updated: 2025/11/21 17:21:49 by dgaillet ### ########lyon.fr */ +/* Updated: 2025/11/21 18:57:06 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ +#include "get_next_line.h" + +int index_of_nl(char *str, int limit) +{ + int i; + + i = 0; + while (i < limit) + { + if (str[i] == '\n') + return (i); + i++; + } + return (-1); +} + +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) + { + str[i + j] = s2[j]; + j++; + } + str[i + j] = '\0'; + free((void *) s1); + return (str); +} + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + size_t i; + char *sub_str; + size_t s_len; + + if (!s) + return (NULL); + s_len = ft_strlen(s); + if (start > s_len) + len = 0; + else if (s_len < (start + len)) + len = s_len - start; + sub_str = malloc(sizeof(char) * (len + 1)); + if (!sub_str) + return (NULL); + i = 0; + while (i < len) + { + sub_str[i] = s[start + i]; + i++; + } + sub_str[i] = '\0'; + return (sub_str); +}