/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* checker.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */ /* Updated: 2019/03/09 22:44:29 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" #define VISU_SPEED 1000000 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 %4d|| %5d %4d\n", as->nb, as->ind, bs->nb, bs->ind); else if (!as && bs) ft_printf("%5c %4c|| %5d %4d\n", ' ', ' ', bs->nb, bs->ind); else if (as && !bs) ft_printf("%5d %4d|| %5c %c\n", as->nb, as->ind, ' ', ' '); 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_strlen(buff) == 2) { 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]); } else if (buff[2] != 'r') ps_rerot(data, buff[2]); else if (buff[2] == 'r') ps_rrerot(data); debug(data, buff); nop = nop->next; } } int read_ops(t_psdata *data) { t_list *nop; char *buff; int ret; buff = NULL; while ((ret = get_next_line(0, &buff)) > 0) { if (ft_strlen(buff) > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL) { ft_printf("'%s' %d %d %d\n", buff, ft_strlen(buff) > 4, !is_op(buff), nop == NULL); ft_putendl_fd("Error 2", 2); return (1); } ft_lstaddend(&(data->op), nop); } return (0); } void check_stack(t_psdata *data) { t_stack *ind; int nb; ind = data->a; nb = ind->nb; while (ind) { if (ind->nb < nb) { ft_putendl("KO"); return ; } nb = ind->nb; ind = ind->next; } if (!ind) ft_putendl("OK"); } 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)) return (0); new = data.a; if (new == NULL) ft_putendl("Empty"); if (new == NULL || read_ops(&data)) { ps_stkclean(&data); return (0); } exec_actions(&data); check_stack(&data); ps_stkclean(&data); return (0); }