added ft_printf_fd for errors
This commit is contained in:
@ -3,10 +3,10 @@
|
|||||||
# ::: :::::::: #
|
# ::: :::::::: #
|
||||||
# Makefile :+: :+: :+: #
|
# Makefile :+: :+: :+: #
|
||||||
# +:+ +:+ +:+ #
|
# +:+ +:+ +:+ #
|
||||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
||||||
# Updated: 2023/11/15 14:42:45 by tomoron ### ########.fr #
|
# Updated: 2024/02/16 14:22:14 by marde-vr ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -25,7 +25,9 @@ SRCS = ft_protected_atoi.c\
|
|||||||
ft_putchar.c\
|
ft_putchar.c\
|
||||||
ft_putstr.c\
|
ft_putstr.c\
|
||||||
ft_strlen.c\
|
ft_strlen.c\
|
||||||
ft_write_str_part.c
|
ft_write_str_part.c\
|
||||||
|
ft_printf_fd.c\
|
||||||
|
ft_printf_fd_utils.c
|
||||||
|
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_printf.h :+: :+: :+: */
|
/* ft_printf.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
|
/* Created: 2023/11/02 23:42:00 by tomoron #+# #+# */
|
||||||
/* Updated: 2023/11/15 14:31:34 by tomoron ### ########.fr */
|
/* Updated: 2024/02/16 14:27:27 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -43,4 +43,16 @@ size_t ft_print_ptr(void *ptr, t_flags flags);
|
|||||||
size_t ft_print_str(char *s, t_flags flags);
|
size_t ft_print_str(char *s, t_flags flags);
|
||||||
void ft_put_lu_nbr(long unsigned int nb);
|
void ft_put_lu_nbr(long unsigned int nb);
|
||||||
|
|
||||||
|
//PRINTF_FD
|
||||||
|
|
||||||
|
int putchar_fd(int fd, const char c);
|
||||||
|
int putstr_fd(int fd, const char *s);
|
||||||
|
int putptr_fd(int fd, long unsigned int ptr);
|
||||||
|
int putnbr_fd(int fd, long nbr);
|
||||||
|
int puthexa_fd(int fd, unsigned int nbr, char c);
|
||||||
|
int puthexa_ptr_fd(int fd, long unsigned int ptr);
|
||||||
|
|
||||||
|
int ft_printf_fd(int fd, const char *s, ...);
|
||||||
|
int ft_print_arg(int fd, va_list args_lst, char c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
81
libft/ft_printf/ft_printf_fd.c
Normal file
81
libft/ft_printf/ft_printf_fd.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf_fd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/05 20:51:52 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/02/16 14:25:45 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int putchar_fd(int fd, char c)
|
||||||
|
{
|
||||||
|
write(fd, &c, 1);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_print_arg(int fd, va_list args_lst, char c)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (c == 'c')
|
||||||
|
n += putchar_fd(fd, va_arg(args_lst, int));
|
||||||
|
else if (c == 's')
|
||||||
|
n += putstr_fd(fd, va_arg(args_lst, char *));
|
||||||
|
else if (c == 'p')
|
||||||
|
n += putptr_fd(fd, va_arg(args_lst, long unsigned int));
|
||||||
|
else if (c == 'd' || c == 'i')
|
||||||
|
n += putnbr_fd(fd, va_arg(args_lst, int));
|
||||||
|
else if (c == 'u')
|
||||||
|
n += putnbr_fd(fd, va_arg(args_lst, unsigned int));
|
||||||
|
else if (c == 'x' || c == 'X')
|
||||||
|
n += puthexa_fd(fd, va_arg(args_lst, unsigned int), c);
|
||||||
|
else if (c == '\0')
|
||||||
|
return (-1);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n += putchar_fd(fd, '%');
|
||||||
|
}
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_conversion_is_handled(char c)
|
||||||
|
{
|
||||||
|
if ((c == 'c' || c == 's' || c == 'p' || c == 'd' || c == 'i'
|
||||||
|
|| c == 'u' || c == 'x' || c == 'X' || c == '%'))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_printf_fd(int fd, const char *s, ...)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
int i;
|
||||||
|
va_list args_lst;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
i = 0;
|
||||||
|
va_start(args_lst, s);
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
if (s[i] == '%' && ft_conversion_is_handled(s[i + 1]))
|
||||||
|
{
|
||||||
|
n += ft_print_arg(fd, args_lst, s[i + 1]);
|
||||||
|
if (s[i + 1] != '\0')
|
||||||
|
i += 2;
|
||||||
|
else
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
n += putchar_fd(fd, s[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(args_lst);
|
||||||
|
return (n);
|
||||||
|
}
|
90
libft/ft_printf/ft_printf_fd_utils.c
Normal file
90
libft/ft_printf/ft_printf_fd_utils.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_printf_fd_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2023/11/06 21:06:24 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/02/16 14:28:23 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_printf.h"
|
||||||
|
|
||||||
|
int putstr_fd(int fd, const char *s)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!s)
|
||||||
|
{
|
||||||
|
write(fd, &"(null)", 6);
|
||||||
|
return (6);
|
||||||
|
}
|
||||||
|
while (s[i])
|
||||||
|
{
|
||||||
|
write(fd, &s[i], 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int putnbr_fd(int fd, long nb)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nb < 0)
|
||||||
|
{
|
||||||
|
write(fd, "-", 1);
|
||||||
|
nb = nb * -1;
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
if (nb > 9)
|
||||||
|
{
|
||||||
|
n += putnbr_fd(fd, nb / 10);
|
||||||
|
}
|
||||||
|
write(fd, &(char){nb % 10 + 48}, 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int puthexa_ptr_fd(int fd, unsigned long nbr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nbr > 15)
|
||||||
|
n += puthexa_ptr_fd(fd, nbr / 16);
|
||||||
|
write(fd, &"0123456789abcdef"[nbr % 16], 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int putptr_fd(int fd, long unsigned int ptr)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
write(fd, &"(nil)", 5);
|
||||||
|
return (5);
|
||||||
|
}
|
||||||
|
putstr_fd(fd, "0x");
|
||||||
|
n = 2;
|
||||||
|
n += puthexa_ptr_fd(fd, ptr);
|
||||||
|
return (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int puthexa_fd(int fd, unsigned int nbr, char c)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
|
||||||
|
n = 0;
|
||||||
|
if (nbr > 15)
|
||||||
|
n += puthexa_fd(fd, nbr / 16, c);
|
||||||
|
if (c == 'X')
|
||||||
|
write(fd, &"0123456789ABCDEF"[nbr % 16], 1);
|
||||||
|
if (c == 'x')
|
||||||
|
write(fd, &"0123456789abcdef"[nbr % 16], 1);
|
||||||
|
return (n + 1);
|
||||||
|
}
|
Reference in New Issue
Block a user