Version Final

This commit is contained in:
Hugo Manuel Tamayo Olea
2026-01-11 17:57:21 +01:00
commit 17681f5124
10 changed files with 378 additions and 0 deletions

44
Makefile Normal file
View File

@@ -0,0 +1,44 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2026/01/09 16:38:45 by htamayo- #+# #+# #
# Updated: 2026/01/09 16:38:47 by htamayo- ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
SRC = ft_printf.c \
ft_putchar_pf.c \
ft_putstr_pf.c \
ft_putnbr_pf.c \
ft_putuint_pf.c \
ft_puthex_pf.c \
ft_putptr_pf.c \
ft_aux_pf.c \
OBJS = $(SRC:.c=.o)
CC = gcc
CFLAGS = -Wall -Werror -Wextra
RM = rm -rf
AR = ar crs
$(NAME): $(OBJS)
$(AR) $(NAME) $(OBJS)
all: $(NAME)
clean:
$(RM) $(OBJS)
fclean: clean
$(RM) $(NAME)
re: fclean all
.PHONY: all clean fclean re

84
ft_aux_pf.c Normal file
View File

@@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_aux_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/03 19:27:55 by htamayo- #+# #+# */
/* Updated: 2025/12/03 19:57:43 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen_pf(const char *str)
{
size_t i;
if (!str)
return (0);
i = 0;
while (str[i])
i++;
return (i);
}
void ft_bzero_pf(void *mem, size_t len)
{
unsigned char *temp;
temp = (unsigned char *)mem;
while (len > 0)
{
*temp = 0;
temp ++;
len--;
}
}
void *ft_calloc_pf(size_t len, size_t size)
{
void *dest;
dest = malloc(len * size);
if (dest == NULL)
return (NULL);
ft_bzero_pf(dest, len * size);
return (dest);
}
static size_t ft_len(unsigned long long n, char *base)
{
size_t len;
unsigned long long base_len;
len = 1;
base_len = ft_strlen_pf(base);
while (n >= base_len)
{
n /= base_len;
len++;
}
return (len);
}
char *ft_aux_pf(unsigned long long n, char *base)
{
char *str;
int num_len;
int base_len;
num_len = ft_len(n, base);
base_len = ft_strlen_pf(base);
str = ft_calloc_pf((num_len + 1), sizeof(char));
if (!str)
return (NULL);
while (num_len)
{
num_len--;
str[num_len] = base[n % base_len];
n /= base_len;
}
return (str);
}

60
ft_printf.c Normal file
View File

@@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/03 18:38:36 by htamayo- #+# #+# */
/* Updated: 2026/01/09 16:40:22 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_format(va_list va, char *str, size_t *counter)
{
if (*str == 'c')
ft_putchar_pf(va_arg(va, int), counter);
else if (*str == 's')
ft_putstr_pf(va_arg(va, char *), counter);
else if (*str == 'p')
ft_putptr_pf(va_arg(va, void *), counter);
else if (*str == 'i' || *str == 'd')
ft_putnbr_pf(va_arg(va, int), counter);
else if (*str == 'u')
ft_putuint_pf(va_arg(va, unsigned int), counter);
else if (*str == 'x' || *str == 'X')
{
if (*str == 'x')
ft_puthex_pf(va_arg(va, unsigned int), counter, HEX_LOW_BASE);
else
ft_puthex_pf(va_arg(va, unsigned int), counter, HEX_UPP_BASE);
}
else if (*str == '%')
ft_putchar_pf(*str, counter);
}
int ft_printf(char const *str, ...)
{
va_list va;
size_t counter;
if (!str)
return (0);
counter = 0;
va_start(va, str);
while (*str)
{
if (*str == '%')
{
str++;
ft_format(va, (char *)str, &counter);
}
else
ft_putchar_pf(*str, &counter);
str++;
}
va_end(va);
return (counter);
}

35
ft_printf.h Normal file
View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/03 18:55:18 by htamayo- #+# #+# */
/* Updated: 2025/12/03 19:39:37 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdarg.h>
# include <stdlib.h>
# include <unistd.h>
# define HEX_UPP_BASE "0123456789ABCDEF"
# define HEX_LOW_BASE "0123456789abcdef"
int ft_printf(char const *str, ...);
/* format functions */
void ft_putchar_pf(char c, size_t *counter);
void ft_putstr_pf(char *str, size_t *counter);
void ft_putnbr_pf(int num, size_t *counter);
void ft_putuint_pf(unsigned int num, size_t *counter);
void ft_puthex_pf(unsigned int num, size_t *counter, char *base);
void ft_putptr_pf(void *ptr, size_t *counter);
/* aux function */
char *ft_aux_pf(unsigned long long n, char *base);
#endif

19
ft_putchar_pf.c Normal file
View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/20 18:45:06 by htamayo- #+# #+# */
/* Updated: 2025/12/20 18:46:04 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putchar_pf(char c, size_t *counter)
{
write(1, &c, 1);
(*counter)++;
}

22
ft_puthex_pf.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_puthex_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/20 18:42:38 by htamayo- #+# #+# */
/* Updated: 2025/12/20 18:44:47 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_puthex_pf(unsigned int num, size_t *counter, char *base)
{
char *str;
str = ft_aux_pf(num, base);
ft_putstr_pf(str, counter);
free(str);
}

38
ft_putnbr_pf.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/20 19:05:43 by htamayo- #+# #+# */
/* Updated: 2025/12/20 19:19:18 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putnbr_pf(int num, size_t *counter)
{
if (num == -2147483648)
ft_putstr_pf("-2147483648", counter);
else if (num < 0)
{
num = num * -1;
ft_putchar_pf('-', counter);
ft_putnbr_pf(num, counter);
}
else
{
if (num > 9)
{
ft_putnbr_pf(num / 10, counter);
num %= 10;
}
if (num >= 0 && num <= 9)
{
num += '0';
ft_putchar_pf(num, counter);
}
}
}

30
ft_putptr_pf.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putptr_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 16:22:52 by htamayo- #+# #+# */
/* Updated: 2026/01/11 16:02:07 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putptr_pf(void *ptr, size_t *counter)
{
char *str;
unsigned long ptr_address;
ptr_address = (unsigned long)ptr;
if(ptr_address == 0)
{
ft_putstr_pf("(nil)", counter);
return ;
}
ft_putstr_pf("0x", counter);
str = ft_aux_pf(ptr_address, HEX_LOW_BASE);
ft_putstr_pf(str, counter);
free(str);
}

24
ft_putstr_pf.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 16:27:07 by htamayo- #+# #+# */
/* Updated: 2026/01/11 15:54:40 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putstr_pf(char *str, size_t *counter)
{
if (!str)
str = "(null)";
while (*str)
{
ft_putchar_pf(*str, counter);
str++;
}
}

22
ft_putuint_pf.c Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putuint_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: htamayo- <htamayo-@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/09 16:35:37 by htamayo- #+# #+# */
/* Updated: 2026/01/09 16:37:24 by htamayo- ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
void ft_putuint_pf(unsigned int num, size_t *counter)
{
char *str;
str = ft_aux_pf(num, "0123456789");
ft_putstr_pf(str, counter);
free(str);
}