normed out most files still need to split functions accordingly still need to check for hidden norm errors
133 lines
3.0 KiB
C
133 lines
3.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* checker.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
|
/* Updated: 2019/03/15 16:52: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, "rrn") == 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;
|
|
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_putendl_fd("Error", 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, 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);
|
|
}
|