libs: added libft

This commit is contained in:
Jérôme Guélen 2025-02-12 14:51:05 +01:00 committed by Khaïs COLIN
parent 6e1552a35d
commit ddb2306630
63 changed files with 2989 additions and 4 deletions

131
libft/ft_printf_decimal.c Normal file
View file

@ -0,0 +1,131 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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, " "));
}