summaryrefslogtreecommitdiff
path: root/src/errors.c
blob: 63e661e22ad42dc6b20c4fc94776570e9f96dfbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
sort_stack Copyright (C) 2026 yctct

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "../include/push_swap.h"
#include <limits.h>

void	puterror(t_list *src)
{
	if (src)
		free_stack(src);
	write(1, "Error\n", 6);
	exit(1);
}

int	is_dup(t_list *stack)
{
	t_list	*first;
	t_list	*second;

	first = stack;
	second = stack->next;
	while (first)
	{
		second = first->next;
		while (second)
		{
			if (first->value == second->value)
				return (1);
			second = second->next;
		}
		first = first->next;
	}
	return (0);
}

int	is_limit_int(t_list *stack)
{
	while (stack)
	{
		if (stack->value > INT_MAX || stack->value < INT_MIN)
			return (1);
		stack = stack->next;
	}
	return (0);
}

int	is_valid_int(char **array)
{
	int	j;
	int	i;

	j = 0;
	while (array[j])
	{
		i = 0;
		while (array[j][i])
		{
			if (array[j][i] == '-' && i == 0)
				i++;
			if (array[j][i] < '0' || array[j][i] > '9')
				return (0);
			i++;
		}
		j++;
	}
	return (1);
}

void	check_errors(t_list *src)
{
	if (is_dup(src))
		puterror(src);
	if (is_limit_int(src))
		puterror(src);
}