42_solong/libftx/printf/ft_dprintul_base.c
Camille Chauvet 9e58abcd5c kekw
2022-12-22 17:54:17 +01:00

82 lines
1.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dprintul_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static size_t ft_str_size(unsigned long long n, size_t base_size)
{
size_t size;
size = 1;
if (n == 0)
return (2);
while (n != 0)
{
n = n / base_size;
size++;
}
return (size);
}
static int ft_isdup(char *str)
{
char c;
size_t i;
while (*str != 0)
{
c = *str;
i = 1;
while (str[i] != 0)
{
if (str[i] == c)
return (1);
i++;
}
str++;
}
return (0);
}
static size_t ft_base_size(char *base)
{
size_t len;
if (ft_isdup(base))
return (0);
len = ft_strlen(base);
if (len < 2)
return (0);
return (len);
}
int ft_dprintul_base(int fd, unsigned long long n, char *base)
{
size_t base_size;
int str_size;
if (base == NULL)
return (-1);
base_size = ft_base_size(base);
if (base_size == 0)
return (-1);
str_size = ft_str_size(n, base_size);
if (n > base_size - 1)
{
ft_dprintul_base(fd, n / base_size, base);
ft_putchar_fd(fd, base[n % base_size]);
}
else
ft_putchar_fd(fd, base[n]);
return (str_size - 1);
}