From: Lukas Jiriste Date: Tue, 6 Feb 2024 12:23:16 +0000 (+0100) Subject: Refactor dir structure and prog argument parsing X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=32006a38afd97be85400d86518d967f0f00d7bc1;p=42%2Fpush_swap.git Refactor dir structure and prog argument parsing --- diff --git a/Makefile b/Makefile index b5dfb58..d39cbd0 100644 --- a/Makefile +++ b/Makefile @@ -6,22 +6,23 @@ RM := rm -f SUBPROJECTS := Libft -INCDIR := src_checker +INCDIR := src SUBINCDIR := $(addsuffix /inc, $(SUBPROJECTS)); INCLUDE := $(addprefix -I, $(INCDIR) $(SUBINCDIR)) -CHECKSRCDIR := src_checker +CHECKSRCDIR := src + +PUSHSRCDIR = src CHECKSOURCES := checker.c \ actions.c \ stack_manipulation.c \ + arg_parsing.c \ CHECKSOURCES := $(addprefix $(CHECKSRCDIR)/, $(CHECKSOURCES)) CHECKOBJECTS := $(CHECKSOURCES:.c=.o) -PUSHSRCDIR = src_push - PUSHSOURCES := push.c \ PUSHSOURCES := $(addprefix $(PUSHSRCDIR)/, $(PUSHSOURCES)) diff --git a/src_checker/actions.c b/src/actions.c similarity index 100% rename from src_checker/actions.c rename to src/actions.c diff --git a/src/arg_parsing.c b/src/arg_parsing.c new file mode 100644 index 0000000..6d11fae --- /dev/null +++ b/src/arg_parsing.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* arg_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/31 09:58:54 by ljiriste #+# #+# */ +/* Updated: 2024/02/06 12:34:29 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "checker.h" +#include + +static int is_int(const char *str) +{ + char *test; + + test = ft_itoa(ft_atoi(str)); + if (ft_strcmp(str, test)) + { + free(test); + return (0); + } + free(test); + return (1); +} + +static int contains(int new, t_stack *s) +{ + const size_t start_ind = s->ind; + int cur; + + if (s->stack.size == 0) + return (0); + cur = stack_top(s); + if (cur == new) + return (1); + stack_rotate(s, 1); + while (s->ind != start_ind) + { + cur = stack_top(s); + if (cur == new) + return (1); + stack_rotate(s, 1); + } + return (0); +} + +int parse(int argc, char **argv, t_stack *s) +{ + size_t i; + int new; + + if (argc == 1) + return (1); + i = 1; + while (i < (size_t)argc) + { + if (!is_int(argv[i])) + return (1); + new = ft_atoi(argv[i]); + if (contains(new, s)) + return (1); + stack_push(s, new); + stack_rotate(s, 1); + ++i; + } + return (0); +} diff --git a/src_checker/checker.c b/src/checker.c similarity index 73% rename from src_checker/checker.c rename to src/checker.c index a2c6994..f54db04 100644 --- a/src_checker/checker.c +++ b/src/checker.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/24 10:31:06 by ljiriste #+# #+# */ -/* Updated: 2024/01/24 14:41:00 by ljiriste ### ########.fr */ +/* Updated: 2024/02/06 13:19:15 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,20 +16,29 @@ #define COMMANDS_NUM 11 -static int command_parse(const char *input, t_command *command) +static int command_exists(const char *input) { - const size_t len = ft_strlen(input); - const char *com_str[COMMANDS_NUM] = {"sa", "sb", "ss", "pa", "pb", "ra", "rb", "rr", "rra", "rrb", "rrr"}; size_t i; + const char *com_str[COMMANDS_NUM] + = {"sa", "sb", "ss", "pa", "pb", "ra", "rb", "rr", "rra", "rrb", "rrr"}; i = 0; - while (1) + while (i < COMMANDS_NUM) { if (!ft_strcmp(input, com_str[i++])) break ; if (i == COMMANDS_NUM) - return (1); + return (0); } + return (1); +} + +static int command_parse(const char *input, t_command *command) +{ + const size_t len = ft_strlen(input); + + if (!command_exists(input)) + return (1); if (len == 3) command->action = action_reverse_rotate; else if (input[0] == 's') @@ -90,9 +99,9 @@ void free_stack(t_stack *s) int is_sorted(t_stack *s) { - const size_t start_ind = s->ind; - int prev; - int cur; + const size_t start_ind = s->ind; + int prev; + int cur; prev = stack_top(s); stack_rotate(s, 1); @@ -107,63 +116,6 @@ int is_sorted(t_stack *s) return (1); } -int is_int(const char *str) -{ - char *test; - - test = ft_itoa(ft_atoi(str)); - if (ft_strcmp(str, test)) - { - free(test); - return (0); - } - free(test); - return (1); -} - -int contains(int new, t_stack *s) -{ - const size_t start_ind = s->ind; - int cur; - - if (s->stack.size == 0) - return (0); - cur = stack_top(s); - if (cur == new) - return (1); - stack_rotate(s, 1); - while (s->ind != start_ind) - { - cur = stack_top(s); - if (cur == new) - return (1); - stack_rotate(s, 1); - } - return (0); -} - -int parse(int argc, char **argv, t_stack *s) -{ - size_t i; - int new; - - if (argc == 1) - return (1); - i = 1; - while (i < (size_t)argc) - { - if (!is_int(argv[i])) - return (1); - new = ft_atoi(argv[i]); - if (contains(new, s)) - return (1); - stack_push(s, new); - stack_rotate(s, 1); - ++i; - } - return (0); -} - void clean_up(t_stacks *s) { free_stack(&s->a); diff --git a/src_checker/checker.h b/src/checker.h similarity index 78% rename from src_checker/checker.h rename to src/checker.h index ab16b7e..5776dae 100644 --- a/src_checker/checker.h +++ b/src/checker.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/24 12:13:51 by ljiriste #+# #+# */ -/* Updated: 2024/01/24 12:21:55 by ljiriste ### ########.fr */ +/* Updated: 2024/01/31 09:59:33 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,18 +34,25 @@ typedef enum e_target both, } t_target; -typedef void(t_action(t_stacks *s, t_target target)); +typedef void (t_action(t_stacks *s, t_target target)); typedef struct s_command { - t_action *action; + t_action *action; t_target target; } t_command; +/* t_action action_push; t_action action_swap; t_action action_rotate; t_action action_reverse_rotate; +*/ + +void action_push(t_stacks *s, t_target target); +void action_swap(t_stacks *s, t_target target); +void action_rotate(t_stacks *s, t_target target); +void action_reverse_rotate(t_stacks *s, t_target target); void stack_push(t_stack *s, int el); void stack_swap(t_stack *s); @@ -53,4 +60,5 @@ void stack_rotate(t_stack *s, int amount); void stack_pop(t_stack *s); int stack_top(t_stack *s); +int parse(int argc, char **argv, t_stack *s); #endif //CHECKER_H diff --git a/src_push/push.c b/src/push.c similarity index 100% rename from src_push/push.c rename to src/push.c diff --git a/src_checker/stack_manipulation.c b/src/stack_manipulation.c similarity index 100% rename from src_checker/stack_manipulation.c rename to src/stack_manipulation.c