Make ft_parse_tree_print handle NULLs gracefully
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 09:09:49 +0000 (11:09 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 09:09:49 +0000 (11:09 +0200)
ft_parse/ft_parse_tree_print.c

index f8e788515ab0af5cac055b3090049a0ad176dfff..28ba3c843880f1a33625b046e5f5c1a3c99ee2b6 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/21 09:51:43 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 10:33:09 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 11:07:45 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,6 +22,11 @@ static void  print_children_at_depth(t_parse_tree_node *node, size_t depth, char
 {
        size_t  i;
 
+       if (!node)
+       {
+               ft_printf("%s\n", node);
+               return ;
+       }
        i = 0;
        while (i + 1 < node->children.size)
        {
@@ -38,6 +43,11 @@ static void  print_children_at_depth(t_parse_tree_node *node, size_t depth, char
 
 static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
 {
+       if (!node)
+       {
+               ft_printf("%s\n", node);
+               return ;
+       }
        ft_printf("%s-%s\n", branches, node->token.type);
        branches[ft_strlen(branches) - 1] = ' ';
        ft_strlcat(branches, " |", MAX_WIDTH);
@@ -46,6 +56,11 @@ static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char bran
 
 static void    print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH])
 {
+       if (!node)
+       {
+               ft_printf("%s\n", node);
+               return ;
+       }
        if (depth == 0)
        {
                ft_printf("%s\n", node->token.type);