Mandatory finished

This commit is contained in:
2025-11-17 12:40:49 +01:00
parent 506d90d9c1
commit dabbfa7a35
73 changed files with 238 additions and 11 deletions

57
libft/.gitignore vendored Normal file
View File

@@ -0,0 +1,57 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
# debug information files
*.dwo
libft.a

94
libft/Makefile Normal file
View File

@@ -0,0 +1,94 @@
CC= cc
AR= ar
ARFLAGS= rcs
HEADER= libft.h
CFLAGS= -Wall \
-Wextra \
-Werror
NAME= libft.a
SRC= ft_isdigit.c \
ft_isalpha.c \
ft_isalnum.c \
ft_isascii.c \
ft_isprint.c \
ft_strlen.c \
ft_memset.c \
ft_bzero.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_strlcat.c \
ft_toupper.c \
ft_tolower.c \
ft_strchr.c \
ft_strrchr.c \
ft_strncmp.c \
ft_memcmp.c \
ft_memchr.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.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
BONUS_SRC= ft_lstnew_bonus.c \
ft_lstadd_front_bonus.c \
ft_lstsize_bonus.c \
ft_lstlast_bonus.c \
ft_lstadd_back_bonus.c \
ft_lstdelone_bonus.c \
ft_lstclear_bonus.c \
ft_lstiter_bonus.c \
ft_lstmap_bonus.c
OBJ= $(SRC:.c=.o)
BONUS_OBJ= $(BONUS_SRC:.c=.o)
DEP= $(SRC:.c=.d)
BONUS_DEP= $(BONUS_SRC:.c=.d)
ALL_OBJ= $(OBJ) $(BONUS_OBJ)
ALL_DEP= $(DEP) $(BONUS_DEP)
%.o: %.c
$(CC) -MMD -MP -o $@ -c $< $(CFLAGS) -I.
all: $(NAME)
$(NAME): $(OBJ)
$(AR) $(ARFLAGS) $(NAME) $(OBJ)
bonus:
$(MAKE) $(NAME) SRC="$(SRC) $(BONUS_SRC)"
clean:
rm -f $(ALL_OBJ) $(ALL_DEP)
fclean: clean
rm -f $(NAME)
re: fclean all
-include $(DEP)
.PHONY: all clean fclean re bonus

62
libft/ft_atoi.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 19:07:51 by dgaillet #+# #+# */
/* Updated: 2025/11/12 14:17:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long long int str_to_ll(const char *nptr)
{
int i;
long long int res;
i = 0;
res = 0;
while (ft_isdigit(nptr[i]))
{
if (res * 10 < res && res > 0)
return (-1);
res = (10 * res) + nptr[i] - 48;
i++;
}
return (res);
}
static long long int ft_atoll(const char *nptr)
{
int i;
long long int res;
i = 0;
res = 0;
if (!nptr[0])
return (0);
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ')
i++;
if (nptr[i] == '-' && ft_isdigit(nptr[i + 1]))
{
res = -1 * ft_atoll(&nptr[i] + 1);
if ((nptr[i + 1] != '1' && res == 1)
|| (nptr[i + 1] == '1' && ft_isdigit(nptr[i + 2]) && res == 1))
return (0);
return (res);
}
else if (nptr[i] == '+' && ft_isdigit(nptr[i + 1]))
i++;
return (str_to_ll(&nptr[i]));
}
int ft_atoi(const char *nptr)
{
int res;
res = (int) ft_atoll(nptr);
return (res);
}

23
libft/ft_bzero.c Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 11:41:08 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:54:13 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
while (n > 0)
{
*((unsigned char *) s) = '\0';
s++;
n--;
}
}

26
libft/ft_calloc.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 19:26:10 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:05:12 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *all_mem;
if (size && nmemb > SIZE_MAX / size)
return (NULL);
all_mem = malloc(size * nmemb);
if (!all_mem)
return (NULL);
ft_bzero(all_mem, nmemb * size);
return (all_mem);
}

20
libft/ft_isalnum.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 10:46:12 by dgaillet #+# #+# */
/* Updated: 2025/11/05 10:47:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
if (ft_isalpha(c) || ft_isdigit(c))
return (1);
return (0);
}

18
libft/ft_isalpha.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 09:53:19 by dgaillet #+# #+# */
/* Updated: 2025/11/06 09:48:22 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_isalpha(int c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return (1);
return (0);
}

18
libft/ft_isascii.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 10:48:02 by dgaillet #+# #+# */
/* Updated: 2025/11/05 10:50:20 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
if (c >= 0 && c <= 127)
return (1);
return (0);
}

18
libft/ft_isdigit.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 09:57:01 by dgaillet #+# #+# */
/* Updated: 2025/11/11 15:45:19 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
if (c >= '0' && c <= '9')
return (1);
return (0);
}

18
libft/ft_isprint.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 11:14:13 by dgaillet #+# #+# */
/* Updated: 2025/11/06 10:04:03 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
if (c >= 32 && c <= 126)
return (1);
return (0);
}

62
libft/ft_itoa.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/07 13:06:35 by dgaillet #+# #+# */
/* Updated: 2025/11/12 14:20:16 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_digits(int n)
{
size_t size;
size = 0;
if (n <= 0)
size++;
while (n)
{
n /= 10;
size++;
}
return (size);
}
static void insert_char(char *str, unsigned int nbr, size_t index)
{
while (nbr)
{
str[index--] = nbr % 10 + '0';
nbr /= 10;
}
}
char *ft_itoa(int n)
{
unsigned int nbr;
char *str;
size_t size;
nbr = n;
if (n < 0)
nbr = n * -1;
size = count_digits(n);
str = malloc(sizeof(char) * (size + 1));
if (!str)
return (NULL);
str[size] = '\0';
if (nbr == 0)
str[0] = '0';
else
{
if (n < 0)
str[0] = '-';
insert_char(str, nbr, size - 1);
}
return (str);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:06:49 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:11:12 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *temp;
if (!lst)
return ;
if (!(*lst))
{
*lst = new;
return ;
}
temp = ft_lstlast(*lst);
temp->next = new;
}

View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 14:36:17 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:14:35 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (!new || !lst)
return ;
new->next = *lst;
*lst = new;
}

34
libft/ft_lstclear_bonus.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:31:17 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:07:13 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *temp;
t_list *next;
if (!lst || !*lst)
return ;
temp = *lst;
next = temp->next;
while (next)
{
del(temp->content);
free(temp);
temp = next;
next = temp->next;
}
del(temp->content);
free(temp);
*lst = NULL;
}

View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:22:02 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:07:58 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

24
libft/ft_lstiter_bonus.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:51:31 by dgaillet #+# #+# */
/* Updated: 2025/11/12 15:54:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

30
libft/ft_lstlast_bonus.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:03:19 by dgaillet #+# #+# */
/* Updated: 2025/11/11 15:18:37 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
size_t size;
size_t i;
if (!lst)
return (NULL);
size = ft_lstsize(lst);
i = 1;
while (i < size)
{
lst = lst->next;
i++;
}
return (lst);
}

38
libft/ft_lstmap_bonus.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:54:38 by dgaillet #+# #+# */
/* Updated: 2025/11/10 18:19:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_lst;
t_list *temp_lst;
void *temp_content;
if (!lst)
return (NULL);
new_lst = NULL;
while (lst)
{
temp_content = f(lst->content);
temp_lst = ft_lstnew(temp_content);
if (!temp_lst)
{
del(temp_content);
ft_lstclear(&new_lst, del);
return (NULL);
}
ft_lstadd_back(&new_lst, temp_lst);
lst = lst->next;
}
return (new_lst);
}

25
libft/ft_lstnew_bonus.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 14:27:21 by dgaillet #+# #+# */
/* Updated: 2025/11/10 18:19:27 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *new_lst;
new_lst = malloc(sizeof(t_list));
if (!new_lst)
return (NULL);
new_lst->content = content;
new_lst->next = NULL;
return (new_lst);
}

32
libft/ft_lstsize_bonus.c Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 14:43:22 by dgaillet #+# #+# */
/* Updated: 2025/11/11 11:31:23 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int i;
t_list *first_node;
if (!lst)
return (0);
first_node = lst;
i = 1;
while (lst->next)
{
lst = lst->next;
if (lst == first_node)
break ;
i++;
}
return (i);
}

28
libft/ft_memchr.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:54:17 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:56:15 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
void *ptr;
ptr = (void *) s;
while (n > 0)
{
if (*((unsigned char *) ptr) == (unsigned char) c)
return (ptr);
ptr++;
n--;
}
return (NULL);
}

29
libft/ft_memcmp.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 18:00:38 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:54:27 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
if (((unsigned char *) s1)[i] > ((unsigned char *) s2)[i])
return (1);
if (((unsigned char *) s1)[i] < ((unsigned char *) s2)[i])
return (-1);
i++;
}
return (0);
}

28
libft/ft_memcpy.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 11:46:16 by dgaillet #+# #+# */
/* Updated: 2025/11/12 14:24:38 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
if (!dest && !src)
return (dest);
i = 0;
while (i < n)
{
((unsigned char *) dest)[i] = ((unsigned char *) src)[i];
i++;
}
return (dest);
}

34
libft/ft_memmove.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 13:17:42 by dgaillet #+# #+# */
/* Updated: 2025/11/09 20:15:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void *ft_rev_memcpy(void *dest, const void *src, size_t n)
{
if ((!dest && !src) || n == 0)
return (dest);
n--;
while (n > 0)
{
((unsigned char *) dest)[n] = ((unsigned char *) src)[n];
n--;
}
((unsigned char *) dest)[n] = ((unsigned char *) src)[n];
return (dest);
}
void *ft_memmove(void *dest, const void *src, size_t n)
{
if (dest < src)
return (ft_memcpy(dest, src, n));
return (ft_rev_memcpy(dest, src, n));
}

26
libft/ft_memset.c Normal file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 11:20:47 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:54:07 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
((unsigned char *) s)[i] = c;
i++;
}
return (s);
}

18
libft/ft_putchar_fd.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:41:14 by dgaillet #+# #+# */
/* Updated: 2025/11/11 13:54:54 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

20
libft/ft_putendl_fd.c Normal file
View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:46:08 by dgaillet #+# #+# */
/* Updated: 2025/11/11 12:25:15 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

35
libft/ft_putnbr_fd.c Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:50:35 by dgaillet #+# #+# */
/* Updated: 2025/11/11 12:26:48 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void write_nbr(unsigned int n, int fd)
{
if (n)
{
write_nbr(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
}
void ft_putnbr_fd(int n, int fd)
{
if (!n)
ft_putchar_fd('0', fd);
else if (n < 0)
{
ft_putchar_fd('-', fd);
write_nbr(n * -1, fd);
}
else
write_nbr(n, fd);
}

18
libft/ft_putstr_fd.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:43:42 by dgaillet #+# #+# */
/* Updated: 2025/11/11 13:56:30 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
write(fd, s, ft_strlen(s));
}

81
libft/ft_split.c Normal file
View File

@@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 17:02:58 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:30:49 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t strs_size(const char *s, unsigned char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[j])
{
if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c))
i++;
j++;
}
return (i);
}
static int next_match(char *str, unsigned char c)
{
int i;
i = 0;
while (str[i] && str[i] != c)
i++;
return (i);
}
static void clear_strs(char **strs)
{
size_t i;
i = 0;
while (strs[i])
{
free(strs[i]);
i++;
}
free(strs);
}
char **ft_split(char const *s, char c)
{
char **strs;
int i;
int j;
if (!s)
return (NULL);
strs = ft_calloc(sizeof(char *), strs_size(s, c) + 1);
if (!strs)
return (NULL);
i = 0;
j = 0;
while (s[j])
{
if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c))
{
strs[i] = ft_substr(&s[j], 0, next_match((char *) &s[j], c));
if (!strs[i++])
{
clear_strs(strs);
return (NULL);
}
}
j++;
}
return (strs);
}

29
libft/ft_strchr.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:24:37 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:54:20 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
{
if ((unsigned char) s[i] == (unsigned char) c)
return ((char *) s + i);
i++;
}
if ((unsigned char) s[i] == (unsigned char) c)
return ((char *) s + i);
return (NULL);
}

31
libft/ft_strdup.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 19:31:29 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:54:36 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
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);
}

25
libft/ft_striteri.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:36:38 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:21:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
if (!f)
return ;
i = 0;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

38
libft/ft_strjoin.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 13:52:21 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:36:32 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
int i;
int 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 (s2[j])
{
str[i + j] = s2[j];
j++;
}
str[i + j] = '\0';
return (str);
}

33
libft/ft_strlcat.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:11:43 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:38:24 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
size_t i;
size_t j;
i = 0;
j = ft_strlen(dst);
if (!size)
return (ft_strlen(src));
if (j >= size)
return (ft_strlen(src) + size);
while (src[i] && i < ((size - j) - 1))
{
dst[i + j] = src[i];
i++;
}
dst[i + j] = '\0';
return (j + ft_strlen(src));
}

30
libft/ft_strlcpy.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 13:31:13 by dgaillet #+# #+# */
/* Updated: 2025/11/06 12:05:04 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
if (size > 0)
{
while (src[i] && i < (size - 1))
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
}
return (ft_strlen(src));
}

23
libft/ft_strlen.c Normal file
View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 11:17:31 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:36:18 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}

33
libft/ft_strmapi.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:30:25 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:22:49 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
unsigned int i;
if (!s || !f)
return (NULL);
i = 0;
str = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (!str)
return (NULL);
while (s[i])
{
str[i] = f(i, s[i]);
i++;
}
str[i] = '\0';
return (str);
}

29
libft/ft_strncmp.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:34:15 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:41:15 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
while ((s1[i] || s2[i]) && i < n)
{
if ((unsigned char) s1[i] > (unsigned char) s2[i])
return (1);
if ((unsigned char) s1[i] < (unsigned char) s2[i])
return (-1);
i++;
}
return (0);
}

33
libft/ft_strnstr.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 18:05:52 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:42:42 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t little_len;
size_t big_len;
size_t i;
i = 0;
little_len = ft_strlen(little);
big_len = ft_strlen(big);
if (!little[0])
return ((char *) big);
while ((little_len + i) <= len && (little_len + i) <= big_len)
{
if (!ft_strncmp(&big[i], little, little_len))
return ((char *) &big[i]);
i++;
}
return (NULL);
}

29
libft/ft_strrchr.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:28:39 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:43:49 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
int i;
i = 0;
while (s[i])
i++;
while (i >= 0)
{
if ((unsigned char) s[i] == (unsigned char) c)
return ((char *) &s[i]);
i--;
}
return (NULL);
}

87
libft/ft_strtrim.c Normal file
View File

@@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 16:31:38 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:50:53 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int test_charset(char c, char const *set)
{
if (!set[0])
return (0);
if (c == set[0])
return (1);
return (test_charset(c, set + 1));
}
static size_t end_trim(char const *s1, char const *set)
{
int i;
i = 0;
if (!s1[0])
return (0);
while (s1[i])
i++;
i--;
while (test_charset(s1[i], set))
i--;
return (i);
}
static size_t start_trim(char const *s1, char const *set)
{
size_t i;
i = 0;
while (test_charset(s1[i], set))
i++;
return (i);
}
static int is_empty(char const *s1, char const *set)
{
size_t i;
i = 0;
while (s1[i])
{
if (!test_charset(s1[i], set))
return (0);
i++;
}
return (1);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
size_t i;
char *str;
if (!s1 || !set)
return (NULL);
i = 0;
if (is_empty(s1, set))
return (ft_calloc(1, sizeof(char)));
start = start_trim(s1, set);
end = end_trim(s1, set);
str = malloc(sizeof(char) * (end - start + 2));
if (!str)
return (NULL);
while (start + i <= end)
{
str[i] = s1[i + start];
i++;
}
str[i] = '\0';
return (str);
}

39
libft/ft_substr.c Normal file
View File

@@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 13:20:55 by dgaillet #+# #+# */
/* Updated: 2025/11/12 13:51:23 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
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);
}

18
libft/ft_tolower.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:23:29 by dgaillet #+# #+# */
/* Updated: 2025/11/05 17:24:21 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_tolower(int c)
{
if (c >= 'A' && c <= 'Z')
return (c + 32);
return (c);
}

18
libft/ft_toupper.c Normal file
View File

@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 17:20:43 by dgaillet #+# #+# */
/* Updated: 2025/11/05 17:23:02 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_toupper(int c)
{
if (c >= 'a' && c <= 'z')
return (c - 32);
return (c);
}

71
libft/libft.h Normal file
View File

@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/05 09:49:43 by dgaillet #+# #+# */
/* Updated: 2025/11/12 17:05:00 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stdlib.h>
# include <unistd.h>
# include <limits.h>
# include <stdint.h>
typedef struct s_list
{
void *content;
struct s_list *next;
} t_list;
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
size_t ft_strlen(const char *s);
void *ft_memset(void *s, int c, size_t n);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size);
int ft_toupper(int c);
int ft_tolower(int c);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memchr(const void *s, int c, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *s, char c);
char *ft_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char *s, int fd);
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);
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_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 *));
#endif