/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* check.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/18 10:16:08 by cacharle #+# #+# */ /* Updated: 2021/09/10 15:36:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "checker.h" static t_bool stack_sorted(t_stack *stack) { int i; if (stack_length(stack) < 2) return (TRUE); i = stack->top + 1; while (--i > 0) if (stack->elements[i] > stack->elements[i - 1]) return (FALSE); return (TRUE); } t_status check(t_stack *a, t_stack *b) { t_status read_status; read_status = read_action(a, b); while (read_status != STATUS_EOF) { read_status = read_action(a, b); if (read_status == STATUS_ERROR) return (STATUS_ERROR); } if (stack_sorted(a) && stack_empty(b)) return (STATUS_SUCCESS); else return (STATUS_FAILURE); } t_status read_action(t_stack *a, t_stack *b) { int i; int ret; char c; char buf[ACTION_ID_BUF_SIZE]; ft_bzero(buf, sizeof(char) * 4); i = 0; while ((ret = read(STDIN_FILENO, &c, 1)) > 0) { if (c == '\n') return (exec_action(buf, a, b)); if (i >= ACTION_ID_BUF_SIZE - 1) return (STATUS_ERROR); buf[i++] = c; buf[i] = '\0'; } if (ret == 0) return (STATUS_EOF); else return (STATUS_ERROR); } static const t_action g_actions[] = { {"sa", FLAG_ARG_A, {.arg_1 = &stack_swap}}, {"sb", FLAG_ARG_B, {.arg_1 = &stack_swap}}, {"ss", FLAG_ARG_A_B, {.arg_2 = &stack_swap_2}}, {"pa", FLAG_ARG_B_A, {.arg_2 = &stack_push_to}}, {"pb", FLAG_ARG_A_B, {.arg_2 = &stack_push_to}}, {"ra", FLAG_ARG_A, {.arg_1 = &stack_rotate}}, {"rb", FLAG_ARG_B, {.arg_1 = &stack_rotate}}, {"rr", FLAG_ARG_A_B, {.arg_2 = &stack_rotate_2}}, {"rra", FLAG_ARG_A, {.arg_1 = &stack_reverse_rotate}}, {"rrb", FLAG_ARG_B, {.arg_1 = &stack_reverse_rotate}}, {"rrr", FLAG_ARG_A_B, {.arg_2 = &stack_reverse_rotate_2}}, }; static const int g_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) { int i; i = 0; while (i < g_actions_size && ft_strcmp(action_id, g_actions[i].id) != 0) i++; if (i == g_actions_size) return (STATUS_ERROR); if (g_actions[i].args == FLAG_ARG_A) g_actions[i].u_func.arg_1(a); else if (g_actions[i].args == FLAG_ARG_B) g_actions[i].u_func.arg_1(b); else if (g_actions[i].args == FLAG_ARG_A_B) g_actions[i].u_func.arg_2(a, b); else if (g_actions[i].args == FLAG_ARG_B_A) g_actions[i].u_func.arg_2(b, a); else return (STATUS_ERROR); return (STATUS_SUCCESS); }