Archived
1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
ft_printf/ft_printf.c

88 lines
2.3 KiB
C
Raw Normal View History

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