Archived
1
0
This commit is contained in:
Adam Joly
2024-01-04 16:36:32 +01:00
parent 32983af238
commit fb3fae1e1a
27 changed files with 432 additions and 261 deletions

1
printf/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
printfTester/

46
printf/Makefile Normal file
View File

@ -0,0 +1,46 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/17 12:35:27 by adjoly #+# #+# #
# Updated: 2023/11/20 15:55:52 by adjoly ### ########.fr #
# #
# **************************************************************************** #
NAME = libftprintf.a
CC = cc
SRCS = ft_printf.c \
ft_putchar.c \
ft_putnbr.c \
ft_putnbrbase.c \
ft_putstr.c \
ft_strlen.c \
OBJS = $(SRCS:.c=.o)
FLAGS = -Werror -Wall -Wextra
HEADER = libftprintf.h
$(NAME): $(OBJS)
ar -rcs $(NAME) $(OBJS)
%.o: %.c
$(CC) $(FLAGS) -I $(HEADER) $< -c -o $@
all: $(NAME)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
re: fclean all
.PHONY: clean all re fclean

92
printf/ft_printf.c Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:48:37 by adjoly #+# #+# */
/* Updated: 2023/12/06 14:27:05 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putaddr(void *ptr)
{
int r;
if (ptr == NULL)
return (write(1, "(nil)", 5));
write(1, "0x", 2);
r = ft_putnbrbase_p((long unsigned int)ptr, "0123456789abcdef");
return (2 + r);
}
int ft_putnbrulong(unsigned int n)
{
int len;
len = 0;
if (n < 10)
len += write(1, &(char){n + '0'}, 1);
else
{
len += ft_putnbrulong(n / 10);
len += write(1, &(char){n % 10 + '0'}, 1);
}
return (len);
}
int ft_printconversion(char conversion, va_list args)
{
int count;
count = 0;
if (conversion == '%')
count = ft_putchar('%');
else if (conversion == 's')
count = ft_putstr(va_arg(args, char *));
else if (conversion == 'c')
count = ft_putchar(va_arg(args, int));
else if (conversion == 'i' || conversion == 'd')
count = ft_putnbr(va_arg(args, int));
else if (conversion == 'u')
count = ft_putnbrulong(va_arg(args, unsigned int));
else if (conversion == 'p')
count = ft_putaddr(va_arg(args, void *));
else if (conversion == 'x')
count = ft_putnbrbase(va_arg(args, unsigned long), "0123456789abcdef");
else if (conversion == 'X')
count = ft_putnbrbase(va_arg(args, unsigned long), "0123456789ABCDEF");
return (count);
}
int ft_printf(const char *format, ...)
{
int i;
va_list args;
int count;
va_start(args, format);
count = 0;
i = 0;
if (format == NULL)
return (-1);
while (format[i])
{
if (format[i] == '%')
{
i++;
if (format[i])
count += ft_printconversion(format[i], args);
else
return (-1);
}
else
count += ft_putchar(format[i]);
i++;
}
va_end(args);
return (count);
}

33
printf/ft_printf.h Normal file
View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/17 16:50:36 by adjoly #+# #+# */
/* Updated: 2023/11/22 14:00:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_PRINTF_H
# define FT_PRINTF_H
# include <stdlib.h>
# include <stdarg.h>
# include <unistd.h>
int ft_printf(const char *format, ...);
int ft_printconversion(char conversion, va_list args);
int ft_putnbrulong(unsigned int n);
int ft_putaddr(void *ptr);
int ft_putstr(char *s);
int ft_putnbrbase_p(unsigned long int n, char *base);
int ft_putnbrbase(unsigned int n, char *base);
int ft_putchar(char c);
int ft_putnbr(int n);
size_t ft_strlen(const char *s);
#endif

BIN
printf/ft_printf.o Normal file

Binary file not shown.

19
printf/ft_putchar.c Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:49:00 by adjoly #+# #+# */
/* Updated: 2023/11/22 11:07:22 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putchar(char c)
{
write(1, &c, 1);
return (1);
}

BIN
printf/ft_putchar.o Normal file

Binary file not shown.

36
printf/ft_putnbr.c Normal file
View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:52:46 by adjoly #+# #+# */
/* Updated: 2023/11/22 10:51:24 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbr(int n)
{
unsigned int nbr;
int len;
len = 0;
if (n < 0)
{
len += write(1, "-", 1);
nbr = -n;
}
else
nbr = n;
if (nbr < 10)
len += write(1, &(char){nbr + '0'}, 1);
else
{
len += ft_putnbr(nbr / 10);
len += write(1, &(char){nbr % 10 + '0'}, 1);
}
return (len);
}

BIN
printf/ft_putnbr.o Normal file

Binary file not shown.

47
printf/ft_putnbrbase.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrbase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/18 10:57:44 by adjoly #+# #+# */
/* Updated: 2023/11/22 14:02:37 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putnbrbase(unsigned int n, char *base)
{
unsigned int base_len;
int len;
len = 0;
base_len = (int)ft_strlen(base);
if (n < base_len)
len += write(1, &base[n % base_len], 1);
else
{
len += ft_putnbrbase(n / base_len, base);
len += write(1, &base[n % base_len], 1);
}
return (len);
}
int ft_putnbrbase_p(unsigned long int n, char *base)
{
unsigned long int base_len;
int len;
len = 0;
base_len = (int)ft_strlen(base);
if (n < base_len)
len += write(1, &base[n % base_len], 1);
else
{
len += ft_putnbrbase_p(n / base_len, base);
len += write(1, &base[n % base_len], 1);
}
return (len);
}

BIN
printf/ft_putnbrbase.o Normal file

Binary file not shown.

31
printf/ft_putstr.c Normal file
View File

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/31 11:45:55 by adjoly #+# #+# */
/* Updated: 2023/12/06 15:51:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int ft_putstr(char *s)
{
int i;
i = 0;
if (s == NULL)
{
write(1, "(null)", 6);
return (6);
}
while (s[i])
{
write(1, &s[i], 1);
i++;
}
return (i);
}

BIN
printf/ft_putstr.o Normal file

Binary file not shown.

23
printf/ft_strlen.c Normal file
View File

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 18:15:57 by adjoly #+# #+# */
/* Updated: 2023/11/20 15:51:51 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
size_t ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i])
i++;
return (i);
}

BIN
printf/ft_strlen.o Normal file

Binary file not shown.

BIN
printf/libftprintf.a Normal file

Binary file not shown.