-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.c
110 lines (100 loc) · 2.16 KB
/
check.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kristori <kristori@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 16:26:39 by kristori #+# #+# */
/* Updated: 2023/01/12 11:08:44 by kristori ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_check(char **argv, int n)
{
if (ft_check_duplicate(argv, n) == 1)
{
write(2, "Error\n", 7);
return (1);
}
if (ft_check_int(argv, n) == 1)
{
write(2, "Error\n", 7);
return (1);
}
if (ft_check_non_numeric(argv, n) == 1)
{
write(2, "Error\n", 7);
return (1);
}
return (0);
}
int ft_check_duplicate(char **argv, int n)
{
int i;
int j;
i = 1;
j = i + 1;
while (i <= n)
{
while (j <= n)
{
if (ft_str_compare(argv[i], argv[j]) == 0)
return (1);
j++;
}
i++;
j = i + 1;
}
return (0);
}
int ft_check_int(char **argv, int n)
{
int i;
i = 1;
while (i <= n)
{
if (ft_atoli(argv[i]) > INT_MAX)
return (1);
else if (ft_atoli(argv[i]) < INT_MIN)
return (1);
i++;
}
return (0);
}
int ft_check_non_numeric(char **argv, int n)
{
int i;
int j;
i = 1;
j = 0;
while (i <= n)
{
while ((size_t)j < ft_strlen(argv[i]))
{
if (((argv[i][j] >= '0') && (argv[i][j] <= '9'))
|| argv[i][0] == '+' || argv[i][0] == '-')
j++;
else
return (1);
}
j = 0;
i++;
}
return (0);
}
int ft_str_compare(char *str1, char *str2)
{
int i;
i = 0;
while (str1[i] == str2[i])
{
if (str1[i] == '\0' || str2[i] == '\0')
break ;
i++;
}
if (str1[i] == '\0' && str2[i] == '\0')
return (0);
else
return (1);
}