-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_check.c
109 lines (98 loc) · 2.22 KB
/
input_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvonsovs <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/17 14:24:13 by fvonsovs #+# #+# */
/* Updated: 2023/04/17 18:01:31 by fvonsovs ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_number(char *argv)
{
int i;
i = 0;
if (((argv[i] == '+') || (argv[i] == '-')) && argv[i + 1] != '\0')
i++;
while (argv[i] && ft_isdigit(argv[i]))
i++;
if (argv[i] != '\0' && !ft_isdigit(argv[i]))
return (0);
return (1);
}
// compares strings ignoring + sign
int string_compare(const char *s1, const char *s2)
{
int i;
int j;
i = 0;
j = i;
if (s1[i] == '+')
{
if (s2[j] != '+')
i++;
}
else
{
if (s2[j] == '+')
j++;
}
while (s1[i] != '\0' && s2[j] != '\0' && s1[i] == s2[j])
{
i++;
j++;
}
return ((unsigned char)s1[i] - (unsigned char)s2[j]);
}
// checks for duplicates
int check_dupes(char **argv)
{
int i;
int j;
i = 1;
while (argv[i])
{
j = 1;
while (argv[j])
{
if (j != i && !string_compare(argv[i], argv[j]))
return (1);
j++;
}
i++;
}
return (0);
}
int is_zero(char *argv)
{
int i;
i = 0;
if ((argv[i] == '+') || (argv[i] == '-'))
i++;
while (argv[i] && argv[i] == '0')
i++;
if (argv[i] != '\0')
return (0);
return (1);
}
int check_input(char **argv)
{
int i;
int zeros;
zeros = 0;
i = 1;
while (argv[i])
{
if (!is_number(argv[i]))
you_fucked_up("Non-digits in input");
zeros += is_zero(argv[i]);
i++;
}
if (zeros > 1)
you_fucked_up("Multiple zeros in input");
if (check_dupes(argv))
you_fucked_up("Duplicates in input");
return (1);
}