2023-01-31 08:40:15 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* ft_dprintul_base.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
2023-02-17 10:33:52 -05:00
|
|
|
/* Updated: 2023/02/17 16:31:15 by cchauvet ### ########.fr */
|
2023-01-31 08:40:15 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#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);
|
2023-02-17 10:33:52 -05:00
|
|
|
ft_putchar_fd_p(fd, base[n % base_size]);
|
2023-01-31 08:40:15 -05:00
|
|
|
}
|
|
|
|
else
|
2023-02-17 10:33:52 -05:00
|
|
|
ft_putchar_fd_p(fd, base[n]);
|
2023-01-31 08:40:15 -05:00
|
|
|
return (str_size - 1);
|
|
|
|
}
|