feat(char): adding function for x / X / p / u / (i / d)

This commit is contained in:
Raphael 2025-09-19 16:13:10 +02:00
parent aa04d9a16c
commit 9c42c97214
No known key found for this signature in database
2 changed files with 175 additions and 0 deletions

96
sources/print_number.c Normal file
View file

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_number.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/18 15:00:27 by rparodi #+# #+# */
/* Updated: 2025/09/19 16:06:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
int flag_p(va_list args, int fd)
{
char *to_print;
int to_ret;
u_int64_t number;
number = va_arg(args, unsigned long long);
to_ret = 0;
if (number != 0)
to_ret += write(fd, "0x", 2);
else if (number == 0)
return (write(fd, "(nil)", 5));
to_print = itoa_base(number, "0123456789abcdef");
to_ret += write(fd, to_print, ft_strlen(to_print));
free(to_print);
return (to_ret);
}
int flag_x_maj(va_list args, int fd)
{
char *to_print;
int to_ret;
unsigned int number;
number = va_arg(args, unsigned int);
to_print = itoa_base(number, "0123456789ABCDEF");
to_ret = write(fd, to_print, ft_strlen(to_print));
free(to_print);
return (to_ret);
}
int flag_x(va_list args, int fd)
{
char *to_print;
int to_ret;
unsigned int number;
number = va_arg(args, unsigned int);
to_print = itoa_base(number, "0123456789abcdef");
to_ret = write(fd, to_print, ft_strlen(to_print));
free(to_print);
return (to_ret);
}
int flag_u(va_list args, int fd)
{
char *to_print;
int to_ret;
unsigned int number;
to_ret = 0;
number = va_arg(args, unsigned int);
to_print = itoa_base(number, "0123456789");
to_ret = write(fd, to_print, ft_strlen(to_print));
free(to_print);
return (to_ret);
}
int flag_i(va_list args, int fd)
{
char *to_print;
int number;
int to_ret;
to_ret = 0;
number = va_arg(args, int);
if (number < 0)
{
if (number == INT_MIN)
return (write(1, "-2147483648", 11));
number = -number;
to_ret += write(1, "-", 1);
}
to_print = itoa_base(number, "0123456789");
to_ret += write(fd, to_print, ft_strlen(to_print));
free(to_print);
return (to_ret);
}

View file

@ -11,6 +11,9 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
size_t ft_strlen(const char *str) size_t ft_strlen(const char *str)
{ {
@ -23,3 +26,79 @@ size_t ft_strlen(const char *str)
i++; i++;
return (i); return (i);
} }
void *ft_memset(void *s, int c, size_t n)
{
char *str;
size_t i;
i = 0;
str = (char *)s;
while (i < n)
{
str[i] = c;
i++;
}
return (str);
}
void *ft_calloc(size_t nmemb, size_t size)
{
size_t total;
char *to_return;
if (nmemb == 0 || size == 0)
return ((void *)malloc(1));
total = nmemb * size;
if (total / nmemb != size && total / size != nmemb)
return (NULL);
to_return = (char *)malloc(total);
if (to_return == NULL)
to_return = NULL;
else
ft_memset(to_return, 0, nmemb * size);
return (to_return);
}
static size_t ft_nbrlen(u_int64_t nbr, size_t base_len)
{
size_t len;
if (nbr == 0)
return (1);
len = 0;
while (nbr > 0)
{
nbr /= base_len;
len++;
}
return (len);
}
char *itoa_base(u_int64_t nbr, char *base)
{
size_t base_len;
size_t nbr_len;
char *result;
int i;
if (!base)
return (NULL);
base_len = ft_strlen(base);
if (base_len < 2)
return (NULL);
nbr_len = ft_nbrlen(nbr, base_len);
result = malloc(sizeof(char) * (nbr_len + 1));
if (!result)
return (NULL);
i = nbr_len - 1;
if (nbr == 0)
result[0] = base[0];
while (nbr > 0)
{
result[i] = base[nbr % base_len];
nbr /= base_len;
i--;
}
return (result);
}