]> Untitled Git - axy/ft/ft_printf.git/commitdiff
Void ptr format :D
authorAxy <gilliardmarthey.axel@gmail.com>
Thu, 30 Oct 2025 23:29:23 +0000 (00:29 +0100)
committerAxy <gilliardmarthey.axel@gmail.com>
Thu, 30 Oct 2025 23:29:23 +0000 (00:29 +0100)
ft_printf.c

index 415463b7a3eaa5da81cb86b9ef5bed7cb0d761ea..746db14bd5673d6aecc3f0ee7d8046387cda699f 100644 (file)
@@ -6,12 +6,13 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/10/29 14:24:47 by agilliar          #+#    #+#             */
-/*   Updated: 2025/10/30 18:00:09 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/10/31 00:28:58 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <stdarg.h>
 #include <stdbool.h>
+#include <stddef.h>
 #include <unistd.h>
 
 /*
@@ -87,17 +88,46 @@ int ft_putchar_sim(char c, bool simulated)
        return (1);
 }
 
-int    ft_putnchar(char c, int n)
+int    ft_putnchar(char c, int n, bool simulated)
 {
        int     i;
 
        i = 0;
        while (i++ < n)
-               if (ft_putchar_sim(c, false) == -1)
+               if (ft_putchar_sim(c, simulated) == -1)
                        return (-1);
        return (n);
 }
 
+int    ft_put_size(size_t n, bool simulated, int base, const char *base_s,
+               bool print_zero)
+{
+       int                     written;
+
+       if (!n && !print_zero)
+               return (0);
+       if (n < base)
+               written = 0;
+       else
+               written = ft_put_size(n / base, simulated, base, base_s, true);
+       if (written == -1 || ft_putchar_sim(base_s[n % base], simulated) == -1)
+               return (-1);
+       return (written + 1);
+}
+
+int    ft_precision_pad(const t_format *format, int written, bool simulated)
+{
+       if (written >= format->precision || !format->enable_precision)
+               return (0);
+       return (ft_putnchar('0', format->precision - written, simulated));
+}
+
+int    ft_zero_pad(const t_format *format, int written, bool simulated)
+{
+       if (written >= format->width || !format->zero_pad)
+               return (0);
+       return (ft_putnchar('0', format->width - written, simulated));
+}
 
 int    ft_putformat_char(const t_format *format, t_step_arg arg, bool simulated)
 {
@@ -121,15 +151,30 @@ int       ft_putformat_str(const t_format *format, t_step_arg arg, bool simulated)
 
 int    ft_putformat_ptr(const t_format *format, t_step_arg arg, bool simulated)
 {
-       t_format        format2;
+       int                     written;
+       int                     n;
+       size_t          val;
 
        if (!arg.v_ptr)
        {
-               format2 = *format;
-               format2.specifier = SPECIFIER_STR;
                arg.v_ptr = "(nil)";
-               return (ft_putformat_str(&format2, arg, simulated));
+               return (ft_putformat_str(format, arg, simulated));
        }
+       val = (size_t) arg.v_ptr;
+       if (ft_putchar_sim('0', simulated) == -1
+                       || ft_putchar_sim('x', simulated) == -1)
+               return (-1);
+       written = ft_put_size(val, true, 16, "0123456789abcdef",
+                       format->enable_precision);
+       n = ft_precision_pad(format, written, simulated);
+       if (n == -1)
+               return (-1);
+       written += n + 2;
+       n = ft_zero_pad(format, written, simulated);
+       if (n == -1 || ft_put_size(val, simulated, 16, "0123456789abcdef",
+                               format->enable_precision) == -1)
+               return (-1);
+       return (written + n);
 }
 
 int    ft_putformat(const t_format *format, t_step_arg arg)
@@ -147,7 +192,7 @@ int ft_putformat(const t_format *format, t_step_arg arg)
                return (-1);
        len = f(format, arg, true);
        if ((format->justify == JUSTIFY_LEFT && f(format, arg, false) == -1)
-               || (format->width > len && ft_putnchar(' ', format->width - len) == -1)
+               || (format->width > len && ft_putnchar(' ', format->width - len, false) == -1)
                || (format->justify == JUSTIFY_RIGHT && f(format, arg, false) == -1))
                return (-1);
        return (len);
@@ -321,6 +366,6 @@ int ft_printf(const char *s, ...)
 int    main(void)
 {
        const char      *s = "hai";
-       ft_printf("c%-10p", s);
-       printf("c%-10p", s);
+       ft_printf("c%-25.17p", s);
+       printf("c%-25.17p", s);
 }