push_swap/parsing.c
Etienne Rey-bethbeder 61a3861d39 Projet Fini
2022-12-08 17:47:06 +01:00

93 lines
2 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/07 19:23:59 by erey-bet #+# #+# */
/* Updated: 2022/12/08 11:22:53 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void choice(long *tab, int y)
{
if (y == 4 || y > 5)
pre_push_swap(tab, y);
else if (y == 5)
push_swap_5(tab, y);
else if (y == 3)
push_swap_3(tab, y);
else if (y == 2)
if (tab[0] > tab[1])
write(1, "ra\n", 3);
}
int put_error(void)
{
write(2, "Error\n", 6);
return (1);
}
char **set_only_space(char *argv[], int i)
{
int x;
x = 0;
while (argv[i][x])
if (ft_strchr("\a\b\t\n\v\f\r", argv[i][x++]) != NULL)
argv[i][x - 1] = ' ';
return (argv);
}
int if_there_is_double(long *tab, int y)
{
int i;
int x;
i = 0;
while (i < y)
{
x = 0;
while (x < y)
{
if (tab[i] == tab[x] && i != x)
return (1);
x++;
}
i++;
}
return (0);
}
int parsing(int argc, char *argv[], long *tab)
{
int i;
int y;
int x;
char **tmp;
i = 0;
y = 0;
while (++i < argc)
{
argv = set_only_space(argv, i);
x = 0;
tmp = ft_split(argv[i], ' ');
while (tmp[x] != NULL)
{
if (ft_atoi_check(tmp[x]))
tab[y++] = ft_atoi(tmp[x]);
else
return (put_error());
x++;
}
}
if (if_there_is_double(tab, y) || (y < argc - 1))
return (put_error());
choice(tab, y);
return (0);
}