Adding the libft

This commit is contained in:
Raphael 2024-10-31 17:24:36 +01:00
parent 1b98b537b7
commit c947e43e51
62 changed files with 2098 additions and 19 deletions

96
libft/print/ft_printf.c Normal file
View file

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
/* Updated: 2024/10/31 16:12:37 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void _print_char(int fd, char c, int *ret_value);
void _print_nbr(int fd, int nb, int *ret_value);
void _print_base(\
int fd, unsigned long long nbr, int *ret_value, char c);
void _print_unsigned(int fd, unsigned int nb, int *ret_value);
void _print_str(int fd, char *str, int *ret_value);
int _check_args(int fd, char c, va_list args, int *ret_value)
{
if (c == 'c')
_print_char(fd, (char)va_arg(args, int), ret_value);
else if (c == 's')
_print_str(fd, (char *)va_arg(args, char *), ret_value);
else if (c == 'i' || c == 'd')
_print_nbr(fd, (int)va_arg(args, int), ret_value);
else if (c == '%')
_print_char(fd, '%', ret_value);
else if (c == 'u')
_print_unsigned(\
fd, (unsigned int)va_arg(args, unsigned int), ret_value);
else if (c == 'x')
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c);
else if (c == 'X')
_print_base(fd, (unsigned int)va_arg(args, unsigned int), ret_value, c);
else if (c == 'p')
_print_base(fd, (unsigned long long)va_arg(args, unsigned long long), \
ret_value, c);
va_end(args);
return (1);
}
int ft_dprintf(int fd, const char *s, ...)
{
size_t i;
va_list args;
char *str;
int ret_value;
ret_value = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
while (str[i])
{
if (str[i] == '%')
{
_check_args(fd, str[i + 1], args, &ret_value);
i++;
}
else
_print_char(fd, str[i], &ret_value);
i++;
}
free(str);
return (ret_value);
}
int ft_printf(const char *s, ...)
{
size_t i;
va_list args;
char *str;
int ret_value;
ret_value = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
while (str[i])
{
if (str[i] == '%')
{
_check_args(1, str[i + 1], args, &ret_value);
i++;
}
else
_print_char(1, str[i], &ret_value);
i++;
}
free(str);
return (ret_value);
}

97
libft/print/ft_put.c Normal file
View file

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
/* Updated: 2024/10/31 16:16:45 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void _print_char(int fd, char c, int *ret_value);
void _print_nbr(int fd, int nb, int *ret_value);
void _print_base(\
int fd, unsigned long long nbr, int *ret_value, char c);
void _print_unsigned(int fd, unsigned int nb, int *ret_value);
void _print_str(int fd, char *str, int *ret_value);
void _print_char(int fd, char c, int *ret_value)
{
write(fd, &c, 1);
(*ret_value)++;
}
void _print_nbr(int fd, int nb, int *ret_value)
{
if (nb < 0)
{
if (nb == INT_MIN)
{
write(1, "-2147483648", 11);
*ret_value += 11;
return ;
}
nb = -nb;
_print_char(fd, '-', ret_value);
}
if (nb >= 10)
{
_print_nbr(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
_print_char(fd, nb + 48, ret_value);
}
void _print_base(\
int fd, unsigned long long nbr, int *ret_value, char c)
{
char base[16];
if (c == 'x' || c == 'q')
ft_strcpy(base, "0123456789abcdef");
else if (c == 'X')
ft_strcpy(base, "0123456789ABCDEF");
else if (c == 'p')
{
if (nbr != 0)
_print_str(fd, "0x", ret_value);
else if (nbr == 0)
{
_print_str(fd, "(nil)", ret_value);
return ;
}
else
ft_strcpy(base, "0123456789abcdef");
c++;
}
if (c != 'p')
{
if (nbr >= 16)
_print_base(fd, nbr / 16, ret_value, c);
_print_char(fd, base[nbr % 16], ret_value);
}
}
void _print_unsigned(int fd, unsigned int nb, int *ret_value)
{
if (nb >= 10)
{
_print_unsigned(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
_print_char(fd, nb + 48, ret_value);
}
void _print_str(int fd, char *str, int *ret_value)
{
if (!str)
*ret_value += write(fd, "(null)", 6);
else
*ret_value += write(fd, str, ft_strlen(str));
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:58:22 by rparodi #+# #+# */
/* Updated: 2023/11/10 15:31:57 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:59:01 by rparodi #+# #+# */
/* Updated: 2023/11/10 15:57:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
}

View file

@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:59:18 by rparodi #+# #+# */
/* Updated: 2023/11/13 20:13:20 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check_sign(int n, char *str, size_t *i, int fd)
{
size_t modulus;
short int maiboyerlpb;
maiboyerlpb = 0;
modulus = 1000000000;
if (n < 0)
{
n = -n;
str[(*i)] = '-';
(*i)++;
}
while (modulus != 0)
{
if (n / modulus != 0 || maiboyerlpb != 0)
{
str[(*i)++] = (n / modulus) + 48;
maiboyerlpb++;
}
n %= modulus;
modulus /= 10;
}
ft_putstr_fd(str, fd);
return (n);
}
void ft_putnbr_fd(int n, int fd)
{
size_t i;
char str[13];
i = 0;
ft_bzero(str, 13);
if (n == 0)
ft_putstr_fd("0", fd);
else if (n == -2147483648)
ft_putstr_fd("-2147483648", fd);
else
ft_check_sign(n, str, &i, fd);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:58:46 by rparodi #+# #+# */
/* Updated: 2023/11/10 15:57:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putstr_fd(char *s, int fd)
{
write(fd, s, ft_strlen(s));
}