From c947e43e515d6af1b0f0759daceeffde69b49300 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 31 Oct 2024 17:24:36 +0100 Subject: [PATCH] Adding the libft --- Makefile | 18 ++-- includes/cub3d.h | 3 +- includes/libft | 1 + libft/.clangd | 4 + libft/Makefile | 170 +++++++++++++++++++++++++++++++++ libft/char/ft_isalnum.c | 26 +++++ libft/char/ft_isalpha.c | 26 +++++ libft/char/ft_isascii.c | 28 ++++++ libft/char/ft_isdigit.c | 26 +++++ libft/char/ft_isprint.c | 26 +++++ libft/char/ft_tolower.c | 26 +++++ libft/char/ft_toupper.c | 26 +++++ libft/convert/ft_atoi.c | 60 ++++++++++++ libft/convert/ft_atoll.c | 60 ++++++++++++ libft/convert/ft_itoa.c | 58 +++++++++++ libft/includes/libft/char.h | 31 ++++++ libft/includes/libft/convert.h | 27 ++++++ libft/includes/libft/libft.h | 23 +++++ libft/includes/libft/list.h | 39 ++++++++ libft/includes/libft/memory.h | 31 ++++++ libft/includes/libft/print.h | 31 ++++++ libft/includes/libft/str.h | 40 ++++++++ libft/list/ft_lstadd_back.c | 28 ++++++ libft/list/ft_lstadd_front.c | 24 +++++ libft/list/ft_lstclear.c | 25 +++++ libft/list/ft_lstdelone.c | 19 ++++ libft/list/ft_lstiter.c | 22 +++++ libft/list/ft_lstlast.c | 22 +++++ libft/list/ft_lstmap.c | 37 +++++++ libft/list/ft_lstnew.c | 26 +++++ libft/list/ft_lstsize.c | 26 +++++ libft/memory/ft_bzero.c | 18 ++++ libft/memory/ft_calloc.c | 31 ++++++ libft/memory/ft_memchr.c | 29 ++++++ libft/memory/ft_memcmp.c | 31 ++++++ libft/memory/ft_memcpy.c | 32 +++++++ libft/memory/ft_memmove.c | 51 ++++++++++ libft/memory/ft_memset.c | 28 ++++++ libft/print/ft_printf.c | 96 +++++++++++++++++++ libft/print/ft_put.c | 97 +++++++++++++++++++ libft/print/ft_putchar_fd.c | 18 ++++ libft/print/ft_putendl_fd.c | 19 ++++ libft/print/ft_putnbr_fd.c | 55 +++++++++++ libft/print/ft_putstr_fd.c | 18 ++++ libft/str/ft_split.c | 93 ++++++++++++++++++ libft/str/ft_strchr.c | 29 ++++++ libft/str/ft_strcmp.c | 31 ++++++ libft/str/ft_strcpy.c | 27 ++++++ libft/str/ft_strdup.c | 26 +++++ libft/str/ft_striteri.c | 25 +++++ libft/str/ft_strjoin.c | 39 ++++++++ libft/str/ft_strlcat.c | 49 ++++++++++ libft/str/ft_strlcpy.c | 31 ++++++ libft/str/ft_strlen.c | 23 +++++ libft/str/ft_strmapi.c | 34 +++++++ libft/str/ft_strncmp.c | 31 ++++++ libft/str/ft_strnstr.c | 37 +++++++ libft/str/ft_strrchr.c | 29 ++++++ libft/str/ft_strtrim.c | 40 ++++++++ libft/str/ft_substr.c | 28 ++++++ parsing/arguments.c | 8 +- sources/main.c | 5 +- 62 files changed, 2098 insertions(+), 19 deletions(-) create mode 120000 includes/libft create mode 100644 libft/.clangd create mode 100644 libft/Makefile create mode 100644 libft/char/ft_isalnum.c create mode 100644 libft/char/ft_isalpha.c create mode 100644 libft/char/ft_isascii.c create mode 100644 libft/char/ft_isdigit.c create mode 100644 libft/char/ft_isprint.c create mode 100644 libft/char/ft_tolower.c create mode 100644 libft/char/ft_toupper.c create mode 100644 libft/convert/ft_atoi.c create mode 100644 libft/convert/ft_atoll.c create mode 100644 libft/convert/ft_itoa.c create mode 100644 libft/includes/libft/char.h create mode 100644 libft/includes/libft/convert.h create mode 100644 libft/includes/libft/libft.h create mode 100644 libft/includes/libft/list.h create mode 100644 libft/includes/libft/memory.h create mode 100644 libft/includes/libft/print.h create mode 100644 libft/includes/libft/str.h create mode 100644 libft/list/ft_lstadd_back.c create mode 100644 libft/list/ft_lstadd_front.c create mode 100644 libft/list/ft_lstclear.c create mode 100644 libft/list/ft_lstdelone.c create mode 100644 libft/list/ft_lstiter.c create mode 100644 libft/list/ft_lstlast.c create mode 100644 libft/list/ft_lstmap.c create mode 100644 libft/list/ft_lstnew.c create mode 100644 libft/list/ft_lstsize.c create mode 100644 libft/memory/ft_bzero.c create mode 100644 libft/memory/ft_calloc.c create mode 100644 libft/memory/ft_memchr.c create mode 100644 libft/memory/ft_memcmp.c create mode 100644 libft/memory/ft_memcpy.c create mode 100644 libft/memory/ft_memmove.c create mode 100644 libft/memory/ft_memset.c create mode 100644 libft/print/ft_printf.c create mode 100644 libft/print/ft_put.c create mode 100644 libft/print/ft_putchar_fd.c create mode 100644 libft/print/ft_putendl_fd.c create mode 100644 libft/print/ft_putnbr_fd.c create mode 100644 libft/print/ft_putstr_fd.c create mode 100644 libft/str/ft_split.c create mode 100644 libft/str/ft_strchr.c create mode 100644 libft/str/ft_strcmp.c create mode 100644 libft/str/ft_strcpy.c create mode 100644 libft/str/ft_strdup.c create mode 100644 libft/str/ft_striteri.c create mode 100644 libft/str/ft_strjoin.c create mode 100644 libft/str/ft_strlcat.c create mode 100644 libft/str/ft_strlcpy.c create mode 100644 libft/str/ft_strlen.c create mode 100644 libft/str/ft_strmapi.c create mode 100644 libft/str/ft_strncmp.c create mode 100644 libft/str/ft_strnstr.c create mode 100644 libft/str/ft_strrchr.c create mode 100644 libft/str/ft_strtrim.c create mode 100644 libft/str/ft_substr.c diff --git a/Makefile b/Makefile index 884e77a..83a8af3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/10/31 11:16:34 by rparodi ### ########.fr # +# Updated: 2024/10/31 17:19:01 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -15,6 +15,7 @@ # Name NAME = Cub3D +NAME_BONUS = Cub3D_bonus # Commands CC = cc @@ -28,14 +29,14 @@ CFLAGS += -g3 -MMD # CFLAGS += -fsanitize=address # CFLAGS += -fsanitize=thread -INCLUDES = ./includes/ +INCLUDES = -I ./includes/ -I ./includes/libft/ SRC = sources/main.c \ sources/error.c \ parsing/arguments.c # Objects -OBJDIRNAME = ./objects +OBJDIRNAME = ./build OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o)) # Colors @@ -53,10 +54,8 @@ all: header $(NAME) footer # Bonus (make bonus) bonus: header $(OBJ) $(LIB_OBJ) footer @mkdir -p $(OBJDIRNAME) - @mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME) @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' - @printf '$(GREY) Be Carefull ur in $(END)$(GREEN)Debug Mode$(END)\n' - @cc $(CFLAGS) -D BONUS=1 -o $(NAME) $(OBJ) $(LIB_OBJ) + @cc $(CFLAGS) -D BONUS=1 -o $(NAME_BONUS) $(OBJ) $(LIB_OBJ) # Clean (make clean) clean: @@ -68,6 +67,7 @@ clean: fclean: clean @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' @$(RM) $(NAME) + @$(RM) $(NAME_BONUS) @echo "" # Restart (make re) @@ -75,16 +75,16 @@ re: header fclean all # Dependences for all $(NAME): $(OBJ) + @make --no-print-directory -C ./libft lib @mkdir -p $(OBJDIRNAME) - @mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME) @printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n' - @cc $(CFLAGS) -o $(NAME) $(OBJ) + @cc $(CFLAGS) ./build/libft.a -o $(NAME) $(OBJ) # Creating the objects $(OBJDIRNAME)/%.o: %.c @mkdir -p $(dir $@) @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' - @cc $(CFLAGS) -o $@ -c $< -I$(INCLUDES) + @cc $(CFLAGS)-o $@ -c $< $(INCLUDES) # Header header: diff --git a/includes/cub3d.h b/includes/cub3d.h index c2d5320..94cf8bc 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ -/* Updated: 2024/10/31 12:15:18 by rparodi ### ########.fr */ +/* Updated: 2024/10/31 16:54:38 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ # include # include +# include "libft.h" # include "message_error.h" # ifndef BONUS diff --git a/includes/libft b/includes/libft new file mode 120000 index 0000000..21c6d09 --- /dev/null +++ b/includes/libft @@ -0,0 +1 @@ +../libft/includes/libft \ No newline at end of file diff --git a/libft/.clangd b/libft/.clangd new file mode 100644 index 0000000..5f65c4b --- /dev/null +++ b/libft/.clangd @@ -0,0 +1,4 @@ +CompilerFlags: + Add: + - "-xc" + - "-I/Users/raphael/Documents/42_cursus/circle0/libft/includes" diff --git a/libft/Makefile b/libft/Makefile new file mode 100644 index 0000000..7202ef2 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,170 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: rparodi +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # +# Updated: 2024/10/31 17:19:36 by rparodi ### ########.fr # +# # +# **************************************************************************** # + +# Variables + +# Name +NAME=../build/libft.a + +# Commands +CC = cc +RM = rm -rf + +# Flags +CFLAGS = -Werror -Wextra -Wall +CFLAGS += -g3 -MMD +# CFLAGS += -lm + +# CFLAGS += -fsanitize=address +# CFLAGS += -fsanitize=thread + +LDFLAGS = -L. +LDLIBS = -lft + +INCLUDES = ./includes/libft/ + +SRC = char/ft_isdigit.c \ + char/ft_isalnum.c \ + char/ft_isalpha.c \ + char/ft_isascii.c \ + char/ft_isprint.c \ + char/ft_tolower.c \ + char/ft_toupper.c \ + convert/ft_atoi.c \ + convert/ft_atoll.c \ + convert/ft_itoa.c \ + list/ft_lstadd_back.c \ + list/ft_lstadd_front.c \ + list/ft_lstclear.c \ + list/ft_lstdelone.c \ + list/ft_lstiter.c \ + list/ft_lstlast.c \ + list/ft_lstmap.c \ + list/ft_lstnew.c \ + list/ft_lstsize.c \ + memory/ft_bzero.c \ + memory/ft_calloc.c \ + memory/ft_memchr.c \ + memory/ft_memcmp.c \ + memory/ft_memcpy.c \ + memory/ft_memmove.c \ + memory/ft_memset.c \ + print/ft_printf.c \ + print/ft_put.c \ + print/ft_putchar_fd.c \ + print/ft_putendl_fd.c \ + print/ft_putnbr_fd.c \ + print/ft_putstr_fd.c \ + str/ft_split.c \ + str/ft_strchr.c \ + str/ft_strcmp.c \ + str/ft_strcpy.c \ + str/ft_strdup.c \ + str/ft_striteri.c \ + str/ft_strjoin.c \ + str/ft_strlcat.c \ + str/ft_strlcpy.c \ + str/ft_strlen.c \ + str/ft_strmapi.c \ + str/ft_strncmp.c \ + str/ft_strnstr.c \ + str/ft_strrchr.c \ + str/ft_strtrim.c \ + str/ft_substr.c + +# Objects +OBJDIRNAME = ../build +OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o)) + +# Colors +GREEN = \033[32m +GREY = \033[0;90m +RED = \033[0;31m +GOLD = \033[38;5;220m +END = \033[0m + +# Rules + +# All (make all) +all: header $(NAME) footer + +lib: $(NAME) + +# Bonus (make bonus) +bonus: header $(OBJ) $(LIB_OBJ) footer + @mkdir -p $(OBJDIRNAME) + @mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME) + @printf '$(GREY) Be Carefull ur in $(END)$(GREEN)Debug Mode$(END)\n' + @cc $(CFLAGS) -D BONUS=1 -o $(NAME) $(OBJ) $(LIB_OBJ) + +# Clean (make clean) +clean: + @printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n' + @printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n' + @$(RM) $(OBJDIRNAME) + +# Clean (make fclean) +fclean: clean + @printf '$(GREY) Removing $(END)$(RED)Program$(END)\n' + @$(RM) $(NAME) + @echo "" + +# Restart (make re) +re: header fclean all + +# Dependences for all +$(NAME): $(OBJ) + @mkdir -p $(OBJDIRNAME) + @ar rc $(NAME) $(OBJ) + @ranlib $(NAME) + +# Creating the objects +$(OBJDIRNAME)/%.o: %.c + @mkdir -p $(dir $@) + @printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n' + @cc $(CFLAGS) -o $@ -c $< -I$(INCLUDES) + +# Header +header: + @clear + @printf '\n\n' + @printf '$(GOLD) ******* ****** ******* $(END)\n' + @printf '$(GOLD) ****** *** ******* $(END)\n' + @printf '$(GOLD) ******* * ******* $(END)\n' + @printf '$(GOLD) ****** ******* $(END)\n' + @printf '$(GOLD) ******* ******* $(END)\n' + @printf '$(GOLD) ******************* ******* * $(END)\n' + @printf '$(GOLD) ******************* ******* *** $(END)\n' + @printf '$(GOLD) ****** ******* ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GOLD) ****** $(END)\n' + @printf '$(GREY) Made by rparodi$(END)\n\n' + +# Footer +footer: + @printf "\n" + @printf "$(GOLD) ,_ _,$(END)\n" + @printf "$(GOLD) | \\___//|$(END)\n" + @printf "$(GOLD) |=6 6=|$(END)\n" + @printf "$(GOLD) \\=._Y_.=/$(END)\n" + @printf "$(GOLD) ) \` ( ,$(END)\n" + @printf "$(GOLD) / \\ (('$(END)\n" + @printf "$(GOLD) | | ))$(END)\n" + @printf "$(GOLD) /| | | |\\_//$(END)\n" + @printf "$(GOLD) \\| |._.| |/-\`$(END)\n" + @printf "$(GOLD) '\"' '\"'$(END)\n" + @printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n' + +# Phony +.PHONY: all bonus clean fclean re + +-include ${OBJ:.o=.d} diff --git a/libft/char/ft_isalnum.c b/libft/char/ft_isalnum.c new file mode 100644 index 0000000..6898862 --- /dev/null +++ b/libft/char/ft_isalnum.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:47:28 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:54:42 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief Check if the character is alpha-numeric + * + * @param c the character + * @return the character if alphanumeric or 0 if not + */ +int ft_isalnum(int c) +{ + if (ft_isalpha(c) || ft_isdigit(c)) + return (c); + return (0); +} diff --git a/libft/char/ft_isalpha.c b/libft/char/ft_isalpha.c new file mode 100644 index 0000000..8f93d59 --- /dev/null +++ b/libft/char/ft_isalpha.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:58:37 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:54:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief Check if the character is alpha + * + * @param c the character + * @return the character if alpha or 0 if not + */ +int ft_isalpha(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) + return (c); + return (0); +} diff --git a/libft/char/ft_isascii.c b/libft/char/ft_isascii.c new file mode 100644 index 0000000..942e780 --- /dev/null +++ b/libft/char/ft_isascii.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:04:26 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:49:55 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief Check if the character is in the ascii table + * + * @param c the character + * @return the character if in the ascii table or 0 if not + */ +int ft_isascii(int c) +{ + if (c == 0) + return (1); + if (c > 0 && c <= 127) + return (c); + return (0); +} diff --git a/libft/char/ft_isdigit.c b/libft/char/ft_isdigit.c new file mode 100644 index 0000000..ac3315e --- /dev/null +++ b/libft/char/ft_isdigit.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 12:44:28 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:49:05 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief Check if the character is alpha numeric + * + * @param c the character + * @return the character if numeric or 0 if not + */ +int ft_isdigit(int c) +{ + if (c >= '0' && c <= '9') + return (c); + return (0); +} diff --git a/libft/char/ft_isprint.c b/libft/char/ft_isprint.c new file mode 100644 index 0000000..d77c82a --- /dev/null +++ b/libft/char/ft_isprint.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 14:06:53 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:50:37 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief Check if the character is printable + * + * @param c the character + * @return the character if can be print or 0 if not + */ +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (c); + return (0); +} diff --git a/libft/char/ft_tolower.c b/libft/char/ft_tolower.c new file mode 100644 index 0000000..5130815 --- /dev/null +++ b/libft/char/ft_tolower.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:38:54 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:55:28 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief convert the upper case to lower case + * + * @param c the character + * @return the character to lower case if is on upper + */ +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + c += 32; + return (c); +} diff --git a/libft/char/ft_toupper.c b/libft/char/ft_toupper.c new file mode 100644 index 0000000..d3c01b4 --- /dev/null +++ b/libft/char/ft_toupper.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 10:44:26 by rparodi #+# #+# */ +/* Updated: 2024/10/31 12:55:14 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/** + * @brief convert the lower case to upper case + * + * @param c the character + * @return the character to upper case if is on lower + */ +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + c -= 32; + return (c); +} diff --git a/libft/convert/ft_atoi.c b/libft/convert/ft_atoi.c new file mode 100644 index 0000000..d4830ec --- /dev/null +++ b/libft/convert/ft_atoi.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:16:10 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_check_space(int c) +{ + if (c == 32 || (c >= 9 && c <= 13)) + return (1); + return (0); +} + +static int ft_check_sign(const char *nptr, int *i) +{ + while (ft_check_space(nptr[*i])) + (*i)++; + if (nptr[*i] == '-') + { + (*i)++; + return (-1); + } + else if (nptr[*i] == '+') + (*i)++; + return (1); +} + +/** + * @brief Converts string to integer + * + * @param nptr the string that will be converted + * @return The integer on the string + */ +int ft_atoi(const char *nptr) +{ + int i; + int sign; + int number; + + i = 0; + sign = ft_check_sign(nptr, &i); + number = 0; + while (nptr[i]) + { + if (nptr[i] >= '0' && nptr[i] <= '9') + number = (number * 10) + nptr[i] - '0'; + else + break ; + i++; + } + return (sign * number); +} diff --git a/libft/convert/ft_atoll.c b/libft/convert/ft_atoll.c new file mode 100644 index 0000000..414010e --- /dev/null +++ b/libft/convert/ft_atoll.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoll.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 15:12:07 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:14:50 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_check_space(int c) +{ + if (c == 32 || (c >= 9 && c <= 13)) + return (1); + return (0); +} + +static int ft_check_sign(const char *nptr, size_t *i) +{ + while (ft_check_space(nptr[*i])) + (*i)++; + if (nptr[*i] == '-') + { + (*i)++; + return (-1); + } + else if (nptr[*i] == '+') + (*i)++; + return (1); +} + +/** + * @brief Converts string to long long integer + * + * @param nptr the string that will be converted + * @return The long long int on the string + */ +long long int ft_atoll(const char *nptr) +{ + size_t i; + int sign; + long long int number; + + i = 0; + sign = ft_check_sign(nptr, &i); + number = 0; + while (nptr[i]) + { + if (nptr[i] >= '0' && nptr[i] <= '9') + number = (number * 10) + nptr[i] - '0'; + else + break ; + i++; + } + return (sign * number); +} diff --git a/libft/convert/ft_itoa.c b/libft/convert/ft_itoa.c new file mode 100644 index 0000000..6104fe3 --- /dev/null +++ b/libft/convert/ft_itoa.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:30 by rparodi #+# #+# */ +/* Updated: 2024/10/31 13:02:55 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t ft_check_sign(char *str, long *nb) +{ + if ((*nb) == 0) + str[0] = '0'; + if ((*nb) < 0) + { + str[0] = '-'; + *nb = -*nb; + return (1); + } + return (0); +} + +/** + * @brief Converts integer to string + * + * @param n the integer that will be converted + * @return The string with this integer + */ +char *ft_itoa(int n) +{ + size_t i; + size_t modulus; + short int maiboyerlpb; + char str[13]; + long nb; + + nb = n; + maiboyerlpb = 0; + modulus = 1000000000; + ft_bzero(str, 13); + i = ft_check_sign(str, &nb); + while (modulus != 0) + { + if (nb / modulus != 0 || maiboyerlpb != 0) + { + str[i++] = (nb / modulus) + 48; + maiboyerlpb++; + } + nb %= modulus; + modulus /= 10; + } + return (ft_strdup(str)); +} diff --git a/libft/includes/libft/char.h b/libft/includes/libft/char.h new file mode 100644 index 0000000..c967dbe --- /dev/null +++ b/libft/includes/libft/char.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* char.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 14:54:04 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:22:26 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CHAR_H +# define CHAR_H + +# include +# include +# include +# include +# include +# include + +int ft_isalnum(int c); +int ft_isalpha(int c); +int ft_isascii(int c); +int ft_isdigit(int c); +int ft_isprint(int c); +int ft_tolower(int c); +int ft_toupper(int c); + +#endif diff --git a/libft/includes/libft/convert.h b/libft/includes/libft/convert.h new file mode 100644 index 0000000..9996986 --- /dev/null +++ b/libft/includes/libft/convert.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* convert.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 14:57:24 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:27:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef CONVERT_H +# define CONVERT_H + +# include +# include +# include +# include +# include +# include + +char *ft_itoa(int n); +int ft_atoi(const char *nptr); +long long int ft_atoll(const char *nptr); + +#endif diff --git a/libft/includes/libft/libft.h b/libft/includes/libft/libft.h new file mode 100644 index 0000000..d3488b2 --- /dev/null +++ b/libft/includes/libft/libft.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/06 11:14:57 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:27:14 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H + +# include "char.h" +# include "convert.h" +# include "list.h" +# include "memory.h" +# include "print.h" +# include "str.h" + +#endif diff --git a/libft/includes/libft/list.h b/libft/includes/libft/list.h new file mode 100644 index 0000000..75167b0 --- /dev/null +++ b/libft/includes/libft/list.h @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 15:00:12 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:27:20 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIST_H +# define LIST_H + +# include +# include +# include +# include +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +t_list *ft_lstnew(void *content); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstadd_front(t_list **lst, t_list *new); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); + +#endif diff --git a/libft/includes/libft/memory.h b/libft/includes/libft/memory.h new file mode 100644 index 0000000..aaac8e5 --- /dev/null +++ b/libft/includes/libft/memory.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* memory.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 15:18:17 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:21:00 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MEMORY_H +# define MEMORY_H + +# include +# include +# include +# include +# include +# include + +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_calloc(size_t nmemb, size_t size); +void *ft_memchr(const 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_memset(void *s, int c, size_t n); +void ft_bzero(void *s, size_t n); + +#endif diff --git a/libft/includes/libft/print.h b/libft/includes/libft/print.h new file mode 100644 index 0000000..1cc0bcd --- /dev/null +++ b/libft/includes/libft/print.h @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 15:07:30 by rparodi #+# #+# */ +/* Updated: 2024/10/31 15:41:54 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PRINT_H +# define PRINT_H + +# include +# include +# include +# include +# include +# include +# include + +int ft_dprintf(int fd, const char *s, ...); +int ft_printf(const char *s, ...); +void ft_putchar_fd(char c, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +void ft_putstr_fd(char *s, int fd); + +#endif diff --git a/libft/includes/libft/str.h b/libft/includes/libft/str.h new file mode 100644 index 0000000..cfaf4ca --- /dev/null +++ b/libft/includes/libft/str.h @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* str.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 15:04:59 by rparodi #+# #+# */ +/* Updated: 2024/10/31 17:06:53 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STR_H +# define STR_H + +# include +# include +# include +# include +# include +# include + +char **ft_split(char const *s, char c); +char *ft_strchr(const char *s, int c); +char *ft_strcpy(char *dst, const char *src); +char *ft_strdup(const char *s); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +char *ft_strnstr(const char *big, const char *little, size_t len); +char *ft_strrchr(const char *s, int c); +char *ft_strtrim(char const *s1, char const *set); +char *ft_substr(char const *s, unsigned int start, size_t len); +int ft_strcmp(const char *s1, const char *s2); +int ft_strncmp(const char *s1, const char *s2, size_t n); +size_t ft_strlcat(char *dst, const char *src, size_t size); +size_t ft_strlcpy(char *dst, const char *src, size_t size); +size_t ft_strlen(const char *s); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); + +#endif diff --git a/libft/list/ft_lstadd_back.c b/libft/list/ft_lstadd_back.c new file mode 100644 index 0000000..3b7be38 --- /dev/null +++ b/libft/list/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:39:56 by rparodi #+# #+# */ +/* Updated: 2023/11/12 17:11:53 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tempo; + + if (*lst == NULL) + { + *lst = new; + return ; + } + tempo = *lst; + while (tempo->next != NULL) + tempo = tempo->next; + tempo->next = new; +} diff --git a/libft/list/ft_lstadd_front.c b/libft/list/ft_lstadd_front.c new file mode 100644 index 0000000..3e3794e --- /dev/null +++ b/libft/list/ft_lstadd_front.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:40:16 by rparodi #+# #+# */ +/* Updated: 2023/11/12 17:14:33 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + if (*lst == NULL) + { + *lst = new; + return ; + } + new->next = *lst; + *lst = new; +} diff --git a/libft/list/ft_lstclear.c b/libft/list/ft_lstclear.c new file mode 100644 index 0000000..3cf5595 --- /dev/null +++ b/libft/list/ft_lstclear.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:40:37 by rparodi #+# #+# */ +/* Updated: 2023/11/12 19:34:19 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void *)) +{ + t_list *tempo; + + while (*lst) + { + tempo = (*lst)->next; + ft_lstdelone(*lst, del); + (*lst) = tempo; + } +} diff --git a/libft/list/ft_lstdelone.c b/libft/list/ft_lstdelone.c new file mode 100644 index 0000000..bffd7cf --- /dev/null +++ b/libft/list/ft_lstdelone.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:42:11 by rparodi #+# #+# */ +/* Updated: 2023/11/12 18:10:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + del(lst->content); + free(lst); +} diff --git a/libft/list/ft_lstiter.c b/libft/list/ft_lstiter.c new file mode 100644 index 0000000..30547eb --- /dev/null +++ b/libft/list/ft_lstiter.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:42:24 by rparodi #+# #+# */ +/* Updated: 2023/11/12 18:07:02 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst != NULL) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/list/ft_lstlast.c b/libft/list/ft_lstlast.c new file mode 100644 index 0000000..d0b6b53 --- /dev/null +++ b/libft/list/ft_lstlast.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:42:54 by rparodi #+# #+# */ +/* Updated: 2023/11/12 17:25:48 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + if (lst == NULL) + return (lst); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/libft/list/ft_lstmap.c b/libft/list/ft_lstmap.c new file mode 100644 index 0000000..6282a67 --- /dev/null +++ b/libft/list/ft_lstmap.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:43:28 by rparodi #+# #+# */ +/* Updated: 2023/11/12 19:28:02 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *tempo; + t_list *tempo_content; + + tempo_content = ft_lstnew(f(lst->content)); + if (!tempo_content) + return (NULL); + tempo = tempo_content; + lst = lst->next; + while (lst) + { + tempo_content = ft_lstnew(f(lst->content)); + if (!tempo_content) + { + ft_lstclear(&tempo, del); + return (NULL); + } + ft_lstadd_back(&tempo, tempo_content); + lst = lst->next; + } + return (tempo); +} diff --git a/libft/list/ft_lstnew.c b/libft/list/ft_lstnew.c new file mode 100644 index 0000000..6d2a4fc --- /dev/null +++ b/libft/list/ft_lstnew.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:44:02 by rparodi #+# #+# */ +/* Updated: 2023/11/12 14:25:12 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *to_return; + + to_return = NULL; + to_return = (t_list *)malloc(sizeof(t_list)); + if (!to_return) + return (NULL); + to_return->content = content; + to_return->next = NULL; + return (to_return); +} diff --git a/libft/list/ft_lstsize.c b/libft/list/ft_lstsize.c new file mode 100644 index 0000000..99d1d45 --- /dev/null +++ b/libft/list/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/12 11:44:24 by rparodi #+# #+# */ +/* Updated: 2023/11/13 12:22:02 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int count; + + count = 0; + while (lst != NULL) + { + lst = lst->next; + count++; + } + return (count); +} diff --git a/libft/memory/ft_bzero.c b/libft/memory/ft_bzero.c new file mode 100644 index 0000000..5b8fb1e --- /dev/null +++ b/libft/memory/ft_bzero.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:43:13 by rparodi #+# #+# */ +/* Updated: 2023/11/07 17:25:28 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + ft_memset(s, 0, n); +} diff --git a/libft/memory/ft_calloc.c b/libft/memory/ft_calloc.c new file mode 100644 index 0000000..a057429 --- /dev/null +++ b/libft/memory/ft_calloc.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:47:17 by rparodi #+# #+# */ +/* Updated: 2023/11/13 14:47:17 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t nmemb, size_t size) +{ + size_t total; + char *to_return; + + if (nmemb == 0 || size == 0) + return ((void *)malloc(1)); + total = nmemb * size; + if (total / nmemb != size && total / size != nmemb) + return (NULL); + to_return = (char *)malloc(total); + if (to_return == NULL) + to_return = NULL; + else + ft_bzero(to_return, total); + return (to_return); +} diff --git a/libft/memory/ft_memchr.c b/libft/memory/ft_memchr.c new file mode 100644 index 0000000..ec3f19c --- /dev/null +++ b/libft/memory/ft_memchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:48:30 by rparodi #+# #+# */ +/* Updated: 2023/11/07 18:45:57 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + size_t i; + char *str; + + i = 0; + str = (char *)s; + while (i < n) + { + if ((char)c == str[i]) + return ((void *)str + i); + i++; + } + return (NULL); +} diff --git a/libft/memory/ft_memcmp.c b/libft/memory/ft_memcmp.c new file mode 100644 index 0000000..d1a3ba5 --- /dev/null +++ b/libft/memory/ft_memcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:49:04 by rparodi #+# #+# */ +/* Updated: 2023/11/07 18:46:15 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + unsigned char *str1; + unsigned char *str2; + + i = 0; + str1 = (unsigned char *)s1; + str2 = (unsigned char *)s2; + while (i < n) + { + if (str1[i] != str2[i]) + return (str1[i] - str2[i]); + i++; + } + return (0); +} diff --git a/libft/memory/ft_memcpy.c b/libft/memory/ft_memcpy.c new file mode 100644 index 0000000..2715322 --- /dev/null +++ b/libft/memory/ft_memcpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:49:46 by rparodi #+# #+# */ +/* Updated: 2023/11/13 19:14:23 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + char *d; + const char *s; + size_t i; + + i = 0; + if (!dest && !src) + return (NULL); + d = (char *)dest; + s = (char *)src; + while (i < n) + { + d[i] = s[i]; + i++; + } + return ((void *)d); +} diff --git a/libft/memory/ft_memmove.c b/libft/memory/ft_memmove.c new file mode 100644 index 0000000..986a36f --- /dev/null +++ b/libft/memory/ft_memmove.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:51:35 by rparodi #+# #+# */ +/* Updated: 2023/11/13 19:55:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_checks(char *s, char *d, size_t n) +{ + size_t i; + + i = 0; + if (d < s) + { + while (i < n) + { + d[i] = s[i]; + i++; + } + } + else + { + i = n; + while (i > 0) + { + i--; + d[i] = s[i]; + } + } + return (d); +} + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + char *d; + char *s; + + if (!dest && !src) + return (NULL); + d = (char *)dest; + s = (char *)src; + ft_checks(s, d, n); + return (dest); +} diff --git a/libft/memory/ft_memset.c b/libft/memory/ft_memset.c new file mode 100644 index 0000000..1a68559 --- /dev/null +++ b/libft/memory/ft_memset.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:50:29 by rparodi #+# #+# */ +/* Updated: 2023/11/07 18:58:02 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + char *str; + size_t i; + + i = 0; + str = (char *)s; + while (i < n) + { + str[i] = c; + i++; + } + return (str); +} diff --git a/libft/print/ft_printf.c b/libft/print/ft_printf.c new file mode 100644 index 0000000..ffeee23 --- /dev/null +++ b/libft/print/ft_printf.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */ +/* Updated: 2024/10/31 16:12:37 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void _print_char(int fd, char c, int *ret_value); +void _print_nbr(int fd, int nb, int *ret_value); +void _print_base(\ + int fd, unsigned long long nbr, int *ret_value, char c); +void _print_unsigned(int fd, unsigned int nb, int *ret_value); +void _print_str(int fd, char *str, int *ret_value); + +int _check_args(int fd, char c, va_list args, int *ret_value) +{ + if (c == 'c') + _print_char(fd, (char)va_arg(args, int), ret_value); + else if (c == 's') + _print_str(fd, (char *)va_arg(args, char *), ret_value); + else if (c == 'i' || c == 'd') + _print_nbr(fd, (int)va_arg(args, int), ret_value); + else if (c == '%') + _print_char(fd, '%', ret_value); + else if (c == 'u') + _print_unsigned(\ + fd, (unsigned int)va_arg(args, unsigned int), ret_value); + else if (c == 'x') + _print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c); + else if (c == 'X') + _print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c); + else if (c == 'p') + _print_base(fd, (unsigned long long)va_arg(args, unsigned long long), \ + ret_value, c); + va_end(args); + return (1); +} + +int ft_dprintf(int fd, const char *s, ...) +{ + size_t i; + va_list args; + char *str; + int ret_value; + + ret_value = 0; + str = ft_strdup(s); + va_start(args, s); + i = 0; + while (str[i]) + { + if (str[i] == '%') + { + _check_args(fd, str[i + 1], args, &ret_value); + i++; + } + else + _print_char(fd, str[i], &ret_value); + i++; + } + free(str); + return (ret_value); +} + +int ft_printf(const char *s, ...) +{ + size_t i; + va_list args; + char *str; + int ret_value; + + ret_value = 0; + str = ft_strdup(s); + va_start(args, s); + i = 0; + while (str[i]) + { + if (str[i] == '%') + { + _check_args(1, str[i + 1], args, &ret_value); + i++; + } + else + _print_char(1, str[i], &ret_value); + i++; + } + free(str); + return (ret_value); +} diff --git a/libft/print/ft_put.c b/libft/print/ft_put.c new file mode 100644 index 0000000..840aa6a --- /dev/null +++ b/libft/print/ft_put.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */ +/* Updated: 2024/10/31 16:16:45 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void _print_char(int fd, char c, int *ret_value); +void _print_nbr(int fd, int nb, int *ret_value); +void _print_base(\ + int fd, unsigned long long nbr, int *ret_value, char c); +void _print_unsigned(int fd, unsigned int nb, int *ret_value); +void _print_str(int fd, char *str, int *ret_value); + +void _print_char(int fd, char c, int *ret_value) +{ + write(fd, &c, 1); + (*ret_value)++; +} + +void _print_nbr(int fd, int nb, int *ret_value) +{ + if (nb < 0) + { + if (nb == INT_MIN) + { + write(1, "-2147483648", 11); + *ret_value += 11; + return ; + } + nb = -nb; + _print_char(fd, '-', ret_value); + } + if (nb >= 10) + { + _print_nbr(fd, nb / 10, ret_value); + nb = nb % 10; + } + if (nb < 10) + _print_char(fd, nb + 48, ret_value); +} + +void _print_base(\ + int fd, unsigned long long nbr, int *ret_value, char c) +{ + char base[16]; + + if (c == 'x' || c == 'q') + ft_strcpy(base, "0123456789abcdef"); + else if (c == 'X') + ft_strcpy(base, "0123456789ABCDEF"); + else if (c == 'p') + { + if (nbr != 0) + _print_str(fd, "0x", ret_value); + else if (nbr == 0) + { + _print_str(fd, "(nil)", ret_value); + return ; + } + else + ft_strcpy(base, "0123456789abcdef"); + c++; + } + if (c != 'p') + { + if (nbr >= 16) + _print_base(fd, nbr / 16, ret_value, c); + _print_char(fd, base[nbr % 16], ret_value); + } +} + +void _print_unsigned(int fd, unsigned int nb, int *ret_value) +{ + if (nb >= 10) + { + _print_unsigned(fd, nb / 10, ret_value); + nb = nb % 10; + } + if (nb < 10) + _print_char(fd, nb + 48, ret_value); +} + +void _print_str(int fd, char *str, int *ret_value) +{ + if (!str) + *ret_value += write(fd, "(null)", 6); + else + *ret_value += write(fd, str, ft_strlen(str)); +} diff --git a/libft/print/ft_putchar_fd.c b/libft/print/ft_putchar_fd.c new file mode 100644 index 0000000..5b4613f --- /dev/null +++ b/libft/print/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:58:22 by rparodi #+# #+# */ +/* Updated: 2023/11/10 15:31:57 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/print/ft_putendl_fd.c b/libft/print/ft_putendl_fd.c new file mode 100644 index 0000000..8c43b1c --- /dev/null +++ b/libft/print/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:59:01 by rparodi #+# #+# */ +/* Updated: 2023/11/10 15:57:53 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libft/print/ft_putnbr_fd.c b/libft/print/ft_putnbr_fd.c new file mode 100644 index 0000000..d596c3a --- /dev/null +++ b/libft/print/ft_putnbr_fd.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:59:18 by rparodi #+# #+# */ +/* Updated: 2023/11/13 20:13:20 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_check_sign(int n, char *str, size_t *i, int fd) +{ + size_t modulus; + short int maiboyerlpb; + + maiboyerlpb = 0; + modulus = 1000000000; + if (n < 0) + { + n = -n; + str[(*i)] = '-'; + (*i)++; + } + while (modulus != 0) + { + if (n / modulus != 0 || maiboyerlpb != 0) + { + str[(*i)++] = (n / modulus) + 48; + maiboyerlpb++; + } + n %= modulus; + modulus /= 10; + } + ft_putstr_fd(str, fd); + return (n); +} + +void ft_putnbr_fd(int n, int fd) +{ + size_t i; + char str[13]; + + i = 0; + ft_bzero(str, 13); + if (n == 0) + ft_putstr_fd("0", fd); + else if (n == -2147483648) + ft_putstr_fd("-2147483648", fd); + else + ft_check_sign(n, str, &i, fd); +} diff --git a/libft/print/ft_putstr_fd.c b/libft/print/ft_putstr_fd.c new file mode 100644 index 0000000..3f5f4e0 --- /dev/null +++ b/libft/print/ft_putstr_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:58:46 by rparodi #+# #+# */ +/* Updated: 2023/11/10 15:57:46 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + write(fd, s, ft_strlen(s)); +} diff --git a/libft/str/ft_split.c b/libft/str/ft_split.c new file mode 100644 index 0000000..32a5d2d --- /dev/null +++ b/libft/str/ft_split.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */ +/* Updated: 2023/11/13 12:14:57 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int count_words(const char *str, char sep) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + while (str[i] == sep && str[i]) + i++; + if (str[i] != sep && str[i]) + { + while (str[i] != sep && str[i]) + i++; + count++; + } + } + return (count); +} + +static char *ft_strndup(const char *s, int j) +{ + int i; + char *str; + + i = 0; + str = (char *)malloc((j + 1)); + if (!str) + return (NULL); + while (s[i] && i < j) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +static char **ext_w(char **words_array, const char *str, char sep, int size) +{ + int i; + int j; + + i = 0; + j = 0; + while (j < size) + { + while (str[i] == sep && str[i]) + i++; + str = str + i; + i = 0; + while (str[i] != sep && str[i]) + i++; + words_array[j++] = ft_strndup(str, i); + str = str + i; + i = 0; + } + words_array[j] = 0; + return (words_array); +} + +char **ft_split(char const *s, char c) +{ + int size; + char **words_array; + + size = count_words(s, c); + words_array = (char **)malloc(sizeof(char *) * (size + 1)); + if (!words_array) + return (NULL); + if (size == 0) + { + words_array[0] = NULL; + return (words_array); + } + words_array = ext_w(words_array, s, c, size); + return (words_array); +} diff --git a/libft/str/ft_strchr.c b/libft/str/ft_strchr.c new file mode 100644 index 0000000..f29c896 --- /dev/null +++ b/libft/str/ft_strchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:32:19 by rparodi #+# #+# */ +/* Updated: 2023/11/09 12:08:01 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + unsigned int i; + + i = 0; + if (c == 0) + return ((char *)s + ft_strlen(s)); + while (s[i] != '\0') + { + if (s[i] == (char)c) + return ((char *)s + i); + i++; + } + return (NULL); +} diff --git a/libft/str/ft_strcmp.c b/libft/str/ft_strcmp.c new file mode 100644 index 0000000..e2da6f7 --- /dev/null +++ b/libft/str/ft_strcmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* strcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:56:56 by rparodi #+# #+# */ +/* Updated: 2024/10/31 17:05:03 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + int diff; + + i = 0; + while ((s1[i] || s2[i])) + { + if (s1[i] != s2[i]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + return (diff); + } + i++; + } + return (0); +} diff --git a/libft/str/ft_strcpy.c b/libft/str/ft_strcpy.c new file mode 100644 index 0000000..6ad4595 --- /dev/null +++ b/libft/str/ft_strcpy.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/31 16:14:10 by rparodi #+# #+# */ +/* Updated: 2024/10/31 16:15:46 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strcpy(char *dst, const char *src) +{ + size_t i; + + i = 0; + while (src[i]) + { + dst[i] = src[i]; + i++; + } + dst[i] = '\0'; + return (dst); +} diff --git a/libft/str/ft_strdup.c b/libft/str/ft_strdup.c new file mode 100644 index 0000000..38a98fc --- /dev/null +++ b/libft/str/ft_strdup.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */ +/* Updated: 2023/11/13 20:02:09 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s) +{ + size_t len; + char *to_return; + + len = ft_strlen(s) + 1; + to_return = (char *)malloc(sizeof(char) * len); + if (!to_return) + return (NULL); + ft_strlcpy(to_return, s, len); + return (to_return); +} diff --git a/libft/str/ft_striteri.c b/libft/str/ft_striteri.c new file mode 100644 index 0000000..bf9869a --- /dev/null +++ b/libft/str/ft_striteri.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:57:34 by rparodi #+# #+# */ +/* Updated: 2023/11/11 17:26:00 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + size_t i; + + i = 0; + while (s[i] != '\0') + { + f(i, s + i); + i++; + } +} diff --git a/libft/str/ft_strjoin.c b/libft/str/ft_strjoin.c new file mode 100644 index 0000000..18844b3 --- /dev/null +++ b/libft/str/ft_strjoin.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:55:15 by rparodi #+# #+# */ +/* Updated: 2023/11/09 17:47:46 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + size_t i; + size_t save_i; + char *str; + + i = 0; + str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2)) + 1); + if (!str) + return (NULL); + while (s1[i] != '\0') + { + str[i] = s1[i]; + i++; + } + save_i = i; + i = 0; + while (s2[i] != '\0') + { + str[save_i + i] = s2[i]; + i++; + } + str[i + save_i] = '\0'; + return (str); +} diff --git a/libft/str/ft_strlcat.c b/libft/str/ft_strlcat.c new file mode 100644 index 0000000..5ef6701 --- /dev/null +++ b/libft/str/ft_strlcat.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 22:28:26 by rparodi #+# #+# */ +/* Updated: 2023/11/12 11:45:52 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static size_t ft_strnlen(char *dest, size_t size) +{ + size_t i; + + i = 0; + while (i < size && dest[i] != '\0') + i++; + return (i); +} + +size_t ft_strlcat(char *dest, const char *src, size_t size) +{ + size_t i; + size_t j; + size_t destlen; + + i = 0; + j = 0; + destlen = ft_strnlen(dest, size); + while (i < size && dest[i]) + i++; + if (i == size) + return (i + ft_strlen(src)); + while (src[j]) + { + if (j < size - destlen - 1) + { + dest[i] = src[j]; + i++; + } + j++; + } + dest[i] = '\0'; + return (destlen + j); +} diff --git a/libft/str/ft_strlcpy.c b/libft/str/ft_strlcpy.c new file mode 100644 index 0000000..bfd75e5 --- /dev/null +++ b/libft/str/ft_strlcpy.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */ +/* Updated: 2023/11/13 19:16:30 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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'; + i++; + } + return (ft_strlen(src)); +} diff --git a/libft/str/ft_strlen.c b/libft/str/ft_strlen.c new file mode 100644 index 0000000..ea2a01e --- /dev/null +++ b/libft/str/ft_strlen.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */ +/* Updated: 2023/11/08 12:37:09 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *s) +{ + size_t i; + + i = 0; + while (s[i] != '\0') + i++; + return (i); +} diff --git a/libft/str/ft_strmapi.c b/libft/str/ft_strmapi.c new file mode 100644 index 0000000..163c12f --- /dev/null +++ b/libft/str/ft_strmapi.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:57 by rparodi #+# #+# */ +/* Updated: 2023/11/13 19:49:30 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + size_t i; + size_t len; + char *str; + + i = 0; + len = ft_strlen(s); + str = (char *)malloc(len + 1); + if (!str) + return (NULL); + else + ft_bzero((char *)str, len + 1); + while (i < len) + { + str[i] = f(i, s[i]); + i++; + } + return (str); +} diff --git a/libft/str/ft_strncmp.c b/libft/str/ft_strncmp.c new file mode 100644 index 0000000..9afff02 --- /dev/null +++ b/libft/str/ft_strncmp.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:56:56 by rparodi #+# #+# */ +/* Updated: 2023/11/09 13:16:39 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + int diff; + + i = 0; + while ((s1[i] || s2[i]) && i < n) + { + if (s1[i] != s2[i]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + return (diff); + } + i++; + } + return (0); +} diff --git a/libft/str/ft_strnstr.c b/libft/str/ft_strnstr.c new file mode 100644 index 0000000..6c20844 --- /dev/null +++ b/libft/str/ft_strnstr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:57:44 by rparodi #+# #+# */ +/* Updated: 2023/11/13 19:17:49 by rparodi ### ########.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 (len == 0 && (!big || !little)) + return (NULL); + if (!little[i]) + return ((char *)big); + while (big[i] && i < len) + { + j = 0; + while (i + j < len && little[j] == big[i + j]) + { + j++; + if (little[j] == '\0') + return ((char *)(big + i)); + } + i++; + } + return (0); +} diff --git a/libft/str/ft_strrchr.c b/libft/str/ft_strrchr.c new file mode 100644 index 0000000..3f7177a --- /dev/null +++ b/libft/str/ft_strrchr.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/07 16:58:22 by rparodi #+# #+# */ +/* Updated: 2023/11/11 18:46:54 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + size_t i; + + i = ft_strlen(s); + while (i > 0) + { + if (s[i] == c % 256) + return ((char *)s + i); + i--; + } + if (s[i] == c % 256) + return ((char *)s + i); + return (NULL); +} diff --git a/libft/str/ft_strtrim.c b/libft/str/ft_strtrim.c new file mode 100644 index 0000000..2d3e105 --- /dev/null +++ b/libft/str/ft_strtrim.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:55:44 by rparodi #+# #+# */ +/* Updated: 2023/11/13 20:08:51 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strtrim(char const *s1, char const *set) +{ + size_t i; + size_t j; + size_t k; + char *str; + + if (!s1) + return (NULL); + i = 0; + while (ft_strchr(set, s1[i]) != NULL && s1[i]) + i++; + if (i == ft_strlen(s1)) + return (ft_strdup("")); + j = ft_strlen(s1); + while (ft_strrchr(set, s1[j - 1]) != NULL && j > i) + j--; + str = (char *)malloc(j - i + 1); + if (!str) + return (NULL); + k = 0; + while (i < j) + str[k++] = s1[i++]; + str[k] = '\0'; + return (str); +} diff --git a/libft/str/ft_substr.c b/libft/str/ft_substr.c new file mode 100644 index 0000000..fd634f4 --- /dev/null +++ b/libft/str/ft_substr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:54:42 by rparodi #+# #+# */ +/* Updated: 2023/11/13 20:08:02 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *str; + + if (start >= ft_strlen(s)) + return (ft_strdup("")); + if (len + start > (ft_strlen(s))) + len = ft_strlen(s) - start; + str = (char *)malloc(len + 1); + if (!str || !s) + return (free(str), ft_strdup("")); + ft_strlcpy(str, s + start, len + 1); + return (str); +} diff --git a/parsing/arguments.c b/parsing/arguments.c index d807fed..6a444fb 100644 --- a/parsing/arguments.c +++ b/parsing/arguments.c @@ -6,14 +6,12 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:41:32 by rparodi #+# #+# */ -/* Updated: 2024/10/31 11:23:05 by rparodi ### ########.fr */ +/* Updated: 2024/10/31 16:54:21 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -#include - /** * @brief checking if the args given to the executable is valid * @@ -25,8 +23,8 @@ bool ft_parse_args(int argc, char *argv[]) { if (argc != 2) return (print_error(ERR_ARGS_COUNT), false); - if (strlen(argv[1]) < 4 || \ - strcmp((argv[1] + (strlen(argv[1]) - 4)), ".cub") != 0) + if (ft_strlen(argv[1]) < 4 || \ + ft_strcmp((argv[1] + (strlen(argv[1]) - 4)), ".cub") != 0) return (print_error(INV_NAME_MAP), false); return (true); } diff --git a/sources/main.c b/sources/main.c index d92d1fd..cd1890b 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/10/31 12:04:36 by rparodi ### ########.fr */ +/* Updated: 2024/10/31 16:43:19 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,10 +14,7 @@ int main(int argc, char *argv[]) { - t_info *cub; - if (!ft_parse_args(argc, argv)) return (1); - return (0); }