Nice job for a week-end =)
finished checker programm tested actions functions all normed and sexy ;)
This commit is contained in:
129
srcs/checker.c
129
srcs/checker.c
@@ -6,95 +6,102 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/28 17:02:28 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/02 18:28:08 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int check_input(char *in, int *ret)
|
||||
void exec_actions(t_psdata *data)
|
||||
{
|
||||
int i;
|
||||
char *buff;
|
||||
t_list *nop;
|
||||
|
||||
i = 0;
|
||||
if (in[i] && ft_issign(in[i]))
|
||||
i++;
|
||||
while (in[i] && ft_isdigit(in[i]))
|
||||
i++;
|
||||
if (in[i] == '\0' && ft_hasdigit(in) && ft_atois(in, ret))
|
||||
return (1);
|
||||
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);
|
||||
}
|
||||
|
||||
int get_params(t_psdata *data, int ac, char **av)
|
||||
void check_stack(t_psdata *data)
|
||||
{
|
||||
t_stack *new;
|
||||
char **tab;
|
||||
t_stack *ind;
|
||||
int nb;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = ac;
|
||||
nb = 0;
|
||||
while (--i > 0 && !(j = 0))
|
||||
if ((tab = ft_strsplitwhitespace(av[i])) != NULL)
|
||||
ind = data->a;
|
||||
nb = ind->nb;
|
||||
while (ind)
|
||||
{
|
||||
if (ind->nb < nb)
|
||||
{
|
||||
while (tab[j])
|
||||
j++;
|
||||
while (--j >= 0)
|
||||
if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||
ps_stkpsh(data, 'a', new);
|
||||
else
|
||||
{
|
||||
ft_putendl_fd("Error", 2);
|
||||
ps_stkclean(data);
|
||||
return (0);
|
||||
}
|
||||
ft_del_words_tables(&tab);
|
||||
ft_putendl("KO");
|
||||
return ;
|
||||
}
|
||||
return (1);
|
||||
nb = ind->nb;
|
||||
ind = ind->next;
|
||||
}
|
||||
if (!ind)
|
||||
ft_putendl("OK");
|
||||
}
|
||||
|
||||
int is_op(char *buff)
|
||||
{
|
||||
static char ops[11][3] = {"sa", "sb", "ss", "pa", "pb", "ra",
|
||||
"rb", "rr", "rra", "rrb", "rrr"};
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (i < 11 && ft_strcmp(buff, ops[i]) != 0)
|
||||
i++;
|
||||
return (i < 11 && ft_strcmp(buff, ops[i]) == 0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_psdata data;
|
||||
t_stack *new;
|
||||
t_list *nop;
|
||||
char buff[5];
|
||||
int ret;
|
||||
|
||||
ps_initdata(&data);
|
||||
ft_memset(buff, '\0', 5);
|
||||
ret = 0;
|
||||
if (!get_params(&data, ac, av))
|
||||
return (0);
|
||||
new = data.a;
|
||||
if (new == NULL)
|
||||
ft_putendl("Empty");
|
||||
else
|
||||
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);
|
||||
ps_stkclean(&data);
|
||||
return (1);
|
||||
}
|
||||
printf("%d\n", ret);
|
||||
ft_lstaddend(&(data.op), nop);
|
||||
}
|
||||
if (new == NULL || read_ops(&data))
|
||||
{
|
||||
ps_stkclean(&data);
|
||||
return (0);
|
||||
}
|
||||
exec_actions(&data);
|
||||
check_stack(&data);
|
||||
ps_stkclean(&data);
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user