diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-18 12:11:36 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-18 12:11:36 +0100 |
| commit | 5e0e41652315114a8b0d883c473dbbbfc1c28342 (patch) | |
| tree | 8e1440ccf9d686294c05c6f8b5907fe90ba4fda8 /src/checker | |
| parent | 2d34349b54a9fed4d34c96bdbec5172f7695cf60 (diff) | |
| download | push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.tar.gz push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.tar.bz2 push_swap-5e0e41652315114a8b0d883c473dbbbfc1c28342.zip | |
filled action functions (propably bloat), checker working, stack functions not so much
Diffstat (limited to 'src/checker')
| -rw-r--r-- | src/checker/check.c | 32 | ||||
| -rw-r--r-- | src/checker/checker.h | 37 | ||||
| -rw-r--r-- | src/checker/main.c | 50 |
3 files changed, 61 insertions, 58 deletions
diff --git a/src/checker/check.c b/src/checker/check.c index b1ef4ca..35bd992 100644 --- a/src/checker/check.c +++ b/src/checker/check.c @@ -6,14 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:08 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:52:42 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:04:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "checker.h" -#define ACTION_ID_BUF_SIZE 4 - t_status check(t_stack *a, t_stack *b) { t_status read_status; @@ -46,27 +44,29 @@ t_status read_action(t_stack *a, t_stack *b) return (ret == 0 ? STATUS_EOF : STATUS_ERROR); } -t_action g_actions[] = { +static t_action g_actions[] = { {"sa", FLAG_ARG_A, {.arg_1 = &swap_a}}, {"sb", FLAG_ARG_B, {.arg_1 = &swap_b}}, {"ss", FLAG_ARG_A_B, {.arg_2 = &swap_both}}, - {"pa", FLAG_ARG_A_B, {.arg_2 = push_a}}, - {"pb", FLAG_ARG_B_A, {.arg_2 = push_a}}, - {"ra", FLAG_ARG_A, {.arg_1 = rotate_a}}, - {"rb", FLAG_ARG_B, {.arg_1 = rotate_b}}, - {"rr", FLAG_ARG_A_B, {.arg_2 = rotate_both}}, - {"rra", FLAG_ARG_A, {.arg_1 = reverse_rotate_a}}, - {"rrb", FLAG_ARG_B, {.arg_1 = reverse_rotate_b}}, - {"rrr", FLAG_ARG_A_B, {.arg_2 = reverse_rotate_both}}, + {"pa", FLAG_ARG_A_B, {.arg_2 = &push_a}}, + {"pb", FLAG_ARG_B_A, {.arg_2 = &push_a}}, + {"ra", FLAG_ARG_A, {.arg_1 = &rotate_a}}, + {"rb", FLAG_ARG_B, {.arg_1 = &rotate_b}}, + {"rr", FLAG_ARG_A_B, {.arg_2 = &rotate_both}}, + {"rra", FLAG_ARG_A, {.arg_1 = &reverse_rotate_a}}, + {"rrb", FLAG_ARG_B, {.arg_1 = &reverse_rotate_b}}, + {"rrr", FLAG_ARG_A_B, {.arg_2 = &reverse_rotate_both}}, }; #define ACTIONS_SIZE (sizeof(g_actions) / sizeof(t_action)) -t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], t_stack *a, t_stack *b) +t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], + t_stack *a, t_stack *b) { int i; - - i = -1; + + printf("action id %s\n", action_id); + i = 0; while (ft_strcmp(action_id, g_actions[i].id) != 0) i++; if (i == ACTIONS_SIZE) @@ -80,7 +80,7 @@ t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], t_stack *a, t_stack *b) return (STATUS_SUCCESS); } -t_bool stack_sorted(t_stack *stack) +t_bool stack_sorted(t_stack *stack) { int i; diff --git a/src/checker/checker.h b/src/checker/checker.h index a6a2cc3..e6973b6 100644 --- a/src/checker/checker.h +++ b/src/checker/checker.h @@ -6,9 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:12 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:51:42 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:01:00 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ +#include <stdio.h> #ifndef CHECKER_H # define CHECKER_H @@ -17,6 +18,11 @@ # include "common.h" # include "libft.h" +# define FLAG_ARG_A (1 << 0) +# define FLAG_ARG_B (1 << 1) +# define FLAG_ARG_A_B (1 << 2) +# define FLAG_ARG_B_A (1 << 3) + typedef enum { STATUS_SUCCESS, @@ -25,35 +31,13 @@ typedef enum STATUS_EOF, } t_status; -typedef enum -{ - ACTION_SA, - ACTION_SB, - ACTION_SS, - ACTION_PA, - ACTION_PB, - ACTION_RA, - ACTION_RB, - ACTION_RR, - ACTION_RRA, - ACTION_RRB, - ACTION_RRR, - ACTION_ERROR, - ACTION_EOF -} t_action_id; - - - -# define FLAG_ARG_A (1 << 0) -# define FLAG_ARG_B (1 << 1) -# define FLAG_ARG_A_B (1 << 2) -# define FLAG_ARG_B_A (1 << 3) +# define ACTION_ID_BUF_SIZE 4 typedef char t_flag_arg; typedef struct { - char *id; + const char *id; t_flag_arg args; union { @@ -64,7 +48,8 @@ typedef struct t_status check(t_stack *a, t_stack *b); t_status read_action(t_stack *a, t_stack *b); -t_status exec_action(char *action_id, t_stack *a, t_stack *b); +t_status exec_action(char action_id[ACTION_ID_BUF_SIZE], + t_stack *a, t_stack *b); t_bool stack_sorted(t_stack *stack); #endif diff --git a/src/checker/main.c b/src/checker/main.c index fdac230..3866800 100644 --- a/src/checker/main.c +++ b/src/checker/main.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:14 by cacharle #+# #+# */ -/* Updated: 2020/01/18 10:44:28 by cacharle ### ########.fr */ +/* Updated: 2020/01/18 12:04:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,35 +16,53 @@ int main(int argc, char **argv) { t_status s; + t_stack *a; + t_stack *b; if (argc == 1) return (0); - t_stack *a = stack_new(argc - 1); - while (--argc > 1) + if ((a = stack_new(argc - 1)) == NULL) + return (1); + while (--argc >= 1) { + printf("argc %d\n", argc); errno = 0; - /* ft_strict_atoi(argv[argc]); */ stack_push(a, ft_strict_atoi(argv[argc])); if (errno != 0) { - stack_destroy(a); ft_putendl_fd("Error", STDERR_FILENO); + stack_destroy(a); return (1); } } - t_stack *b = stack_new(argc - 1); - for (int i = 0; i < a->top; i++) + if ((b = stack_new(stack_length(a))) == NULL) + { + stack_destroy(a); + return (1); + } + printf("stack a: "); + for (int i = 0; i <= a->top; i++) + printf("[%d]", a->elements[i]); + printf("\nstack b: "); + for (int i = 0; i <= b->top; i++) + printf("[%d]", b->elements[i]); + printf("\n"); + + s = check(a, b); + if (s == STATUS_SUCCESS) + ft_putendl("OK"); + else if (s == STATUS_FAILURE) + ft_putendl("KO"); + else if (s == STATUS_ERROR) + ft_putendl_fd("Error", STDERR_FILENO); + + printf("stack a: "); + for (int i = 0; i <= a->top; i++) printf("[%d]", a->elements[i]); - for (int i = 0; i < b->top; i++) + printf("\nstack b: "); + for (int i = 0; i <= b->top; i++) printf("[%d]", b->elements[i]); - printf("\n%d", stack_empty(b)); - /* s = check(a, b); */ - /* if (s == STATUS_SUCCESS) */ - /* ft_putendl("OK"); */ - /* else if (s == STATUS_FAILURE) */ - /* ft_putendl("KO"); */ - /* else if (s == STATUS_ERROR) */ - /* ft_putendl_fd("Error", STDERR_FILENO); */ + printf("\n"); stack_destroy(a); stack_destroy(b); return 0; |
