From eea56ccef44bb9e91a09b9cb3fd24d4cb38e12df Mon Sep 17 00:00:00 2001 From: Tom Moron Date: Fri, 2 Feb 2024 23:05:45 +0100 Subject: [PATCH] I'm totally adding this to epic win. +300 --- libft/Makefile | 101 ++++++++++++++++++++ libft/ft_atoi.c | 35 +++++++ libft/ft_bzero.c | 26 ++++++ libft/ft_calloc.c | 31 ++++++ libft/ft_free_str_arr.c | 27 ++++++ libft/ft_isalnum.c | 19 ++++ libft/ft_isalpha.c | 18 ++++ libft/ft_isascii.c | 18 ++++ libft/ft_isdigit.c | 18 ++++ libft/ft_isprint.c | 18 ++++ libft/ft_itoa.c | 60 ++++++++++++ libft/ft_lstadd_back.c | 27 ++++++ libft/ft_lstadd_front.c | 19 ++++ libft/ft_lstclear.c | 27 ++++++ libft/ft_lstdelone.c | 20 ++++ libft/ft_lstiter.c | 21 +++++ libft/ft_lstlast.c | 19 ++++ libft/ft_lstmap.c | 41 ++++++++ libft/ft_lstnew.c | 25 +++++ libft/ft_lstsize.c | 26 ++++++ libft/ft_memchr.c | 28 ++++++ libft/ft_memcmp.c | 28 ++++++ libft/ft_memcpy.c | 29 ++++++ libft/ft_memmove.c | 32 +++++++ libft/ft_memset.c | 27 ++++++ libft/ft_printf/Makefile | 52 +++++++++++ libft/ft_printf/ft_convert.c | 85 +++++++++++++++++ libft/ft_printf/ft_isdigit.c | 18 ++++ libft/ft_printf/ft_parse_arg.c | 89 ++++++++++++++++++ libft/ft_printf/ft_print_hex_ptr.c | 97 +++++++++++++++++++ libft/ft_printf/ft_print_int.c | 99 ++++++++++++++++++++ libft/ft_printf/ft_print_unsigned_int.c | 55 +++++++++++ libft/ft_printf/ft_printf.c | 67 +++++++++++++ libft/ft_printf/ft_printf.h | 46 +++++++++ libft/ft_printf/ft_protected_atoi.c | 37 ++++++++ libft/ft_printf/ft_putchar.c | 18 ++++ libft/ft_printf/ft_putstr.c | 19 ++++ libft/ft_printf/ft_strlen.c | 22 +++++ libft/ft_printf/ft_write_str_part.c | 26 ++++++ libft/ft_putchar_fd.c | 17 ++++ libft/ft_putendl_fd.c | 21 +++++ libft/ft_putnbr_fd.c | 33 +++++++ libft/ft_putstr_fd.c | 18 ++++ libft/ft_set_color.c | 23 +++++ libft/ft_split.c | 73 +++++++++++++++ libft/ft_split_set.c | 84 +++++++++++++++++ libft/ft_strchr.c | 30 ++++++ libft/ft_strcmp.c | 34 +++++++ libft/ft_strdup.c | 36 +++++++ libft/ft_striteri.c | 23 +++++ libft/ft_strjoin.c | 63 +++++++++++++ libft/ft_strlcat.c | 38 ++++++++ libft/ft_strlcpy.c | 32 +++++++ libft/ft_strlen.c | 22 +++++ libft/ft_strmapi.c | 33 +++++++ libft/ft_strnstr.c | 39 ++++++++ libft/ft_strrchr.c | 30 ++++++ libft/ft_strtrim.c | 78 ++++++++++++++++ libft/ft_substr.c | 37 ++++++++ libft/ft_swap.c | 20 ++++ libft/ft_tolower.c | 18 ++++ libft/ft_toupper.c | 18 ++++ libft/gnl/Makefile | 42 +++++++++ libft/gnl/get_next_line_bonus.c | 113 ++++++++++++++++++++++ libft/gnl/get_next_line_bonus.h | 46 +++++++++ libft/gnl/get_next_line_utils_bonus.c | 119 ++++++++++++++++++++++++ libft/libft.h | 77 +++++++++++++++ 67 files changed, 2687 insertions(+) create mode 100755 libft/Makefile create mode 100755 libft/ft_atoi.c create mode 100755 libft/ft_bzero.c create mode 100755 libft/ft_calloc.c create mode 100755 libft/ft_free_str_arr.c create mode 100755 libft/ft_isalnum.c create mode 100755 libft/ft_isalpha.c create mode 100755 libft/ft_isascii.c create mode 100755 libft/ft_isdigit.c create mode 100755 libft/ft_isprint.c create mode 100755 libft/ft_itoa.c create mode 100755 libft/ft_lstadd_back.c create mode 100755 libft/ft_lstadd_front.c create mode 100755 libft/ft_lstclear.c create mode 100755 libft/ft_lstdelone.c create mode 100755 libft/ft_lstiter.c create mode 100755 libft/ft_lstlast.c create mode 100755 libft/ft_lstmap.c create mode 100755 libft/ft_lstnew.c create mode 100755 libft/ft_lstsize.c create mode 100755 libft/ft_memchr.c create mode 100755 libft/ft_memcmp.c create mode 100755 libft/ft_memcpy.c create mode 100755 libft/ft_memmove.c create mode 100755 libft/ft_memset.c create mode 100755 libft/ft_printf/Makefile create mode 100755 libft/ft_printf/ft_convert.c create mode 100755 libft/ft_printf/ft_isdigit.c create mode 100755 libft/ft_printf/ft_parse_arg.c create mode 100755 libft/ft_printf/ft_print_hex_ptr.c create mode 100755 libft/ft_printf/ft_print_int.c create mode 100755 libft/ft_printf/ft_print_unsigned_int.c create mode 100755 libft/ft_printf/ft_printf.c create mode 100755 libft/ft_printf/ft_printf.h create mode 100755 libft/ft_printf/ft_protected_atoi.c create mode 100755 libft/ft_printf/ft_putchar.c create mode 100755 libft/ft_printf/ft_putstr.c create mode 100755 libft/ft_printf/ft_strlen.c create mode 100755 libft/ft_printf/ft_write_str_part.c create mode 100755 libft/ft_putchar_fd.c create mode 100755 libft/ft_putendl_fd.c create mode 100755 libft/ft_putnbr_fd.c create mode 100755 libft/ft_putstr_fd.c create mode 100755 libft/ft_set_color.c create mode 100755 libft/ft_split.c create mode 100755 libft/ft_split_set.c create mode 100755 libft/ft_strchr.c create mode 100755 libft/ft_strcmp.c create mode 100755 libft/ft_strdup.c create mode 100755 libft/ft_striteri.c create mode 100755 libft/ft_strjoin.c create mode 100755 libft/ft_strlcat.c create mode 100755 libft/ft_strlcpy.c create mode 100755 libft/ft_strlen.c create mode 100755 libft/ft_strmapi.c create mode 100755 libft/ft_strnstr.c create mode 100755 libft/ft_strrchr.c create mode 100755 libft/ft_strtrim.c create mode 100755 libft/ft_substr.c create mode 100755 libft/ft_swap.c create mode 100755 libft/ft_tolower.c create mode 100755 libft/ft_toupper.c create mode 100755 libft/gnl/Makefile create mode 100755 libft/gnl/get_next_line_bonus.c create mode 100755 libft/gnl/get_next_line_bonus.h create mode 100755 libft/gnl/get_next_line_utils_bonus.c create mode 100755 libft/libft.h diff --git a/libft/Makefile b/libft/Makefile new file mode 100755 index 0000000..2137506 --- /dev/null +++ b/libft/Makefile @@ -0,0 +1,101 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # +# Updated: 2024/02/02 22:04:52 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +NAME = libft.a + +CC = cc + +SRCS = ft_atoi.c\ + ft_bzero.c\ + ft_calloc.c\ + ft_isalnum.c\ + ft_isalpha.c\ + ft_isascii.c\ + ft_isdigit.c\ + ft_isprint.c\ + ft_itoa.c\ + ft_memchr.c\ + ft_memcmp.c\ + ft_memcpy.c\ + ft_memmove.c\ + ft_memset.c\ + ft_putchar_fd.c\ + ft_putendl_fd.c\ + ft_putnbr_fd.c\ + ft_putstr_fd.c\ + ft_split.c\ + ft_strchr.c\ + ft_strdup.c\ + ft_striteri.c\ + ft_strjoin.c\ + ft_strlcat.c\ + ft_strlcpy.c\ + ft_strlen.c\ + ft_strmapi.c\ + ft_strcmp.c\ + ft_strnstr.c\ + ft_strrchr.c\ + ft_strtrim.c\ + ft_substr.c\ + ft_tolower.c\ + ft_toupper.c\ + ft_split_set.c\ + ft_free_str_arr.c\ + ft_set_color.c + +SRCS_BONUS = ft_lstnew.c\ + ft_lstadd_front.c\ + ft_lstsize.c\ + ft_lstlast.c\ + ft_lstadd_back.c\ + ft_lstdelone.c\ + ft_lstclear.c\ + ft_lstiter.c\ + ft_lstmap.c\ + +OBJS = $(SRCS:.c=.o) +OBJS_BONUS = $(SRCS_BONUS:.c=.o) + +FLAGS = -Wall -Wextra -Werror + +all: $(NAME) + + +$(NAME): ft_printf $(OBJS) + make --no-print-directory -C ./ft_printf + cp ./ft_printf/libftprintf.a ./gnl/gnl.a + make --no-print-directory -C ./gnl + cp ./gnl/gnl.a ./$(NAME) + ar rcs $(NAME) $(OBJS) + +.c.o: + $(CC) $(FLAGS) -c $< -o $@ + +bonus: $(OBJS) $(OBJS_BONUS) + ar rcs $(NAME) $(OBJS) $(OBJS_BONUS) + +clean: + rm -f $(OBJS) $(OBJS_BONUS) + make --no-print-directory -C ./ft_printf fclean + make --no-print-directory -C ./gnl fclean + +fclean: clean + rm -f $(NAME) + +re: fclean all + +so: + $(CC) -nostartfiles -fPIC $(FLAGS) $(SRCS) + gcc -nostartfiles -shared -o libft.so $(OBJS) $(OBJS_BONUS) + + +.PHONY: bonus so clean all re fclean diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c new file mode 100755 index 0000000..3b0badd --- /dev/null +++ b/libft/ft_atoi.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/17 13:41:15 by tomoron #+# #+# */ +/* Updated: 2023/10/31 15:35:21 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_atoi(const char *str) +{ + int res; + int inv; + + res = 0; + inv = 1; + while (*str == ' ' || (*str >= '\t' && *str <= '\r')) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + inv *= -1; + str++; + } + while (*str >= '0' && *str <= '9') + { + res *= 10; + res += *str - '0'; + str++; + } + return (res * inv); +} diff --git a/libft/ft_bzero.c b/libft/ft_bzero.c new file mode 100755 index 0000000..8e328c6 --- /dev/null +++ b/libft/ft_bzero.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_bzero.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:39:29 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:41:24 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_bzero(void *s, size_t n) +{ + unsigned long int i; + char *ptr; + + ptr = (char *)s; + i = 0; + while (i < n) + { + ptr[i] = 0; + i++; + } +} diff --git a/libft/ft_calloc.c b/libft/ft_calloc.c new file mode 100755 index 0000000..9314306 --- /dev/null +++ b/libft/ft_calloc.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_calloc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/31 02:38:46 by tomoron #+# #+# */ +/* Updated: 2023/10/31 18:15:42 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_calloc(size_t nb, size_t size) +{ + char *res; + unsigned long int i; + + if (nb == 0 || size == 0) + return (malloc(1)); + if ((long long)nb < 0 || (long long) size < 0) + return (0); + res = malloc(nb * size); + i = 0; + while (i < (nb * size) && res) + { + res[i] = 0; + i++; + } + return ((void *)res); +} diff --git a/libft/ft_free_str_arr.c b/libft/ft_free_str_arr.c new file mode 100755 index 0000000..c4bd417 --- /dev/null +++ b/libft/ft_free_str_arr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_free_str_arr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/10 14:42:28 by tomoron #+# #+# */ +/* Updated: 2023/12/10 14:53:07 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_free_str_arr(char **arr) +{ + int i; + + if (!arr) + return ; + i = 0; + while (arr[i]) + { + free(arr[i]); + i++; + } + free(arr); +} diff --git a/libft/ft_isalnum.c b/libft/ft_isalnum.c new file mode 100755 index 0000000..a1fe540 --- /dev/null +++ b/libft/ft_isalnum.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalnum.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:09:17 by tomoron #+# #+# */ +/* Updated: 2023/10/30 23:17:18 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalnum(int c) +{ + if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9')) + return (1); + return (0); +} diff --git a/libft/ft_isalpha.c b/libft/ft_isalpha.c new file mode 100755 index 0000000..6a8b074 --- /dev/null +++ b/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isalpha.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 18:10:01 by tomoron #+# #+# */ +/* Updated: 2023/10/30 12:14:17 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) + return (0); + return (1); +} diff --git a/libft/ft_isascii.c b/libft/ft_isascii.c new file mode 100755 index 0000000..5b85ccc --- /dev/null +++ b/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isascii.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:15:36 by tomoron #+# #+# */ +/* Updated: 2023/10/30 12:31:38 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libft/ft_isdigit.c b/libft/ft_isdigit.c new file mode 100755 index 0000000..fe77533 --- /dev/null +++ b/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 18:10:01 by tomoron #+# #+# */ +/* Updated: 2023/10/30 12:13:53 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (!(c >= '0' && c <= '9')) + return (0); + return (1); +} diff --git a/libft/ft_isprint.c b/libft/ft_isprint.c new file mode 100755 index 0000000..7b5ceb6 --- /dev/null +++ b/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isprint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:31:55 by tomoron #+# #+# */ +/* Updated: 2023/10/30 12:34:32 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c <= 126) + return (1); + return (0); +} diff --git a/libft/ft_itoa.c b/libft/ft_itoa.c new file mode 100755 index 0000000..e076598 --- /dev/null +++ b/libft/ft_itoa.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 02:36:12 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:16:45 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int get_number_len(long int n) +{ + int res; + + res = 1; + if (n < 0) + { + res++; + n *= -1; + } + while (n > 9) + { + res++; + n /= 10; + } + return (res); +} + +char *ft_itoa(int n) +{ + char *res; + int nb_len; + int i; + long int temp_nb; + + temp_nb = (long int)n; + nb_len = get_number_len(n); + res = malloc((nb_len + 1) * sizeof(char)); + if (!res) + return (res); + i = 0; + if (temp_nb < 0) + { + res[0] = '-'; + temp_nb *= -1; + } + while (temp_nb > 9) + { + res[nb_len - i - 1] = (temp_nb % 10) + '0'; + temp_nb /= 10; + i++; + } + res[nb_len - i - 1] = temp_nb + '0'; + res[nb_len] = 0; + return (res); +} diff --git a/libft/ft_lstadd_back.c b/libft/ft_lstadd_back.c new file mode 100755 index 0000000..3aa43d7 --- /dev/null +++ b/libft/ft_lstadd_back.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:44:09 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:13:03 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *current; + + if (!lst) + return ; + current = *lst; + while (current && current->next) + current = current->next; + if (!current) + *lst = new; + else + current->next = new; +} diff --git a/libft/ft_lstadd_front.c b/libft/ft_lstadd_front.c new file mode 100755 index 0000000..4e6a56a --- /dev/null +++ b/libft/ft_lstadd_front.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstadd_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:51:22 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:12:41 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; +} diff --git a/libft/ft_lstclear.c b/libft/ft_lstclear.c new file mode 100755 index 0000000..fc79c03 --- /dev/null +++ b/libft/ft_lstclear.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstclear.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:52:45 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:12:06 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void*)) +{ + t_list *next; + + if (!lst || !del) + return ; + while (*lst) + { + next = (*lst)-> next; + del((*lst)-> content); + free(*lst); + *lst = next; + } +} diff --git a/libft/ft_lstdelone.c b/libft/ft_lstdelone.c new file mode 100755 index 0000000..80bbcfb --- /dev/null +++ b/libft/ft_lstdelone.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:56:36 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:13:47 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void*)) +{ + if (!lst || !del) + return ; + del(lst->content); + free(lst); +} diff --git a/libft/ft_lstiter.c b/libft/ft_lstiter.c new file mode 100755 index 0000000..0430cfd --- /dev/null +++ b/libft/ft_lstiter.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 16:35:30 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:13:21 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libft/ft_lstlast.c b/libft/ft_lstlast.c new file mode 100755 index 0000000..6fa8909 --- /dev/null +++ b/libft/ft_lstlast.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:47:49 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:13:36 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + while (lst && lst -> next) + lst = lst -> next; + return (lst); +} diff --git a/libft/ft_lstmap.c b/libft/ft_lstmap.c new file mode 100755 index 0000000..7715da4 --- /dev/null +++ b/libft/ft_lstmap.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 17:29:05 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:47:05 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *res; + t_list *current; + + if (!lst) + return (0); + res = malloc(sizeof(t_list)); + if (!res) + return (0); + res -> content = f(lst->content); + lst = lst->next; + current = res; + while (lst) + { + current->next = malloc(sizeof(t_list)); + if (!current->next) + { + ft_lstclear(&res, del); + return (0); + } + current = current->next; + current->content = f(lst->content); + lst = lst->next; + } + current->next = NULL; + return (res); +} diff --git a/libft/ft_lstnew.c b/libft/ft_lstnew.c new file mode 100755 index 0000000..8e78fb4 --- /dev/null +++ b/libft/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:39:52 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:11:12 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + t_list *res; + + res = malloc (sizeof(t_list)); + if (!res) + return (0); + res->content = content; + res->next = NULL; + return (res); +} diff --git a/libft/ft_lstsize.c b/libft/ft_lstsize.c new file mode 100755 index 0000000..4fdfadf --- /dev/null +++ b/libft/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 15:46:36 by tomoron #+# #+# */ +/* Updated: 2023/11/02 01:12:32 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int res; + + res = 0; + while (lst) + { + res++; + lst = lst->next; + } + return (res); +} diff --git a/libft/ft_memchr.c b/libft/ft_memchr.c new file mode 100755 index 0000000..1691bba --- /dev/null +++ b/libft/ft_memchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 13:35:44 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:28:37 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + unsigned long int i; + unsigned char *p; + + i = 0; + p = (unsigned char *) s; + while (i < n) + { + if (p[i] == (unsigned char)c) + return ((char *)p + i); + i++; + } + return (0); +} diff --git a/libft/ft_memcmp.c b/libft/ft_memcmp.c new file mode 100755 index 0000000..dc57e59 --- /dev/null +++ b/libft/ft_memcmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 13:59:31 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:23:04 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + unsigned int i; + unsigned char *str1; + unsigned char *str2; + + str1 = (unsigned char *)s1; + str2 = (unsigned char *)s2; + i = 0; + if (!n) + return (0); + while (str1[i] == str2[i] && i < n - 1) + i++; + return (str1[i] - str2[i]); +} diff --git a/libft/ft_memcpy.c b/libft/ft_memcpy.c new file mode 100755 index 0000000..7e1aced --- /dev/null +++ b/libft/ft_memcpy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:42:28 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:05:58 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memcpy(void *dest, const void *src, size_t n) +{ + unsigned int i; + char *d; + char *s; + + d = (char *)dest; + s = (char *)src; + i = 0; + while (i < n && d && s) + { + d[i] = s[i]; + i++; + } + return (d); +} diff --git a/libft/ft_memmove.c b/libft/ft_memmove.c new file mode 100755 index 0000000..2f1c08b --- /dev/null +++ b/libft/ft_memmove.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memmove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 13:20:17 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:23:24 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memmove(void *dest, const void *src, size_t n) +{ + unsigned int i; + unsigned char *d; + unsigned char *s; + + d = (unsigned char *) dest; + s = (unsigned char *) src; + i = 0; + while (i < n && d && s) + { + if (d > s) + d[n - i - 1] = s[n - i - 1]; + else + d[i] = s[i]; + i++; + } + return (dest); +} diff --git a/libft/ft_memset.c b/libft/ft_memset.c new file mode 100755 index 0000000..a8408d0 --- /dev/null +++ b/libft/ft_memset.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_memset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:36:09 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:23:45 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void *ft_memset(void *s, int c, size_t n) +{ + unsigned int i; + char *p; + + p = (char *)s; + i = 0; + while (i < n) + { + p[i] = c; + i++; + } + return (s); +} diff --git a/libft/ft_printf/Makefile b/libft/ft_printf/Makefile new file mode 100755 index 0000000..ecba3b8 --- /dev/null +++ b/libft/ft_printf/Makefile @@ -0,0 +1,52 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/04 08:03:00 by tomoron #+# #+# # +# Updated: 2023/11/15 14:42:45 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +NAME = libftprintf.a + +CC = gcc + +SRCS = ft_protected_atoi.c\ + ft_convert.c\ + ft_isdigit.c\ + ft_parse_arg.c\ + ft_print_hex_ptr.c\ + ft_print_int.c\ + ft_print_unsigned_int.c\ + ft_printf.c\ + ft_putchar.c\ + ft_putstr.c\ + ft_strlen.c\ + ft_write_str_part.c + +OBJS = $(SRCS:.c=.o) + +FLAGS = -Wall -Wextra -Werror + +$(NAME): $(OBJS) + ar rcs $(NAME) $(OBJS) + +.c.o: + $(CC) $(FLAGS) -c $< -o $@ + +all: $(NAME) + +bonus: all + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: bonus clean all re fclean diff --git a/libft/ft_printf/ft_convert.c b/libft/ft_printf/ft_convert.c new file mode 100755 index 0000000..61cc97c --- /dev/null +++ b/libft/ft_printf/ft_convert.c @@ -0,0 +1,85 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_convert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 23:24:31 by tomoron #+# #+# */ +/* Updated: 2023/11/07 23:55:33 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +size_t ft_fill_width(char c, int n) +{ + int res; + + res = n; + if (res < 0) + res = 0; + while (n > 0) + { + ft_putchar(c); + n--; + } + return (res); +} + +size_t ft_print_char(int c, t_flags flags) +{ + int res; + + res = 0; + if (flags.min_width && !flags.left_justify) + res += ft_fill_width(' ', flags.min_width - 1); + res += ft_putchar((char)c); + if (flags.min_width && flags.left_justify) + res += ft_fill_width(' ', flags.min_width - 1); + return (res); +} + +size_t ft_print_str(char *s, t_flags flags) +{ + int nb_to_print; + int res; + + res = 0; + if (!s && (flags.precision >= 6 || flags.precision == -1)) + s = "(null)"; + else if (!s) + s = ""; + nb_to_print = ft_strlen(s); + if (flags.precision != -1 && nb_to_print > flags.precision) + nb_to_print = flags.precision; + if (flags.min_width && !flags.left_justify) + res += ft_fill_width(' ', flags.min_width - nb_to_print); + write(1, s, nb_to_print); + if (flags.min_width && flags.left_justify) + res += ft_fill_width(' ', flags.min_width - nb_to_print); + return (res + nb_to_print); +} + +size_t ft_convert(char *s, t_flags flags, va_list args, int err) +{ + if (*s == 'c') + return (ft_print_char(va_arg(args, int), flags)); + else if (*s == 's') + return (ft_print_str(va_arg(args, char *), flags)); + else if (*s == 'p') + return (ft_print_ptr(va_arg(args, void *), flags)); + else if (*s == 'd' || *s == 'i') + return (ft_print_signed_int(va_arg(args, int), flags)); + else if (*s == 'u') + return (ft_print_unsigned_int(va_arg(args, unsigned int), flags)); + else if (*s == 'x') + return (ft_print_hex(va_arg(args, unsigned int), flags, 'L')); + else if (*s == 'X') + return (ft_print_hex(va_arg(args, unsigned int), flags, 'U')); + else if (*s == '%') + return (ft_putstr("%")); + else if (err != 2) + return (ft_putstr("%")); + else + return (0); +} diff --git a/libft/ft_printf/ft_isdigit.c b/libft/ft_printf/ft_isdigit.c new file mode 100755 index 0000000..fe77533 --- /dev/null +++ b/libft/ft_printf/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isdigit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 18:10:01 by tomoron #+# #+# */ +/* Updated: 2023/10/30 12:13:53 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (!(c >= '0' && c <= '9')) + return (0); + return (1); +} diff --git a/libft/ft_printf/ft_parse_arg.c b/libft/ft_printf/ft_parse_arg.c new file mode 100755 index 0000000..09af1e2 --- /dev/null +++ b/libft/ft_printf/ft_parse_arg.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_arg.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 11:43:10 by tomoron #+# #+# */ +/* Updated: 2023/11/15 14:32:59 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_is_char_in_flags(char *s, char c) +{ + int i; + + i = 0; + while (s[i] == '-' || s[i] == '0' || s[i] == '#' + || s[i] == ' ' || s[i] == '+') + { + if (s[i] == c) + return (1); + i++; + } + return (0); +} + +void ft_get_width_precision(char **s, va_list args, t_flags *flags, int *err) +{ + if (**s == '*') + flags->min_width = va_arg(args, int); + else + flags->min_width = ft_protected_atoi(*s); + while (ft_isdigit(**s) || **s == '*') + (*s)++; + flags->precision = -1; + if (**s == '.' && *(*s + 1) == '*') + flags->precision = va_arg(args, int); + else if (**s == '.') + { + if (ft_isdigit(*(*s + 1))) + flags->precision = ft_protected_atoi(*s + 1); + else + flags->precision = 0; + (*s)++; + } + while (ft_isdigit(**s) || **s == '.' || **s == '*') + (*s)++; + if (flags->min_width == -2 || flags->precision == -2) + *err = 3; +} + +void ft_get_flags(char **s, va_list args, t_flags *flags, int *err) +{ + flags->left_justify = ft_is_char_in_flags(*s, '-'); + flags->zero_padding = ft_is_char_in_flags(*s, '0'); + flags->always_sign_number = ft_is_char_in_flags(*s, '+'); + flags->blank_positive = ft_is_char_in_flags(*s, ' '); + flags->zero_x_prefix = ft_is_char_in_flags(*s, '#'); + while (**s == '-' || **s == '0' || **s == '#' + || **s == ' ' || **s == '+') + { + (*s)++; + } + return (ft_get_width_precision(s, args, flags, err)); +} + +size_t ft_parse_arg(char **s, va_list args, int *err) +{ + t_flags flags; + size_t res; + + res = 0; + if (**s == '%') + { + (*s)++; + ft_get_flags(s, args, &flags, err); + if (*err == 3) + return (0); + res += ft_convert(*s, flags, args, *err); + if (**s == 'c' || **s == 's' || **s == 'p' || **s == 'd' + || **s == 'i' || **s == 'u' || **s == 'x' + || **s == 'X' || **s == '%') + (*s)++; + } + return (res); +} diff --git a/libft/ft_printf/ft_print_hex_ptr.c b/libft/ft_printf/ft_print_hex_ptr.c new file mode 100755 index 0000000..c957134 --- /dev/null +++ b/libft/ft_printf/ft_print_hex_ptr.c @@ -0,0 +1,97 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_hex_ptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 15:58:57 by tomoron #+# #+# */ +/* Updated: 2023/11/05 15:58:58 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +int ft_calc_hex_len(long unsigned int n, t_flags flags) +{ + int res; + + res = 1; + if (n == 0 && flags.precision == 0) + return (0); + while (n > 15) + { + res++; + n /= 16; + } + return (res); +} + +void ft_put_hex(unsigned long int n, char mode) +{ + if (n > 15) + ft_put_hex(n / 16, mode); + if (mode == 'L') + write(1, &"0123456789abcdef"[n % 16], 1); + if (mode == 'U') + write(1, &"0123456789ABCDEF"[n % 16], 1); +} + +size_t ft_print_ptr(void *ptr, t_flags flags) +{ + int len; + int res; + + res = 0; + if (!ptr) + return (ft_print_str("(nil)", flags)); + len = ft_calc_hex_len((long unsigned int)ptr, flags) + 2; + if (flags.min_width && !flags.left_justify) + res += ft_fill_width(' ', flags.min_width - len); + ft_putstr("0x"); + ft_put_hex((unsigned long int)ptr, 'L'); + if (flags.min_width && flags.left_justify) + res += ft_fill_width(' ', flags.min_width - len); + return (res + len); +} + +size_t ft_write_blank(int blank_len, t_flags flags, char mode) +{ + size_t res; + + res = 0; + if (blank_len > 0 && ((flags.left_justify && mode == 'A') + || (!flags.left_justify && mode == 'B'))) + { + if (flags.zero_padding && flags.precision == -1) + res += ft_fill_width('0', blank_len); + else + res += ft_fill_width(' ', blank_len); + } + return (res); +} + +size_t ft_print_hex(unsigned int nb, t_flags flags, char mode) +{ + int blank_len; + int zero_len; + int number_len; + size_t res; + + number_len = ft_calc_hex_len(nb, flags); + res = number_len; + zero_len = 0; + if (flags.precision > number_len) + zero_len = flags.precision - number_len; + blank_len = flags.min_width - zero_len - number_len; + blank_len -= flags.zero_x_prefix * 2; + res += ft_write_blank(blank_len, flags, 'B'); + if (flags.zero_x_prefix && nb && mode == 'L') + res += ft_putstr("0x"); + if (flags.zero_x_prefix && nb && mode == 'U') + res += ft_putstr("0X"); + res += ft_fill_width('0', zero_len); + if (flags.precision != 0 || nb != 0) + ft_put_hex(nb, mode); + res += ft_write_blank(blank_len, flags, 'A'); + return (res); +} diff --git a/libft/ft_printf/ft_print_int.c b/libft/ft_printf/ft_print_int.c new file mode 100755 index 0000000..b710860 --- /dev/null +++ b/libft/ft_printf/ft_print_int.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 15:52:46 by tomoron #+# #+# */ +/* Updated: 2023/11/05 15:53:56 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +int ft_calc_signed_int_len(int nb, t_flags flags) +{ + int res; + long int n; + + res = 1; + n = (long int) nb; + if (flags.precision == 0 && nb == 0) + return (0); + if (n < 0) + { + n = n * -1; + } + while (n > 9) + { + res++; + n /= 10; + } + return (res); +} + +size_t ft_putsign(int nb, t_flags flags, int sign) +{ + if (nb < 0) + return (ft_putchar('-')); + if (sign && flags.blank_positive) + return (ft_putchar(' ')); + if (sign) + return (ft_putchar('+')); + return (0); +} + +int ft_print_signed_int_blank_sign(int nb, t_flags flags, size_t *res) +{ + int blank_len; + int zero_len; + int number_len; + int sign; + + number_len = ft_calc_signed_int_len(nb, flags); + sign = (nb < 0) || flags.always_sign_number || flags.blank_positive; + zero_len = 0; + if (flags.precision > number_len) + zero_len = flags.precision - number_len; + blank_len = flags.min_width - zero_len - number_len - sign; + if (blank_len > 0 && !flags.left_justify && ((!flags.zero_padding + && nb != 0) || (flags.zero_padding && flags.precision != -1))) + *res += ft_fill_width(' ', blank_len); + *res += ft_putsign(nb, flags, sign); + if (blank_len > 0 && !flags.left_justify && flags.zero_padding + && flags.precision == -1) + *res += ft_fill_width('0', blank_len); + *res += ft_fill_width('0', zero_len); + return (blank_len); +} + +void ft_put_lu_nbr(long unsigned int nb) +{ + if (nb > 9) + ft_put_lu_nbr(nb / 10); + ft_putchar("0123456789"[nb % 10]); +} + +size_t ft_print_signed_int(int nb, t_flags flags) +{ + int blank_len; + long int n; + size_t res; + + res = ft_calc_signed_int_len(nb, flags); + n = (long int)nb; + if (n < 0) + n *= -1; + blank_len = ft_print_signed_int_blank_sign(nb, flags, &res); + if (flags.precision != 0 || nb != 0) + ft_put_lu_nbr((long unsigned int)n); + if (blank_len > 0 && flags.left_justify) + { + if (flags.zero_padding) + res += ft_fill_width('0', blank_len); + else + res += ft_fill_width(' ', blank_len); + } + return (res); +} diff --git a/libft/ft_printf/ft_print_unsigned_int.c b/libft/ft_printf/ft_print_unsigned_int.c new file mode 100755 index 0000000..e324164 --- /dev/null +++ b/libft/ft_printf/ft_print_unsigned_int.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_unsigned_int.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 16:06:48 by tomoron #+# #+# */ +/* Updated: 2023/11/05 16:06:49 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +int ft_calc_unsigned_int_len(unsigned int nb, t_flags flags) +{ + int res; + + res = 1; + if (nb == 0 && flags.precision == 0) + return (0); + while (nb > 9) + { + res++; + nb /= 10; + } + return (res); +} + +size_t ft_print_unsigned_int(unsigned int nb, t_flags flags) +{ + int blank_len; + int zero_len; + int number_len; + size_t res; + + number_len = ft_calc_unsigned_int_len(nb, flags); + res = number_len; + zero_len = 0; + if (flags.precision > number_len) + zero_len = flags.precision - number_len; + blank_len = flags.min_width - zero_len - number_len; + if (blank_len > 0 && !flags.left_justify && flags.zero_padding + && flags.precision == -1) + res += ft_fill_width('0', blank_len); + else if (blank_len > 0 && !flags.left_justify) + res += ft_fill_width(' ', blank_len); + res += ft_fill_width('0', zero_len); + if (flags.precision != 0 || nb != 0) + ft_put_lu_nbr(nb); + if (blank_len > 0 && flags.left_justify && flags.zero_padding) + res += ft_fill_width('0', blank_len); + else if (blank_len > 0 && flags.left_justify) + res += ft_fill_width(' ', blank_len); + return (res); +} diff --git a/libft/ft_printf/ft_printf.c b/libft/ft_printf/ft_printf.c new file mode 100755 index 0000000..19a0964 --- /dev/null +++ b/libft/ft_printf/ft_printf.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/02 23:41:38 by tomoron #+# #+# */ +/* Updated: 2023/11/08 14:51:19 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +int ft_check_placeholders(const char *str) +{ + int err; + + err = 0; + while (*str) + { + while (*str && *str != '%') + str++; + if (*str == '%') + { + str++; + while (*str == '-' || *str == '0' || *str == '#' + || *str == ' ' || *str == '+' || ft_isdigit(*str)) + str++; + if (*str != 'c' && *str != 's' && *str != 'p' && *str != 'd' + && *str && *str != 'i' && *str != 'u' && *str != 'x' + && *str != 'X' && *str != '%') + err = 1; + if (!*str && err == 0) + err = 2; + if (*str == '%') + str++; + } + } + return (err); +} + +int ft_printf(const char *str, ...) +{ + va_list args; + size_t res; + char *s; + int err; + + if (!str) + return (-1); + va_start(args, str); + res = 0; + s = (char *)str; + err = ft_check_placeholders(s); + res += ft_write_str_part(&s); + while (*s) + { + res += ft_parse_arg(&s, args, &err); + if (err == 3) + break ; + res += ft_write_str_part(&s); + } + va_end(args); + if (err >= 2) + return (-1); + return (res); +} diff --git a/libft/ft_printf/ft_printf.h b/libft/ft_printf/ft_printf.h new file mode 100755 index 0000000..dfffefe --- /dev/null +++ b/libft/ft_printf/ft_printf.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */ +/* Updated: 2023/11/15 14:31:34 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H +# include +# include + +typedef struct s_flags +{ + int left_justify; + int zero_padding; + int always_sign_number; + int blank_positive; + int zero_x_prefix; + int min_width; + int precision; +} t_flags; + +int ft_printf(const char *str, ...); +size_t ft_write_str_part(char **s); +size_t ft_parse_arg(char **s, va_list args, int *err); +size_t ft_convert(char *s, t_flags flags, va_list args, int err); +size_t ft_putchar(char c); +size_t ft_putstr(char *s); +size_t ft_strlen(char const *s); +int ft_protected_atoi(const char *s); +int ft_isdigit(int c); +size_t ft_print_unsigned_int(unsigned int nb, t_flags flags); +size_t ft_print_signed_int(int nb, t_flags flags); +size_t ft_fill_width(char c, int n); +size_t ft_print_hex(unsigned int nb, t_flags flags, char mode); +size_t ft_print_ptr(void *ptr, t_flags flags); +size_t ft_print_str(char *s, t_flags flags); +void ft_put_lu_nbr(long unsigned int nb); + +#endif diff --git a/libft/ft_printf/ft_protected_atoi.c b/libft/ft_printf/ft_protected_atoi.c new file mode 100755 index 0000000..09dcfc9 --- /dev/null +++ b/libft/ft_printf/ft_protected_atoi.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atoi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/17 13:41:15 by tomoron #+# #+# */ +/* Updated: 2023/11/15 14:31:19 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_protected_atoi(const char *str) +{ + long long res; + int inv; + + res = 0; + inv = 1; + while (*str == ' ' || (*str >= '\t' && *str <= '\r')) + str++; + if (*str == '+' || *str == '-') + { + if (*str == '-') + inv *= -1; + str++; + } + while (*str >= '0' && *str <= '9') + { + res *= 10; + res += *str - '0'; + str++; + } + if (res > 2147483647) + return (-2); + return ((int)res * inv); +} diff --git a/libft/ft_printf/ft_putchar.c b/libft/ft_printf/ft_putchar.c new file mode 100755 index 0000000..84bbe70 --- /dev/null +++ b/libft/ft_printf/ft_putchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:10:53 by tomoron #+# #+# */ +/* Updated: 2023/11/05 15:51:33 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +size_t ft_putchar(char c) +{ + write(1, &c, 1); + return (1); +} diff --git a/libft/ft_printf/ft_putstr.c b/libft/ft_printf/ft_putstr.c new file mode 100755 index 0000000..c742ffc --- /dev/null +++ b/libft/ft_printf/ft_putstr.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:13:29 by tomoron #+# #+# */ +/* Updated: 2023/11/05 14:42:32 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +size_t ft_putstr(char *s) +{ + if (s) + write(1, s, ft_strlen(s)); + return (ft_strlen(s)); +} diff --git a/libft/ft_printf/ft_strlen.c b/libft/ft_printf/ft_strlen.c new file mode 100755 index 0000000..e9f507f --- /dev/null +++ b/libft/ft_printf/ft_strlen.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:34:41 by tomoron #+# #+# */ +/* Updated: 2023/11/04 08:19:20 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ft_printf.h" + +size_t ft_strlen(const char *str) +{ + unsigned int n; + + n = 0; + while (str[n]) + n++; + return (n); +} diff --git a/libft/ft_printf/ft_write_str_part.c b/libft/ft_printf/ft_write_str_part.c new file mode 100755 index 0000000..3dc57ff --- /dev/null +++ b/libft/ft_printf/ft_write_str_part.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_write_str_part.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 11:16:38 by tomoron #+# #+# */ +/* Updated: 2023/11/05 15:50:41 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" + +size_t ft_write_str_part(char **s) +{ + size_t i; + + i = 0; + while ((*s)[i] != '%' && (*s)[i]) + i++; + if (i) + write(1, *s, i); + *s += i; + return (i); +} diff --git a/libft/ft_putchar_fd.c b/libft/ft_putchar_fd.c new file mode 100755 index 0000000..e9fe3be --- /dev/null +++ b/libft/ft_putchar_fd.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:10:53 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:27:29 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libft/ft_putendl_fd.c b/libft/ft_putendl_fd.c new file mode 100755 index 0000000..1f3cd0d --- /dev/null +++ b/libft/ft_putendl_fd.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putendl_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:13:29 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:25:27 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + if (s) + { + write(fd, s, ft_strlen(s)); + write(fd, "\n", 1); + } +} diff --git a/libft/ft_putnbr_fd.c b/libft/ft_putnbr_fd.c new file mode 100755 index 0000000..2b3059d --- /dev/null +++ b/libft/ft_putnbr_fd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/11 23:27:29 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:06:01 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putnbr_fd(int nb, int fd) +{ + if (nb >= 10) + { + ft_putnbr_fd(nb / 10, fd); + ft_putchar_fd((nb % 10) + 48, fd); + } + else if (nb < 0) + { + if (nb == -2147483648) + write(fd, "-2147483648", 11); + else + { + ft_putchar_fd('-', fd); + ft_putnbr_fd(nb * -1, fd); + } + } + else + ft_putchar_fd((nb % 10) + 48, fd); +} diff --git a/libft/ft_putstr_fd.c b/libft/ft_putstr_fd.c new file mode 100755 index 0000000..8f58d10 --- /dev/null +++ b/libft/ft_putstr_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putstr_fd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:13:29 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:25:39 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + if (s) + write(fd, s, ft_strlen(s)); +} diff --git a/libft/ft_set_color.c b/libft/ft_set_color.c new file mode 100755 index 0000000..f44b6e6 --- /dev/null +++ b/libft/ft_set_color.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_set_color.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/10 10:56:11 by tomoron #+# #+# */ +/* Updated: 2024/01/10 14:10:42 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_set_color(int r, int g, int b) +{ + ft_printf("\033[38;2;%d;%d;%dm", r, g, b); +} + +void ft_reset_color(void) +{ + ft_printf("\033[0m"); +} diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100755 index 0000000..000e1b4 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,73 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/25 10:30:03 by tomoron #+# #+# */ +/* Updated: 2023/11/01 15:18:57 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int ft_count_parts(char *str, char charset) +{ + int res; + + res = 0; + while (*str == charset && *str) + str++; + while (*str) + { + while (!(*str == charset) && *str) + str++; + while (*str == charset && *str) + str++; + res++; + } + return (res + 1); +} + +static int ft_strlen_sep(char *str, char charset) +{ + int res; + + res = 0; + while (*str && !(*str == charset)) + { + res++; + str++; + } + return (res + 1); +} + +char **ft_split(const char *str, char charset) +{ + int nb_str; + char **res; + int i; + int j; + + if (!str) + return (0); + nb_str = ft_count_parts((char *)str, charset); + res = (char **)malloc(nb_str * sizeof(char *)); + i = -1; + while (res && *str && *str == charset) + str++; + while (++i < nb_str - 1 && *str && res) + { + res[i] = malloc(ft_strlen_sep((char *)str, charset)); + j = 0; + while (res[i] && *str && !(*str == charset)) + res[i][j++] = *(str++); + res[i][j] = 0; + while (*str == charset) + str++; + } + if (res) + res[i] = 0; + return (res); +} diff --git a/libft/ft_split_set.c b/libft/ft_split_set.c new file mode 100755 index 0000000..023f0ce --- /dev/null +++ b/libft/ft_split_set.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_set.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/25 10:30:03 by tomoron #+# #+# */ +/* Updated: 2023/11/21 15:37:48 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int ft_is_sep(char c, char *sep) +{ + while (*sep) + { + if (*sep == c) + return (1); + sep++; + } + return (0); +} + +int ft_count_parts(char *str, char *charset) +{ + int res; + + res = 0; + while (ft_is_sep(*str, charset) && *str) + str++; + while (*str) + { + while (!ft_is_sep(*str, charset) && *str) + str++; + while (ft_is_sep(*str, charset) && *str) + str++; + res++; + } + return (res + 1); +} + +int ft_strlen_sep(char *str, char *charset) +{ + int res; + + res = 0; + while (*str && !ft_is_sep(*str, charset)) + { + res++; + str++; + } + return (res + 1); +} + +char **ft_split_set(char *str, char *charset) +{ + int str_len; + int nb_str; + char **res; + int i; + int j; + + nb_str = ft_count_parts(str, charset); + res = (char **)malloc(nb_str * sizeof(char *)); + i = -1; + while (ft_is_sep(*str, charset)) + str++; + while (++i < nb_str - 1 && *str && res) + { + str_len = ft_strlen_sep(str, charset); + res[i] = malloc(str_len * sizeof(char)); + j = 0; + while (res[i] && *str && !ft_is_sep(*str, charset)) + res[i][j++] = *(str++); + res[i][j] = 0; + while (ft_is_sep(*str, charset)) + str++; + } + if (res) + res[i] = 0; + return (res); +} diff --git a/libft/ft_strchr.c b/libft/ft_strchr.c new file mode 100755 index 0000000..978f990 --- /dev/null +++ b/libft/ft_strchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 13:35:44 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:08:17 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + int i; + char *res; + + i = 0; + res = (char *)s; + while (res[i]) + { + if (res[i] == (unsigned char)c) + return (res + i); + i++; + } + if (res[i] == (unsigned char)c) + return (res + i); + return (NULL); +} diff --git a/libft/ft_strcmp.c b/libft/ft_strcmp.c new file mode 100755 index 0000000..6157587 --- /dev/null +++ b/libft/ft_strcmp.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strncmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/15 21:47:47 by tomoron #+# #+# */ +/* Updated: 2023/12/03 17:06:39 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + unsigned int i; + + i = 0; + if (!n) + return (0); + while (s1[i] == s2[i] && s1[i] && i < n - 1) + i++; + return ((unsigned char)s1[i] - (unsigned char)s2[i]); +} + +int ft_strcmp(char *s1, char *s2) +{ + int i; + + i = 0; + while (s1[i] == s2[i] && s1[i]) + i++; + return (s1[i] - s2[i]); +} diff --git a/libft/ft_strdup.c b/libft/ft_strdup.c new file mode 100755 index 0000000..8a968e4 --- /dev/null +++ b/libft/ft_strdup.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strdup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/24 14:55:26 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:13:46 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strdup(const char *src) +{ + char *res; + int len; + int i; + + len = 0; + i = 0; + if (!src) + return (0); + while (src[len]) + len++; + res = (char *)malloc((len + 1) * sizeof(char)); + if (!res) + return (0); + while (src[i]) + { + res[i] = src[i]; + i++; + } + res[i] = 0; + return (res); +} diff --git a/libft/ft_striteri.c b/libft/ft_striteri.c new file mode 100755 index 0000000..488b17e --- /dev/null +++ b/libft/ft_striteri.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_striteri.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 04:08:53 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:04:59 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + int i; + + i = 0; + while (s && s[i]) + { + f(i, s + i); + i++; + } +} diff --git a/libft/ft_strjoin.c b/libft/ft_strjoin.c new file mode 100755 index 0000000..cec1deb --- /dev/null +++ b/libft/ft_strjoin.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/24 18:06:14 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:26:09 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static int calc_str_len(char const *s1, char const *s2) +{ + int res; + int i; + + res = 0; + i = 0; + while (s1[i]) + { + i++; + res++; + } + i = 0; + while (s2[i]) + { + i++; + res++; + } + return (res); +} + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *res; + int len; + int i; + int j; + + j = 0; + if (!s1 || !s2) + return (0); + len = calc_str_len(s1, s2); + res = malloc((len + 1) * sizeof(char)); + while (res && s1[j]) + { + res[j] = s1[j]; + j++; + } + i = 0; + while (res && s2[i]) + { + res[j] = s2[i]; + i++; + j++; + } + if (res) + res[j] = 0; + return (res); +} diff --git a/libft/ft_strlcat.c b/libft/ft_strlcat.c new file mode 100755 index 0000000..2444604 --- /dev/null +++ b/libft/ft_strlcat.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/16 03:16:22 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:47:42 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t size) +{ + unsigned int dst_len; + unsigned int i; + unsigned int ret_val; + + if (!dst || !src) + return (0); + dst_len = ft_strlen(dst); + if (!size) + return (ft_strlen(src)); + if (size < dst_len) + return (ft_strlen(src) + size); + i = 0; + ret_val = dst_len + ft_strlen(src); + while (src[i] && size > dst_len + 1) + { + dst[dst_len] = src[i]; + i++; + dst_len++; + } + dst[dst_len] = 0; + return (ret_val); +} diff --git a/libft/ft_strlcpy.c b/libft/ft_strlcpy.c new file mode 100755 index 0000000..9b82fdd --- /dev/null +++ b/libft/ft_strlcpy.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlcpy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/14 01:08:34 by tomoron #+# #+# */ +/* Updated: 2023/10/31 18:26:25 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dest, const char *src, size_t size) +{ + unsigned int i; + + i = 0; + if (size == 0) + return (ft_strlen(src)); + while (src[i] && i < size) + { + dest[i] = src[i]; + i++; + } + if (i == size) + dest[i - 1] = 0; + else + dest[i] = 0; + return (ft_strlen(src)); +} diff --git a/libft/ft_strlen.c b/libft/ft_strlen.c new file mode 100755 index 0000000..0e7d7dc --- /dev/null +++ b/libft/ft_strlen.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strlen.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 12:34:41 by tomoron #+# #+# */ +/* Updated: 2023/10/31 14:53:10 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + unsigned int n; + + n = 0; + while (str[n]) + n++; + return (n); +} diff --git a/libft/ft_strmapi.c b/libft/ft_strmapi.c new file mode 100755 index 0000000..ac725ae --- /dev/null +++ b/libft/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/01 03:54:38 by tomoron #+# #+# */ +/* Updated: 2023/11/01 14:06:34 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + int i; + char *res; + + i = 0; + if (!s) + return (0); + res = malloc((ft_strlen(s) + 1) * sizeof(char)); + if (!res) + return (res); + while (s[i]) + { + res[i] = f(i, s[i]); + i++; + } + res[i] = 0; + return (res); +} diff --git a/libft/ft_strnstr.c b/libft/ft_strnstr.c new file mode 100755 index 0000000..02c8a18 --- /dev/null +++ b/libft/ft_strnstr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/16 02:50:08 by tomoron #+# #+# */ +/* Updated: 2023/11/01 13:21:17 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strnstr(const char *str, const char *to_find, size_t len) +{ + unsigned int s_i; + unsigned int tf_i; + unsigned int tf_l; + char *res; + + s_i = 0; + tf_l = 0; + res = (char *)str; + while (to_find && to_find[tf_l]) + tf_l++; + if (tf_l == 0) + return (res); + while (str && str[s_i] && s_i <= len) + { + tf_i = 0; + while (str[s_i + tf_i] == to_find[tf_i] + && to_find[tf_i] && s_i + tf_i <= len) + tf_i++; + if (tf_i == tf_l && s_i + tf_i <= len) + return (res + s_i); + s_i++; + } + return (0); +} diff --git a/libft/ft_strrchr.c b/libft/ft_strrchr.c new file mode 100755 index 0000000..56fae39 --- /dev/null +++ b/libft/ft_strrchr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 13:35:44 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:25:44 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + char *p; + + p = (char *)s; + i = 0; + while (s[i]) + i++; + while (i >= 0) + { + if (p[i] == (unsigned char)c) + return (p + i); + i--; + } + return (NULL); +} diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c new file mode 100755 index 0000000..1f6bb08 --- /dev/null +++ b/libft/ft_strtrim.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strtrim.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/31 12:07:01 by tomoron #+# #+# */ +/* Updated: 2023/11/02 10:24:37 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +static int is_char_in_set(char c, char const *set) +{ + int i; + + i = 0; + while (set[i]) + { + if (set[i] == c) + return (1); + i++; + } + return (0); +} + +static int calc_len(char const *s1, char const *set) +{ + int len; + int i; + + len = 0; + i = 0; + while (is_char_in_set(s1[i], set)) + i++; + while (s1[i]) + { + i++; + len++; + } + i--; + while (is_char_in_set(s1[i], set) && i) + { + i--; + len--; + } + if (len < 0) + len = 0; + return (len); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + char *res; + int len; + int i; + int res_i; + + if (!s1 || !set) + return (0); + len = calc_len(s1, set); + res = malloc((len + 1) * sizeof(char)); + i = 0; + if (!res) + return (res); + while (is_char_in_set(s1[i], set)) + i++; + res_i = 0; + while (res_i < len) + { + res[res_i] = s1[i]; + res_i++; + i++; + } + res[res_i] = 0; + return (res); +} diff --git a/libft/ft_substr.c b/libft/ft_substr.c new file mode 100755 index 0000000..aae0351 --- /dev/null +++ b/libft/ft_substr.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/31 11:59:20 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:00:04 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + unsigned int i; + char *res; + unsigned int res_len; + + res_len = 0; + if (!s) + return (0); + if (ft_strlen(s) > start) + while (res_len < len && s[start + res_len]) + res_len++; + res = malloc((res_len + 1) * sizeof(char)); + if (!res) + return (res); + i = 0; + while (i < res_len) + { + res[i] = s[start + i]; + i++; + } + res[i] = 0; + return (res); +} diff --git a/libft/ft_swap.c b/libft/ft_swap.c new file mode 100755 index 0000000..851ca4e --- /dev/null +++ b/libft/ft_swap.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/31 12:44:19 by tomoron #+# #+# */ +/* Updated: 2023/12/31 12:45:27 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +void ft_swap(int *n1, int *n2) +{ + int tmp; + + tmp = *n1; + *n1 = *n2; + *n2 = tmp; +} diff --git a/libft/ft_tolower.c b/libft/ft_tolower.c new file mode 100755 index 0000000..44cdea6 --- /dev/null +++ b/libft/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_tolower.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 21:59:21 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:00:47 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c >= 'A' && c <= 'Z') + c += 32; + return (c); +} diff --git a/libft/ft_toupper.c b/libft/ft_toupper.c new file mode 100755 index 0000000..e6e06b3 --- /dev/null +++ b/libft/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_toupper.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/07/13 21:59:21 by tomoron #+# #+# */ +/* Updated: 2023/11/02 11:00:56 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c >= 'a' && c <= 'z') + c -= 32; + return (c); +} diff --git a/libft/gnl/Makefile b/libft/gnl/Makefile new file mode 100755 index 0000000..42150c1 --- /dev/null +++ b/libft/gnl/Makefile @@ -0,0 +1,42 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tomoron +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2023/11/04 08:03:00 by tomoron #+# #+# # +# Updated: 2023/12/04 09:49:24 by tomoron ### ########.fr # +# # +# **************************************************************************** # + +NAME = gnl.a + +CC = gcc + +SRCS = get_next_line_bonus.c\ + get_next_line_utils_bonus.c + +OBJS = $(SRCS:.c=.o) + +FLAGS = -Wall -Wextra -Werror + +$(NAME): $(OBJS) + ar rcs $(NAME) $(OBJS) + +.c.o: + $(CC) $(FLAGS) -c $< -o $@ + +all: $(NAME) + +bonus: all + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: bonus clean all re fclean diff --git a/libft/gnl/get_next_line_bonus.c b/libft/gnl/get_next_line_bonus.c new file mode 100755 index 0000000..658e574 --- /dev/null +++ b/libft/gnl/get_next_line_bonus.c @@ -0,0 +1,113 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 17:03:11 by tomoron #+# #+# */ +/* Updated: 2023/12/04 09:56:26 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "get_next_line_bonus.h" + +char *get_next_line(int fd) +{ + static t_buffer *buffer_start; + t_buffer *buffer; + int nl_found; + int start; + t_result *result; + + nl_found = 0; + result = 0; + start = 0; + buffer = create_find_buffer(&buffer_start, fd); + if (BUFFER_SIZE < 0 || !buffer) + return (0); + if (fd >= 0 && (buffer->i == 0 || buffer->i == buffer->rd_l)) + buffer->rd_l = read(fd, buffer->str, BUFFER_SIZE); + else + start = buffer->i; + while (fd >= 0 && !nl_found && buffer->rd_l > 0) + { + nl_found = find_nl(buffer, &start); + if (!ft_lstadd_bak(&result, buffer->str, buffer->i, start)) + return (ft_lstclr(&result, NULL, NULL)); + if (!nl_found) + buffer->rd_l = read(fd, buffer->str, BUFFER_SIZE); + } + return (result_to_str(result, &buffer_start, buffer)); +} + +int find_nl(t_buffer *buffer, int *start) +{ + int nl_found; + + nl_found = 0; + if (buffer->i == BUFFER_SIZE) + *start = 0; + buffer->i = *start; + while (buffer->i < buffer->rd_l && !nl_found) + { + if (buffer->str[buffer->i] == '\n') + nl_found = 1; + (buffer->i)++; + } + return (nl_found); +} + +t_buffer *create_find_buffer(t_buffer **buffer_start, int fd) +{ + t_buffer *current; + t_buffer *last; + + current = *buffer_start; + last = 0; + while (current) + { + if (current->fd == fd) + return (current); + last = current; + current = current->next; + } + if (last) + { + last->next = malloc(sizeof(t_buffer)); + last = last->next; + } + else + { + last = malloc(sizeof(t_buffer)); + *buffer_start = last; + } + if (last) + init_buffer(last, fd); + return (last); +} + +void init_buffer(t_buffer *last, int fd) +{ + last->fd = fd; + last->i = 0; + last->rd_l = 0; + last->next = 0; +} + +void ft_lstdelon(t_buffer *node, t_buffer **start) +{ + t_buffer *current; + t_buffer *next; + + if (!start || !node || !*start) + return ; + current = *start; + while (current->next && current->next != node) + current = current->next; + next = node->next; + free(node); + if (node == *start) + *start = next; + else + current->next = next; +} diff --git a/libft/gnl/get_next_line_bonus.h b/libft/gnl/get_next_line_bonus.h new file mode 100755 index 0000000..28facee --- /dev/null +++ b/libft/gnl/get_next_line_bonus.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 17:24:39 by tomoron #+# #+# */ +/* Updated: 2023/12/04 09:57:06 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef GET_NEXT_LINE_BONUS_H +# define GET_NEXT_LINE_BONUS_H +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 42 +# endif + +typedef struct s_result +{ + char part[BUFFER_SIZE + 1]; + struct s_result *next; + +} t_result; + +typedef struct s_buffer +{ + char str[BUFFER_SIZE]; + int i; + int rd_l; + int fd; + struct s_buffer *next; +} t_buffer; + +char *get_next_line(int fd); +char *result_to_str(t_result *lst, t_buffer **buf_st, t_buffer *buffer); +t_result *ft_lstadd_bak(t_result **lst, char *buffer, int n, int start); +char *ft_lstclr(t_result **lst, t_buffer **buf_str, t_buffer *buffer); +int find_nl(t_buffer *buffer, int *start); +t_buffer *create_find_buffer(t_buffer **buffer_start, int fd); +void init_buffer(t_buffer *last, int fd); +void ft_lstdelon(t_buffer *node, t_buffer **start); +#endif diff --git a/libft/gnl/get_next_line_utils_bonus.c b/libft/gnl/get_next_line_utils_bonus.c new file mode 100755 index 0000000..1233d60 --- /dev/null +++ b/libft/gnl/get_next_line_utils_bonus.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/05 17:35:15 by tomoron #+# #+# */ +/* Updated: 2023/12/04 09:58:25 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "get_next_line_bonus.h" + +char *ft_strncpy(char *dest, char *src, size_t n) +{ + size_t i; + + i = 0; + while (i < (n)) + { + dest[i] = src[i]; + i++; + } + dest[i] = 0; + return (dest); +} + +t_result *ft_lstadd_bak(t_result **lst, char *buffer, int n, int start) +{ + t_result *current; + + if (!lst) + return (0); + current = *lst; + while (current && current->next) + current = current->next; + if (!current) + { + *lst = malloc(sizeof(t_result)); + if (!*lst) + return (0); + ft_strncpy((*lst)->part, buffer + start, n - start); + (*lst)->next = 0; + } + else + { + current->next = malloc(sizeof(t_result)); + if (!current->next) + return (0); + ft_strncpy(current->next->part, buffer + start, n - start); + current->next->next = 0; + } + return (*lst); +} + +char *ft_lstclr(t_result **lst, t_buffer **buf_srt, t_buffer *buffer) +{ + t_result *next; + + if (lst) + { + while (*lst) + { + next = (*lst)->next; + free(*lst); + *lst = next; + } + } + if (buf_srt && buffer && (buffer->rd_l == 0 || buffer->i == buffer->rd_l + || buffer->rd_l == -1)) + ft_lstdelon(buffer, buf_srt); + return (0); +} + +int lst_str_len(t_result *lst) +{ + int i; + int res; + + res = 0; + while (lst) + { + i = 0; + while (lst->part[i]) + i++; + res += i; + lst = lst->next; + } + return (res); +} + +char *result_to_str(t_result *lst, t_buffer **buf_srt, t_buffer *buffer) +{ + char *res; + int res_i; + int p_i; + t_result *start; + + if (!lst) + return (ft_lstclr(&lst, buf_srt, buffer)); + res = malloc((lst_str_len(lst) + 1) * sizeof(char)); + start = lst; + res_i = 0; + while (res && lst) + { + p_i = 0; + while (lst->part[p_i]) + { + res[res_i] = lst->part[p_i]; + p_i++; + res_i++; + } + lst = lst->next; + } + if (res) + res[res_i] = 0; + ft_lstclr(&start, buf_srt, buffer); + return (res); +} diff --git a/libft/libft.h b/libft/libft.h new file mode 100755 index 0000000..87fa052 --- /dev/null +++ b/libft/libft.h @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* libft.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */ +/* Updated: 2024/01/10 14:10:51 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include +# include +# include +# include "./ft_printf/ft_printf.h" +# include "./gnl/get_next_line_bonus.h" + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_atoi(const char *str); +void ft_bzero(void *s, size_t n); +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); +void *ft_memchr(const void *s, int c, size_t n); +int ft_memcmp(const void *s1, const void *s2, 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); +char *ft_strchr(const char *s, int c); +size_t ft_strlcat(char *dest, const char *src, size_t size); +size_t ft_strlcpy(char *dest, const char *src, size_t size); +size_t ft_strlen(const char *str); +int ft_strncmp(const char *s1, const char *s2, size_t n); +char *ft_strnstr(const char *str, const char *to_find, size_t len); +char *ft_strrchr(const char *s, int c); +int ft_tolower(int c); +int ft_toupper(int c); +void *ft_calloc(size_t nb, size_t size); +char *ft_strdup(const char *src); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char *ft_substr(char const *s, unsigned int start, size_t len); +char **ft_split(char const *str, char charset); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstclear(t_list **lst, void (*del)(void*)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +char **ft_split_set(char *str, char *charset); +int ft_strcmp(char *s1, char *s2); +void ft_free_str_arr(char **arr); +void ft_swap(int *n1, int *n2); +void ft_set_color(int r, int g, int b); +void ft_reset_color(void); + +#endif