2022-10-10 13:33:47 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
2022-10-23 13:01:50 -04:00
|
|
|
/* ft_dprintul_base.c :+: :+: :+: */
|
2022-10-10 13:33:47 -04:00
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/09/29 13:49:45 by cchauvet #+# #+# */
|
2022-10-23 13:01:50 -04:00
|
|
|
/* Updated: 2022/10/23 14:23:44 by cchauvet ### ########.fr */
|
2022-10-10 13:33:47 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2022-10-23 13:01:50 -04:00
|
|
|
#include "ft_printf.h"
|
2022-10-10 13:33:47 -04:00
|
|
|
|
2022-10-23 13:01:50 -04:00
|
|
|
static size_t ft_str_size(unsigned long long n, size_t base_size)
|
2022-10-10 13:33:47 -04:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-23 13:01:50 -04:00
|
|
|
int ft_dprintul_base(int fd, unsigned long long n, char *base)
|
2022-10-10 13:33:47 -04:00
|
|
|
{
|
|
|
|
size_t base_size;
|
2022-10-23 13:01:50 -04:00
|
|
|
int str_size;
|
2022-10-10 13:33:47 -04:00
|
|
|
|
|
|
|
if (base == NULL)
|
2022-10-23 13:01:50 -04:00
|
|
|
return (-1);
|
2022-10-10 13:33:47 -04:00
|
|
|
base_size = ft_base_size(base);
|
|
|
|
if (base_size == 0)
|
2022-10-23 13:01:50 -04:00
|
|
|
return (-1);
|
2022-10-10 13:33:47 -04:00
|
|
|
str_size = ft_str_size(n, base_size);
|
2022-10-23 13:01:50 -04:00
|
|
|
if (n > base_size - 1)
|
2022-10-10 13:33:47 -04:00
|
|
|
{
|
2022-10-23 13:01:50 -04:00
|
|
|
ft_dprintul_base(fd, n / base_size, base);
|
|
|
|
ft_putchar_fd(fd, base[n % base_size]);
|
2022-10-10 13:33:47 -04:00
|
|
|
}
|
2022-10-23 13:01:50 -04:00
|
|
|
else
|
|
|
|
ft_putchar_fd(fd, base[n]);
|
|
|
|
return (str_size - 1);
|
2022-10-10 13:33:47 -04:00
|
|
|
}
|