From 66878549ebc98f1f1dc8876bd3f81caa1c393000 Mon Sep 17 00:00:00 2001 From: David Gailleton Date: Wed, 19 Nov 2025 15:14:53 +0100 Subject: [PATCH] Bonus preparartion --- .gitignore | 2 + Makefile | 41 ++++++++++--- bonus/ft_printf_bonus.c | 59 ++++++++++++++++++ bonus/ft_putnbr_base_bonus.c | 24 ++++++++ bonus/print_char_bonus.c | 18 ++++++ bonus/print_hex_bonus.c | 24 ++++++++ bonus/print_number_bonus.c | 29 +++++++++ bonus/print_pointer_bonus.c | 25 ++++++++ bonus/print_str_bonus.c | 24 ++++++++ bonus/print_unsigned_bonus.c | 21 +++++++ include/ft_printf_bonus.h | 38 ++++++++++++ test.c | 114 ----------------------------------- 12 files changed, 297 insertions(+), 122 deletions(-) create mode 100644 bonus/ft_printf_bonus.c create mode 100644 bonus/ft_putnbr_base_bonus.c create mode 100644 bonus/print_char_bonus.c create mode 100644 bonus/print_hex_bonus.c create mode 100644 bonus/print_number_bonus.c create mode 100644 bonus/print_pointer_bonus.c create mode 100644 bonus/print_str_bonus.c create mode 100644 bonus/print_unsigned_bonus.c create mode 100644 include/ft_printf_bonus.h delete mode 100644 test.c diff --git a/.gitignore b/.gitignore index 845cda6..6c18869 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,5 @@ dkms.conf # debug information files *.dwo + +.build/ diff --git a/Makefile b/Makefile index 3a85689..0f2ead2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ NAME= libftprintf.a +LIBFT= $(BUILD_DIR)/libft.a +BONUS= $(BUILD_DIR)/libftprintf_bonus.a SRC_DIR= src +B_SRC_DIR= bonus INC_DIR= include LIBFT_DIR= libft BUILD_DIR= .build @@ -14,8 +17,20 @@ SRC= $(SRC_DIR)/ft_printf.c \ $(SRC_DIR)/print_str.c \ $(SRC_DIR)/print_unsigned.c \ +B_SRC= $(B_SRC_DIR)/ft_printf_bonus.c \ + $(B_SRC_DIR)/ft_putnbr_base_bonus.c \ + $(B_SRC_DIR)/print_char_bonus.c \ + $(B_SRC_DIR)/print_hex_bonus.c \ + $(B_SRC_DIR)/print_number_bonus.c \ + $(B_SRC_DIR)/print_pointer_bonus.c \ + $(B_SRC_DIR)/print_str_bonus.c \ + $(B_SRC_DIR)/print_unsigned_bonus.c \ + + OBJ= $(SRC:%.c=$(BUILD_DIR)/%.o) +B_OBJ= $(B_SRC:%.c=$(BUILD_DIR)/%.o) DEP= $(SRC:%.c=$(BUILD_DIR)/%.d) +B_DEP= $(B_SRC:%.c=$(BUILD_DIR)/%.d) CC= cc CFLAGS= -Wall -Wextra -Werror -I$(INC_DIR) -I$(LIBFT_DIR) -MMD -MP @@ -23,25 +38,35 @@ AR= ar rcs all: $(NAME) -$(NAME): $(OBJ) - $(MAKE) -C $(LIBFT_DIR) - cp $(LIBFT_DIR)/libft.a $(NAME) +bonus: $(BONUS) + cp $(BONUS) $(NAME) + +$(NAME): $(OBJ) $(LIBFT) + cp $(LIBFT) $(NAME) $(AR) $(NAME) $(OBJ) $(BUILD_DIR)/%.o: %.c - @mkdir -p $(dir $@) + mkdir -p $(dir $@) $(CC) $(CFLAGS) -c $< -o $@ +$(LIBFT): + $(MAKE) -C $(LIBFT_DIR) + cp $(LIBFT_DIR)/libft.a $(LIBFT) + +$(BONUS): $(B_OBJ) $(LIBFT) + cp $(LIBFT) $(BONUS) + $(AR) $(BONUS) $(B_OBJ) + clean: - rm -f $(OBJ) $(DEP) + rm -f $(OBJ) $(DEP) $(B_OBJ) $(B_DEP) $(MAKE) -C $(LIBFT_DIR) clean fclean: clean - rm -f $(NAME) + rm -f $(NAME) $(BONUS) $(LIBFT) $(MAKE) -C $(LIBFT_DIR) fclean re: fclean all --include $(DEP) +-include $(DEP) $(B_DEP) -.PHONY: all clean fclean re +.PHONY: all clean fclean re bonus diff --git a/bonus/ft_printf_bonus.c b/bonus/ft_printf_bonus.c new file mode 100644 index 0000000..3df5400 --- /dev/null +++ b/bonus/ft_printf_bonus.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 10:54:03 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:35:28 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "ft_printf_bonus.h" + +static int ft_print_arg(char c, va_list args) +{ + if (c == 'c') + return (print_char(va_arg(args, int))); + else if (c == 's') + return (print_str(va_arg(args, char *))); + else if (c == 'p') + return (print_pointer(va_arg(args, unsigned long long))); + else if (c == 'd' || c == 'i') + return (print_number(va_arg(args, int))); + else if (c == 'u') + return (print_unsigned(va_arg(args, unsigned int))); + else if (c == 'x') + return (print_hex(va_arg(args, unsigned int), 0)); + else if (c == 'X') + return (print_hex(va_arg(args, unsigned int), 1)); + else if (c == '%') + return (print_char('%')); + return (0); +} + +int ft_printf(const char *first_arg, ...) +{ + va_list args; + int nb_print; + int i; + + nb_print = 0; + va_start(args, first_arg); + i = 0; + while (first_arg[i]) + { + if (first_arg[i] == '%') + { + i++; + nb_print += ft_print_arg(first_arg[i], args); + } + else + nb_print += write(1, &first_arg[i], 1); + i++; + } + return (nb_print); +} diff --git a/bonus/ft_putnbr_base_bonus.c b/bonus/ft_putnbr_base_bonus.c new file mode 100644 index 0000000..72414d7 --- /dev/null +++ b/bonus/ft_putnbr_base_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putnbr_base.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:16:44 by dgaillet #+# #+# */ +/* Updated: 2025/11/17 12:43:17 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_putnbr_base(unsigned long long nbr, char *base, int base_size) +{ + int i; + + if (nbr == 0) + return (0); + i = 1 + ft_putnbr_base(nbr / base_size, base, base_size); + ft_putchar_fd(base[nbr % base_size], 1); + return (i); +} diff --git a/bonus/print_char_bonus.c b/bonus/print_char_bonus.c new file mode 100644 index 0000000..70d8ec3 --- /dev/null +++ b/bonus/print_char_bonus.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_char.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 11:56:36 by dgaillet #+# #+# */ +/* Updated: 2025/11/17 12:45:57 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include + +int print_char(char c) +{ + return (write(1, &c, 1)); +} diff --git a/bonus/print_hex_bonus.c b/bonus/print_hex_bonus.c new file mode 100644 index 0000000..3517be2 --- /dev/null +++ b/bonus/print_hex_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_hex_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:06:35 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:35:40 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf_bonus.h" +#include + +int print_hex(unsigned long long nbr, int upper) +{ + if (nbr == 0) + return (write(1, "0", 1)); + if (upper) + return (ft_putnbr_base(nbr, "0123456789ABCDEF", 16)); + else + return (ft_putnbr_base(nbr, "0123456789abcdef", 16)); +} diff --git a/bonus/print_number_bonus.c b/bonus/print_number_bonus.c new file mode 100644 index 0000000..ec86c71 --- /dev/null +++ b/bonus/print_number_bonus.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_number_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:35:48 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf_bonus.h" +#include + +int print_number(int nbr) +{ + unsigned int p_nbr; + + if (nbr == 0) + return (write(1, "0", 1)); + if (nbr < 0) + { + write(1, "-", 1); + p_nbr = nbr * -1; + return (1 + ft_putnbr_base(p_nbr, "0123456789", 10)); + } + return (ft_putnbr_base(nbr, "0123456789", 10)); +} diff --git a/bonus/print_pointer_bonus.c b/bonus/print_pointer_bonus.c new file mode 100644 index 0000000..ab27edc --- /dev/null +++ b/bonus/print_pointer_bonus.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_pointer_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:19:16 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:35:58 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "ft_printf_bonus.h" + +int print_pointer(unsigned long long p) +{ + if (!p) + { + ft_putstr_fd("(nil)", 1); + return (5); + } + ft_putstr_fd("0x", 1); + return (2 + ft_putnbr_base(p, "0123456789abcdef", 16)); +} diff --git a/bonus/print_str_bonus.c b/bonus/print_str_bonus.c new file mode 100644 index 0000000..ae7726c --- /dev/null +++ b/bonus/print_str_bonus.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_str.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:02:03 by dgaillet #+# #+# */ +/* Updated: 2025/11/17 12:51:56 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int print_str(char *str) +{ + if (!str) + { + ft_putstr_fd("(null)", 1); + return (6); + } + ft_putstr_fd(str, 1); + return (ft_strlen(str)); +} diff --git a/bonus/print_unsigned_bonus.c b/bonus/print_unsigned_bonus.c new file mode 100644 index 0000000..259a7e6 --- /dev/null +++ b/bonus/print_unsigned_bonus.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_unsigned_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 12:26:23 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:36:08 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf_bonus.h" +#include + +int print_unsigned(unsigned int nbr) +{ + if (nbr == 0) + return (write(1, "0", 1)); + return (ft_putnbr_base(nbr, "0123456789", 10)); +} diff --git a/include/ft_printf_bonus.h b/include/ft_printf_bonus.h new file mode 100644 index 0000000..1c48920 --- /dev/null +++ b/include/ft_printf_bonus.h @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf_bonus.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/11/17 10:52:23 by dgaillet #+# #+# */ +/* Updated: 2025/11/19 13:30:52 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_PRINTF_BONUS_H +# define FT_PRINTF_BONUS_H + +typedef struct s_arg +{ + char arg; + int minus; + int zero; + int dot; + int hash; + int space; + int plus; +} t_arg; + +int ft_printf(const char *first_arg, ...); + +int ft_putnbr_base(unsigned long long nbr, char *base, int base_size); + +int print_char(char c); +int print_str(char *str); +int print_pointer(unsigned long long p); +int print_number(int nbr); +int print_unsigned(unsigned int nbr); +int print_hex(unsigned long long p, int upper); + +#endif diff --git a/test.c b/test.c deleted file mode 100644 index a38c91b..0000000 --- a/test.c +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include -#include "include/ft_printf.h" - -void test(const char *name, int (*test_func)(void)) -{ - printf("\n===== %s =====\n", name); - test_func(); -} - -int test_char(void) -{ - int r1, r2; - - r1 = printf("printf: [%c]\n", 'A'); - r2 = ft_printf("ft_printf: [%c]\n", 'A'); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_string(void) -{ - int r1, r2; - - r1 = printf("printf: [%s]\n", "Hello"); - r2 = ft_printf("ft_printf: [%s]\n", "Hello"); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - r1 = printf("printf: [%s]\n", NULL); - r2 = ft_printf("ft_printf: [%s]\n", NULL); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_pointer(void) -{ - int x = 42; - int r1, r2; - - r1 = printf("printf: [%p]\n", &x); - r2 = ft_printf("ft_printf: [%p]\n", &x); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - r1 = printf("printf: [%p]\n", NULL); - r2 = ft_printf("ft_printf: [%p]\n", NULL); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_decimal(void) -{ - int r1, r2; - - r1 = printf("printf: [%d] [%i]\n", 42, -42); - r2 = ft_printf("ft_printf: [%d] [%i]\n", 42, -42); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - r1 = printf("printf: [%d]\n", INT_MIN); - r2 = ft_printf("ft_printf: [%d]\n", INT_MIN); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_unsigned(void) -{ - int r1, r2; - - r1 = printf("printf: [%u]\n", 4294967295u); - r2 = ft_printf("ft_printf: [%u]\n", 4294967295u); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_hex(void) -{ - int r1, r2; - - r1 = printf("printf: [%x] [%X]\n", 3735928559u, 3735928559u); - r2 = ft_printf("ft_printf: [%x] [%X]\n", 3735928559u, 3735928559u); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int test_percent(void) -{ - int r1, r2; - - r1 = printf("printf: [%%]\n"); - r2 = ft_printf("ft_printf: [%%]\n"); - printf("Return printf: %d | Return ft_printf: %d\n", r1, r2); - - return (0); -} - -int main(void) -{ - test("CHAR", test_char); - test("STRING", test_string); - test("POINTER", test_pointer); - test("DECIMAL", test_decimal); - test("UNSIGNED", test_unsigned); - test("HEX", test_hex); - test("PERCENT", test_percent); - - printf("\n===== ALL TESTS DONE =====\n"); - return (0); -} -