Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions includes/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# include <unistd.h>
# include <limits.h>

# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# endif

typedef struct s_list
{
void *content;
Expand Down Expand Up @@ -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
97 changes: 97 additions & 0 deletions src/ft_gnl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmouta-g <lmouta-g@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 (!stash || !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);
}
10 changes: 5 additions & 5 deletions src/ft_lstadd_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 5 additions & 5 deletions src/ft_lstadd_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}