111 lines
2.2 KiB
C
111 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
|
|
/* Updated: 2022/12/11 17:52:08 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "pushswap.h"
|
|
|
|
int ft_isin(long int *tab, int len, long int value)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < len)
|
|
{
|
|
if (tab[i] == value)
|
|
return (1);
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
void ft_error(char **splitted)
|
|
{
|
|
int len;
|
|
|
|
write(2, "Error\n", 6);
|
|
len = 0;
|
|
while (splitted[len] != NULL)
|
|
len++;
|
|
ft_cancel(splitted, len);
|
|
}
|
|
|
|
int ft_parse(long int *tab, const char *s, int index)
|
|
{
|
|
char **splitted;
|
|
int i;
|
|
|
|
splitted = ft_split(s, ' ');
|
|
if (splitted == NULL)
|
|
return (1);
|
|
i = -1;
|
|
if (splitted[0] == NULL)
|
|
write(2, "Error\n", 6);
|
|
while (splitted[++i] != NULL)
|
|
{
|
|
if (!ft_isnum(splitted[i]))
|
|
{
|
|
ft_error(splitted);
|
|
return (0);
|
|
}
|
|
tab[index + i] = ft_atoi(splitted[i]);
|
|
if (ft_isin(tab, index + i, tab[index + i]))
|
|
{
|
|
ft_error(splitted);
|
|
return (0);
|
|
}
|
|
}
|
|
ft_cancel(splitted, i + 1);
|
|
return (i);
|
|
}
|
|
|
|
size_t ft_total_len(int argc, char **argv)
|
|
{
|
|
size_t len;
|
|
int i;
|
|
|
|
len = 0;
|
|
i = 1;
|
|
while (i < argc)
|
|
{
|
|
len += ft_seglen(argv[i], ' ');
|
|
i++;
|
|
}
|
|
return (len);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i;
|
|
int y;
|
|
size_t len;
|
|
long int *tab_a;
|
|
|
|
tab_a = malloc(ft_total_len(argc, argv) * sizeof(long int));
|
|
if (tab_a == NULL)
|
|
return (1);
|
|
i = 0;
|
|
len = 0;
|
|
while (++i < argc)
|
|
{
|
|
y = ft_parse(tab_a, argv[i], (int) len);
|
|
if (y == 0)
|
|
{
|
|
free(tab_a);
|
|
return (1);
|
|
}
|
|
len += y;
|
|
}
|
|
if (len >= 2)
|
|
ft_sort(tab_a, len);
|
|
free(tab_a);
|
|
return (0);
|
|
}
|