]> git.dkaiser.de - 42/push_swap.git/commitdiff
Finish command handling and add command printing
authorDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 12:39:57 +0000 (14:39 +0200)
committerDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 12:39:57 +0000 (14:39 +0200)
command_handling.c
push_swap.h
stack_utils.c

index 484c858440ea912509f4b92c82d5db24d7756dbd..0fffb1e6f62f5a02c89d271f8adefc6c5ee5778b 100644 (file)
@@ -1,48 +1,99 @@
-#include "libft/libft.h"
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   command_handling.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/04/13 14:38:47 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/04/13 14:39:09 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
 #include "push_swap.h"
 
-static int add_cmd_to_queue(t_list **cmds, enum e_pscmd cmd)
+static int     add_cmd_to_queue(t_list **cmds, enum e_pscmd cmd)
+{
+       t_list                  *new_elem;
+       enum e_pscmd    *ptr_cmd;
+
+       ptr_cmd = malloc(sizeof(enum e_pscmd));
+       if (!ptr_cmd)
+               return (1);
+       new_elem = ft_lstnew(ptr_cmd);
+       if (!new_elem)
+       {
+               free(ptr_cmd);
+               return (1);
+       }
+       ft_lstadd_back(cmds, new_elem);
+       return (0);
+}
+
+static void    run_for_both(t_list **stack_a, t_list **stack_b,
+               void (*f)(t_list **))
+{
+       f(stack_a);
+       f(stack_b);
+}
+
+void   run_command(t_list **stack_a, t_list **stack_b, t_list **cmds,
+               enum e_pscmd cmd)
+{
+       if (cmd == SA)
+               stack_swap(stack_a);
+       else if (cmd == SB)
+               stack_swap(stack_b);
+       else if (cmd == SS)
+               run_for_both(stack_a, stack_b, stack_swap);
+       else if (cmd == PA)
+               stack_push(stack_a, stack_b);
+       else if (cmd == PB)
+               stack_push(stack_b, stack_a);
+       else if (cmd == RA)
+               stack_rotate(stack_a);
+       else if (cmd == RB)
+               stack_rotate(stack_b);
+       else if (cmd == RR)
+               run_for_both(stack_a, stack_b, stack_rotate);
+       else if (cmd == RRA)
+               stack_rrotate(stack_a);
+       else if (cmd == RRB)
+               stack_rrotate(stack_b);
+       else if (cmd == RRR)
+               run_for_both(stack_a, stack_b, stack_rrotate);
+       add_cmd_to_queue(cmds, cmd);
+}
+
+static void    print_cmd(void *ptr_cmd)
 {
-    t_list *new_elem;
-    enum e_pscmd *ptr_cmd;
-
-    ptr_cmd = malloc(sizeof(enum e_pscmd));
-    if (!ptr_cmd)
-        return 1;
-
-    new_elem = ft_lstnew(ptr_cmd);
-    if (!new_elem)
-    {
-        free(ptr_cmd);
-        return 1;
-    }
-    ft_lstadd_back(cmds, new_elem);
-    return 0;
+       enum e_pscmd    cmd;
+
+       cmd = *(enum e_pscmd *)ptr_cmd;
+       if (cmd == SA)
+               ft_putendl_fd("sa", 1);
+       else if (cmd == SB)
+               ft_putendl_fd("sb", 1);
+       else if (cmd == SS)
+               ft_putendl_fd("ss", 1);
+       else if (cmd == PA)
+               ft_putendl_fd("pa", 1);
+       else if (cmd == PB)
+               ft_putendl_fd("pb", 1);
+       else if (cmd == RA)
+               ft_putendl_fd("ra", 1);
+       else if (cmd == RB)
+               ft_putendl_fd("rb", 1);
+       else if (cmd == RR)
+               ft_putendl_fd("rr", 1);
+       else if (cmd == RRA)
+               ft_putendl_fd("rra", 1);
+       else if (cmd == RRB)
+               ft_putendl_fd("rrb", 1);
+       else if (cmd == RRR)
+               ft_putendl_fd("rrr", 1);
 }
 
-void run_command(t_list **stack_a, t_list **stack_b, t_list **cmds, enum e_pscmd cmd)
+void   print_commands(t_list *cmds)
 {
-    if (cmd == SA)
-        ;
-    else if (cmd == SB)
-        ;
-    else if (cmd == SS)
-        ;
-    else if (cmd == PA)
-        ;
-    else if (cmd == PB)
-        ;
-    else if (cmd == RA)
-        ;
-    else if (cmd == RB)
-        ;
-    else if (cmd == RR)
-        ;
-    else if (cmd == RRA)
-        ;
-    else if (cmd == RRB)
-        ;
-    else if (cmd == RRR)
-        ;
-    add_cmd_to_queue(cmds, cmd);
 }
index 0c86ee766152b5b74348e0c70d81b3b0dccaf0e0..d8c13f4d92e1fdeafb61aa6fbc4eef4cc1eb0d0d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/12 16:59:09 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/12 18:15:21 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/13 14:33:46 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -32,4 +32,12 @@ enum e_pscmd
 
 t_list *create_stack(int argc, char *argv[]);
 
+void   stack_swap(t_list **stack);
+void   stack_push(t_list **dst_stack, t_list **src_stack);
+void   stack_rotate(t_list **stack);
+void   stack_rrotate(t_list **stack);
+
+void   run_command(t_list **stack_a, t_list **stack_b, t_list **cmds,
+                       enum e_pscmd cmd);
+
 #endif // PUSH_SWAP_H
index 7a22b47e6ea8ee3274e772b6a44294182fc23f78..ad14504753f92c02710e7d02b60cbb798c8db289 100644 (file)
@@ -6,11 +6,10 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/12 20:31:30 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/12 20:50:17 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/13 14:33:35 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
-#include "libft/libft.h"
 #include "push_swap.h"
 
 void   stack_swap(t_list **stack)
@@ -44,11 +43,11 @@ void        stack_rotate(t_list **stack)
 
 void   stack_rrotate(t_list **stack)
 {
-    t_list *first_elem;
+       t_list  *first_elem;
 
-    first_elem = *stack;
-    while((*stack)->next->next)
-        *stack = (*stack)->next;
-    (*stack)->next->next = first_elem;
-    (*stack)->next = NULL;
+       first_elem = *stack;
+       while ((*stack)->next->next)
+               *stack = (*stack)->next;
+       (*stack)->next->next = first_elem;
+       (*stack)->next = NULL;
 }