From a826dcd8b387f1de574d3974ddc4f31525472e15 Mon Sep 17 00:00:00 2001 From: lmouta-g Date: Wed, 28 Jan 2026 23:33:17 +0100 Subject: [PATCH 1/3] Add ft_gnl function --- .gitignore | 22 +++++++++- Makefile | 3 +- includes/libft.h | 10 ++++- src/ft_gnl.c | 97 +++++++++++++++++++++++++++++++++++++++++++ src/ft_lstadd_back.c | 10 ++--- src/ft_lstadd_front.c | 10 ++--- 6 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 src/ft_gnl.c diff --git a/.gitignore b/.gitignore index 1dc6660..70a3c7e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,23 @@ --e *.o +# Object files +*.o + +# Static library *.a + +# macOS .DS_Store + +# IDE / Editors +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# Debug +*.dSYM/ + +# Test binaries +a.out +test +main diff --git a/Makefile b/Makefile index 737b7b9..7554713 100755 --- a/Makefile +++ b/Makefile @@ -16,7 +16,8 @@ SRCS = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \ ft_itoa.c ft_strmapi.c ft_striteri.c ft_putchar_fd.c \ ft_putstr_fd.c ft_putendl_fd.c ft_putnbr_fd.c \ ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \ - ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c + ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c \ + ft_gnl.c SRCS := $(addprefix $(SRC_DIR)/, $(SRCS)) OBJS = $(SRCS:.c=.o) diff --git a/includes/libft.h b/includes/libft.h index 0244740..a8b64cb 100755 --- a/includes/libft.h +++ b/includes/libft.h @@ -19,6 +19,10 @@ # include # include +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif + typedef struct s_list { void *content; @@ -66,14 +70,16 @@ void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstadd_front(t_list **lst, t_list *new_node); int ft_lstsize(t_list *lst); t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_back(t_list **lst, t_list *new_node); void ft_lstdelone(t_list *lst, void (*del)(void *)); void ft_lstclear(t_list **lst, void (*del)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +char *ft_gnl(int fd); + #endif diff --git a/src/ft_gnl.c b/src/ft_gnl.c new file mode 100644 index 0000000..c78ed9c --- /dev/null +++ b/src/ft_gnl.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_gnl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lmouta-g +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/19 16:18:23 by lm0uta #+# #+# */ +/* Updated: 2025/12/19 14:26:07 by lmouta-g ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/libft.h" + +static char *gnl_strjoin(char *stash, char *buffer) +{ + char *tmp; + char *result; + + if (!stash) + return (ft_strdup(buffer)); + tmp = ft_strjoin(stash, buffer); + free(stash); + if (!tmp) + return (NULL); + result = tmp; + return (result); +} + +static char *extract_line(char *stash) +{ + size_t len; + char *newline; + + if (!stash || !*stash) + return (NULL); + newline = ft_strchr(stash, '\n'); + if (newline) + len = newline - stash + 1; + else + len = ft_strlen(stash); + return (ft_substr(stash, 0, len)); +} + +static char *update_stash(char *stash) +{ + size_t i; + char *new_stash; + + if (!stash) + return (NULL); + i = 0; + while (stash[i] && stash[i] != '\n') + i++; + if (stash[i] == '\n') + i++; + if (!stash[i]) + return (free(stash), NULL); + new_stash = ft_strdup(stash + i); + free(stash); + return (new_stash); +} + +/** + * ft_gnl - reads a line from file descriptor + * @fd: file descriptor to read from + * Return: line read or NULL if EOF/error + */ +char *ft_gnl(int fd) +{ + static char *stash; + char *buffer; + ssize_t bytes; + char *line; + + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + buffer = malloc(BUFFER_SIZE + 1); + if (!buffer) + return (NULL); + while (!ft_strchr(stash, '\n')) + { + bytes = read(fd, buffer, BUFFER_SIZE); + if (bytes == -1) + return (free(buffer), free(stash), stash = NULL, NULL); + if (bytes == 0) + break ; + buffer[bytes] = '\0'; + stash = gnl_strjoin(stash, buffer); + if (!stash) + return (free(buffer), NULL); + } + free(buffer); + line = extract_line(stash); + stash = update_stash(stash); + return (line); +} diff --git a/src/ft_lstadd_back.c b/src/ft_lstadd_back.c index 96dd389..113e3de 100755 --- a/src/ft_lstadd_back.c +++ b/src/ft_lstadd_back.c @@ -15,21 +15,21 @@ /** * ft_lstadd_back - adds node at list end * @lst: pointer to list head - * @new: node to add + * @new_node: node to add */ -void ft_lstadd_back(t_list **lst, t_list *new) +void ft_lstadd_back(t_list **lst, t_list *new_node) { t_list *tmp; - if (!lst || !new) + if (!lst || !new_node) return ; if (!(*lst)) { - *lst = new; + *lst = new_node; return ; } tmp = *lst; while (tmp->next) tmp = tmp->next; - tmp->next = new; + tmp->next = new_node; } diff --git a/src/ft_lstadd_front.c b/src/ft_lstadd_front.c index 9aa2baf..79b5a2a 100755 --- a/src/ft_lstadd_front.c +++ b/src/ft_lstadd_front.c @@ -15,12 +15,12 @@ /** * ft_lstadd_front - adds node at list start * @lst: pointer to list head - * @new: node to add + * @new_node: node to add */ -void ft_lstadd_front(t_list **lst, t_list *new) +void ft_lstadd_front(t_list **lst, t_list *new_node) { - if (!lst || !new) + if (!lst || !new_node) return ; - new->next = *lst; - *lst = new; + new_node->next = *lst; + *lst = new_node; } From ef20be7bc91ef9d7d2b22dac284c7edd5e521793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20MOUTA?= Date: Wed, 28 Jan 2026 23:43:39 +0100 Subject: [PATCH 2/3] Update src/ft_gnl.c Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/ft_gnl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ft_gnl.c b/src/ft_gnl.c index c78ed9c..a12f61b 100644 --- a/src/ft_gnl.c +++ b/src/ft_gnl.c @@ -78,7 +78,7 @@ char *ft_gnl(int fd) buffer = malloc(BUFFER_SIZE + 1); if (!buffer) return (NULL); - while (!ft_strchr(stash, '\n')) + while (!stash || !ft_strchr(stash, '\n')) { bytes = read(fd, buffer, BUFFER_SIZE); if (bytes == -1) From 03b9c552b5bc42dab4bffb9e5b9ce84af0561d79 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 28 Jan 2026 22:44:21 +0000 Subject: [PATCH 3/3] Initial plan