From 563ebe60a60a313bae81d8246cc2461e88f6bca9 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 5 Nov 2024 13:42:20 +0100 Subject: [PATCH] main reads arguments and prints the stacks, debug option added for vscode and in the makefile --- .gitignore | 2 - .vscode/launch.json | 31 ++++++++++++++ .vscode/tasks.json | 17 ++++++++ Makefile | 9 ++++- compile_flags.txt | 4 +- inc/push_swap.h | 33 +++++++++++++++ libft/Makefile | 3 ++ src/moves/push.c | 0 src/moves/reverse_rotate.c | 0 src/moves/rotate.c | 0 src/moves/swap.c | 0 src/push_swap.c | 83 ++++++++++++++++++++++++++++++++++++-- 12 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 inc/push_swap.h create mode 100644 src/moves/push.c create mode 100644 src/moves/reverse_rotate.c create mode 100644 src/moves/rotate.c create mode 100644 src/moves/swap.c diff --git a/.gitignore b/.gitignore index 52250be..5cf2a4e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,5 @@ a.out *.o *.a *~ -.vscode/ push_swap obj/ -lib/ diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..50e7029 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,31 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug push_swap", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/push_swap", + "args": ["2", "5", "4", "1"], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "build push_swap", + "miDebuggerPath": "/usr/bin/gdb", + "logging": { + "trace": true, + "traceResponse": true, + "engineLogging": true + } + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..eea5da1 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build push_swap", + "type": "shell", + "command": "make", + "args": [], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"], + "detail": "Generated task for building the project using make" + } + ] +} diff --git a/Makefile b/Makefile index 83739c2..4536892 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: whaffman +#+ +:+ +#++#++:++#++ # # +#+ +#+#+ +#++#+ +#+ \o/ # # Created: 2024/10/15 11:48:46 by whaffman #+#+# #+#+# #+# #+# | # -# Updated: 2024/11/04 13:26:31 by whaffman ### ### ### ### / \ # +# Updated: 2024/11/04 13:56:19 by whaffman ### ### ### ### / \ # # # # **************************************************************************** # @@ -28,7 +28,7 @@ CC = cc RM = rm -rf INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra -Werror -g LDFLAGS = -L./libft LDLIBS = -lft @@ -65,4 +65,9 @@ run: all @$(eval ARG = $(shell shuf -i 0-100 -n 5)) ./$(NAME) $(ARG) +debug: CFLAGS += -DDEBUG -g +debug: all + @echo $(CFLAGS) + @$(MAKE) re + .PHONY: all bonus clean fclean re diff --git a/compile_flags.txt b/compile_flags.txt index 4f2f45a..a4512e9 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,2 +1,4 @@ -I -./inc/ \ No newline at end of file +./inc/ +-I +./libft/inc/ diff --git a/inc/push_swap.h b/inc/push_swap.h new file mode 100644 index 0000000..61441e4 --- /dev/null +++ b/inc/push_swap.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* push_swap.h :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/04 14:11:03 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +typedef struct s_clist +{ + void *content; + struct s_clist *next; + struct s_clist *prev; +} t_clist; + +typedef struct s_node +{ + int n; + int ra; + int rb; + int rr; + int rra; + int rrb; + int rrr; + int pa; + int pb; + int sa; + int sb; +} t_node; diff --git a/libft/Makefile b/libft/Makefile index 16e73b9..6cf0c5b 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -69,4 +69,7 @@ fclean: clean re: fclean all +debug: CFLAGS += -DDEBUG -g +debug: $(NAME) + .PHONY: all clean fclean re diff --git a/src/moves/push.c b/src/moves/push.c new file mode 100644 index 0000000..e69de29 diff --git a/src/moves/reverse_rotate.c b/src/moves/reverse_rotate.c new file mode 100644 index 0000000..e69de29 diff --git a/src/moves/rotate.c b/src/moves/rotate.c new file mode 100644 index 0000000..e69de29 diff --git a/src/moves/swap.c b/src/moves/swap.c new file mode 100644 index 0000000..e69de29 diff --git a/src/push_swap.c b/src/push_swap.c index eb1e5f7..0c23cb5 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -12,13 +12,88 @@ #include "libft.h" #include +#include + +int new_element(t_list **stack, const char *str) +{ + int *n; + char *test; + t_list *elem; + + n = malloc(sizeof (int)); + if (!n) + return (0); + *n = ft_atoi(str); + test = ft_itoa(*n); + if (ft_strncmp(str, test, 20)) + { + free(test); + return (0); + } + free(test); + elem = ft_lstnew(n); + if (!elem) + return (0); + ft_lstadd_front(stack, elem); + return (1); +} + +t_list *ft_lstat(t_list *list, int n) +{ + if (!list) + return (NULL); + while (list->next && n > 0) + { + list = list->next; + n--; + } + if (n > 0) + return (NULL); + return (list); +} + +void print_stack(const char *name, t_list *stack) +{ + int lst_size; + t_list *node; + + ft_printf("%s |", name); + if (!stack) + { + ft_printf("\n"); + return ; + } + lst_size = ft_lstsize(stack); + while (lst_size > 0) + { + node = ft_lstat(stack, lst_size - 1); + ft_printf("%d ", *((int *) node->content)); + lst_size--; + } + ft_printf("\n"); +} + +void print_stacks(t_list *stack_a, t_list *stack_b) +{ + print_stack("A", stack_a); + print_stack("B", stack_b); +} int main(int argc, char *argv[]) { - int i; + t_list *stack_a; + t_list *stack_b; - i = 1; - while (i < argc) - ft_printf("%s\n", argv[i++]); + stack_a = NULL; + stack_b = NULL; + while (argc-- > 1) + { + if (!new_element(&stack_a, argv[argc])) + { + ft_putstr_fd("Error\n", 2); + return (1); + } + } + print_stacks(stack_a, stack_b); return (0); }