push_swap/srcs/checker.c
vagrant 770bfc428d Revert "got a working sort"
This reverts commit 2d351417dcbb66b6fb46c5af30ba9d5df5bd4c11.
2019-03-06 16:24:42 +00:00

108 lines
2.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
/* Updated: 2019/03/02 18:28:08 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
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);
nop = nop->next;
}
}
int read_ops(t_psdata *data)
{
t_list *nop;
char buff[5];
int ret;
ft_memset(buff, '\0', 5);
while ((ret = read(1, buff, 4)) > 0)
{
buff[ft_strlen(buff) - 1] = '\0';
if (ret > 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))
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);
}