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.
-[submodule "ft_printf"]
- path = ft_printf
- url = git://78.102.58.167/ft_printf
- branch = inLibft
-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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_at.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_find.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:47:42 by ljiriste #+# #+# */
+/* Updated: 2023/11/03 16:26:31 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_foreach_if.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_merge.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_remove_if.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:49:53 by ljiriste #+# #+# */
+/* Updated: 2023/11/03 16:30:53 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_reverse.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 14:22:47 by ljiriste #+# #+# */
+/* Updated: 2023/11/03 16:31:38 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stddef.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sort.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sorted_insert.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/06/28 15:53:26 by ljiriste #+# #+# */
+/* Updated: 2023/11/03 16:49:00 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_lst_sorted_merge.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
void ft_lstadd_back(t_list **lst, t_list *new)
{
+ if (!new)
+ return ;
if (*lst == NULL)
{
*lst = new;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
void ft_lstadd_front(t_list **lst, t_list *new)
{
+ if (!new)
+ return ;
new->next = *lst;
*lst = new;
return ;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
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);
}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
{
int res;
- if (lst == NULL)
- return (0);
- res = 1;
- while (lst->next)
+ res = 0;
+ while (lst)
{
lst = lst->next;
++res;
+++ /dev/null
-Subproject commit 21c446f15c1c1e65d3fe339933b869e930bd25a5
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* conversion.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */
+/* Updated: 2023/09/27 15:15:22 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h> // NULL, free
+#include <stdarg.h> //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));
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* formatting.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 11:28:21 by ljiriste #+# #+# */
+/* Updated: 2023/09/27 17:01:46 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h> // NULL, free
+#include <limits.h>
+#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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */
+/* Updated: 2023/09/06 18:01:29 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdarg.h> // va_*
+#include <unistd.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_printf.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 <stdarg.h>
+
+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
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* padding.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 11:25:46 by ljiriste #+# #+# */
+/* Updated: 2023/09/27 16:48:26 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h> //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 ;
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parsing.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2023/09/05 10:50:54 by ljiriste #+# #+# */
+/* Updated: 2023/09/05 12:12:41 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdarg.h> // 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);
+}
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_swap.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* 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 ;
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
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);
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