Added a test for printing char * str = NULL and implementing printf behaviour for...
authorLukas Jiriste <ljiriste@student.42prague.com>
Mon, 11 Sep 2023 11:22:33 +0000 (13:22 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Mon, 11 Sep 2023 11:22:33 +0000 (13:22 +0200)
Libft
conversion.c
formatting.c
main.c

diff --git a/Libft b/Libft
index f56fd060021c191397b4bbb600d5e9f4f1203fee..b1a54dd5836019e70a715e04420298092def5f3e 160000 (submodule)
--- a/Libft
+++ b/Libft
@@ -1 +1 @@
-Subproject commit f56fd060021c191397b4bbb600d5e9f4f1203fee
+Subproject commit b1a54dd5836019e70a715e04420298092def5f3e
index f10d4cd34aa326fc278004cd8b28ef1219757c18..9423cb3d4bd07b6baf826998b04634a212f0c385 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/05 11:30:56 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/08 17:52:41 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 12:41:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -36,11 +36,6 @@ static char  *base_str_constr(char type, va_list *args)
                res = ft_strdup(va_arg(*args, char *));
        else if (type == 'p')
                res = ft_itoa_base((intptr_t)va_arg(*args, void *), "0123456789abcdef");
-       if (type == 'p' && ft_strncmp(res, "0", 2) == 0)
-       {
-               free (res);
-               res = ft_strdup("(nil)");
-       }
        return (res);
 }
 
index ae6f8ecc836fa846ecba35cffc8310b16617ba0c..1b66f0be66c9582efba24165e0e18666963e6174 100644 (file)
@@ -6,11 +6,11 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/05 11:28:21 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/08 16:39:07 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"
@@ -88,6 +88,13 @@ 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, &res, conv);
        create_alt(&res, conv);
diff --git a/main.c b/main.c
index feedf7412fc474a6e1c918238ecb421636e3a903..42ccfd712ba7704cd6e922f804bdf1e5664cb8f1 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/09/06 17:26:17 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/09/08 17:58:29 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/09/11 12:32:32 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -883,8 +883,10 @@ int        main(void)
        i += ft_printf("\t%s\n", "Hello");
        i += ft_printf("\t%.31s\n", "This is a not a very long text. Because this second part is not printed.");
 
-       char    *s;
+       char    *s = NULL;
    
+       i += ft_printf("Asd%s\n", s);
+
        s = ft_strdup("The end.");
        i += ft_printf("\t%s%*s\n", "The end is 20 spaces away", ft_strlen(s) + 20, s);
        free(s);