base struct
This commit is contained in:
100
lib/libft/srcs/print/ft_printf.c
Normal file
100
lib/libft/srcs/print/ft_printf.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_printf.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/30 11:15:11 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/25 16:15:33 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
static int is_format(char c)
|
||||
{
|
||||
if (c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i' || c == 'u' \
|
||||
|| c == 'x' || c == 'X' || c == '%')
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_put_p(va_list ap)
|
||||
{
|
||||
int count;
|
||||
|
||||
if (ft_put_s("0x") == -1)
|
||||
return (-1);
|
||||
count = ft_put_uli_base((unsigned long)va_arg(ap, void *), BASE16);
|
||||
if (count == -1)
|
||||
return (-1);
|
||||
return (count + 2);
|
||||
}
|
||||
|
||||
static int formater(va_list ap, const char *fstr)
|
||||
{
|
||||
if (*fstr == 'c')
|
||||
return (ft_put_c((char)va_arg(ap, int)));
|
||||
if (*fstr == 's')
|
||||
return (ft_put_s((char *)va_arg(ap, char *)));
|
||||
if (*fstr == 'p')
|
||||
return (ft_put_p(ap));
|
||||
if (*fstr == 'd' || *fstr == 'i')
|
||||
return (ft_put_i((int)va_arg(ap, int)));
|
||||
if (*fstr == 'u')
|
||||
return (ft_put_ui((unsigned int)va_arg(ap, unsigned int)));
|
||||
if (*fstr == 'x')
|
||||
return (ft_put_ui_base((unsigned int)va_arg(ap, int), BASE16));
|
||||
if (*fstr == 'X')
|
||||
return (ft_put_ui_base((unsigned int)va_arg(ap, int), BASE16UP));
|
||||
else
|
||||
return (ft_put_s("%"));
|
||||
}
|
||||
|
||||
int ft_printf(const char *fstr, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int count;
|
||||
int tmp;
|
||||
|
||||
count = 0;
|
||||
va_start(ap, fstr);
|
||||
while (*fstr != 0)
|
||||
{
|
||||
if (*fstr == '%' && is_format(*(fstr + 1)))
|
||||
tmp = formater(ap, ++fstr);
|
||||
else
|
||||
tmp = ft_put_c(*fstr);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
count += tmp;
|
||||
fstr++;
|
||||
}
|
||||
va_end(ap);
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_debug(const char *fstr, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int count;
|
||||
int tmp;
|
||||
|
||||
count = 0;
|
||||
va_start(ap, fstr);
|
||||
ft_put_s("\033[33m[DEBUG]\033[0m");
|
||||
while (*fstr != 0)
|
||||
{
|
||||
if (*fstr == '%' && is_format(*(fstr + 1)))
|
||||
tmp = formater(ap, ++fstr);
|
||||
else
|
||||
tmp = ft_put_c(*fstr);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
count += tmp;
|
||||
fstr++;
|
||||
}
|
||||
va_end(ap);
|
||||
return (count);
|
||||
}
|
||||
74
lib/libft/srcs/print/ft_put.c
Normal file
74
lib/libft/srcs/print/ft_put.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/25 09:09:26 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/25 16:15:47 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
int ft_put_c(char c)
|
||||
{
|
||||
return (write(1, &c, 1));
|
||||
}
|
||||
|
||||
int ft_put_s(char *s)
|
||||
{
|
||||
if (!s)
|
||||
return (ft_put_s("(null)"));
|
||||
return (write(1, s, ft_strlen(s)));
|
||||
}
|
||||
|
||||
int ft_put_i(int n)
|
||||
{
|
||||
int count;
|
||||
int tmp;
|
||||
long nb;
|
||||
|
||||
nb = n;
|
||||
count = 0;
|
||||
if (nb < 0)
|
||||
{
|
||||
if (write(1, "-", 1) == -1)
|
||||
return (-1);
|
||||
count ++;
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb >= 10)
|
||||
{
|
||||
tmp = ft_put_i(nb / 10);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
count += tmp;
|
||||
}
|
||||
tmp = ft_put_c((nb % 10) + '0');
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
return (tmp + count);
|
||||
}
|
||||
|
||||
int ft_put_ui(unsigned int n)
|
||||
{
|
||||
char c;
|
||||
int count;
|
||||
int tmp;
|
||||
|
||||
count = 0;
|
||||
if (n >= 10)
|
||||
{
|
||||
tmp = ft_put_ui(n / 10);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
count += tmp;
|
||||
}
|
||||
c = (n % 10) + '0';
|
||||
tmp = write(1, &c, 1);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
return (tmp + count);
|
||||
}
|
||||
54
lib/libft/srcs/print/ft_put_fd.c
Normal file
54
lib/libft/srcs/print/ft_put_fd.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/25 16:07:21 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/25 16:40:00 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
void ft_put_i_fd(int n, int fd)
|
||||
{
|
||||
char c;
|
||||
|
||||
if (n == -2147483648)
|
||||
{
|
||||
write(fd, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (n < 0)
|
||||
{
|
||||
write(fd, "-", 1);
|
||||
n = -n;
|
||||
}
|
||||
if (n >= 10)
|
||||
{
|
||||
ft_put_i_fd(n / 10, fd);
|
||||
}
|
||||
c = (n % 10) + '0';
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
||||
void ft_put_s_fd(char *s, int fd)
|
||||
{
|
||||
if (!s)
|
||||
return ;
|
||||
write(fd, s, ft_strlen(s));
|
||||
}
|
||||
|
||||
void ft_put_c_fd(char c, int fd)
|
||||
{
|
||||
write(fd, &c, 1);
|
||||
}
|
||||
|
||||
void ft_error(char *e)
|
||||
{
|
||||
ft_put_s_fd(RED, 2);
|
||||
ft_put_s_fd(e, 2);
|
||||
ft_put_s_fd(RESET, 2);
|
||||
}
|
||||
54
lib/libft/srcs/print/ft_put_u_base.c
Normal file
54
lib/libft/srcs/print/ft_put_u_base.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_put_u_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/25 09:01:21 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/25 16:15:43 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
int ft_put_ui_base(unsigned int nbr, char *base)
|
||||
{
|
||||
unsigned int base_len;
|
||||
int count;
|
||||
int tmp;
|
||||
|
||||
count = 0;
|
||||
base_len = ft_strlen(base);
|
||||
if (base_len < 2)
|
||||
return (0);
|
||||
if (nbr >= base_len)
|
||||
{
|
||||
tmp = ft_put_ui_base(nbr / base_len, base);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
count += tmp;
|
||||
}
|
||||
tmp = write(1, &base[nbr % base_len], 1);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
return (tmp + count);
|
||||
}
|
||||
|
||||
int ft_put_uli_base(unsigned long int nbr, char *base)
|
||||
{
|
||||
unsigned long base_len;
|
||||
int count;
|
||||
int tmp;
|
||||
|
||||
count = 0;
|
||||
base_len = ft_strlen(base);
|
||||
if (base_len < 2)
|
||||
return (0);
|
||||
if (nbr >= base_len)
|
||||
count += ft_put_uli_base(nbr / base_len, base);
|
||||
tmp = write(1, &base[nbr % base_len], 1);
|
||||
if (tmp == -1)
|
||||
return (-1);
|
||||
return (tmp + count);
|
||||
}
|
||||
22
lib/libft/srcs/print/ft_putendl_fd.c
Normal file
22
lib/libft/srcs/print/ft_putendl_fd.c
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putendl_fd.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 18:01:01 by nalebrun #+# #+# */
|
||||
/* Updated: 2024/11/25 16:15:50 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
void ft_putendl_fd(char *s, int fd)
|
||||
{
|
||||
int d;
|
||||
|
||||
d = 10;
|
||||
write(fd, s, ft_strlen(s));
|
||||
write(fd, &d, 1);
|
||||
}
|
||||
Reference in New Issue
Block a user