mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-05 23:18:08 +01:00
131 lines
3.7 KiB
C
131 lines
3.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_printf_decimal.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/18 10:52:39 by jguelen #+# #+# */
|
|
/* Updated: 2025/02/04 09:56:04 by jguelen ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ft_printf.h"
|
|
|
|
static int ft_display_absint(int fd, int n)
|
|
{
|
|
int i;
|
|
int calc;
|
|
char buf[20];
|
|
int j;
|
|
|
|
i = 0;
|
|
calc = n;
|
|
j = 19;
|
|
if (n == 0)
|
|
return (write(fd, "0", 1));
|
|
while (calc != 0)
|
|
{
|
|
buf[j] = (calc % 10) * (1 - 2 * (n < 0)) + '0';
|
|
calc /= 10;
|
|
j--;
|
|
i++;
|
|
}
|
|
return (write(fd, buf + j + 1, i));
|
|
}
|
|
|
|
static int ft_printf_putint(int fd, t_printf_format *format)
|
|
{
|
|
int i;
|
|
int calc;
|
|
|
|
calc = format->var.i;
|
|
i = ft_int_len(calc, 10);
|
|
if (format->var.i < 0 && write(fd, "-", 1) == -1)
|
|
return (-1);
|
|
while ((format->flags & PRINTF_DOTFLAG) && (i++ < format->precision))
|
|
{
|
|
if (write(fd, "0", 1) == -1)
|
|
return (-1);
|
|
}
|
|
if (((format->flags & PRINTF_DOTFLAG) && (format->precision == 0))
|
|
&& (format->var.i == 0))
|
|
return (0);
|
|
return (ft_display_absint(fd, calc) + (format->var.i < 0));
|
|
}
|
|
|
|
int ft_printf_int(int fd, t_printf_format *format)
|
|
{
|
|
if (format->var.i < 0)
|
|
format->total_len++;
|
|
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
|
|
&& (format->var.i == 0))
|
|
format->total_len = 0;
|
|
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
|
|
{
|
|
if ((format->flags & ~PRINTF_ZEROFLAG) == format->flags
|
|
&& ft_pad_char(fd, format, " ") == -1)
|
|
return (-1);
|
|
else if (format->flags & PRINTF_ZEROFLAG)
|
|
{
|
|
format->precision = max_int(max_int((int)format->minwidth,
|
|
format->precision), 1) - (format->var.i < 0);
|
|
format->total_len = max_int((int)format->minwidth,
|
|
format->total_len);
|
|
format->flags |= PRINTF_DOTFLAG;
|
|
}
|
|
}
|
|
if (ft_printf_str(fd, format->prefix, INT_MAX) == -1
|
|
|| ft_printf_putint(fd, format) == -1)
|
|
return (-1);
|
|
return (ft_pad_char(fd, format, " "));
|
|
}
|
|
|
|
/*base is presumed to be a valid base and base_len to be its number
|
|
of "digits"*/
|
|
int ft_printf_putuint_base(int fd, t_printf_format *format, char *base,
|
|
int base_len)
|
|
{
|
|
unsigned int calc;
|
|
char buf[20];
|
|
int i;
|
|
|
|
calc = format->var.u;
|
|
i = ft_uint_len(calc, 10);
|
|
while (i++ < format->precision)
|
|
{
|
|
if (write(fd, "0", 1) == -1)
|
|
return (-1);
|
|
}
|
|
i = 19;
|
|
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
|
|
&& (format->var.u == 0))
|
|
return (0);
|
|
if (calc == 0)
|
|
return (write(fd, "0", 1));
|
|
while (calc)
|
|
{
|
|
buf[i] = base[calc % base_len];
|
|
calc /= base_len;
|
|
i--;
|
|
}
|
|
return (write(fd, buf + i + 1, 19 - i));
|
|
}
|
|
|
|
int ft_printf_uint(int fd, t_printf_format *format)
|
|
{
|
|
if ((format->flags & PRINTF_DOTFLAG) && (format->precision == 0)
|
|
&& (format->var.u == 0))
|
|
format->total_len = 0;
|
|
if ((format->flags & ~PRINTF_MINUSFLAG) == format->flags)
|
|
{
|
|
if ((format->flags & ~PRINTF_ZEROFLAG) == format->flags
|
|
&& ft_pad_char(fd, format, " ") == -1)
|
|
return (-1);
|
|
else if (ft_pad_char(fd, format, "0") == -1)
|
|
return (-1);
|
|
}
|
|
if (ft_printf_putuint_base(fd, format, "0123456789", 10) == -1)
|
|
return (-1);
|
|
return (ft_pad_char(fd, format, " "));
|
|
}
|