Add ft_dprintf for outputing to file descriptor
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Mar 2024 08:24:30 +0000 (09:24 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Mar 2024 08:34:10 +0000 (09:34 +0100)
ft_io/ft_printf/conversion.c
ft_io/ft_printf/ft_printf.c
ft_io/ft_printf/ft_printf.h
inc/ft_io.h

index f7e2010781d291998afe6fc622b4c04c493349da..3b7b082c4ee78036230f133f07c25b3c3ec75081 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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));
 }
index 9d056d99eb8b2acd4198a2944e95ccad113e4152..d131df19cc4263c9803bb619516036f796e4fe3b 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stdarg.h>    // va_*
 #include <unistd.h>    // 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);
 }
index 2faba1641cc7c5ddcf45dd08863df37f43b7049d..cf636968d49522983bd0b01aaf5a87f77e5a9170 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index ddb28c84043f6b0cd53a8f2465ca878ceb9a89fb..a081002e3581dc5bcbc3e0475dc6866d3810224c 100644 (file)
@@ -6,18 +6,22 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stdarg.h>
+
 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