mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-05-13 16:08:45 +02:00
「🔨」 fix(libft): replaced libft by a better one :D
This commit is contained in:
83
libft/src/io/ft_printf.c
Normal file
83
libft/src/io/ft_printf.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/14 16:46:39 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/02/05 14:14:59 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_putptr_fd(unsigned long long value, int fd)
|
||||
{
|
||||
if (!value)
|
||||
return (write(1, "(nil)", 5));
|
||||
write(1, "0x", 2);
|
||||
return (ft_putuhex_fd(value, "0123456789abcdef", fd) + 2);
|
||||
}
|
||||
|
||||
static int ft_putstack_fd(t_stack *stack, int fd)
|
||||
{
|
||||
if (stack)
|
||||
{
|
||||
return (ft_printf("nb_init:%d | nb:%d\n", stack->nb_init, stack->nb)
|
||||
+ ft_putstack_fd(stack->next, fd));
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void print_arg(va_list argsl, char type, size_t *l)
|
||||
{
|
||||
if (type == 'c')
|
||||
*l += ft_putchar_fd(va_arg(argsl, int), 1);
|
||||
else if (type == 's')
|
||||
*l += ft_putstr_fd(va_arg(argsl, char *), 1);
|
||||
else if (type == 'p')
|
||||
*l += ft_putptr_fd(va_arg(argsl, unsigned long long), 1);
|
||||
else if (type == 'd' || type == 'i')
|
||||
*l += ft_putnbr_fd(va_arg(argsl, int), 1);
|
||||
else if (type == 'u')
|
||||
*l += ft_putnbr_fd(va_arg(argsl, unsigned int), 1);
|
||||
else if (type == 'x')
|
||||
*l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789abcdef", 1);
|
||||
else if (type == 'X')
|
||||
*l += ft_putuhex_fd(va_arg(argsl, unsigned int), "0123456789ABCDEF", 1);
|
||||
else if (type == 'S')
|
||||
*l += ft_putstack_fd(va_arg(argsl, t_stack *), 1);
|
||||
else if (type)
|
||||
{
|
||||
*l += ft_putchar_fd('%', 1);
|
||||
if (type != '%')
|
||||
*l += ft_putchar_fd(type, 1);
|
||||
}
|
||||
else
|
||||
*l = -1;
|
||||
}
|
||||
|
||||
int ft_printf(const char *str, ...)
|
||||
{
|
||||
va_list argsl;
|
||||
size_t i;
|
||||
size_t l;
|
||||
|
||||
if (!str)
|
||||
return (-1);
|
||||
va_start(argsl, str);
|
||||
i = 0;
|
||||
l = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%')
|
||||
print_arg(argsl, str[++i], &l);
|
||||
else
|
||||
l += ft_putchar_fd(str[i], 1);
|
||||
if (str[i])
|
||||
i++;
|
||||
}
|
||||
va_end(argsl);
|
||||
return (l);
|
||||
}
|
83
libft/src/io/ft_printf_fd.c
Normal file
83
libft/src/io/ft_printf_fd.c
Normal file
@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/14 16:46:39 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/02/05 14:17:15 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_putptr_fd(unsigned long long value, int fd)
|
||||
{
|
||||
if (!value)
|
||||
return (write(1, "(nil)", 5));
|
||||
write(1, "0x", 2);
|
||||
return (ft_putuhex_fd(value, "0123456789abcdef", fd) + 2);
|
||||
}
|
||||
|
||||
static int ft_putstack_fd(t_stack *s, int fd)
|
||||
{
|
||||
if (s)
|
||||
{
|
||||
return (ft_printf_fd(fd, "nb_init:%d | nb:%d\n", s->nb_init, s->nb)
|
||||
+ ft_putstack_fd(s->next, fd));
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void print_arg_fd(va_list argsl, char type, size_t *l, int fd)
|
||||
{
|
||||
if (type == 'c')
|
||||
*l += ft_putchar_fd(va_arg(argsl, int), fd);
|
||||
else if (type == 's')
|
||||
*l += ft_putstr_fd(va_arg(argsl, char *), fd);
|
||||
else if (type == 'p')
|
||||
*l += ft_putptr_fd(va_arg(argsl, unsigned long long), fd);
|
||||
else if (type == 'd' || type == 'i')
|
||||
*l += ft_putnbr_fd(va_arg(argsl, int), fd);
|
||||
else if (type == 'u')
|
||||
*l += ft_putnbr_fd(va_arg(argsl, uint32_t), fd);
|
||||
else if (type == 'x')
|
||||
*l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789abcdef", fd);
|
||||
else if (type == 'X')
|
||||
*l += ft_putuhex_fd(va_arg(argsl, uint32_t), "0123456789ABCDEF", fd);
|
||||
else if (type == 'S')
|
||||
*l += ft_putstack_fd(va_arg(argsl, t_stack *), fd);
|
||||
else if (type)
|
||||
{
|
||||
*l += ft_putchar_fd('%', 1);
|
||||
if (type != '%')
|
||||
*l += ft_putchar_fd(type, 1);
|
||||
}
|
||||
else
|
||||
*l = -1;
|
||||
}
|
||||
|
||||
int ft_printf_fd(int fd, const char *str, ...)
|
||||
{
|
||||
va_list argsl;
|
||||
size_t i;
|
||||
size_t l;
|
||||
|
||||
if (!str)
|
||||
return (-1);
|
||||
va_start(argsl, str);
|
||||
i = 0;
|
||||
l = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == '%')
|
||||
print_arg_fd(argsl, str[++i], &l, fd);
|
||||
else
|
||||
l += ft_putchar_fd(str[i], fd);
|
||||
if (str[i])
|
||||
i++;
|
||||
}
|
||||
va_end(argsl);
|
||||
return (l);
|
||||
}
|
18
libft/src/io/ft_putchar_fd.c
Normal file
18
libft/src/io/ft_putchar_fd.c
Normal file
@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putchar_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 14:31:22 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/03 21:50:11 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putchar_fd(char c, int fd)
|
||||
{
|
||||
return (write(fd, &c, 1));
|
||||
}
|
22
libft/src/io/ft_putendl_fd.c
Normal file
22
libft/src/io/ft_putendl_fd.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 14:37:21 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/03 21:51:46 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
int l;
|
||||
|
||||
l = ft_putstr_fd(s, fd);
|
||||
l += ft_putchar_fd('\n', fd);
|
||||
return (l);
|
||||
}
|
27
libft/src/io/ft_putnbr_fd.c
Normal file
27
libft/src/io/ft_putnbr_fd.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 14:38:33 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/03 21:53:28 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putnbr_fd(int n, int fd)
|
||||
{
|
||||
if (n == -2147483648)
|
||||
return (ft_putstr_fd("-2147483648", fd));
|
||||
if (n < 0)
|
||||
{
|
||||
ft_putchar_fd('-', fd);
|
||||
return (ft_putnbr_fd(-n, fd) + 1);
|
||||
}
|
||||
if (n < 10)
|
||||
return (ft_putchar_fd(n + '0', fd));
|
||||
return (ft_putnbr_fd(n / 10, fd) + ft_putchar_fd((n % 10) + '0', fd));
|
||||
}
|
20
libft/src/io/ft_putstr_fd.c
Normal file
20
libft/src/io/ft_putstr_fd.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putstr_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/07 14:33:48 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/03 22:00:20 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putstr_fd(char *s, int fd)
|
||||
{
|
||||
if (!s)
|
||||
return (write(1, "(null)", 6));
|
||||
return (write(fd, s, ft_strlen(s)));
|
||||
}
|
24
libft/src/io/ft_putuhex_fd.c
Normal file
24
libft/src/io/ft_putuhex_fd.c
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putuhex_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/03 21:48:12 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/01/03 22:02:08 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
int ft_putuhex_fd(unsigned long long nbr, char *base, int fd)
|
||||
{
|
||||
if (nbr < 16)
|
||||
return (ft_putchar_fd(base[nbr], 1));
|
||||
else
|
||||
{
|
||||
return (ft_putuhex_fd(nbr / 16, base, fd)
|
||||
+ ft_putchar_fd(base[nbr % 16], 1));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user