ft_nm/src/ft_printf.c

67 lines
1.1 KiB
C
Raw Normal View History

2024-03-20 20:47:46 +00:00
#include "ft_nm.h"
2024-10-14 19:56:32 +00:00
char ft_get_hex_digit(char c) {
if (c > 9) {
return c + 'W';
} else {
return c + '0';
}
}
2024-03-20 20:47:46 +00:00
void ft_print_hex_digit(char c) {
if (c > 9) {
ft_putchar(c + 'W');
} else {
ft_putchar(c + '0');
}
}
void ft_put_address_32(uint32_t addr) {
for (char i = 7; i >= 0; i--) {
2024-03-25 10:06:09 +00:00
ft_print_hex_digit((addr >> i * 4) & 0xF);
2024-03-20 20:47:46 +00:00
}
}
void ft_put_address_64(uint64_t addr) {
for (char i = 15; i >= 0; i--) {
2024-03-25 10:06:09 +00:00
ft_print_hex_digit((addr >> i * 4) & 0xF);
2024-03-20 20:47:46 +00:00
}
}
void ft_printf(const char *format, ...) {
va_list ap;
int argc = ft_strlen(format);
va_start(ap, format);
for (int i = 0; i < argc; i++) {
switch (format[i]) {
case 's':
ft_putstr(va_arg(ap, char *));
break;
case 'd':
ft_putnbr(va_arg(ap, int));
break;
case 'x':
ft_put_address_32(va_arg(ap, uint32_t));
break;
case 'X':
ft_put_address_64(va_arg(ap, uint64_t));
break;
case ' ':
ft_putchar(' ');
break;
case 'p':
ft_putstr(" ");
break;
case 'P':
ft_putstr(" ");
break;
2024-10-13 17:13:00 +00:00
default:
ft_putchar(format[i]);
break;
2024-03-20 20:47:46 +00:00
}
}
va_end(ap);
}
2024-03-25 10:06:09 +00:00