/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* checker.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */ /* Updated: 2019/03/19 13:41:54 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" #define VISU_SPEED 500000 void debug(t_psdata *data, char *com) { t_stack *as; t_stack *bs; as = data->a; bs = data->b; ft_printf(FT_CLEAR); ft_printf("============== %4s ==============\n", com); while (as || bs) { if (as && bs) ft_printf("%5d || %5d\n", as->nb, bs->nb); else if (!as && bs) ft_printf("%5c || %5d\n", ' ', bs->nb); else if (as && !bs) ft_printf("%5d || %5c\n", as->nb, ' '); as = (as) ? as->next : as; bs = (bs) ? bs->next : bs; } ft_printf("==================================\n"); usleep(VISU_SPEED); } void exec_actions(t_psdata *data) { char *buff; t_list *nop; nop = data->op; while (nop && (buff = (char*)nop->content)) { if (ft_strcmp(buff, "rra") == 0 || ft_strcmp(buff, "rrb") == 0) ps_rerot(data, buff[2]); else if (ft_strcmp(buff, "rrr") == 0) ps_rrerot(data); else if (buff[0] == 's' && buff[1] != 's') ps_swap(data, buff[1]); else if (buff[0] == 's' && buff[1] == 's') ps_sswap(data); else if (buff[0] == 'r' && buff[1] != 'r') ps_rot(data, buff[1]); else if (buff[0] == 'r' && buff[1] == 'r') ps_rrot(data); else if (buff[0] == 'p') ps_push(data, buff[1]); if (data->viz) debug(data, buff); nop = nop->next; } } int read_ops(t_psdata *data) { t_list *nop; char *buff; char tmp[4]; int ret; buff = NULL; ft_bzero(tmp, 4); while ((ret = ft_getline(&buff)) > 0) { ft_printf("=== debug ===\nbuff: '%s'\n", buff); ft_strncpy(tmp, buff, 3); if (ft_strlen(buff) > 4 || !is_op(buff) || (nop = ft_lstnew(tmp, 4)) == NULL) { ft_putendl_fd("Error", 2); ft_strdel(&buff); return (1); } ft_lstaddend(&(data->op), nop); ft_strdel(&buff); } ft_strdel(&buff); return (0); } void check_stack(t_psdata *data) { t_stack *ind; int nb; ind = data->a; if (ind) nb = ind->nb; while (ind) { if (ind->nb < nb || data->size_b > 0) { ft_putendl("KO"); return ; } nb = ind->nb; ind = ind->next; } if (!ind && data->size_b == 0) ft_putendl("OK"); else ft_putendl("KO"); } int main(int ac, char **av) { t_psdata data; t_stack *new; int ret; ps_initdata(&data); ret = 0; if (!get_params(&data, ac, av, 1)) return (0); new = data.a; if (new == NULL) ft_putendl_fd("Error", 2); if (new == NULL || read_ops(&data)) { ps_stkclean(&data); return (0); } exec_actions(&data); check_stack(&data); ps_stkclean(&data); return (0); }