Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Philosophers-42
Path: blob/master/philo/ft_parsing.c
882 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* ft_parsing.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: yabtaour <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/05/19 22:04:56 by yabtaour #+# #+# */
9
/* Updated: 2022/05/19 22:06:07 by yabtaour ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
#include "philosophers.h"
13
14
int ft_check_args(char **arguments)
15
{
16
int i;
17
int j;
18
19
i = 0;
20
while (arguments[i])
21
{
22
j = 0;
23
while (arguments[i][j])
24
{
25
if ((arguments[i][j] != '+') && (arguments[i][j] > '9'
26
|| arguments[i][j] < '0'))
27
{
28
free_split(arguments);
29
return (0);
30
}
31
j++;
32
}
33
i++;
34
}
35
return (1);
36
}
37
38
int ft_check_plus(char **arguments)
39
{
40
int i;
41
int j;
42
43
i = 0;
44
while (arguments[i])
45
{
46
j = 0;
47
while (arguments[i][j])
48
{
49
if (arguments[i][j] == '+')
50
{
51
if (arguments[i][j + 1] > '9' || arguments[i][j + 1] < '0')
52
{
53
free_split(arguments);
54
return (0);
55
}
56
}
57
j++;
58
}
59
i++;
60
}
61
return (1);
62
}
63
64
int ft_check_max(char **arg)
65
{
66
int i;
67
int j;
68
int found;
69
70
i = 1;
71
j = 0;
72
found = 0;
73
while (arg[i])
74
{
75
j = 0;
76
while (arg[i][j])
77
{
78
if (found != 1 && ft_d(arg[i][j]))
79
found = 1;
80
if (found == 1 && arg[i][j] == ' ' && ft_d(arg[i][j + 1]))
81
return (0);
82
j++;
83
}
84
i++;
85
if (found == 1)
86
found = 0;
87
else
88
return (0);
89
}
90
return (1);
91
}
92
93
int ft_parsing(t_data *data)
94
{
95
data->args = ft_join_args(data->argv, data->argc);
96
if (!data->args)
97
return (0);
98
data->arguments = ft_split(data->args, ' ');
99
if (!data->arguments)
100
{
101
free(data->args);
102
return (0);
103
}
104
free(data->args);
105
if (!ft_check_args(data->arguments))
106
return (0);
107
if (!ft_check_plus(data->arguments))
108
return (0);
109
if (!ft_check_max(data->arguments))
110
return (0);
111
free_split(data->arguments);
112
return (1);
113
}
114
115