Push the final version
This commit is contained in:
commit
d0fe1fb2fb
28 changed files with 1459 additions and 0 deletions
53
sources/error.c
Executable file
53
sources/error.c
Executable file
|
|
@ -0,0 +1,53 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:13 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/18 15:17:25 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void error_message(char *msg, t_list **maix, t_list **sb, char **s)
|
||||
{
|
||||
(void)msg;
|
||||
write(2, "Error\n", 6);
|
||||
if (maix)
|
||||
free_stack(maix);
|
||||
if (sb)
|
||||
free_stack(sb);
|
||||
if (s)
|
||||
free_string(s);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void free_string(char **str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
while (i >= 0)
|
||||
free(str[i--]);
|
||||
free(str);
|
||||
}
|
||||
|
||||
void free_stack(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
t_list *tmp;
|
||||
|
||||
head = *stack;
|
||||
while (head)
|
||||
{
|
||||
tmp = head;
|
||||
head = head->next;
|
||||
free(tmp);
|
||||
}
|
||||
free(stack);
|
||||
}
|
||||
67
sources/main.c
Executable file
67
sources/main.c
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/13 23:58:38 by jotavare #+# #+# */
|
||||
/* Updated: 2024/04/18 15:18:22 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static void init_stack(t_list **stack, int argc, char **argv)
|
||||
{
|
||||
t_list *new;
|
||||
char **args;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (argc == 2)
|
||||
args = ft_split(argv[1]);
|
||||
else
|
||||
{
|
||||
i = 1;
|
||||
args = argv;
|
||||
}
|
||||
while (args[i])
|
||||
{
|
||||
new = ft_lstnew(ft_atoi(args[i]));
|
||||
ft_lstadd_back(stack, new);
|
||||
i++;
|
||||
}
|
||||
index_stack(stack);
|
||||
if (argc == 2)
|
||||
free_string(args);
|
||||
}
|
||||
|
||||
static void sort_stack(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if (ft_lstsize(*stack_a) <= 5)
|
||||
simple_sort(stack_a, stack_b);
|
||||
else
|
||||
radix_sort(stack_a, stack_b);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_list **stack_a;
|
||||
t_list **stack_b;
|
||||
|
||||
check_args(argv);
|
||||
stack_a = (t_list **)malloc(sizeof(t_list));
|
||||
if (!stack_a)
|
||||
error_message("alloc stack_a", NULL, NULL, NULL);
|
||||
*stack_a = NULL;
|
||||
init_stack(stack_a, argc, argv);
|
||||
if (is_sorted(stack_a) == 1)
|
||||
(free_stack(stack_a), exit(0));
|
||||
stack_b = (t_list **)malloc(sizeof(t_list));
|
||||
*stack_b = NULL;
|
||||
sort_stack(stack_a, stack_b);
|
||||
free_stack(stack_a);
|
||||
free_stack(stack_b);
|
||||
return (0);
|
||||
}
|
||||
56
sources/push.c
Executable file
56
sources/push.c
Executable file
|
|
@ -0,0 +1,56 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:03 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int push(t_list **stack_to, t_list **stack_from)
|
||||
{
|
||||
t_list *tmp;
|
||||
t_list *head_to;
|
||||
t_list *head_from;
|
||||
|
||||
if (ft_lstsize(*stack_from) == 0)
|
||||
return (-1);
|
||||
head_to = *stack_to;
|
||||
head_from = *stack_from;
|
||||
tmp = head_from;
|
||||
head_from = head_from->next;
|
||||
*stack_from = head_from;
|
||||
if (!head_to)
|
||||
{
|
||||
head_to = tmp;
|
||||
head_to->next = NULL;
|
||||
*stack_to = head_to;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp->next = head_to;
|
||||
*stack_to = tmp;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int pa(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if (push(stack_a, stack_b) == -1)
|
||||
return (-1);
|
||||
write(1, "pa\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int pb(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if (push(stack_b, stack_a) == -1)
|
||||
return (-1);
|
||||
write(1, "pb\n", 3);
|
||||
return (0);
|
||||
}
|
||||
62
sources/reverse.c
Executable file
62
sources/reverse.c
Executable file
|
|
@ -0,0 +1,62 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* reverse.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:06 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int reverse(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
t_list *tail;
|
||||
|
||||
if (ft_lstsize(*stack) < 2)
|
||||
return (-1);
|
||||
head = *stack;
|
||||
tail = ft_lstlast(head);
|
||||
while (head)
|
||||
{
|
||||
if (head->next->next == NULL)
|
||||
{
|
||||
head->next = NULL;
|
||||
break ;
|
||||
}
|
||||
head = head->next;
|
||||
}
|
||||
tail->next = *stack;
|
||||
*stack = tail;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int rra(t_list **stack_a)
|
||||
{
|
||||
if (reverse(stack_a) == -1)
|
||||
return (-1);
|
||||
write(1, "rra\n", 4);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int rrb(t_list **stack_b)
|
||||
{
|
||||
if (reverse(stack_b) == -1)
|
||||
return (-1);
|
||||
write(1, "rrb\n", 4);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int rrr(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2))
|
||||
return (-1);
|
||||
reverse(stack_a);
|
||||
reverse(stack_b);
|
||||
write(1, "rrr\n", 4);
|
||||
return (0);
|
||||
}
|
||||
54
sources/rotate.c
Executable file
54
sources/rotate.c
Executable file
|
|
@ -0,0 +1,54 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rotate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:07 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int rotate(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
t_list *tail;
|
||||
|
||||
if (ft_lstsize(*stack) < 2)
|
||||
return (-1);
|
||||
head = *stack;
|
||||
tail = ft_lstlast(head);
|
||||
*stack = head->next;
|
||||
head->next = NULL;
|
||||
tail->next = head;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ra(t_list **stack_a)
|
||||
{
|
||||
if (rotate(stack_a) == -1)
|
||||
return (-1);
|
||||
write(1, "ra\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int rb(t_list **stack_b)
|
||||
{
|
||||
if (rotate(stack_b) == -1)
|
||||
return (-1);
|
||||
write(1, "rb\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int rr(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2))
|
||||
return (-1);
|
||||
rotate(stack_a);
|
||||
rotate(stack_b);
|
||||
write(1, "rr\n", 3);
|
||||
return (0);
|
||||
}
|
||||
62
sources/sort_radix.c
Executable file
62
sources/sort_radix.c
Executable file
|
|
@ -0,0 +1,62 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_radix.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:02 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int get_max_bits(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
int max;
|
||||
int max_bits;
|
||||
|
||||
head = *stack;
|
||||
max = head->index;
|
||||
max_bits = 0;
|
||||
while (head)
|
||||
{
|
||||
if (head->index > max)
|
||||
max = head->index;
|
||||
head = head->next;
|
||||
}
|
||||
while ((max >> max_bits) != 0)
|
||||
max_bits++;
|
||||
return (max_bits);
|
||||
}
|
||||
|
||||
void radix_sort(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
t_list *head_a;
|
||||
int i;
|
||||
int j;
|
||||
int size;
|
||||
int max_bits;
|
||||
|
||||
i = 0;
|
||||
head_a = *stack_a;
|
||||
size = ft_lstsize(head_a);
|
||||
max_bits = get_max_bits(stack_a);
|
||||
while (i < max_bits)
|
||||
{
|
||||
j = 0;
|
||||
while (j++ < size)
|
||||
{
|
||||
head_a = *stack_a;
|
||||
if (((head_a->index >> i) & 1) == 1)
|
||||
ra(stack_a);
|
||||
else
|
||||
pb(stack_a, stack_b);
|
||||
}
|
||||
while (ft_lstsize(*stack_b) != 0)
|
||||
pa(stack_a, stack_b);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
79
sources/sort_simple.c
Executable file
79
sources/sort_simple.c
Executable file
|
|
@ -0,0 +1,79 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_simple.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:08 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/16 20:16:06 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
void sort_4(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int distance;
|
||||
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
distance = index_distance_head(stack_a, get_min(stack_a, -1));
|
||||
if (distance == 1)
|
||||
ra(stack_a);
|
||||
else if (distance == 2)
|
||||
{
|
||||
ra(stack_a);
|
||||
ra(stack_a);
|
||||
}
|
||||
else if (distance == 3)
|
||||
rra(stack_a);
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
pb(stack_a, stack_b);
|
||||
sort_3(stack_a);
|
||||
pa(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void sort_5(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int distance;
|
||||
|
||||
distance = index_distance_head(stack_a, get_min(stack_a, -1));
|
||||
if (distance == 1)
|
||||
ra(stack_a);
|
||||
else if (distance == 2)
|
||||
{
|
||||
ra(stack_a);
|
||||
ra(stack_a);
|
||||
}
|
||||
else if (distance == 3)
|
||||
{
|
||||
rra(stack_a);
|
||||
rra(stack_a);
|
||||
}
|
||||
else if (distance == 4)
|
||||
rra(stack_a);
|
||||
if (is_sorted(stack_a))
|
||||
return ;
|
||||
pb(stack_a, stack_b);
|
||||
sort_4(stack_a, stack_b);
|
||||
pa(stack_a, stack_b);
|
||||
}
|
||||
|
||||
void simple_sort(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = ft_lstsize(*stack_a);
|
||||
if (is_sorted(stack_a) || size == 0 || size == 1)
|
||||
return ;
|
||||
if (size == 2)
|
||||
sa(stack_a);
|
||||
else if (size == 3)
|
||||
sort_3(stack_a);
|
||||
else if (size == 4)
|
||||
sort_4(stack_a, stack_b);
|
||||
else if (size == 5)
|
||||
sort_5(stack_a, stack_b);
|
||||
}
|
||||
58
sources/sort_small.c
Executable file
58
sources/sort_small.c
Executable file
|
|
@ -0,0 +1,58 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_small.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:07:14 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static void sort_132(t_list **stack_a)
|
||||
{
|
||||
ra(stack_a);
|
||||
sa(stack_a);
|
||||
rra(stack_a);
|
||||
}
|
||||
|
||||
static void sort_231(t_list **stack_a, t_list *head, int min)
|
||||
{
|
||||
if (head->next->index == min)
|
||||
sa(stack_a);
|
||||
else
|
||||
rra(stack_a);
|
||||
}
|
||||
|
||||
static void sort_312(t_list **stack_a, t_list *head, int min)
|
||||
{
|
||||
if (head->next->index == min)
|
||||
ra(stack_a);
|
||||
else
|
||||
{
|
||||
sa(stack_a);
|
||||
rra(stack_a);
|
||||
}
|
||||
}
|
||||
|
||||
void sort_3(t_list **stack_a)
|
||||
{
|
||||
t_list *head;
|
||||
int min;
|
||||
int next_min;
|
||||
|
||||
head = *stack_a;
|
||||
min = get_min(stack_a, -1);
|
||||
next_min = get_min(stack_a, min);
|
||||
if (is_sorted(stack_a) == 1)
|
||||
return ;
|
||||
if (head->index == min && head->next->index != next_min)
|
||||
sort_132(stack_a);
|
||||
else if (head->index == next_min)
|
||||
sort_231(stack_a, head, min);
|
||||
else
|
||||
sort_312(stack_a, head, min);
|
||||
}
|
||||
61
sources/swap.c
Executable file
61
sources/swap.c
Executable file
|
|
@ -0,0 +1,61 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* swap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:09 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/18 15:18:54 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int swap(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
t_list *next;
|
||||
int tmp_val;
|
||||
int tmp_index;
|
||||
|
||||
if (ft_lstsize(*stack) < 2)
|
||||
return (-1);
|
||||
head = *stack;
|
||||
next = head->next;
|
||||
if (!head && !next)
|
||||
error_message("Error swap", stack, NULL, NULL);
|
||||
tmp_val = head->value;
|
||||
tmp_index = head->index;
|
||||
head->value = next->value;
|
||||
head->index = next->index;
|
||||
next->value = tmp_val;
|
||||
next->index = tmp_index;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int sa(t_list **stack_a)
|
||||
{
|
||||
if (swap(stack_a) == -1)
|
||||
return (-1);
|
||||
write(1, "sa\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int sb(t_list **stack_b)
|
||||
{
|
||||
if (swap(stack_b) == -1)
|
||||
return (-1);
|
||||
write(1, "sb\n", 3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ss(t_list **stack_a, t_list **stack_b)
|
||||
{
|
||||
if ((ft_lstsize(*stack_a) < 2) || (ft_lstsize(*stack_b) < 2))
|
||||
return (-1);
|
||||
swap(stack_a);
|
||||
swap(stack_b);
|
||||
write(1, "ss\n", 3);
|
||||
return (0);
|
||||
}
|
||||
98
sources/utilities.c
Executable file
98
sources/utilities.c
Executable file
|
|
@ -0,0 +1,98 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utilities.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:14 by rparodi #+# #+# */
|
||||
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
int index_distance_head(t_list **stack, int index)
|
||||
{
|
||||
t_list *head;
|
||||
int distance;
|
||||
|
||||
distance = 0;
|
||||
head = *stack;
|
||||
while (head)
|
||||
{
|
||||
if (head->index == index)
|
||||
break ;
|
||||
distance++;
|
||||
head = head->next;
|
||||
}
|
||||
return (distance);
|
||||
}
|
||||
|
||||
int get_min(t_list **stack, int val)
|
||||
{
|
||||
t_list *head;
|
||||
int min;
|
||||
|
||||
head = *stack;
|
||||
min = head->index;
|
||||
while (head->next)
|
||||
{
|
||||
head = head->next;
|
||||
if ((head->index < min) && head->index != val)
|
||||
min = head->index;
|
||||
}
|
||||
return (min);
|
||||
}
|
||||
|
||||
static t_list *get_next_min(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
t_list *min;
|
||||
int has_min;
|
||||
|
||||
min = NULL;
|
||||
has_min = 0;
|
||||
head = *stack;
|
||||
if (head)
|
||||
{
|
||||
while (head)
|
||||
{
|
||||
if ((head->index == -1) && (!has_min || head->value < min->value))
|
||||
{
|
||||
min = head;
|
||||
has_min = 1;
|
||||
}
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
return (min);
|
||||
}
|
||||
|
||||
void index_stack(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
head = get_next_min(stack);
|
||||
while (head)
|
||||
{
|
||||
head->index = index++;
|
||||
head = get_next_min(stack);
|
||||
}
|
||||
}
|
||||
|
||||
int is_sorted(t_list **stack)
|
||||
{
|
||||
t_list *head;
|
||||
|
||||
head = *stack;
|
||||
while (head && head->next)
|
||||
{
|
||||
if (head->value > head->next->value)
|
||||
return (0);
|
||||
head = head->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
97
sources/validation.c
Executable file
97
sources/validation.c
Executable file
|
|
@ -0,0 +1,97 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* validation.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/13 12:08:16 by rparodi #+# #+# */
|
||||
/* Updated: 2024/04/18 15:20:44 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/push_swap.h"
|
||||
|
||||
static int ft_contains(int num, char **argv, int i)
|
||||
{
|
||||
i++;
|
||||
while (argv[i])
|
||||
{
|
||||
if (ft_atoi(argv[i]) == num)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_isnum(char *num)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (num[0] == '-' || num[0] == '+')
|
||||
i++;
|
||||
if ((num[0] == '-' || num[0] == '+') && !num[1])
|
||||
return (0);
|
||||
while (num[i])
|
||||
{
|
||||
if (!ft_isdigit(num[i]))
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void ft_check_args_utils(int i, char **args)
|
||||
{
|
||||
long tmp;
|
||||
|
||||
tmp = 0;
|
||||
if (i == 0)
|
||||
{
|
||||
while (args[i] != NULL)
|
||||
{
|
||||
tmp = ft_atoi(args[i]);
|
||||
if (!ft_isnum(args[i]) || *args[i] == '\0')
|
||||
error_message("not number heap", NULL, NULL, args);
|
||||
if (ft_contains(tmp, args, i) == 1 || (ft_len(args[i]) > 11))
|
||||
error_message("overflow heap", NULL, NULL, args);
|
||||
if (tmp < -2147483648 || tmp > 2147483647)
|
||||
error_message("over int heap", NULL, NULL, args);
|
||||
i++;
|
||||
}
|
||||
free_string(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (args[i] != NULL)
|
||||
{
|
||||
tmp = ft_atoi(args[i]);
|
||||
if (!ft_isnum(args[i]) || *args[i] == '\0')
|
||||
error_message("not number stack", NULL, NULL, NULL);
|
||||
if (ft_contains(tmp, args, i) == 1 || (ft_len(args[i]) > 11))
|
||||
error_message("overflow stack", NULL, NULL, NULL);
|
||||
if (tmp < -2147483648 || tmp > 2147483647)
|
||||
error_message("over int stack", NULL, NULL, NULL);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void check_args(char **argv)
|
||||
{
|
||||
int i;
|
||||
char **args;
|
||||
|
||||
if (argv[2] == NULL)
|
||||
{
|
||||
args = ft_split(argv[1]);
|
||||
i = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
args = argv;
|
||||
i = 1;
|
||||
}
|
||||
ft_check_args_utils(i, args);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue