intra to gitea

This commit is contained in:
lfirmin 2025-11-16 09:55:41 +01:00
commit 9c022a14b9
21 changed files with 632 additions and 0 deletions

65
Makefile Normal file
View File

@ -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

48
readme.md Normal file
View File

@ -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.
---

27
srcs/ft_adrr_len.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_adrr_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

30
srcs/ft_conv_ith.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_conv_ith.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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');
}
}

27
srcs/ft_count.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_count.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

27
srcs/ft_hex_len.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hex_len.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

36
srcs/ft_itoa.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

20
srcs/ft_print_ith.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_ith.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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));
}

23
srcs/ft_print_nbr.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_nbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

18
srcs/ft_print_null.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_null.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

30
srcs/ft_print_ptr.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

23
srcs/ft_print_unsigned.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

18
srcs/ft_printchar.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

39
srcs/ft_printf.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

18
srcs/ft_printpercent.c Normal file
View File

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printpercent.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

27
srcs/ft_printstr.c Normal file
View File

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

28
srcs/ft_put_ptr.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put_ptr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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');
}
}

17
srcs/ft_putchar.c Normal file
View File

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

34
srcs/ft_sorting.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

34
srcs/ft_unsigned_itoa.c Normal file
View File

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigned_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

43
srcs/ftprintf.h Normal file
View File

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ftprintf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdarg.h>
# include <unistd.h>
# include <stdio.h>
# 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