Files
42_INT_06_pushwap/srcs/push_swap.c
2021-06-27 12:26:18 +02:00

84 lines
1.8 KiB
C

#include "push_swap.h"
void is_valid(int ac, char **av)
{
if (ac < 2)
ps_stop(NULL, NULL, 1);
(void)av;
// check more error
}
int check_flag(int *ac, char ***av)
{
if (ft_strcmp((*av)[1], "-p") != 0)
return (0);
(*av)++;;
(*ac)--;
return (1);
}
t_stack *init_stack(int ac, char **av)
{
t_stack *start;
t_stack *tmp;
tmp = NULL;
while (--ac)
{
if (!(start = ft_calloc(1, sizeof(t_stack))))
ps_stop(NULL, NULL, 2);
start->n = ft_atoi(av[ac]);
start->next = tmp;
tmp = start;
}
return (start);
}
/*
** this function creates the stack b and the list solution
** then it init the list solution with :
** - first element : pointer to stack a
** - secnd element : pointer to stack b
** - third element : initial values of stack a (in case of flag -p)
** then it calls the sorting algorithm
** then it calls ps_stop() to free everything that needs to be freed
*/
t_list *launch_algo(t_stack *a, int flag)
{
t_stack *b;
t_list *solution;
b = NULL;
solution = ft_lstnew(&a);
ft_lstadd_back(&solution, ft_lstnew(&b));
if (flag)
fill_solution(solution, "start");
hugo_sort(&a, &b, solution);
//bubble_sort(&a, &b, solution);
return (solution);
}
/*
** this programme will sort a list
** this function first check the validity of the arguments
** then it checks for flags (currently only -p)
** then it parse the list into a linked list
** then it calls the sorting algorithm (it actually calls a launch function that will execute some actions and calls the sorting algorithm)
** and finally it frees everything and leaves
*/
int main(int ac, char **av)
{
t_stack *stack;
t_list *result;
int flag;
is_valid(ac, av);
flag = check_flag(&ac, &av);
stack = init_stack(ac, av);
result = launch_algo(stack, flag);
print_result(result, flag);
ps_stop(NULL, result, 0);
return(0);
}