Applying changes made in ft_printf and its dependecies.
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 12 Sep 2023 12:33:36 +0000 (14:33 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 12 Sep 2023 12:33:36 +0000 (14:33 +0200)
These changes should have been made in master in the first place and then merged to testing.
But that is not possible now so I'm doing it this way.

Libft
conversion.c
formatting.c
ft_printf.c
padding.c

diff --git a/Libft b/Libft
index f3d297fe160d8798b15c8a830b57281288abaaf6..b1a54dd5836019e70a715e04420298092def5f3e 160000 (submodule)
--- a/Libft
+++ b/Libft
@@ -1 +1 @@
-Subproject commit f3d297fe160d8798b15c8a830b57281288abaaf6
+Subproject commit b1a54dd5836019e70a715e04420298092def5f3e
index c7f722a38295beb1b24650f6d9b0eae984d99e32..9423cb3d4bd07b6baf826998b04634a212f0c385 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/05 11:30:56 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/05 12:10:06 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 12:41:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -44,17 +44,43 @@ static int  valid_toprint(t_to_print tp)
        return (tp.left_pad && tp.zero_pad && tp.main_part && tp.right_pad);
 }
 
-static size_t  to_print_len(t_to_print tp)
+static size_t  to_print_len(t_to_print *tp)
 {
        size_t  len;
 
        len = 0;
-       len += ft_strlen(tp.left_pad);
-       len += (tp.sign != '\0');
-       len += ft_strlen(tp.alt);
-       len += ft_strlen(tp.zero_pad);
-       len += ft_strlen(tp.main_part);
-       len += ft_strlen(tp.right_pad);
+       len += ft_strlen(tp->left_pad);
+       len += (tp->sign != '\0');
+       len += ft_strlen(tp->alt);
+       len += ft_strlen(tp->zero_pad);
+       len += ft_strlen(tp->main_part);
+       len += ft_strlen(tp->right_pad);
+       return (len);
+}
+
+int    tp_print_and_free_members(t_to_print *tp, t_conv conv)
+{
+       int     len;
+
+       len = to_print_len(tp);
+       ft_putstr_fd(tp->left_pad, 1);
+       if (tp->sign != '\0')
+               ft_putchar_fd(tp->sign, 1);
+       ft_putstr_fd(tp->alt, 1);
+       ft_putstr_fd(tp->zero_pad, 1);
+       if (*(tp->main_part) == '\0' && conv.type == 'c')
+       {
+               ft_putchar_fd('\0', 1);
+               ++len;
+       }
+       else
+               ft_putstr_fd(tp->main_part, 1);
+       ft_putstr_fd(tp->right_pad, 1);
+       free(tp->left_pad);
+       free(tp->alt);
+       free(tp->zero_pad);
+       free(tp->main_part);
+       free(tp->right_pad);
        return (len);
 }
 
@@ -66,16 +92,15 @@ int handle_conversion(const char **format, va_list *args)
 
        ++(*format);
        conv = parse_format(format, args);
+       if (conv.type == '%')
+       {
+               ft_putchar_fd('%', 1);
+               return (1);
+       }
        temp = base_str_constr(conv.type, args);
        to_print = formatting(temp, conv);
        free(temp);
        if (!valid_toprint(to_print))
                return (-1);
-       ft_putstr_fd(to_print.left_pad, 1);
-       ft_putchar_fd(to_print.sign, 1);
-       ft_putstr_fd(to_print.zero_pad, 1);
-       ft_putstr_fd(to_print.alt, 1);
-       ft_putstr_fd(to_print.main_part, 1);
-       ft_putstr_fd(to_print.right_pad, 1);
-       return (to_print_len(to_print));
+       return (tp_print_and_free_members(&to_print, conv));
 }
index 089ea6f900ea96e08c2913194a711d7f505e68d4..1b66f0be66c9582efba24165e0e18666963e6174 100644 (file)
@@ -6,33 +6,51 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/05 11:28:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/05 12:11:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 12:42:41 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include <stddef.h>    // NULL
+#include <stdlib.h>    // NULL, free
+#include <limits.h>
 #include "libft.h"
 #include "ft_printf.h"
 
-static char    *create_alt(t_conv conv)
+static int     starts_with_zero(t_to_print *tp, t_conv conv)
 {
-       if (conv.type == 'p')
-               return (ft_strdup("0x"));
+       return ((intmax_t)ft_strlen(tp->main_part) < conv.prec);
+}
+
+static void    create_alt(t_to_print *tp, t_conv conv)
+{
+       if (conv.type == 'p' && *(tp->main_part) != '(')
+               tp->alt = ft_strdup("0x");
+       else if (conv.type == 's' || conv.type == 'c'
+               || ft_strncmp(tp->main_part, "0", 2) == 0
+               || ((*(tp->main_part) == '\0' || !conv.flags.alt_mode)
+                       && conv.type != 'o'))
+               tp->alt = ft_strdup("");
        else if (conv.flags.alt_mode)
        {
-               if (conv.type == 'o')
-                       return (ft_strdup("0"));
-               else if (conv.type == 'x')
-                       return (ft_strdup("0x"));
+               if (conv.type == 'x')
+                       tp->alt = ft_strdup("0x");
                else if (conv.type == 'X')
-                       return (ft_strdup("0X"));
+                       tp->alt = ft_strdup("0X");
+               else if (conv.type == 'o' && !starts_with_zero(tp, conv))
+                       tp->alt = ft_strdup("0");
+               else
+                       tp->alt = ft_strdup("");
        }
-       return (ft_strdup(""));
+       else
+               tp->alt = ft_strdup("");
+       return ;
 }
 
-static void    create_main(char *str, t_conv conv, t_to_print *tp)
+static void    create_main(char *str, t_to_print *tp, t_conv conv)
 {
-       if (conv.type == 'd' || conv.type == 'i')
+       if (conv.type != 's' && conv.type != 'c'
+               && ft_strncmp(str, "0", 2) == 0 && conv.prec == 0)
+               tp->main_part = ft_strdup("");
+       else if (conv.type == 'd' || conv.type == 'i')
        {
                if (*str == '-')
                {
@@ -70,9 +88,16 @@ t_to_print   formatting(char *str, t_conv conv)
 {
        t_to_print      res;
 
+       if ((conv.type == 'p' && ft_strncmp(str, "0", 2) == 0))
+       {
+               free (str);
+               str = ft_strdup("(nil)");
+       }
+       else if (conv.type == 's' && str == NULL)
+               str = ft_strdup("(null)");
        init_printed(&res);
-       create_main(str, conv, &res);
-       res.alt = create_alt(conv);
+       create_main(str, &res, conv);
+       create_alt(&res, conv);
        create_padding(&res, conv);
        return (res);
 }
index db1fbce7ad81b1927b9e05c7ae2ae1852a70e7ad..8fdd78b632c3aebc4b62814feb936fa7473fe1fc 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/08/17 09:14:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/05 12:10:57 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/06 18:01:29 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
index 600d5cf83e9e46dadcaeb501748f5f4f1f63db9d..2551dfbb65192966849d505c099d6e00db0ac863 100644 (file)
--- a/padding.c
+++ b/padding.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/05 11:25:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/05 12:13:14 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/08 17:19:54 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -57,7 +57,10 @@ void create_padding(t_to_print *tp, t_conv conv)
        size_t  len;
 
        len = ft_strlen(tp->main_part);
-       tp->zero_pad = repeat_char('0', conv.prec - len);
+       if (conv.type == 's' || conv.type == 'c')
+               tp->zero_pad = ft_strdup("");
+       else
+               tp->zero_pad = repeat_char('0', conv.prec - len);
        len += ft_strlen(tp->zero_pad) + ft_strlen(tp->alt) + (tp->sign != '\0');
        if (conv.flags.left_adjust)
        {
@@ -67,7 +70,7 @@ void  create_padding(t_to_print *tp, t_conv conv)
        else
        {
                tp->right_pad = ft_strdup("");
-               if (!conv.flags.zero_pad || conv.type == 's')
+               if (!conv.flags.zero_pad || conv.type == 's' || conv.type == 'c')
                        tp->left_pad = repeat_char(' ', conv.minwidth - len);
                else if (conv.flags.zero_pad)
                {