From cfccb906992539b81ed8443dae11cbfefe72b450 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 3 Nov 2023 17:03:35 +0100 Subject: [PATCH] Change subproject handling, add advanced list funcs Turn submodules into standard folder. - Submodules were probably not the correct choice int the first place - Makefile handling of submodules was bad and hacky Turn list handling to subproject ft_lst, add advanced list handling. Change Makefile and header accordingly. --- .gitmodules | 4 - Makefile | 118 ++++++++++++------ ft_lst/ft_lst_at.c | 26 ++++ ft_lst/ft_lst_find.c | 25 ++++ ft_lst/ft_lst_foreach_if.c | 25 ++++ ft_lst/ft_lst_merge.c | 27 ++++ ft_lst/ft_lst_remove_if.c | 39 ++++++ ft_lst/ft_lst_reverse.c | 33 +++++ ft_lst/ft_lst_sort.c | 43 +++++++ ft_lst/ft_lst_sorted_insert.c | 51 ++++++++ ft_lst/ft_lst_sorted_merge.c | 35 ++++++ ft_lstadd_back.c => ft_lst/ft_lstadd_back.c | 4 +- ft_lstadd_front.c => ft_lst/ft_lstadd_front.c | 4 +- ft_lstclear.c => ft_lst/ft_lstclear.c | 0 ft_lstdelone.c => ft_lst/ft_lstdelone.c | 0 ft_lstiter.c => ft_lst/ft_lstiter.c | 0 ft_lstlast.c => ft_lst/ft_lstlast.c | 0 ft_lstmap.c => ft_lst/ft_lstmap.c | 0 ft_lstnew.c => ft_lst/ft_lstnew.c | 11 +- ft_lstsize.c => ft_lst/ft_lstsize.c | 8 +- ft_printf | 1 - ft_printf/conversion.c | 107 ++++++++++++++++ ft_printf/formatting.c | 106 ++++++++++++++++ ft_printf/ft_printf.c | 46 +++++++ ft_printf/ft_printf.h | 51 ++++++++ ft_printf/padding.c | 82 ++++++++++++ ft_printf/parsing.c | 78 ++++++++++++ ft_swap.c | 23 ++++ libft.h | 17 ++- 29 files changed, 907 insertions(+), 57 deletions(-) create mode 100644 ft_lst/ft_lst_at.c create mode 100644 ft_lst/ft_lst_find.c create mode 100644 ft_lst/ft_lst_foreach_if.c create mode 100644 ft_lst/ft_lst_merge.c create mode 100644 ft_lst/ft_lst_remove_if.c create mode 100644 ft_lst/ft_lst_reverse.c create mode 100644 ft_lst/ft_lst_sort.c create mode 100644 ft_lst/ft_lst_sorted_insert.c create mode 100644 ft_lst/ft_lst_sorted_merge.c rename ft_lstadd_back.c => ft_lst/ft_lstadd_back.c (92%) rename ft_lstadd_front.c => ft_lst/ft_lstadd_front.c (91%) rename ft_lstclear.c => ft_lst/ft_lstclear.c (100%) rename ft_lstdelone.c => ft_lst/ft_lstdelone.c (100%) rename ft_lstiter.c => ft_lst/ft_lstiter.c (100%) rename ft_lstlast.c => ft_lst/ft_lstlast.c (100%) rename ft_lstmap.c => ft_lst/ft_lstmap.c (100%) rename ft_lstnew.c => ft_lst/ft_lstnew.c (87%) rename ft_lstsize.c => ft_lst/ft_lstsize.c (88%) delete mode 160000 ft_printf create mode 100644 ft_printf/conversion.c create mode 100644 ft_printf/formatting.c create mode 100644 ft_printf/ft_printf.c create mode 100644 ft_printf/ft_printf.h create mode 100644 ft_printf/padding.c create mode 100644 ft_printf/parsing.c create mode 100644 ft_swap.c diff --git a/.gitmodules b/.gitmodules index f29e137..e69de29 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +0,0 @@ -[submodule "ft_printf"] - path = ft_printf - url = git://78.102.58.167/ft_printf - branch = inLibft diff --git a/Makefile b/Makefile index b9c0162..92ef2da 100644 --- a/Makefile +++ b/Makefile @@ -1,55 +1,97 @@ -CC = cc -CFLAGS = -Wall -Wextra -Werror -AR = ar +CC := cc +CFLAGS := -Wall -Wextra -Werror +AR := ar -RM = rm -f +RM := rm -f -SRCDIR = . -INCDIR = $(SRCDIR) -SOURCES = $(shell find $(SRCDIR) -maxdepth 1 -name "*.c") -OBJECTS = $(SOURCES:.c=.o) - -# Temporary dir to extract all sub-libraries to -LIBTEMP = .libtemp -# This finds all the directories except hidden ones and '.' (assumes only visible dirs are project ones) -# It may prove better to find dirs with Makefiles inside -COMPLEX_PROJECTS := $(shell find $(SRCDIR) -maxdepth 1 -not -path '*/.*' -not -name . -type d) - -COMPLEX_LIBS = $(foreach DIR, $(COMPLEX_PROJECTS:./%=%), ./$(DIR)/$(DIR).a) +SRCDIR := . +INCLUDE := $(addprefix -I, $(SRCDIR)) +SOURCES := ft_strndup.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_atoi.c \ + ft_strtrim.c \ + ft_itoa.c \ + ft_strnstr.c \ + ft_memmove.c \ + ft_putnbr_fd.c \ + ft_memchr.c \ + ft_strrchr.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_memcmp.c \ + ft_substr.c \ + ft_tolower.c \ + ft_isalnum.c \ + ft_strlen.c \ + ft_isspace.c \ + ft_isdigit.c \ + ft_calloc.c \ + ft_strjoin.c \ + ft_isprint.c \ + ft_strmapi.c \ + ft_split.c \ + ft_toupper.c \ + ft_strlcat.c \ + ft_bzero.c \ + ft_abs.c \ + ft_isalpha.c \ + ft_memset.c \ + ft_striteri.c \ + ft_isupper.c \ + ft_isascii.c \ + ft_islower.c \ + ft_strlcpy.c \ + ft_memcpy.c \ + ft_itoa_base.c \ + ft_putchar_fd.c \ + ft_ctoa.c \ + ft_uitoa_base.c \ + ft_strncmp.c \ + ft_swap.c \ + \ + ft_lst/ft_lstnew.c \ + ft_lst/ft_lstiter.c \ + ft_lst/ft_lstmap.c \ + ft_lst/ft_lstclear.c \ + ft_lst/ft_lstlast.c \ + ft_lst/ft_lstsize.c \ + ft_lst/ft_lstadd_front.c \ + ft_lst/ft_lstadd_back.c \ + ft_lst/ft_lstdelone.c \ + ft_lst/ft_lst_at.c \ + ft_lst/ft_lst_find.c \ + ft_lst/ft_lst_foreach_if.c \ + ft_lst/ft_lst_merge.c \ + ft_lst/ft_lst_remove_if.c \ + ft_lst/ft_lst_reverse.c \ + ft_lst/ft_lst_sort.c \ + ft_lst/ft_lst_sorted_insert.c \ + ft_lst/ft_lst_sorted_merge.c \ + \ + ft_printf/conversion.c \ + ft_printf/formatting.c \ + ft_printf/ft_printf.c \ + ft_printf/padding.c \ + ft_printf/parsing.c \ + +OBJECTS := $(SOURCES:.c=.o) NAME = libft.a all : $(NAME) -$(NAME) : $(OBJECTS) $(COMPLEX_LIBS) $(LIBTEMP) - for lib in $(COMPLEX_LIBS) ; do \ - $(AR) x $$lib --output $(LIBTEMP) ; \ - done - $(AR) rcs $(NAME) $(OBJECTS) $(shell find $(LIBTEMP) -type f) +$(NAME) : $(OBJECTS) + $(AR) rcs $@ $^ %.o : %.c - $(CC) $(CFLAGS) -o $@ -c $< -I $(INCDIR) - -$(COMPLEX_LIBS) : - $(MAKE) -C $(shell dirname $@) - -$(LIBTEMP): - mkdir -p $(LIBTEMP) + $(CC) $(CFLAGS) -o $@ -c $< $(INCLUDE) clean : $(RM) $(OBJECTS) - $(RM) -r $(LIBTEMP) - for subpr in $(COMPLEX_PROJECTS) ; do \ - $(MAKE) -C $$subpr clean ; \ - done -fclean : +fclean : clean $(RM) $(NAME) - $(RM) $(OBJECTS) - $(RM) -r $(LIBTEMP) - for subpr in $(COMPLEX_PROJECTS) ; do \ - $(MAKE) -C $$subpr fclean ; \ - done re : fclean $(MAKE) all diff --git a/ft_lst/ft_lst_at.c b/ft_lst/ft_lst_at.c new file mode 100644 index 0000000..edd6816 --- /dev/null +++ b/ft_lst/ft_lst_at.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_at.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:19:21 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:25:36 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lst_at(t_list *lst, unsigned int i) +{ + unsigned int j; + + j = 0; + while (j < i && lst) + { + lst = lst->next; + ++j; + } + return (lst); +} diff --git a/ft_lst/ft_lst_find.c b/ft_lst/ft_lst_find.c new file mode 100644 index 0000000..a369a10 --- /dev/null +++ b/ft_lst/ft_lst_find.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_find.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:47:42 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:26:31 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +t_list *ft_lst_find(t_list *lst, void *content_ref, int (*cmp)()) +{ + while (lst) + { + if (cmp(lst->content, content_ref) == 0) + return (lst); + lst = lst->next; + } + return (NULL); +} diff --git a/ft_lst/ft_lst_foreach_if.c b/ft_lst/ft_lst_foreach_if.c new file mode 100644 index 0000000..56703c9 --- /dev/null +++ b/ft_lst/ft_lst_foreach_if.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_foreach_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:42:33 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:31:10 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_foreach_if(t_list *lst, void (*f)(void *), + void *content_ref, int (*cmp)()) +{ + while (lst) + { + if (cmp(lst->content, content_ref) == 0) + f(lst->content); + lst = lst->next; + } + return ; +} diff --git a/ft_lst/ft_lst_merge.c b/ft_lst/ft_lst_merge.c new file mode 100644 index 0000000..ceef0bd --- /dev/null +++ b/ft_lst/ft_lst_merge.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:09:00 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:29:28 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_merge(t_list **lst1, t_list *lst2) +{ + t_list *last; + + if (!*lst1) + { + *lst1 = lst2; + return ; + } + last = ft_lstlast(*lst1); + last->next = lst2; + return ; +} diff --git a/ft_lst/ft_lst_remove_if.c b/ft_lst/ft_lst_remove_if.c new file mode 100644 index 0000000..cc111ce --- /dev/null +++ b/ft_lst/ft_lst_remove_if.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_remove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:49:53 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:30:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static void remove_next_el(t_list *prev, t_list **cur, void (*free_fct)(void *)) +{ + prev->next = (*cur)->next; + free_fct((*cur)->content); + free(*cur); + *cur = prev->next; + return ; +} + +void ft_lst_remove_if(t_list **lst, void *content_ref, + int (*cmp)(), void (*free_fct)(void *)) +{ + t_list *prev; + + prev = NULL; + while (*lst) + { + if (cmp((*lst)->content, content_ref) == 0) + remove_next_el(prev, lst, free_fct); + else + *lst = (*lst)->next; + } + return ; +} diff --git a/ft_lst/ft_lst_reverse.c b/ft_lst/ft_lst_reverse.c new file mode 100644 index 0000000..236a586 --- /dev/null +++ b/ft_lst/ft_lst_reverse.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_reverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 14:22:47 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:31:38 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +void ft_lst_reverse(t_list **lst) +{ + t_list *prev; + t_list *current; + t_list *next; + + prev = NULL; + current = *lst; + while (current) + { + next = current->next; + current->next = prev; + prev = current; + current = next; + } + *lst = prev; + return ; +} diff --git a/ft_lst/ft_lst_sort.c b/ft_lst/ft_lst_sort.c new file mode 100644 index 0000000..7aa562b --- /dev/null +++ b/ft_lst/ft_lst_sort.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:13:29 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:53:27 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void ft_max_to_end(t_list *lst, int (*cmp)(), int size) +{ + int i; + + i = 0; + while (i < size - 1) + { + if (cmp(lst->content, lst->next->content) > 0) + ft_swap(&(lst->content), &(lst->next->content)); + lst = lst->next; + } + return ; +} + +// Bubble sort +void ft_lst_sort(t_list **lst, int (*cmp)()) +{ + int size; + int i; + + size = ft_lstsize(*lst); + i = 0; + while (i < size) + { + ft_max_to_end(*lst, cmp, size - i); + ++i; + } + return ; +} diff --git a/ft_lst/ft_lst_sorted_insert.c b/ft_lst/ft_lst_sorted_insert.c new file mode 100644 index 0000000..edb84ff --- /dev/null +++ b/ft_lst/ft_lst_sorted_insert.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 15:53:26 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:49:00 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "libft.h" + +static void insert_after(t_list *prev, t_list *new) +{ + if (!new) + return ; + new->next = prev->next; + prev->next = new->next; + return ; +} + +void ft_lst_sorted_insert(t_list **lst, t_list *new, int (*cmp)()) +{ + t_list *prev; + t_list *cur; + + if (!lst || !new) + return ; + if (!*lst) + { + *lst = new; + return ; + } + prev = NULL; + cur = *lst; + while (cur) + { + if (cmp(new->content, cur->content) <= 0) + break ; + prev = cur; + cur = cur->next; + } + if (prev) + insert_after(prev, new); + else + ft_lstadd_front(lst, new); + return ; +} diff --git a/ft_lst/ft_lst_sorted_merge.c b/ft_lst/ft_lst_sorted_merge.c new file mode 100644 index 0000000..0e61033 --- /dev/null +++ b/ft_lst/ft_lst_sorted_merge.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lst_sorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/06/28 17:13:15 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:59:19 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lst_sorted_merge(t_list **lst1, t_list *lst2, int (*cmp)()) +{ + t_list *main; + + if (cmp((*lst1)->content, lst2->content) > 0) + { + main = lst2; + lst2 = *lst1; + *lst1 = main; + } + else + main = *lst1; + while (main->next) + { + if (cmp(main->next->content, lst2->content) > 0) + ft_swap((void **)&main, (void **)&lst2); + main = main->next; + } + main->next = lst2; + return ; +} diff --git a/ft_lstadd_back.c b/ft_lst/ft_lstadd_back.c similarity index 92% rename from ft_lstadd_back.c rename to ft_lst/ft_lstadd_back.c index be63a9e..78615a2 100644 --- a/ft_lstadd_back.c +++ b/ft_lst/ft_lstadd_back.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:33:28 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 16:52:43 by ljiriste ### ########.fr */ +/* Updated: 2023/11/03 16:49:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ void ft_lstadd_back(t_list **lst, t_list *new) { + if (!new) + return ; if (*lst == NULL) { *lst = new; diff --git a/ft_lstadd_front.c b/ft_lst/ft_lstadd_front.c similarity index 91% rename from ft_lstadd_front.c rename to ft_lst/ft_lstadd_front.c index 4995275..892c2a6 100644 --- a/ft_lstadd_front.c +++ b/ft_lst/ft_lstadd_front.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:26:20 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 13:42:49 by ljiriste ### ########.fr */ +/* Updated: 2023/11/03 16:50:39 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ void ft_lstadd_front(t_list **lst, t_list *new) { + if (!new) + return ; new->next = *lst; *lst = new; return ; diff --git a/ft_lstclear.c b/ft_lst/ft_lstclear.c similarity index 100% rename from ft_lstclear.c rename to ft_lst/ft_lstclear.c diff --git a/ft_lstdelone.c b/ft_lst/ft_lstdelone.c similarity index 100% rename from ft_lstdelone.c rename to ft_lst/ft_lstdelone.c diff --git a/ft_lstiter.c b/ft_lst/ft_lstiter.c similarity index 100% rename from ft_lstiter.c rename to ft_lst/ft_lstiter.c diff --git a/ft_lstlast.c b/ft_lst/ft_lstlast.c similarity index 100% rename from ft_lstlast.c rename to ft_lst/ft_lstlast.c diff --git a/ft_lstmap.c b/ft_lst/ft_lstmap.c similarity index 100% rename from ft_lstmap.c rename to ft_lst/ft_lstmap.c diff --git a/ft_lstnew.c b/ft_lst/ft_lstnew.c similarity index 87% rename from ft_lstnew.c rename to ft_lst/ft_lstnew.c index 912fdc7..fe75d7c 100644 --- a/ft_lstnew.c +++ b/ft_lst/ft_lstnew.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:23:00 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 13:26:10 by ljiriste ### ########.fr */ +/* Updated: 2023/11/03 13:16:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,10 @@ t_list *ft_lstnew(void *content) t_list *res; res = malloc(sizeof(t_list)); - if (res == NULL) - return (res); - res->content = content; - res->next = NULL; + if (res) + { + res->content = content; + res->next = NULL; + } return (res); } diff --git a/ft_lstsize.c b/ft_lst/ft_lstsize.c similarity index 88% rename from ft_lstsize.c rename to ft_lst/ft_lstsize.c index cb2dc8f..e9b64d6 100644 --- a/ft_lstsize.c +++ b/ft_lst/ft_lstsize.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:28:58 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 14:59:47 by ljiriste ### ########.fr */ +/* Updated: 2023/11/03 13:26:55 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,8 @@ int ft_lstsize(t_list *lst) { int res; - if (lst == NULL) - return (0); - res = 1; - while (lst->next) + res = 0; + while (lst) { lst = lst->next; ++res; diff --git a/ft_printf b/ft_printf deleted file mode 160000 index 21c446f..0000000 --- a/ft_printf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 21c446f15c1c1e65d3fe339933b869e930bd25a5 diff --git a/ft_printf/conversion.c b/ft_printf/conversion.c new file mode 100644 index 0000000..26a1149 --- /dev/null +++ b/ft_printf/conversion.c @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* conversion.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */ +/* Updated: 2023/09/27 15:15:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // NULL, free +#include //va* +#include "libft.h" +#include "ft_printf.h" + +static char *base_str_constr(char type, va_list *args) +{ + char *res; + + res = NULL; + if (type == 'd' || type == 'i') + res = ft_itoa(va_arg(*args, int)); + else if (type == 'o') + res = ft_uitoa_base(va_arg(*args, unsigned int), "01234567"); + else if (type == 'u') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789"); + else if (type == 'x') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789abcdef"); + else if (type == 'X') + res = ft_uitoa_base(va_arg(*args, unsigned int), "0123456789ABCDEF"); + else if (type == 'c') + res = ft_ctoa(va_arg(*args, int)); + else if (type == 's') + res = ft_strdup(va_arg(*args, char *)); + else if (type == 'p') + res = ft_uitoa_base((uintptr_t)va_arg(*args, void *), + "0123456789abcdef"); + return (res); +} + +static int valid_toprint(t_to_print tp) +{ + return (tp.left_pad && tp.zero_pad && tp.main_part && tp.right_pad); +} + +static size_t to_print_len(t_to_print *tp) +{ + size_t len; + + len = 0; + len += ft_strlen(tp->left_pad); + len += (tp->sign != '\0'); + len += ft_strlen(tp->alt); + len += ft_strlen(tp->zero_pad); + len += ft_strlen(tp->main_part); + len += ft_strlen(tp->right_pad); + return (len); +} + +int tp_print_and_free_members(t_to_print *tp, t_conv conv) +{ + int len; + + len = to_print_len(tp); + ft_putstr_fd(tp->left_pad, 1); + if (tp->sign != '\0') + ft_putchar_fd(tp->sign, 1); + ft_putstr_fd(tp->alt, 1); + ft_putstr_fd(tp->zero_pad, 1); + if (*(tp->main_part) == '\0' && conv.type == 'c') + { + ft_putchar_fd('\0', 1); + ++len; + } + else + ft_putstr_fd(tp->main_part, 1); + ft_putstr_fd(tp->right_pad, 1); + free(tp->left_pad); + free(tp->alt); + free(tp->zero_pad); + free(tp->main_part); + free(tp->right_pad); + return (len); +} + +int handle_conversion(const char **format, va_list *args) +{ + t_conv conv; + char *temp; + t_to_print to_print; + + ++(*format); + conv = parse_format(format, args); + if (conv.type == '%') + { + ft_putchar_fd('%', 1); + return (1); + } + temp = base_str_constr(conv.type, args); + to_print = formatting(&temp, conv); + free(temp); + if (!valid_toprint(to_print)) + return (-1); + return (tp_print_and_free_members(&to_print, conv)); +} diff --git a/ft_printf/formatting.c b/ft_printf/formatting.c new file mode 100644 index 0000000..0b3be25 --- /dev/null +++ b/ft_printf/formatting.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* formatting.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */ +/* Updated: 2023/09/27 17:01:46 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // NULL, free +#include +#include "libft.h" +#include "ft_printf.h" + +static int starts_with_zero(t_to_print *tp, t_conv conv) +{ + return ((intmax_t)ft_strlen(tp->main_part) < conv.prec); +} + +static void create_alt(t_to_print *tp, t_conv conv) +{ + if (conv.type == 'p' && *(tp->main_part) != '(') + tp->alt = ft_strdup("0x"); + else if (conv.type == 's' || conv.type == 'c' + || ft_strncmp(tp->main_part, "0", 2) == 0 + || ((*(tp->main_part) == '\0' || !conv.flags.alt_mode) + && conv.type != 'o')) + tp->alt = ft_strdup(""); + else if (conv.flags.alt_mode) + { + if (conv.type == 'x') + tp->alt = ft_strdup("0x"); + else if (conv.type == 'X') + tp->alt = ft_strdup("0X"); + else if (conv.type == 'o' && !starts_with_zero(tp, conv)) + tp->alt = ft_strdup("0"); + else + tp->alt = ft_strdup(""); + } + else + tp->alt = ft_strdup(""); + return ; +} + +static void create_main(char *str, t_to_print *tp, t_conv conv) +{ + if (conv.type != 's' && conv.type != 'c' + && ft_strncmp(str, "0", 2) == 0 && conv.prec == 0) + tp->main_part = ft_strdup(""); + else if (conv.type == 'd' || conv.type == 'i') + { + if (*str == '-') + { + tp->sign = '-'; + tp->main_part = ft_strdup(str + 1); + } + else + { + tp->main_part = ft_strdup(str); + if (conv.flags.sign_show) + tp->sign = '+'; + else if (conv.flags.sign_allign) + tp->sign = ' '; + } + } + else if (conv.type == 's' + && (size_t)conv.prec < ft_strlen(str) && conv.prec >= 0) + tp->main_part = ft_strndup(str, conv.prec); + else + tp->main_part = ft_strdup(str); + return ; +} + +static void init_printed(t_to_print *tp) +{ + tp->left_pad = NULL; + tp->sign = '\0'; + tp->zero_pad = NULL; + tp->main_part = NULL; + tp->right_pad = NULL; + return ; +} + +t_to_print formatting(char **str, t_conv conv) +{ + t_to_print res; + + if ((conv.type == 'p' && ft_strncmp(*str, "0", 2) == 0)) + { + free (*str); + *str = ft_strdup("(nil)"); + } + else if (conv.type == 's' && *str == NULL + && (conv.prec >= 6 || conv.prec < 0)) + *str = ft_strdup("(null)"); + else if (conv.type == 's' && *str == NULL) + *str = ft_strdup(""); + init_printed(&res); + create_main(*str, &res, conv); + create_alt(&res, conv); + create_padding(&res, conv); + return (res); +} diff --git a/ft_printf/ft_printf.c b/ft_printf/ft_printf.c new file mode 100644 index 0000000..8fdd78b --- /dev/null +++ b/ft_printf/ft_printf.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */ +/* Updated: 2023/09/06 18:01:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // va_* +#include // write +#include "libft.h" +#include "ft_printf.h" + +int print_ordinary(const char **s) +{ + int len; + + len = 0; + while ((*s)[len] && (*s)[len] != '%') + ++len; + write(1, *s, len); + *s += len; + return (len); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int res; + + res = 0; + va_start(args, format); + while (*format) + { + res += print_ordinary(&format); + if (!*format) + break ; + res += handle_conversion(&format, &args); + } + va_end(args); + return (res); +} diff --git a/ft_printf/ft_printf.h b/ft_printf/ft_printf.h new file mode 100644 index 0000000..bcfe53e --- /dev/null +++ b/ft_printf/ft_printf.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 12:00:16 by ljiriste #+# #+# */ +/* Updated: 2023/09/15 15:34:21 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_H +# define FT_PRINTF_H + +# include + +typedef struct s_flags +{ + int alt_mode; + int zero_pad; + int left_adjust; + int sign_allign; + int sign_show; +} t_flags; + +typedef struct s_conv +{ + t_flags flags; + int minwidth; + int prec; + char type; +} t_conv; + +typedef struct s_to_print +{ + char *left_pad; + char sign; + char *alt; + char *zero_pad; + char *main_part; + char *right_pad; +} t_to_print; + +int ft_printf(const char *format, ...); +int handle_conversion(const char **format, va_list *args); +t_to_print formatting(char **str, t_conv conv); +t_conv parse_format(const char **format, va_list *args); +void create_padding(t_to_print *tp, t_conv conv); + +#endif diff --git a/ft_printf/padding.c b/ft_printf/padding.c new file mode 100644 index 0000000..9f3c1fc --- /dev/null +++ b/ft_printf/padding.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* padding.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 11:25:46 by ljiriste #+# #+# */ +/* Updated: 2023/09/27 16:48:26 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include //malloc +#include "libft.h" +#include "ft_printf.h" + +static char *repeat_char(char c, int n) +{ + char *res; + + if (n < 0) + n = 0; + res = malloc(++n); + if (res == NULL) + return (res); + res[--n] = '\0'; + while (n > 0) + res[--n] = c; + return (res); +} + +static void lengthen_by_zeros(char **str, int n) +{ + char *temp; + size_t size; + + if (n <= 0) + return ; + temp = *str; + size = ft_strlen(temp) + n + 1; + *str = malloc(size); + if (*str == NULL) + { + free(temp); + return ; + } + ft_strlcpy(*str, temp, size); + free(temp); + while (n > 0) + (*str)[size - (n--) - 1] = '0'; + (*str)[size - 1] = '\0'; + return ; +} + +void create_padding(t_to_print *tp, t_conv cv) +{ + size_t len; + + len = ft_strlen(tp->main_part) + (!tp->main_part[0] && cv.type == 'c'); + if (cv.type == 's' || cv.type == 'c') + tp->zero_pad = ft_strdup(""); + else + tp->zero_pad = repeat_char('0', cv.prec - len); + len += ft_strlen(tp->zero_pad) + ft_strlen(tp->alt) + (tp->sign != '\0'); + if (cv.flags.left_adjust) + { + tp->right_pad = repeat_char(' ', cv.minwidth - len); + tp->left_pad = ft_strdup(""); + } + else + { + tp->right_pad = ft_strdup(""); + if (cv.flags.zero_pad && cv.prec < 0 && ft_strchr("diouxX", cv.type)) + { + tp->left_pad = ft_strdup(""); + lengthen_by_zeros(&(tp->zero_pad), cv.minwidth - len); + } + else + tp->left_pad = repeat_char(' ', cv.minwidth - len); + } + return ; +} diff --git a/ft_printf/parsing.c b/ft_printf/parsing.c new file mode 100644 index 0000000..4058ed4 --- /dev/null +++ b/ft_printf/parsing.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/09/05 10:50:54 by ljiriste #+# #+# */ +/* Updated: 2023/09/05 12:12:41 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include // va_* +#include "libft.h" +#include "ft_printf.h" + +static t_flags parse_flags(const char **format) +{ + t_flags flags; + + flags.alt_mode = 0; + flags.zero_pad = 0; + flags.left_adjust = 0; + flags.sign_allign = 0; + flags.sign_show = 0; + while (1) + { + if (**format == '#') + flags.alt_mode = 1; + else if (**format == '0') + flags.zero_pad = 1; + else if (**format == '-') + flags.left_adjust = 1; + else if (**format == ' ') + flags.sign_allign = 1; + else if (**format == '+') + flags.sign_show = 1; + else + return (flags); + ++(*format); + } +} + +static int parse_int(const char **format, va_list *args) +{ + int res; + + res = 0; + if (**format == '*') + { + res = va_arg(*args, int); + ++(*format); + } + else if (**format == '-' || ft_isdigit(**format)) + { + res = ft_atoi(*format); + while (**format == '-' || ft_isdigit(**format)) + ++(*format); + } + return (res); +} + +t_conv parse_format(const char **format, va_list *args) +{ + t_conv conv; + + conv.flags = parse_flags(format); + conv.minwidth = parse_int(format, args); + if (**format == '.') + { + ++(*format); + conv.prec = parse_int(format, args); + } + else + conv.prec = -1; + conv.type = *((*format)++); + return (conv); +} diff --git a/ft_swap.c b/ft_swap.c new file mode 100644 index 0000000..ec46c10 --- /dev/null +++ b/ft_swap.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_swap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/03 13:59:28 by ljiriste #+# #+# */ +/* Updated: 2023/11/03 16:57:06 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_swap(void **a, void **b) +{ + void *tmp; + + tmp = *a; + *a = *b; + *b = tmp; + return ; +} diff --git a/libft.h b/libft.h index 4178e77..e40e688 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/10/03 16:28:50 by ljiriste ### ########.fr */ +/* Updated: 2023/11/03 16:57:31 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,10 +69,12 @@ void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); int ft_printf(const char *format, ...); +void ft_swap(void **a, void **b); + typedef struct s_list { - void *content; struct s_list *next; + void *content; } t_list; t_list *ft_lstnew(void *content); @@ -84,5 +86,16 @@ 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 *)); +t_list *ft_lst_at(t_list *lst, unsigned int i); +t_list *ft_lst_find(t_list *lst, void *content_red, int (*cmp)()); +void ft_lst_foreach_if(t_list *lst, void (*f)(void *), + void *content_ref, int (*cmp)()); +void ft_lst_merge(t_list **lst1, t_list *lst2); +void ft_lst_remove_if(t_list **lst, void *content_ref, + int (*cmp)(), void (*free_fct)(void *)); +void ft_lst_reverse(t_list **lst); +void ft_lst_sort(t_list **lst, int (*cmp)()); +void ft_lst_sorted_insert(t_list **lst, t_list *new, int (*cmp)()); +void ft_lst_sorted_merge(t_list **lst1, t_list *lst2, int (*cmp)()); #endif -- 2.30.2