From db179cd200163de7d1cd25cc02b868edd0766cb2 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Tue, 5 Mar 2024 09:24:30 +0100 Subject: [PATCH] Add ft_dprintf for outputing to file descriptor --- ft_io/ft_printf/conversion.c | 22 ++++++++++---------- ft_io/ft_printf/ft_printf.c | 39 +++++++++++++++++++++++++++--------- ft_io/ft_printf/ft_printf.h | 6 ++++-- inc/ft_io.h | 6 +++++- 4 files changed, 50 insertions(+), 23 deletions(-) diff --git a/ft_io/ft_printf/conversion.c b/ft_io/ft_printf/conversion.c index f7e2010..3b7b082 100644 --- a/ft_io/ft_printf/conversion.c +++ b/ft_io/ft_printf/conversion.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 11:30:56 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:49:45 by ljiriste ### ########.fr */ +/* Updated: 2024/03/05 09:16:23 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,24 +59,24 @@ static size_t to_print_len(t_to_print *tp) return (len); } -int tp_print_and_free_members(t_to_print *tp, t_conv conv) +static int tp_print_and_free_members(int fd, t_to_print *tp, t_conv conv) { int len; len = to_print_len(tp); - ft_putstr_fd(tp->left_pad, 1); + ft_putstr_fd(tp->left_pad, fd); if (tp->sign != '\0') - ft_putchar_fd(tp->sign, 1); - ft_putstr_fd(tp->alt, 1); - ft_putstr_fd(tp->zero_pad, 1); + ft_putchar_fd(tp->sign, fd); + ft_putstr_fd(tp->alt, fd); + ft_putstr_fd(tp->zero_pad, fd); if (*(tp->main_part) == '\0' && conv.type == 'c') { - ft_putchar_fd('\0', 1); + ft_putchar_fd('\0', fd); ++len; } else - ft_putstr_fd(tp->main_part, 1); - ft_putstr_fd(tp->right_pad, 1); + ft_putstr_fd(tp->main_part, fd); + ft_putstr_fd(tp->right_pad, fd); free(tp->left_pad); free(tp->alt); free(tp->zero_pad); @@ -85,7 +85,7 @@ int tp_print_and_free_members(t_to_print *tp, t_conv conv) return (len); } -int handle_conversion(const char **format, va_list *args) +int handle_conversion(int fd, const char **format, va_list *args) { t_conv conv; char *temp; @@ -103,5 +103,5 @@ int handle_conversion(const char **format, va_list *args) free(temp); if (!valid_toprint(to_print)) return (-1); - return (tp_print_and_free_members(&to_print, conv)); + return (tp_print_and_free_members(fd, &to_print, conv)); } diff --git a/ft_io/ft_printf/ft_printf.c b/ft_io/ft_printf/ft_printf.c index 9d056d9..d131df1 100644 --- a/ft_io/ft_printf/ft_printf.c +++ b/ft_io/ft_printf/ft_printf.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/17 09:14:21 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:41:36 by ljiriste ### ########.fr */ +/* Updated: 2024/03/05 09:21:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,32 +14,53 @@ #include // va_* #include // write -int print_ordinary(const char **s) +static int print_ordinary(int fd, const char **s) { int len; len = 0; while ((*s)[len] && (*s)[len] != '%') ++len; - write(1, *s, len); + write(fd, *s, len); *s += len; return (len); } -int ft_printf(const char *format, ...) +int ft_vdprintf(int fd, const char *format, va_list *args) { - va_list args; - int res; + int res; + if (fd < 0 || write(fd, NULL, 0) == -1) + return (0); res = 0; - va_start(args, format); while (*format) { - res += print_ordinary(&format); + res += print_ordinary(fd, &format); if (!*format) break ; - res += handle_conversion(&format, &args); + res += handle_conversion(fd, &format, args); } + return (res); +} + +int ft_dprintf(int fd, const char *format, ...) +{ + va_list args; + int res; + + va_start(args, format); + res = ft_vdprintf(fd, format, &args); + va_end(args); + return (res); +} + +int ft_printf(const char *format, ...) +{ + va_list args; + int res; + + va_start(args, format); + res = ft_vdprintf(STDOUT_FILENO, format, &args); va_end(args); return (res); } diff --git a/ft_io/ft_printf/ft_printf.h b/ft_io/ft_printf/ft_printf.h index 2faba16..cf63696 100644 --- a/ft_io/ft_printf/ft_printf.h +++ b/ft_io/ft_printf/ft_printf.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 12:00:16 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */ +/* Updated: 2024/03/05 09:15:29 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,9 @@ typedef struct s_to_print } t_to_print; int ft_printf(const char *format, ...); -int handle_conversion(const char **format, va_list *args); +int ft_dprintf(int fd, const char *format, ...); +int ft_vdprintf(int fd, const char *format, va_list *args); +int handle_conversion(int fd, const char **format, va_list *args); t_to_print formatting(char **str, t_conv conv); t_conv parse_format(const char **format, va_list *args); void create_padding(t_to_print *tp, t_conv conv); diff --git a/inc/ft_io.h b/inc/ft_io.h index ddb28c8..a081002 100644 --- a/inc/ft_io.h +++ b/inc/ft_io.h @@ -6,18 +6,22 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 11:38:28 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */ +/* Updated: 2024/03/05 09:30:28 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_IO_H # define FT_IO_H +# include + void ft_putchar_fd(char c, int fd); void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); int ft_printf(const char *format, ...); +int ft_dprintf(int fd, const char *format, ...); +int ft_vdprintf(int fd, const char *format, va_list *args); char *get_next_line(int fd); #endif -- 2.30.2