This commit is contained in:
Willem Haffmans 2024-07-26 22:32:04 +02:00
commit 326f52308e
97 changed files with 2854 additions and 0 deletions

62
Makefile Normal file
View File

@ -0,0 +1,62 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/09 16:25:07 by whaffman #+# #+# #
# Updated: 2024/07/09 17:08:01 by whaffman ### ########.fr #
# #
# **************************************************************************** #
CC = cc
CFLAGS = -Wall -Werror -Wextra
# Add any other static library in the same fashion - 1
# INCLUDE: .h files or .a library folders
# SRC: .c files
SRC_DIR = src
OBJ_DIR = obj
OBJ_SO_DIR = obj_so
INC_DIR = include
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES))
OBJ_FILES_SO = $(patsubst $(SRC_DIR)/%.c,$(OBJ_SO_DIR)/%.o,$(SRC_FILES))
# Add any other static library in the same fashion - 2
NAME = libft.a
NAME_SO = libft.so
.PHONY: all clean fclean re so
all: $(NAME)
so: $(NAME_SO)
$(NAME): $(OBJ_FILES)
ar rcs $(NAME) $(OBJ_FILES)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -I$(INC_DIR) -O3 -c -o $@ $<
$(NAME_SO): $(OBJ_FILES_SO)
$(CC) -shared -o $(NAME_SO) $(OBJ_FILES_SO)
$(OBJ_SO_DIR)/%.o: $(SRC_DIR)/%.c
mkdir -p $(OBJ_SO_DIR)
$(CC) -fPIC $(CFLAGS) -I$(INC_DIR) -c -o $@ $<
clean:
$(RM) -r $(OBJ_DIR)
$(RM) -r $(OBJ_SO_DIR)
fclean: clean
$(RM) $(NAME)
$(RM) $(NAME_SO)
re: fclean all

BIN
en.subject.pdf Normal file

Binary file not shown.

47
extended_libft/Makefile Normal file
View File

@ -0,0 +1,47 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/09 16:25:07 by whaffman #+# #+# #
# Updated: 2024/07/09 17:08:01 by whaffman ### ########.fr #
# #
# **************************************************************************** #
NAME = libft.a
SOURCES = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \
ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c ft_lstadd_back.c\
ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c ft_lstiter.c \
ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c ft_memchr.c \
ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_putchar_fd.c \
ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c ft_split.c ft_strchr.c \
ft_strdup.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlcpy.c \
ft_strlen.c ft_strmapi.c ft_strncmp.c ft_strnstr.c ft_strrchr.c \
ft_strtrim.c ft_substr.c ft_tolower.c ft_toupper.c \
ft_putchar.c ft_putstr.c ft_putnbr.c ft_isspace.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc
CFLAGS = -Wall -Wextra -Werror
all: $(NAME)
$(NAME): $(OBJECTS)
$(AR) -r $@ $?
%.o: %.c
$(CC) -c $(CFLAGS) $?
clean:
rm -f $(OBJECTS) $(BOBJECTS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all bonus clean fclean re

33
extended_libft/ft_atoi.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:02 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:20:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int sign;
int res;
while (ft_isspace(*nptr))
nptr++;
res = 0;
sign = 1;
while (*nptr == '-' || *nptr == '+')
{
if (*nptr == '-')
sign *= -1;
nptr++;
}
while (ft_isdigit(*nptr))
res = 10 * res + sign * (*nptr++ - '0');
return (res);
}

18
extended_libft/ft_bzero.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/08 17:57:45 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, 0, n);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:25:29 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
ptr = malloc(nmemb * size);
if (ptr)
ft_bzero(ptr, nmemb * size);
return (ptr);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/09 16:33:06 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isdigit(c) || ft_isalpha(c));
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:07:31 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return (('A' <= c && 'Z' >= c) || ('a' <= c && 'z' >= c));
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:10:15 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (0 <= c && 127 >= c);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/09 17:12:14 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return ('0' <= c && '9' >= c);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:14:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (32 <= c && 126 >= c);
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_isspace.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/07/26 21:54:01 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/07/26 21:55:03 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(int c)
{
return (' ' == c || '\f' == c || '\n' == c
|| '\r' == c || '\t' == c || '\v' == c);
}

58
extended_libft/ft_itoa.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/10 15:50:44 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static char *itoa_stralloc(int n)
{
int i;
i = 0;
if (n <= 0)
i++;
while (n)
{
i++;
n /= 10;
}
return (ft_calloc((i + 1), sizeof(char)));
}
char *ft_itoa(int n)
{
int i;
long ln;
long divider;
char *result;
result = itoa_stralloc(n);
if (!result)
return (NULL);
ln = n;
i = 0;
divider = 1;
if (ln < 0)
{
ln *= -1;
result[i++] = '-';
}
while (divider * 10 <= ln)
divider *= 10;
while (divider)
{
result[i++] = ln / divider + '0';
ln %= divider;
divider /= 10;
}
return (result);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:51:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (!new)
return ;
if (!*lst)
{
*lst = new;
return ;
}
last = ft_lstlast(*lst);
last->next = new;
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:35:22 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new)
{
new->next = *lst;
*lst = new;
}
}

View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:08:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
if (!del || !lst)
return ;
while (*lst)
{
if ((*lst)->next)
next = (*lst)->next;
else
next = NULL;
del((*lst)->content);
free(*lst);
*lst = next;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:57:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:13:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
(*f)(lst->content);
lst = lst->next;
}
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:41:26 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:21:58 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_list;
t_list *new_elem;
if (!lst || !f)
return (NULL);
new_list = NULL;
while (lst)
{
new_elem = ft_lstnew(f(lst->content));
if (!new_elem)
{
ft_lstclear(&new_list, del);
return (NULL);
}
ft_lstadd_back(&new_list, new_elem);
lst = lst->next;
}
return (new_list);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:32:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *node;
node = malloc(sizeof(t_list));
if (node)
{
node->content = content;
node->next = NULL;
}
return (node);
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:37:42 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int count;
count = 1;
if (!lst)
return (0);
while (lst->next)
{
lst = lst->next;
count++;
}
return (count);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:36:51 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
while (0 < n--)
{
if (*(unsigned char *)s == (unsigned char) c)
return ((void *) s);
s++;
}
return (NULL);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:51:13 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *str1;
unsigned char *str2;
size_t i;
str1 = (unsigned char *)s1;
str2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:44:01 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
if (dest == NULL && src == NULL)
return (NULL);
while (0 < n--)
((unsigned char *)dest)[n] = ((unsigned char *)src)[n];
return (dest);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 23:02:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *dest_ptr;
unsigned char *src_ptr;
dest_ptr = (unsigned char *)dest;
src_ptr = (unsigned char *)src;
if (dest_ptr == src_ptr)
return (dest);
else if (dest_ptr < src_ptr)
{
while (n-- > 0)
*dest_ptr++ = *src_ptr++;
}
else
{
while (n-- > 0)
dest_ptr[n] = src_ptr[n];
}
return (dest);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 16:08:55 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
while (0 < n--)
((unsigned char *)s)[n] = (unsigned char) c;
return (s);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_putchar.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/07/26 21:24:12 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/07/26 21:30:59 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar(char c)
{
ft_putchar_fd(c, 1);
}

View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:52:28 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
if (fd < 0)
return ;
write(fd, &c, 1);
}

View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:56:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (fd < 0 || s == NULL)
return ;
ft_putstr_fd(s, fd);
write(fd, "\n", 1);
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_putnbr.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/07/26 21:32:30 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/07/26 21:37:17 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr(int n)
{
ft_putnbr_fd(n, 1);
}

View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/09 16:23:22 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
long ln;
long divider;
char c;
if (fd < 0)
return ;
ln = n;
divider = 1;
if (n < 0)
{
if (!write(fd, "-", 1))
return ;
ln *= -1;
}
while (divider * 10 <= ln)
divider *= 10;
while (divider)
{
c = ln / divider + '0';
if (!write(fd, &c, 1))
return ;
ln %= divider;
divider /= 10;
}
}

View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* ft_putstr.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/07/26 21:37:29 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/07/26 21:39:15 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr(char *s)
{
ft_putstr_fd(s, 1);
}

View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:54:32 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
size_t length;
if (s == NULL || fd < 0)
return ;
length = ft_strlen(s);
write(fd, s, length);
}

59
extended_libft/ft_split.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:10:57 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int count_words(char const *s, char c)
{
int n;
int i;
n = 0;
i = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
n++;
i++;
}
return (n);
}
char **ft_split(char const *s, char c)
{
char **result;
int n;
int i;
int j;
if (!s)
return (NULL);
result = malloc((count_words(s, c) + 1) * sizeof(char *));
if (!result)
return (NULL);
i = 0;
n = 0;
while (s[i])
{
j = 0;
while (s[i] == c)
i++;
while (s[i + j] && s[i + j] != c)
j++;
if (j)
result[n++] = ft_substr(s, i, j);
i += j;
}
result[n] = NULL;
return (result);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:30:39 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*(unsigned char *) s == (unsigned char) c)
return ((char *) s);
s++;
}
if (*(unsigned char *) s == (unsigned char) c)
return ((char *) s);
return (NULL);
}

View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:22:46 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strdup(const char *s)
{
size_t size;
char *d;
size = ft_strlen(s) + 1;
d = (char *) malloc(size * sizeof(char));
if (d)
ft_strlcpy(d, s, size);
return (d);
}

View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:07:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
if (!s)
return ;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:49:11 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t len_s1;
size_t len_s2;
if (!s1 || !s2)
return (NULL);
len_s1 = ft_strlen(s1);
len_s2 = ft_strlen(s2);
str = (char *)malloc(len_s1 + len_s2 + 1);
if (!str)
return (NULL);
ft_memcpy(str, s1, len_s1);
ft_memcpy(str + len_s1, s2, len_s2);
str[len_s1 + len_s2] = '\0';
return (str);
}

View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:23:36 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
unsigned int src_len;
unsigned int dst_len;
unsigned int i;
src_len = ft_strlen(src);
if (size == 0)
return (src_len);
dst_len = ft_strlen(dst);
if (dst_len >= (long int) size)
return (size + src_len);
i = 0;
while (src[i] && i < size - dst_len - 1)
{
dst[dst_len + i] = src[i];
i++;
}
dst[dst_len + i] = '\0';
return (src_len + dst_len);
}

View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:24:19 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
size_t result;
i = 0;
if (size == 0)
return (ft_strlen(src));
while (src[i] && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
result = ft_strlen(src);
return (result);
}

View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::: ::: */
/* ft_strlen.c :+: :+::+: :+: */
/* +:+ +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ */
/* Created: 2024/07/15 14:51:46 by whaffman #+#+# #+#+# #+# #+# */
/* Updated: 2024/07/15 15:01:58 by whaffman ### ### ### ### */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t length;
length = 0;
while (*s++)
length++;
return (length);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:06:40 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
char *result;
if (!s)
return (NULL);
result = malloc((ft_strlen(s) + 1) * sizeof(char));
if (!result)
return (NULL);
i = 0;
while (s[i])
{
result[i] = f(i, s[i]);
i++;
}
result[i] = '\0';
return (result);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:51:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (*s1 && *s1 == *s2 && i < n - 1)
{
s1++;
s2++;
i++;
}
return ((unsigned char) *s1 - (unsigned char) *s2);
}

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:01:36 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (*little == '\0' || len == 0)
return ((char *) big);
while (big[i] && i < len)
{
j = 0;
while (little[j] && big[i + j] == little [j] && i + j < len)
j++;
if (little[j] == '\0')
return ((char *) &big[i]);
i++;
}
return (NULL);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:35:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
char *result;
result = NULL;
while (*s)
{
if (*(unsigned char *) s == (unsigned char) c)
result = (char *) s;
s++;
}
if ('\0' == c)
result = (char *) s;
return (result);
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:48:45 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
char *front;
char *back;
if (!s1)
return (NULL);
if (!ft_strlen(set))
return (ft_strdup(s1));
front = (char *) s1;
back = (char *) s1;
while (*back)
back++;
back--;
while (ft_strchr(set, *front))
front++;
while (ft_strchr(set, *back))
back--;
return (ft_substr(s1, front - s1, back - front + 1));
}

View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:27:02 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t len_s;
if (!s)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
return (ft_strdup(""));
if (len_s - start < len)
len = len_s - start;
substr = malloc(len * sizeof(char) + 1);
if (!substr)
return (NULL);
ft_memcpy(substr, &s[start], len);
substr[len] = '\0';
return (substr);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:12 by whaffman #+# #+# */
/* Updated: 2024/07/08 15:51:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if ('A' <= c && 'Z' >= c)
return (c - 'A' + 'a');
return (c);
}

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:12 by whaffman #+# #+# */
/* Updated: 2024/07/08 15:53:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if ('a' <= c && 'z' >= c)
return (c - 'a' + 'A');
return (c);
}

81
extended_libft/libft.h Normal file
View File

@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/06 11:15:10 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:27:58 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.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);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, 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);
/*
* BONUS
*/
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 *));
/*
* EXTRA
*/
void ft_putchar(char c);
void ft_putstr(char *s);
void ft_putnbr(int n);
int ft_isspace(int c);
#endif

72
include/libft.h Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/06 11:15:10 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:27:58 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.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);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, 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);
/*
* BONUS
*/
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

46
src/Makefile Normal file
View File

@ -0,0 +1,46 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/07/09 16:25:07 by whaffman #+# #+# #
# Updated: 2024/07/09 17:08:01 by whaffman ### ########.fr #
# #
# **************************************************************************** #
NAME = libft.a
SOURCES = ft_atoi.c ft_bzero.c ft_calloc.c ft_isalnum.c ft_isalpha.c \
ft_isascii.c ft_isdigit.c ft_isprint.c ft_itoa.c ft_lstadd_back.c\
ft_lstadd_front.c ft_lstclear.c ft_lstdelone.c ft_lstiter.c \
ft_lstlast.c ft_lstmap.c ft_lstnew.c ft_lstsize.c ft_memchr.c \
ft_memcmp.c ft_memcpy.c ft_memmove.c ft_memset.c ft_putchar_fd.c \
ft_putendl_fd.c ft_putnbr_fd.c ft_putstr_fd.c ft_split.c ft_strchr.c \
ft_strdup.c ft_striteri.c ft_strjoin.c ft_strlcat.c ft_strlcpy.c \
ft_strlen.c ft_strmapi.c ft_strncmp.c ft_strnstr.c ft_strrchr.c \
ft_strtrim.c ft_substr.c ft_tolower.c ft_toupper.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc
CFLAGS = -Wall -Wextra -Werror
all: $(NAME)
$(NAME): $(OBJECTS)
$(AR) -r $@ $?
%.o: %.c
$(CC) -c $(CFLAGS) $?
clean:
rm -f $(OBJECTS) $(BOBJECTS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: all bonus clean fclean re

40
src/ft_atoi.c Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:02 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:20:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(int c)
{
return (' ' == c || '\f' == c || \
'\n' == c || '\r' == c || \
'\t' == c || '\v' == c);
}
int ft_atoi(const char *nptr)
{
int sign;
int res;
while (ft_isspace(*nptr))
nptr++;
res = 0;
sign = 1;
while (*nptr == '-' || *nptr == '+')
{
if (*nptr == '-')
sign *= -1;
nptr++;
}
while (ft_isdigit(*nptr))
res = 10 * res + sign * (*nptr++ - '0');
return (res);
}

18
src/ft_bzero.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/08 17:57:45 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_bzero(void *s, size_t n)
{
ft_memset(s, 0, n);
}

24
src/ft_calloc.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:25:29 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void *ft_calloc(size_t nmemb, size_t size)
{
void *ptr;
ptr = malloc(nmemb * size);
if (ptr)
ft_bzero(ptr, nmemb * size);
return (ptr);
}

18
src/ft_isalnum.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:03 by whaffman #+# #+# */
/* Updated: 2024/07/09 16:33:06 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalnum(int c)
{
return (ft_isdigit(c) || ft_isalpha(c));
}

18
src/ft_isalpha.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:07:31 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isalpha(int c)
{
return (('A' <= c && 'Z' >= c) || ('a' <= c && 'z' >= c));
}

18
src/ft_isascii.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:10:15 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isascii(int c)
{
return (0 <= c && 127 >= c);
}

18
src/ft_isdigit.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/09 17:12:14 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isdigit(int c)
{
return ('0' <= c && '9' >= c);
}

18
src/ft_isprint.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/06 12:14:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isprint(int c)
{
return (32 <= c && 126 >= c);
}

58
src/ft_itoa.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:04 by whaffman #+# #+# */
/* Updated: 2024/07/10 15:50:44 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static char *itoa_stralloc(int n)
{
int i;
i = 0;
if (n <= 0)
i++;
while (n)
{
i++;
n /= 10;
}
return (ft_calloc((i + 1), sizeof(char)));
}
char *ft_itoa(int n)
{
int i;
long ln;
long divider;
char *result;
result = itoa_stralloc(n);
if (!result)
return (NULL);
ln = n;
i = 0;
divider = 1;
if (ln < 0)
{
ln *= -1;
result[i++] = '-';
}
while (divider * 10 <= ln)
divider *= 10;
while (divider)
{
result[i++] = ln / divider + '0';
ln %= divider;
divider /= 10;
}
return (result);
}

28
src/ft_lstadd_back.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:51:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *last;
if (!new)
return ;
if (!*lst)
{
*lst = new;
return ;
}
last = ft_lstlast(*lst);
last->next = new;
}

22
src/ft_lstadd_front.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:35:22 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (new)
{
new->next = *lst;
*lst = new;
}
}

32
src/ft_lstclear.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:08:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *next;
if (!del || !lst)
return ;
while (*lst)
{
if ((*lst)->next)
next = (*lst)->next;
else
next = NULL;
del((*lst)->content);
free(*lst);
*lst = next;
}
}

22
src/ft_lstdelone.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:57:20 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
free(lst);
}

24
src/ft_lstiter.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:05 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:13:53 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
if (!lst || !f)
return ;
while (lst)
{
(*f)(lst->content);
lst = lst->next;
}
}

22
src/ft_lstlast.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:41:26 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return (lst);
}

35
src/ft_lstmap.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 17:21:58 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_list;
t_list *new_elem;
if (!lst || !f)
return (NULL);
new_list = NULL;
while (lst)
{
new_elem = ft_lstnew(f(lst->content));
if (!new_elem)
{
ft_lstclear(&new_list, del);
return (NULL);
}
ft_lstadd_back(&new_list, new_elem);
lst = lst->next;
}
return (new_list);
}

27
src/ft_lstnew.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:32:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *node;
node = malloc(sizeof(t_list));
if (node)
{
node->content = content;
node->next = NULL;
}
return (node);
}

28
src/ft_lstsize.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:06 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:37:42 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int count;
count = 1;
if (!lst)
return (0);
while (lst->next)
{
lst = lst->next;
count++;
}
return (count);
}

24
src/ft_memchr.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:36:51 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memchr(const void *s, int c, size_t n)
{
while (0 < n--)
{
if (*(unsigned char *)s == (unsigned char) c)
return ((void *) s);
s++;
}
return (NULL);
}

31
src/ft_memcmp.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:51:13 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *str1;
unsigned char *str2;
size_t i;
str1 = (unsigned char *)s1;
str2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (str1[i] != str2[i])
return (str1[i] - str2[i]);
i++;
}
return (0);
}

22
src/ft_memcpy.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:44:01 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
if (dest == NULL && src == NULL)
return (NULL);
while (0 < n--)
((unsigned char *)dest)[n] = ((unsigned char *)src)[n];
return (dest);
}

35
src/ft_memmove.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 23:02:34 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *dest_ptr;
unsigned char *src_ptr;
dest_ptr = (unsigned char *)dest;
src_ptr = (unsigned char *)src;
if (dest_ptr == src_ptr)
return (dest);
else if (dest_ptr < src_ptr)
{
while (n-- > 0)
*dest_ptr++ = *src_ptr++;
}
else
{
while (n-- > 0)
dest_ptr[n] = src_ptr[n];
}
return (dest);
}

20
src/ft_memset.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:07 by whaffman #+# #+# */
/* Updated: 2024/07/08 16:08:55 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void *ft_memset(void *s, int c, size_t n)
{
while (0 < n--)
((unsigned char *)s)[n] = (unsigned char) c;
return (s);
}

21
src/ft_putchar_fd.c Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:52:28 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
if (fd < 0)
return ;
write(fd, &c, 1);
}

22
src/ft_putendl_fd.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:56:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
if (fd < 0 || s == NULL)
return ;
ft_putstr_fd(s, fd);
write(fd, "\n", 1);
}

42
src/ft_putnbr_fd.c Normal file
View File

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/09 16:23:22 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putnbr_fd(int n, int fd)
{
long ln;
long divider;
char c;
if (fd < 0)
return ;
ln = n;
divider = 1;
if (n < 0)
{
if (!write(fd, "-", 1))
return ;
ln *= -1;
}
while (divider * 10 <= ln)
divider *= 10;
while (divider)
{
c = ln / divider + '0';
if (!write(fd, &c, 1))
return ;
ln %= divider;
divider /= 10;
}
}

24
src/ft_putstr_fd.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:08 by whaffman #+# #+# */
/* Updated: 2024/07/08 22:54:32 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
size_t length;
if (s == NULL || fd < 0)
return ;
length = ft_strlen(s);
write(fd, s, length);
}

59
src/ft_split.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:10:57 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int count_words(char const *s, char c)
{
int n;
int i;
n = 0;
i = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
n++;
i++;
}
return (n);
}
char **ft_split(char const *s, char c)
{
char **result;
int n;
int i;
int j;
if (!s)
return (NULL);
result = malloc((count_words(s, c) + 1) * sizeof(char *));
if (!result)
return (NULL);
i = 0;
n = 0;
while (s[i])
{
j = 0;
while (s[i] == c)
i++;
while (s[i + j] && s[i + j] != c)
j++;
if (j)
result[n++] = ft_substr(s, i, j);
i += j;
}
result[n] = NULL;
return (result);
}

26
src/ft_strchr.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:30:39 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*(unsigned char *) s == (unsigned char) c)
return ((char *) s);
s++;
}
if (*(unsigned char *) s == (unsigned char) c)
return ((char *) s);
return (NULL);
}

26
src/ft_strdup.c Normal file
View File

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:22:46 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strdup(const char *s)
{
size_t size;
char *d;
size = ft_strlen(s) + 1;
d = (char *) malloc(size * sizeof(char));
if (d)
ft_strlcpy(d, s, size);
return (d);
}

27
src/ft_striteri.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_striteri.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:07:16 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_striteri(char *s, void (*f)(unsigned int, char*))
{
unsigned int i;
i = 0;
if (!s)
return ;
while (s[i])
{
f(i, &s[i]);
i++;
}
}

33
src/ft_strjoin.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:09 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:49:11 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t len_s1;
size_t len_s2;
if (!s1 || !s2)
return (NULL);
len_s1 = ft_strlen(s1);
len_s2 = ft_strlen(s2);
str = (char *)malloc(len_s1 + len_s2 + 1);
if (!str)
return (NULL);
ft_memcpy(str, s1, len_s1);
ft_memcpy(str + len_s1, s2, len_s2);
str[len_s1 + len_s2] = '\0';
return (str);
}

35
src/ft_strlcat.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:23:36 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcat(char *dst, const char *src, size_t size)
{
unsigned int src_len;
unsigned int dst_len;
unsigned int i;
src_len = ft_strlen(src);
if (size == 0)
return (src_len);
dst_len = ft_strlen(dst);
if (dst_len >= (long int) size)
return (size + src_len);
i = 0;
while (src[i] && i < size - dst_len - 1)
{
dst[dst_len + i] = src[i];
i++;
}
dst[dst_len + i] = '\0';
return (src_len + dst_len);
}

31
src/ft_strlcpy.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/09 22:24:19 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlcpy(char *dest, const char *src, size_t size)
{
size_t i;
size_t result;
i = 0;
if (size == 0)
return (ft_strlen(src));
while (src[i] && i < size - 1)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
result = ft_strlen(src);
return (result);
}

23
src/ft_strlen.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::: ::: */
/* ft_strlen.c :+: :+::+: :+: */
/* +:+ +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ */
/* Created: 2024/07/15 14:51:46 by whaffman #+#+# #+#+# #+# #+# */
/* Updated: 2024/07/15 15:01:58 by whaffman ### ### ### ### */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s)
{
size_t length;
length = 0;
while (*s++)
length++;
return (length);
}

34
src/ft_strmapi.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:10 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:06:40 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
int i;
char *result;
if (!s)
return (NULL);
result = malloc((ft_strlen(s) + 1) * sizeof(char));
if (!result)
return (NULL);
i = 0;
while (s[i])
{
result[i] = f(i, s[i]);
i++;
}
result[i] = '\0';
return (result);
}

29
src/ft_strncmp.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 14:51:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
i = 0;
if (n == 0)
return (0);
while (*s1 && *s1 == *s2 && i < n - 1)
{
s1++;
s2++;
i++;
}
return ((unsigned char) *s1 - (unsigned char) *s2);
}

33
src/ft_strnstr.c Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:01:36 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *big, const char *little, size_t len)
{
size_t i;
size_t j;
i = 0;
if (*little == '\0' || len == 0)
return ((char *) big);
while (big[i] && i < len)
{
j = 0;
while (little[j] && big[i + j] == little [j] && i + j < len)
j++;
if (little[j] == '\0')
return ((char *) &big[i]);
i++;
}
return (NULL);
}

29
src/ft_strrchr.c Normal file
View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 11:35:27 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strrchr(const char *s, int c)
{
char *result;
result = NULL;
while (*s)
{
if (*(unsigned char *) s == (unsigned char) c)
result = (char *) s;
s++;
}
if ('\0' == c)
result = (char *) s;
return (result);
}

34
src/ft_strtrim.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:48:45 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
{
char *front;
char *back;
if (!s1)
return (NULL);
if (!ft_strlen(set))
return (ft_strdup(s1));
front = (char *) s1;
back = (char *) s1;
while (*back)
back++;
back--;
while (ft_strchr(set, *front))
front++;
while (ft_strchr(set, *back))
back--;
return (ft_substr(s1, front - s1, back - front + 1));
}

34
src/ft_substr.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:11 by whaffman #+# #+# */
/* Updated: 2024/07/10 12:27:02 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t len_s;
if (!s)
return (NULL);
len_s = ft_strlen(s);
if (start >= len_s)
return (ft_strdup(""));
if (len_s - start < len)
len = len_s - start;
substr = malloc(len * sizeof(char) + 1);
if (!substr)
return (NULL);
ft_memcpy(substr, &s[start], len);
substr[len] = '\0';
return (substr);
}

20
src/ft_tolower.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:12 by whaffman #+# #+# */
/* Updated: 2024/07/08 15:51:50 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_tolower(int c)
{
if ('A' <= c && 'Z' >= c)
return (c - 'A' + 'a');
return (c);
}

20
src/ft_toupper.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/05 18:42:12 by whaffman #+# #+# */
/* Updated: 2024/07/08 15:53:24 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_toupper(int c)
{
if ('a' <= c && 'z' >= c)
return (c - 'a' + 'A');
return (c);
}

72
src/libft.h Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/06 11:15:10 by whaffman #+# #+# */
/* Updated: 2024/07/10 16:27:58 by whaffman ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <stddef.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);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, 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);
/*
* BONUS
*/
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