From: Lukas Jiriste Date: Fri, 29 Sep 2023 12:04:09 +0000 (+0200) Subject: Changed Libft from submodule to normal directory for Moulinette check. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=refs%2Fremotes%2F42%2F42;p=42%2Fft_printf.git Changed Libft from submodule to normal directory for Moulinette check. Changed Makefile not to use git submodule commands for Libft. --- diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 48b01da..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "Libft"] - path = Libft - url = git://78.102.58.167/Libft.git diff --git a/Libft b/Libft deleted file mode 160000 index 5cda9c6..0000000 --- a/Libft +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5cda9c641f76695f327eec126dfb8e9ed91f7792 diff --git a/Libft/Makefile b/Libft/Makefile new file mode 100644 index 0000000..1867830 --- /dev/null +++ b/Libft/Makefile @@ -0,0 +1,30 @@ +CC = cc +CFLAGS = -Wall -Wextra -Werror +AR = ar + +RM = rm -f + +INCDIR = . +SRCDIR = . +# SOURCES = $(addprefix $(SRCDIR)/, ft_putchar.c ft_putstr.c ft_strcmp.c ft_strlen.c ft_swap.c) +SOURCES = $(shell find $(SRCDIR) -maxdepth 1 -name "*.c") +OBJECTS = $(SOURCES:.c=.o) + +NAME = libft.a + +all : $(NAME) + +$(NAME) : $(OBJECTS) + $(AR) rcs $(NAME) $(OBJECTS) + +%.o : %.c + $(CC) $(CFLAGS) -o $@ -c $< -I $(INCDIR) + +clean : + $(RM) $(OBJECTS) $(BONUS_OBJECTS) + +fclean : clean + $(RM) $(NAME) + +re : fclean + $(MAKE) all diff --git a/Libft/ft_abs.c b/Libft/ft_abs.c new file mode 100644 index 0000000..c277682 --- /dev/null +++ b/Libft/ft_abs.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_abs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:48:21 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 11:47:52 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_abs(int n) +{ + if (n < 0) + return (-n); + else + return (n); +} diff --git a/Libft/ft_atoi.c b/Libft/ft_atoi.c new file mode 100644 index 0000000..881b763 --- /dev/null +++ b/Libft/ft_atoi.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/13 13:44:39 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:11:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *nptr) +{ + int res; + int sign; + + res = 0; + sign = 1; + while (ft_isspace(*nptr)) + ++nptr; + if ((*nptr == '+' || *nptr == '-')) + { + if (*nptr == '-') + sign *= -1; + ++nptr; + } + while (ft_isdigit(*nptr)) + { + res *= 10; + res += *nptr - '0'; + ++nptr; + } + return (sign * res); +} diff --git a/Libft/ft_bzero.c b/Libft/ft_bzero.c new file mode 100644 index 0000000..9346d4b --- /dev/null +++ b/Libft/ft_bzero.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:38:08 by ljiriste #+# #+# */ +/* Updated: 2023/08/14 13:38:32 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); + return ; +} diff --git a/Libft/ft_calloc.c b/Libft/ft_calloc.c new file mode 100644 index 0000000..9976848 --- /dev/null +++ b/Libft/ft_calloc.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */ +/* Updated: 2023/09/06 13:06:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* This should not be commented as to emulate builtin calloc + but errno calls function which is deemed forbidden by Moulinette +#include + if (total_bytes / size != nmemb) + { + errno = ENOMEM; + return (NULL); + } +*/ +#include // for malloc +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + size_t total_bytes; + void *res; + + if (nmemb == 0 || size == 0) + return (malloc(0)); + total_bytes = nmemb * size; + if (total_bytes / size != nmemb) + return (NULL); + res = malloc(total_bytes); + if (res == NULL) + return (NULL); + ft_memset(res, 0, total_bytes); + return (res); +} diff --git a/Libft/ft_ctoa.c b/Libft/ft_ctoa.c new file mode 100644 index 0000000..1574aaf --- /dev/null +++ b/Libft/ft_ctoa.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_ctoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:55:17 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 09:56:02 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include "libft.h" + +char *ft_ctoa(char c) +{ + char *res; + + res = malloc(2 * sizeof(char)); + if (res == NULL) + return (res); + res[0] = c; + res[1] = '\0'; + return (res); +} diff --git a/Libft/ft_isalnum.c b/Libft/ft_isalnum.c new file mode 100644 index 0000000..fd9ed56 --- /dev/null +++ b/Libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:02:16 by ljiriste #+# #+# */ +/* Updated: 2023/08/14 13:34:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalnum(int c) +{ + return (ft_isdigit(c) || ft_isalpha(c)); +} diff --git a/Libft/ft_isalpha.c b/Libft/ft_isalpha.c new file mode 100644 index 0000000..c3a3ebc --- /dev/null +++ b/Libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:32:19 by ljiriste #+# #+# */ +/* Updated: 2023/08/14 13:32:33 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isalpha(int c) +{ + return (ft_isupper(c) || ft_islower(c)); +} diff --git a/Libft/ft_isascii.c b/Libft/ft_isascii.c new file mode 100644 index 0000000..377081b --- /dev/null +++ b/Libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:19:40 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:10:02 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isascii(int c) +{ + return (0x00 <= c && c <= 0x7F); +} diff --git a/Libft/ft_isdigit.c b/Libft/ft_isdigit.c new file mode 100644 index 0000000..6df7e2c --- /dev/null +++ b/Libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:32:47 by ljiriste #+# #+# */ +/* Updated: 2023/08/14 13:33:02 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isdigit(int c) +{ + return ('0' <= c && c <= '9'); +} diff --git a/Libft/ft_islower.c b/Libft/ft_islower.c new file mode 100644 index 0000000..1d26006 --- /dev/null +++ b/Libft/ft_islower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_islower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:31:47 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:55:33 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_islower(int c) +{ + return ('a' <= c && c <= 'z'); +} diff --git a/Libft/ft_isprint.c b/Libft/ft_isprint.c new file mode 100644 index 0000000..4cb7934 --- /dev/null +++ b/Libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:17:32 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:20:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isprint(int c) +{ + return (0x20 <= c && c <= 0x7E); +} diff --git a/Libft/ft_isspace.c b/Libft/ft_isspace.c new file mode 100644 index 0000000..23f6070 --- /dev/null +++ b/Libft/ft_isspace.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:29:54 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:12:30 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(int c) +{ + return (c == ' ' || ('\t' <= c && c <= '\r')); +} diff --git a/Libft/ft_isupper.c b/Libft/ft_isupper.c new file mode 100644 index 0000000..adddb5b --- /dev/null +++ b/Libft/ft_isupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:31:05 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:55:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isupper(int c) +{ + return ('A' <= c && c <= 'Z'); +} diff --git a/Libft/ft_itoa.c b/Libft/ft_itoa.c new file mode 100644 index 0000000..88539db --- /dev/null +++ b/Libft/ft_itoa.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:58:10 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:12:42 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static unsigned int size_needed(int n) +{ + unsigned int res; + + res = 1; + if (n == 0) + return (2); + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= 10; + } + return (res); +} + +char *ft_itoa(int n) +{ + unsigned int size; + char *res; + + size = size_needed(n); + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0 && res) + res[0] = '0'; + else if (n < 0 && res) + { + res[0] = '-'; + res[--size] = '0' - (n % 10); + n = -(n / 10); + } + while (n > 0 && res) + { + res[--size] = '0' + (n % 10); + n /= 10; + } + return (res); +} diff --git a/Libft/ft_itoa_base.c b/Libft/ft_itoa_base.c new file mode 100644 index 0000000..45d19ef --- /dev/null +++ b/Libft/ft_itoa_base.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2023/09/08 12:09:56 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include //intmax_t +#include "libft.h" + +static intmax_t abs_max(intmax_t n) +{ + if (n < 0) + return (-n); + else + return (n); +} + +static int size_needed(intmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + if (n < 0) + ++res; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +char *ft_itoa_base(intmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + if (n < 0) + res[0] = '-'; + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; + while (n != 0) + { + res[--size] = base[abs_max(n % base_len)]; + n /= base_len; + } + return (res); +} diff --git a/Libft/ft_lstadd_back.c b/Libft/ft_lstadd_back.c new file mode 100644 index 0000000..be63a9e --- /dev/null +++ b/Libft/ft_lstadd_back.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:33:28 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 16:52:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + if (*lst == NULL) + { + *lst = new; + return ; + } + ft_lstlast(*lst)->next = new; + return ; +} diff --git a/Libft/ft_lstadd_front.c b/Libft/ft_lstadd_front.c new file mode 100644 index 0000000..4995275 --- /dev/null +++ b/Libft/ft_lstadd_front.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:26:20 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:42:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; + return ; +} diff --git a/Libft/ft_lstclear.c b/Libft/ft_lstclear.c new file mode 100644 index 0000000..14e60d5 --- /dev/null +++ b/Libft/ft_lstclear.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:33:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *temp; + + if (lst == NULL || del == NULL) + return ; + while (*lst) + { + temp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = temp; + } + return ; +} diff --git a/Libft/ft_lstdelone.c b/Libft/ft_lstdelone.c new file mode 100644 index 0000000..b76d40b --- /dev/null +++ b/Libft/ft_lstdelone.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:33:02 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + if (lst == NULL || del == NULL) + return ; + del(lst->content); + free(lst); + return ; +} diff --git a/Libft/ft_lstiter.c b/Libft/ft_lstiter.c new file mode 100644 index 0000000..c188fc0 --- /dev/null +++ b/Libft/ft_lstiter.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:50:31 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:51:48 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->content); + lst = lst->next; + } + return ; +} diff --git a/Libft/ft_lstlast.c b/Libft/ft_lstlast.c new file mode 100644 index 0000000..c544009 --- /dev/null +++ b/Libft/ft_lstlast.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:31:01 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 17:14:03 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (lst); + while (lst->next) + lst = lst->next; + return (lst); +} diff --git a/Libft/ft_lstmap.c b/Libft/ft_lstmap.c new file mode 100644 index 0000000..d6810cc --- /dev/null +++ b/Libft/ft_lstmap.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:51:57 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 16:58:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static t_list *first_el(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + void *cont; + + if (lst == NULL) + return (NULL); + cont = f(lst->content); + res = ft_lstnew(cont); + if (res == NULL) + del(cont); + return (res); +} + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + t_list *new_node; + void *new_cont; + + res = first_el(lst, f, del); + if (res == NULL) + return (res); + new_node = res; + lst = lst->next; + while (lst && res) + { + new_cont = f(lst->content); + new_node->next = ft_lstnew(new_cont); + new_node = new_node->next; + if (new_node == NULL) + { + del(new_cont); + ft_lstclear(&res, del); + } + lst = lst->next; + } + return (res); +} diff --git a/Libft/ft_lstnew.c b/Libft/ft_lstnew.c new file mode 100644 index 0000000..912fdc7 --- /dev/null +++ b/Libft/ft_lstnew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:23:00 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:26:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *res; + + res = malloc(sizeof(t_list)); + if (res == NULL) + return (res); + res->content = content; + res->next = NULL; + return (res); +} diff --git a/Libft/ft_lstsize.c b/Libft/ft_lstsize.c new file mode 100644 index 0000000..cb2dc8f --- /dev/null +++ b/Libft/ft_lstsize.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/16 13:28:58 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 14:59:47 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int res; + + if (lst == NULL) + return (0); + res = 1; + while (lst->next) + { + lst = lst->next; + ++res; + } + return (res); +} diff --git a/Libft/ft_memchr.c b/Libft/ft_memchr.c new file mode 100644 index 0000000..9be9e52 --- /dev/null +++ b/Libft/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:56:53 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:07:39 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if (*((unsigned char *)s + i) == (unsigned char)c) + return ((unsigned char *)s + i); + ++i; + } + return (NULL); +} diff --git a/Libft/ft_memcmp.c b/Libft/ft_memcmp.c new file mode 100644 index 0000000..09dd5a5 --- /dev/null +++ b/Libft/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 11:06:15 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:53:17 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i) + && i + 1 < n) + ++i; + if (n > 0) + return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i)); + else + return (0); +} diff --git a/Libft/ft_memcpy.c b/Libft/ft_memcpy.c new file mode 100644 index 0000000..0ab6249 --- /dev/null +++ b/Libft/ft_memcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:35:59 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:16:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + if (dest == NULL && src == NULL) + return (NULL); + while (n > 0) + { + --n; + *((unsigned char *)dest + n) = *((unsigned char *)src + n); + } + return (dest); +} diff --git a/Libft/ft_memmove.c b/Libft/ft_memmove.c new file mode 100644 index 0000000..aa808ed --- /dev/null +++ b/Libft/ft_memmove.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:36:32 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:17:49 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + size_t i; + + if (dest > src) + { + i = n; + while (i > 0) + { + --i; + *((unsigned char *)dest + i) = *((unsigned char *)src + i); + } + } + else if (dest < src) + { + i = 0; + while (i < n) + { + *((unsigned char *)dest + i) = *((unsigned char *)src + i); + ++i; + } + } + return (dest); +} diff --git a/Libft/ft_memset.c b/Libft/ft_memset.c new file mode 100644 index 0000000..cbc4239 --- /dev/null +++ b/Libft/ft_memset.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:13:22 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 10:39:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + while (n > 0) + *((unsigned char *)s + (--n)) = (unsigned char)c; + return (s); +} diff --git a/Libft/ft_putchar_fd.c b/Libft/ft_putchar_fd.c new file mode 100644 index 0000000..cfa4476 --- /dev/null +++ b/Libft/ft_putchar_fd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:20:18 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); + return ; +} diff --git a/Libft/ft_putendl_fd.c b/Libft/ft_putendl_fd.c new file mode 100644 index 0000000..cb811fa --- /dev/null +++ b/Libft/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:25:41 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:32 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + write(fd, "\n", 1); + return ; +} diff --git a/Libft/ft_putnbr_fd.c b/Libft/ft_putnbr_fd.c new file mode 100644 index 0000000..953510d --- /dev/null +++ b/Libft/ft_putnbr_fd.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:27:25 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 16:38:56 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + char digit; + + if (n < 0) + { + write(fd, "-", 1); + digit = '0' - (n % 10); + if (n / 10 != 0) + ft_putnbr_fd(-(n / 10), fd); + } + else if (n < 10) + digit = '0' + n; + else + { + digit = '0' + n % 10; + ft_putnbr_fd(n / 10, fd); + } + write(fd, &digit, 1); + return ; +} diff --git a/Libft/ft_putstr_fd.c b/Libft/ft_putstr_fd.c new file mode 100644 index 0000000..6a632f3 --- /dev/null +++ b/Libft/ft_putstr_fd.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:29:20 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (s == NULL) + return ; + write(fd, s, ft_strlen(s)); + return ; +} diff --git a/Libft/ft_split.c b/Libft/ft_split.c new file mode 100644 index 0000000..082ebce --- /dev/null +++ b/Libft/ft_split.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 17:53:03 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static void free_str_array(char **arr) +{ + size_t i; + + i = 0; + while (arr[i] != NULL) + free(arr[i++]); + free(arr); + return ; +} + +// Returns 0 when terminating null is encountered, otherwise returns true. +static int to_next_word(const char **s, char c) +{ + while (**s && **s == c) + ++(*s); + return (**s); +} + +static int word_count(const char *s, char c) +{ + int count; + + if (s == NULL) + return (-1); + if (c == '\0') + return (1); + count = 0; + while (*s) + { + while (*s == c) + ++s; + if (*s) + ++count; + while (*s != c && *s) + ++s; + } + return (count); +} + +static size_t strlen_to_char(const char *s, char c) +{ + size_t len; + + len = 0; + while (s[len] != c && s[len]) + ++len; + return (len); +} + +char **ft_split(const char *s, char c) +{ + char **res; + size_t size; + size_t i; + + res = malloc((word_count(s, c) + 1) * sizeof(char *)); + if (res == NULL || s == NULL) + return (res); + i = 0; + while (*s) + { + if (!to_next_word(&s, c)) + break ; + size = (strlen_to_char(s, c) + 1); + res[i] = malloc(size * sizeof(char)); + if (res[i] == NULL) + { + free_str_array(res); + return (NULL); + } + ft_strlcpy(res[i++], s, size); + s += size - 1; + } + res[i] = NULL; + return (res); +} diff --git a/Libft/ft_strchr.c b/Libft/ft_strchr.c new file mode 100644 index 0000000..67dc972 --- /dev/null +++ b/Libft/ft_strchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:41:10 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 17:10:35 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s) + { + if (*s == (char)c) + return ((char *)s); + ++s; + } + if (c == '\0') + return ((char *)s); + return (NULL); +} diff --git a/Libft/ft_strdup.c b/Libft/ft_strdup.c new file mode 100644 index 0000000..1d5d1b6 --- /dev/null +++ b/Libft/ft_strdup.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/17 13:33:49 by ljiriste #+# #+# */ +/* Updated: 2023/09/11 12:37:12 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the malloc function +#include "libft.h" + +char *ft_strdup(const char *s) +{ + char *dest; + size_t s_len; + + if (s == NULL) + return (NULL); + s_len = ft_strlen(s); + dest = malloc((s_len + 1) * sizeof(char)); + if (dest) + ft_strlcpy(dest, s, s_len + 1); + return (dest); +} diff --git a/Libft/ft_striteri.c b/Libft/ft_striteri.c new file mode 100644 index 0000000..bf2c89a --- /dev/null +++ b/Libft/ft_striteri.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:33:39 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char *)) +{ + unsigned int i; + + if (s == NULL || f == NULL) + return ; + i = 0; + while (s[i]) + { + f(i, s + i); + ++i; + } + return ; +} diff --git a/Libft/ft_strjoin.c b/Libft/ft_strjoin.c new file mode 100644 index 0000000..45b8f78 --- /dev/null +++ b/Libft/ft_strjoin.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:33:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strjoin(const char *s1, const char *s2) +{ + char *res; + size_t size; + + if (s1 == NULL || s2 == NULL) + return (NULL); + size = ft_strlen(s1) + ft_strlen(s2) + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + res[0] = '\0'; + ft_strlcat(res, s1, size); + ft_strlcat(res, s2, size); + return (res); +} diff --git a/Libft/ft_strlcat.c b/Libft/ft_strlcat.c new file mode 100644 index 0000000..08395ca --- /dev/null +++ b/Libft/ft_strlcat.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:40:14 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + size_t length; + + if ((dst == NULL && src == NULL) || size == 0) + return (0); + length = 0; + while (*dst && size > length) + { + ++dst; + ++length; + } + while (*src && size > length + 1) + { + *dst = *src; + ++src; + ++dst; + ++length; + } + if (size > length) + *dst = '\0'; + while (*src) + { + ++length; + ++src; + } + return (length); +} diff --git a/Libft/ft_strlcpy.c b/Libft/ft_strlcpy.c new file mode 100644 index 0000000..423e2fd --- /dev/null +++ b/Libft/ft_strlcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/11 17:28:43 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:55:52 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t size) +{ + size_t i; + + i = 0; + while (src[i] && i + 1 < size) + { + dst[i] = src[i]; + ++i; + } + if (size > 0) + dst[i] = '\0'; + while (src[i]) + ++i; + return (i); +} diff --git a/Libft/ft_strlen.c b/Libft/ft_strlen.c new file mode 100644 index 0000000..b27da53 --- /dev/null +++ b/Libft/ft_strlen.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/14 13:40:59 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:05:43 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t len; + + len = 0; + while (s[len] != '\0') + ++len; + return (len); +} diff --git a/Libft/ft_strmapi.c b/Libft/ft_strmapi.c new file mode 100644 index 0000000..614ddb7 --- /dev/null +++ b/Libft/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:32:11 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) +{ + char *res; + unsigned int i; + + if (s == NULL || f == NULL) + return (NULL); + res = malloc((ft_strlen(s) + 1) * sizeof(char)); + if (res == NULL) + return (NULL); + i = 0; + while (s[i]) + { + res[i] = f(i, s[i]); + ++i; + } + res[i] = '\0'; + return (res); +} diff --git a/Libft/ft_strncmp.c b/Libft/ft_strncmp.c new file mode 100644 index 0000000..165753c --- /dev/null +++ b/Libft/ft_strncmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:24:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#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] && s1[i] && s2[i] && i + 1 < n) + ++i; + if (n > 0) + return ((unsigned char)s1[i] - (unsigned char)s2[i]); + else + return (0); +} diff --git a/Libft/ft_strndup.c b/Libft/ft_strndup.c new file mode 100644 index 0000000..cbca646 --- /dev/null +++ b/Libft/ft_strndup.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strndup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/24 09:18:49 by ljiriste #+# #+# */ +/* Updated: 2023/09/27 15:45:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the malloc function +#include "libft.h" + +char *ft_strndup(const char *s, size_t n) +{ + char *dest; + size_t s_len; + + s_len = ft_strlen(s); + if (s_len < n) + n = s_len; + dest = malloc((n + 1) * sizeof(char)); + if (dest == NULL) + return (dest); + ft_strlcpy(dest, s, n + 1); + return (dest); +} diff --git a/Libft/ft_strnstr.c b/Libft/ft_strnstr.c new file mode 100644 index 0000000..b747c19 --- /dev/null +++ b/Libft/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:57:24 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // For the NULL pointer +#include +#include "libft.h" + +char *ft_strnstr(const char *big, const char *little, size_t len) +{ + size_t little_len; + size_t i; + + if (little == NULL && len == 0) + return ((char *)big); + if (*little == '\0') + return ((char *)big); + if (big == NULL && len == 0) + return (NULL); + little_len = ft_strlen(little); + i = 0; + while (big[i] && i + little_len <= len) + { + if (!(ft_strncmp(big + i, little, little_len))) + return ((char *)big + i); + ++i; + } + return (NULL); +} diff --git a/Libft/ft_strrchr.c b/Libft/ft_strrchr.c new file mode 100644 index 0000000..8be5a06 --- /dev/null +++ b/Libft/ft_strrchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:46:09 by ljiriste #+# #+# */ +/* Updated: 2023/08/16 13:16:52 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + char *result; + + result = NULL; + while (*s) + { + if (*s == (char)c) + result = (char *)s; + ++s; + } + if (c == '\0') + return ((char *)s); + return (result); +} diff --git a/Libft/ft_strtrim.c b/Libft/ft_strtrim.c new file mode 100644 index 0000000..6fbdb92 --- /dev/null +++ b/Libft/ft_strtrim.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */ +/* Updated: 2023/09/06 12:58:04 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "libft.h" + +static size_t size_needed(const char *s1, const char *set) +{ + size_t at_start; + size_t at_end; + size_t whole; + + whole = ft_strlen(s1); + at_start = 0; + at_end = 0; + while (ft_strchr(set, *s1) && *s1) + { + ++s1; + ++at_start; + } + if (*s1 == '\0') + return (1); + while (*s1) + ++s1; + --s1; + while (ft_strchr(set, *s1)) + { + --s1; + ++at_end; + } + return (whole - at_start - at_end + 1); +} + +char *ft_strtrim(const char *s1, const char *set) +{ + size_t size; + char *res; + + if (s1 == NULL) + return (NULL); + size = size_needed(s1, set); + res = malloc(size * sizeof(char)); + if (res == NULL) + return (res); + while (ft_strchr(set, *s1) && *s1) + ++s1; + ft_strlcpy(res, s1, size); + return (res); +} diff --git a/Libft/ft_substr.c b/Libft/ft_substr.c new file mode 100644 index 0000000..a80cb6e --- /dev/null +++ b/Libft/ft_substr.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 18:27:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +char *ft_substr(const char *s, unsigned int start, size_t len) +{ + char *res; + size_t size; + + if (s == NULL) + return (NULL); + size = ft_strlen(s) + 1; + if (start > size) + { + res = malloc(1 * sizeof(char)); + if (res == NULL) + return (NULL); + *res = '\0'; + return (res); + } + else + size -= start; + if (size > len + 1) + size = len + 1; + res = malloc(size * sizeof(char)); + if (res == NULL) + return (NULL); + ft_strlcpy(res, s + start, size); + return (res); +} diff --git a/Libft/ft_tolower.c b/Libft/ft_tolower.c new file mode 100644 index 0000000..5e169b0 --- /dev/null +++ b/Libft/ft_tolower.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:36:54 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:53:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_tolower(int c) +{ + if (ft_isupper(c)) + c = c + 'a' - 'A'; + return (c); +} diff --git a/Libft/ft_toupper.c b/Libft/ft_toupper.c new file mode 100644 index 0000000..1e79b9d --- /dev/null +++ b/Libft/ft_toupper.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 10:35:21 by ljiriste #+# #+# */ +/* Updated: 2023/08/15 12:54:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_toupper(int c) +{ + if (ft_islower(c)) + c = c + 'A' - 'a'; + return (c); +} diff --git a/Libft/ft_uitoa_base.c b/Libft/ft_uitoa_base.c new file mode 100644 index 0000000..3d446c2 --- /dev/null +++ b/Libft/ft_uitoa_base.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_uitoa_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ +/* Updated: 2023/09/15 18:43:25 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include //intmax_t +#include "libft.h" + +static int size_needed(uintmax_t n, size_t base_len) +{ + int res; + + if (n == 0) + return (2); + res = 1; + while (n != 0) + { + ++res; + n /= base_len; + } + return (res); +} + +static int valid_base(const char *base) +{ + int i; + int j; + + if (ft_strlen(base) < 2) + return (0); + i = 0; + while (base[i]) + { + j = i + 1; + while (base[j]) + if (base[i] == base[j++]) + return (0); + ++i; + } + return (1); +} + +char *ft_uitoa_base(uintmax_t n, const char *base) +{ + int size; + int base_len; + char *res; + + if (!valid_base(base)) + return (NULL); + base_len = ft_strlen(base); + size = size_needed(n, base_len); + res = malloc(size); + if (res == NULL) + return (res); + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; + while (n != 0) + { + res[--size] = base[n % base_len]; + n /= base_len; + } + return (res); +} diff --git a/Libft/libft.h b/Libft/libft.h new file mode 100644 index 0000000..2a9f9ec --- /dev/null +++ b/Libft/libft.h @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ +/* Updated: 2023/09/15 18:43:23 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include +# include + +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_islower(int c); +int ft_isprint(int c); +int ft_isspace(int c); +int ft_isupper(int c); +int ft_isascii(int c); + +int ft_abs(int n); + +void *ft_memset(void *s, int c, 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); +void *ft_memchr(const void *s, int c, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +void ft_bzero(void *s, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); + +size_t ft_strlen(const char *s); +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_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strdup(const char *s); +char *ft_strndup(const char *s, size_t n); + +int ft_toupper(int c); +int ft_tolower(int c); + +int ft_atoi(const char *nptr); + +char *ft_substr(const char *s, unsigned int start, size_t len); +char *ft_strjoin(const char *s1, const char *s2); +char *ft_strtrim(const char *s1, const char *set); +char **ft_split(const char *s, char c); + +char *ft_itoa(int n); +char *ft_itoa_base(intmax_t n, const char *base); +char *ft_uitoa_base(uintmax_t n, const char *base); +char *ft_ctoa(char c); + +char *ft_strmapi(const char *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); + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +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 diff --git a/Makefile b/Makefile index f2a4059..3de76a1 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,10 @@ $(NAME) : $(OBJECTS) Libft/libft.a cp Libft/libft.a $(NAME) $(AR) rcs $(NAME) $(OBJECTS) -Libft/libft.a : Libft/libft.h +Libft/libft.a : $(MAKE) -C ./Libft -%.o : %.c Libft/libft.h +%.o : %.c $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) clean : @@ -36,10 +36,4 @@ fclean : re : fclean $(MAKE) all -Libft/libft.h : - git submodule update --init Libft - git -C ./Libft checkout no_crash - bonus : all - -.PHONY : Libft