commit 9c022a14b9a5beabbb24f2610c9a772d3127c27e Author: lfirmin Date: Sun Nov 16 09:55:41 2025 +0100 intra to gitea diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b56cd4f --- /dev/null +++ b/Makefile @@ -0,0 +1,65 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: lfirmin lfirmim@student.42.fr> +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2024/08/12 07:07:31 by lfirmin #+# #+# # +# Updated: 2024/08/12 09:15:05 by lfirmin ### ########.fr # +# # +# **************************************************************************** # + +NAME = libftprintf.a +SRCS_DIR = srcs/ +INCS_DIR = includes/ +OBJ_DIR = obj/ + +SRC = ft_putchar.c ft_put_ptr.c ft_conv_ith.c ft_hex_len.c \ + ft_sorting.c ft_printf.c ft_print_ith.c ft_printchar.c \ + ft_printstr.c ft_printpercent.c ft_print_unsigned.c \ + ft_print_nbr.c ft_print_ptr.c ft_adrr_len.c ft_print_null.c \ + ft_itoa.c ft_unsigned_itoa.c ft_count.c + +CC = cc +CFLAGS = -Wall -Wextra -Werror +INCLUDE = -I $(INCS_DIR) +SRCS = $(addprefix $(SRCS_DIR), $(SRC)) +OBJ = $(addprefix $(OBJ_DIR), $(SRC:.c=.o)) + +# Colors +GREEN = \033[0;32m +YELLOW = \033[0;33m +RESET = \033[0m +WHITE = \033[0;97m + +# Loading animation +LOADING_CHARS = ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏ + +all: $(NAME) + +$(NAME): $(OBJ) + @printf "$(YELLOW)Compiling ft_printf, Please wait...$(RESET)" + @for char in $(LOADING_CHARS); do \ + printf "\r$(YELLOW)Compiling ft_printf, Please wait... $$char$(RESET)"; \ + sleep 0.1; \ + done + @ar rc $(NAME) $(OBJ) + @ranlib $(NAME) + @printf "\r$(GREEN)Great news ! $(WHITE)ft_printf compiled successfully ! $(RESET)\n" + +$(OBJ_DIR)%.o: $(SRCS_DIR)%.c + @mkdir -p $(OBJ_DIR) + @$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ + +clean: + @rm -rf $(OBJ_DIR) + @printf "$(WHITE)Clean process completed for $(GREEN)ft_printf.$(RESET)\n" + +fclean: clean + @rm -f $(NAME) + @printf "$(WHITE)Full clean process completed for $(GREEN)ft_printf.$(RESET)\n" + +re: fclean all + +.PHONY: all clean fclean re \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2a989b1 --- /dev/null +++ b/readme.md @@ -0,0 +1,48 @@ +# ft_printf + +Your own implementation of the printf function from libc. + +## 💡 About + +The ft_printf project consists of programming your own version of the famous printf function. You will learn about variadic arguments and will implement formatted output conversion. This project is a great way to improve your programming skills and is frequently reused in subsequent 42 projects. + +## 🛠️ Supported Conversions + +The function handles the following conversions: +- `%c` - Single character +- `%s` - String +- `%p` - Pointer address +- `%d` - Decimal (base 10) integer +- `%i` - Integer in base 10 +- `%u` - Unsigned decimal (base 10) integer +- `%x` - Hexadecimal (base 16) integer lowercase +- `%X` - Hexadecimal (base 16) integer uppercase +- `%%` - Percentage sign + +## 📋 Usage + +```c +#include "ft_printf.h" + +int main(void) +{ + ft_printf("Hello %s!\n", "world"); + ft_printf("Number: %d\n", 42); + ft_printf("Hexadecimal: %x\n", 42); + return (0); +} +``` + +## ⚠️ Function Prototype + +```c +int ft_printf(const char *format, ...); +``` + +Returns the number of characters printed (excluding the null byte used to end output to strings). + +## 📊 Expected Output + +The function should work exactly like the original printf, handling all the specified conversions correctly. + +--- diff --git a/srcs/ft_adrr_len.c b/srcs/ft_adrr_len.c new file mode 100644 index 0000000..47b4153 --- /dev/null +++ b/srcs/ft_adrr_len.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_adrr_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 05:16:57 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:50:34 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_adrr_len(unsigned long long nbr) +{ + int i; + + if (nbr == 0) + return (1); + i = 0; + while (nbr != 0) + { + i++; + nbr = nbr / 16; + } + return (i); +} diff --git a/srcs/ft_conv_ith.c b/srcs/ft_conv_ith.c new file mode 100644 index 0000000..66f5ef6 --- /dev/null +++ b/srcs/ft_conv_ith.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_conv_ith.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:46:47 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:46:23 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +void ft_conv_ith(unsigned int nbr, const char str) +{ + if (nbr >= 16) + { + ft_conv_ith((nbr / 16), str); + nbr = nbr % 16; + } + if (nbr <= 9) + ft_putchar(nbr + '0'); + else + { + if (str == 'x') + ft_putchar(nbr - 10 + 'a'); + else if (str == 'X') + ft_putchar(nbr - 10 + 'A'); + } +} diff --git a/srcs/ft_count.c b/srcs/ft_count.c new file mode 100644 index 0000000..24e6e21 --- /dev/null +++ b/srcs/ft_count.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_count.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:52:36 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:47:07 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +size_t ft_count(long long int n) +{ + size_t c; + + c = 0; + if (n <= 0) + c = 1; + while (n != 0) + { + n = n / 10; + c++; + } + return (c); +} diff --git a/srcs/ft_hex_len.c b/srcs/ft_hex_len.c new file mode 100644 index 0000000..29baa8b --- /dev/null +++ b/srcs/ft_hex_len.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hex_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:45:50 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:49:59 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_hex_len(unsigned int nbr) +{ + int i; + + if (nbr == 0) + return (1); + i = 0; + while (nbr != 0) + { + i++; + nbr = nbr / 16; + } + return (i); +} diff --git a/srcs/ft_itoa.c b/srcs/ft_itoa.c new file mode 100644 index 0000000..138ed77 --- /dev/null +++ b/srcs/ft_itoa.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:51:58 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:53:33 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +char *ft_itoa(int n) +{ + char *str; + size_t size; + long int num; + + num = n; + size = ft_count(num); + str = (char *)malloc(size + 1); + if (!str) + return (NULL); + str[size] = '\0'; + if (num < 0) + num = -num; + while (size--) + { + str[size] = num % 10 + '0'; + num = num / 10; + if (n < 0 && size == 0) + str[size] = '-'; + } + return (str); +} diff --git a/srcs/ft_print_ith.c b/srcs/ft_print_ith.c new file mode 100644 index 0000000..452be4d --- /dev/null +++ b/srcs/ft_print_ith.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_ith.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:49:51 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_print_ith(unsigned int nbr, const char str) +{ + if (nbr == 0) + return (write(1, "0", 1)); + ft_conv_ith(nbr, str); + return (ft_hex_len(nbr)); +} diff --git a/srcs/ft_print_nbr.c b/srcs/ft_print_nbr.c new file mode 100644 index 0000000..399c116 --- /dev/null +++ b/srcs/ft_print_nbr.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_nbr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:52:47 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_print_nbr(int nb) +{ + int len; + char *str; + + str = ft_itoa(nb); + len = ft_printstr(str); + free (str); + return (len); +} diff --git a/srcs/ft_print_null.c b/srcs/ft_print_null.c new file mode 100644 index 0000000..62a640f --- /dev/null +++ b/srcs/ft_print_null.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_null.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 07:01:50 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:46:39 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_print_null(void) +{ + write(1, "(null)", 6); + return (6); +} diff --git a/srcs/ft_print_ptr.c b/srcs/ft_print_ptr.c new file mode 100644 index 0000000..122e0a6 --- /dev/null +++ b/srcs/ft_print_ptr.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_ptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:51:10 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_print_ptr(unsigned long long ptr) +{ + int print_length; + + if (ptr == 0) + { + write(1, "(nil)", 5); + return (5); + } + print_length = 2; + write(1, "0x", 2); + { + ft_put_ptr(ptr); + print_length = print_length + ft_adrr_len(ptr); + } + return (print_length); +} diff --git a/srcs/ft_print_unsigned.c b/srcs/ft_print_unsigned.c new file mode 100644 index 0000000..38638ae --- /dev/null +++ b/srcs/ft_print_unsigned.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_print_unsigned.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:50:53 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:51:31 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_print_unsigned(unsigned int nb) +{ + int len; + char *str; + + str = ft_unsigned_itoa(nb); + len = ft_printstr(str); + free (str); + return (len); +} diff --git a/srcs/ft_printchar.c b/srcs/ft_printchar.c new file mode 100644 index 0000000..44beee6 --- /dev/null +++ b/srcs/ft_printchar.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:56:02 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:56:25 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_printchar(int c) +{ + write (1, &c, 1); + return (1); +} diff --git a/srcs/ft_printf.c b/srcs/ft_printf.c new file mode 100644 index 0000000..704a723 --- /dev/null +++ b/srcs/ft_printf.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:55:41 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_printf(const char *str, ...) +{ + int i; + va_list list; + int count; + + i = 0; + count = 0; + va_start(list, str); + while (str[i]) + { + if (str[i] == '%') + { + count += ft_sorting(list, str[i + 1]); + i++; + } + else + { + write (1, &str[i], 1); + count++; + } + i++; + } + va_end(list); + return (count); +} diff --git a/srcs/ft_printpercent.c b/srcs/ft_printpercent.c new file mode 100644 index 0000000..261f01c --- /dev/null +++ b/srcs/ft_printpercent.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printpercent.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:47:38 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:50:08 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_printpercent(void) +{ + ft_putchar('%'); + return (1); +} diff --git a/srcs/ft_printstr.c b/srcs/ft_printstr.c new file mode 100644 index 0000000..169f3d8 --- /dev/null +++ b/srcs/ft_printstr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_printstr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:55:08 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:46:05 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_printstr(char *s) +{ + int i; + + if (!s) + return (ft_print_null()); + i = 0; + while (s[i]) + { + write (1, &s[i], 1); + i++; + } + return (i); +} diff --git a/srcs/ft_put_ptr.c b/srcs/ft_put_ptr.c new file mode 100644 index 0000000..567fc23 --- /dev/null +++ b/srcs/ft_put_ptr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_put_ptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:54:06 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:46:52 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +void ft_put_ptr(unsigned long long num) +{ + if (num >= 16) + { + ft_put_ptr(num / 16); + ft_put_ptr(num % 16); + } + else + { + if (num <= 9) + ft_putchar(num + '0'); + else + ft_putchar(num - 10 + 'a'); + } +} diff --git a/srcs/ft_putchar.c b/srcs/ft_putchar.c new file mode 100644 index 0000000..47062bf --- /dev/null +++ b/srcs/ft_putchar.c @@ -0,0 +1,17 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_putchar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:45:04 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 04:49:11 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +void ft_putchar(char c) +{ + write(1, &c, 1); +} diff --git a/srcs/ft_sorting.c b/srcs/ft_sorting.c new file mode 100644 index 0000000..b2c1434 --- /dev/null +++ b/srcs/ft_sorting.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sorting.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:56:45 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:51:17 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +int ft_sorting(va_list list, const char str) +{ + int count; + + count = 0; + if (str == 'c') + count += ft_printchar(va_arg(list, int)); + else if (str == 's') + count += ft_printstr(va_arg(list, char *)); + else if (str == 'p') + count += ft_print_ptr(va_arg(list, unsigned long long)); + else if (str == 'd' || str == 'i') + count += ft_print_nbr(va_arg(list, int)); + else if (str == 'u') + count += ft_print_unsigned(va_arg(list, unsigned int)); + else if (str == 'x' || str == 'X') + count += ft_print_ith(va_arg(list, unsigned int), str); + else if (str == '%') + count += ft_printpercent(); + return (count); +} diff --git a/srcs/ft_unsigned_itoa.c b/srcs/ft_unsigned_itoa.c new file mode 100644 index 0000000..5b897cf --- /dev/null +++ b/srcs/ft_unsigned_itoa.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_unsigned_itoa.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:51:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:45:46 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "ftprintf.h" + +char *ft_unsigned_itoa(unsigned int n) +{ + char *str; + size_t size; + long int num; + + num = n; + size = ft_count(num); + str = (char *)malloc(size + 1); + if (!str) + return (NULL); + str[size] = '\0'; + if (num < 0) + num = -num; + while (size--) + { + str[size] = num % 10 + '0'; + num = num / 10; + } + return (str); +} diff --git a/srcs/ftprintf.h b/srcs/ftprintf.h new file mode 100644 index 0000000..7694f5a --- /dev/null +++ b/srcs/ftprintf.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ftprintf.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lfirmin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/19 04:44:27 by lfirmin #+# #+# */ +/* Updated: 2024/06/19 07:49:08 by lfirmin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FTPRINTF_H +# define FTPRINTF_H + +# include +# include +# include +# include "stdlib.h" + +void ft_putchar(char c); +void ft_put_ptr(unsigned long long num); +void ft_conv_ith(unsigned int nbr, const char str); + +int ft_hex_len(unsigned int nbr); +int ft_sorting(va_list list, const char str); +int ft_printf(const char *str, ...); +int ft_print_ith(unsigned int nbr, const char str); +int ft_printchar(int c); +int ft_printstr(char *s); +int ft_printpercent(void); +int ft_print_unsigned(unsigned int nb); +int ft_print_nbr(int nb); +int ft_print_ptr(unsigned long long ptr); +int ft_adrr_len(unsigned long long nbr); +int ft_print_null(void); + +char *ft_itoa(int n); +char *ft_unsigned_itoa(unsigned int n); + +size_t ft_count(long long int n); + +#endif \ No newline at end of file