This commit is contained in:
2023-12-12 17:35:30 +01:00
commit 729c3a52f7
63 changed files with 2155 additions and 0 deletions

46
printf/Makefile Executable file
View File

@@ -0,0 +1,46 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/03/18 16:20:00 by fgras-ca #+# #+# #
# Updated: 2023/03/18 16:32:40 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
SOURCES = libftprintf/ft_decimal_pf.c \
libftprintf/ft_hexnum_pf.c \
libftprintf/ft_print_type.c \
libftprintf/ft_ptrhex_pf.c \
libftprintf/ft_putchar_pf.c \
libftprintf/ft_putstr_pf.c \
libftprintf/ft_unsigned_pf.c \
ft_printf.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc
RM = rm -f
CFLAGS = -Wall -Werror -Wextra
all: $(NAME)
$(NAME): $(OBJECTS)
ar rcs $(NAME) $(OBJECTS)
main:
$(CC) $(CFLAGS) main.c -L. -lftprintf
clean:
$(RM) $(OBJECTS)
fclean: clean
$(RM) $(NAME)
re: fclean all

37
printf/ft_printf.c Executable file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 16:36:14 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:51:46 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_printf(const char *s, ...)
{
int i;
int print_len;
va_list args;
i = 0;
print_len = 0;
va_start(args, s);
while (s[i])
{
if (s[i] == '%')
{
print_len += ft_print_type(&args, s[i + 1]);
i++;
}
else
print_len += ft_putchar_pf(s[i]);
i++;
}
va_end(args);
return (print_len);
}

28
printf/ft_printf.h Executable file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 15:16:04 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 15:56:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdio.h>
# include <unistd.h>
# include <stdarg.h>
int ft_printf(const char *s, ...);
int ft_decimal_pf(int c);
int ft_hexnum_pf(unsigned int a, char c);
int ft_ptrhex_pf(unsigned long c, int pref);
int ft_unsigned_pf(unsigned int c);
int ft_print_type(va_list *args, char c);
int ft_putchar_pf(char c);
int ft_putstr_pf(char *s);
#endif

View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_decimal_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 18:32:20 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 15:47:12 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_decimal_pf(int c)
{
int count;
count = 0;
if (c == 0)
return (write(1, "0", 1));
if (c == -2147483648)
return (write(1, "-2147483648", 11));
if (c < 0)
{
count += write(1, "-", 1);
c *= -1;
}
if (c >= 10)
count += ft_decimal_pf(c / 10);
write(1, &"0123456789"[c % 10], 1);
return (count + 1);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_hexnum_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 14:34:00 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 16:42:41 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_hexnum_pf(unsigned int a, char c)
{
int count;
count = 0;
if (a >= 16)
count += ft_hexnum_pf(a / 16, c);
if (c == 'x')
write(1, &"0123456789abcdef"[a % 16], 1);
if (c == 'X')
write(1, &"0123456789ABCDEF"[a % 16], 1);
return (count +1);
}

View File

@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_print_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 15:28:13 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 17:13:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_print_type(va_list *args, char c)
{
if (c == 'c')
return (ft_putchar_pf(va_arg((*args), int)));
else if (c == 's')
return (ft_putstr_pf(va_arg((*args), char *)));
else if (c == 'p')
return (ft_ptrhex_pf(va_arg((*args), unsigned long), 1));
else if (c == 'd')
return (ft_decimal_pf(va_arg((*args), int)));
else if (c == 'i')
return (ft_decimal_pf(va_arg((*args), int)));
else if (c == 'u')
return (ft_unsigned_pf(va_arg((*args), unsigned int)));
else if (c == 'x' || c == 'X')
return (ft_hexnum_pf(va_arg((*args), unsigned int), c));
else if (c == '%')
return (write(1, "%", 1));
else
return (0);
}

View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ptrhex_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 13:31:16 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:45:30 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_ptrhex_pf(unsigned long c, int pref)
{
int count;
count = 0;
if (!c)
return (write(1, "(nil)", 5));
if (pref == 1)
{
count += write(1, "0x", 2);
pref = 0;
}
if (c >= 16)
count += ft_ptrhex_pf(c / 16, pref);
write(1, &"0123456789abcdef"[c % 16], 1);
return (count + 1);
}

View File

@@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 17:54:21 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 17:01:04 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_putchar_pf(char c)
{
write(1, &c, 1);
return (1);
}

View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/17 18:08:18 by fgras-ca #+# #+# */
/* Updated: 2023/03/18 17:49:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_putstr_pf(char *s)
{
int i;
i = 0;
if (!s)
return (write(1, "(null)", 6));
while (s[i])
{
write(1, &s[i], 1);
i++;
}
return (i);
}

View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigned_pf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 13:12:40 by fgras-ca #+# #+# */
/* Updated: 2023/03/21 14:47:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_printf.h"
int ft_unsigned_pf(unsigned int c)
{
int count;
count = 0;
if (c >= 10)
count += ft_unsigned_pf(c / 10);
write(1, &"0123456789"[c % 10], 1);
return (count + 1);
}