-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
141 lines (129 loc) · 2.6 KB
/
Copy pathparser.c
File metadata and controls
141 lines (129 loc) · 2.6 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dporhomo <dporhomo@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/06/02 16:22:09 by dporhomo #+# #+# */
/* Updated: 2026/06/11 12:36:51 by dporhomo ### ########.fr */
/* */
/* ************************************************************************** */
/* Parses the CLI input string, validates it and sets up the prog variables*/
#include "codexion.h"
static int validate_number(char *str)
{
long long res;
int i;
i = 0;
res = 0;
if (str[i] == '+')
i++;
if (!str[i])
return (0);
while (str[i])
{
if (str[i] < '0' || str[i] > '9')
return (0);
res = res * 10 + (str[i] - '0');
if (res > 2147483647)
return (0);
i++;
}
return (1);
}
static char *join_args(int argc, char **argv)
{
int i;
int j;
int len;
char *str;
len = 0;
i = 1;
while (i < argc)
len += strlen(argv[i++]) + 1;
str = malloc(len + 1);
if (!str)
return (NULL);
memset(str, 0, len + 1);
i = 1;
len = 0;
while (i < argc)
{
j = 0;
while (argv[i][j])
str[len++] = argv[i][j++];
str[len++] = ' ';
i++;
}
return (str);
}
static int extract_tokens(char *str, char **tokens)
{
int count;
int in_word;
count = 0;
in_word = 0;
while (*str)
{
if (*str == ' ' || *str == '\t')
{
*str = '\0';
in_word = 0;
}
else if (!in_word)
{
if (count >= 8)
return (-1);
tokens[count++] = str;
in_word = 1;
}
str++;
}
return (count);
}
static int validate_and_populate(char **tokens, t_program *prog)
{
int i;
i = -1;
while (++i < 7)
{
if (!validate_number(tokens[i]))
return (0);
}
prog->num_coders = atoi(tokens[0]);
if (prog->num_coders <= 0)
return (0);
prog->t_burnout = atoi(tokens[1]);
prog->t_compile = atoi(tokens[2]);
prog->t_debug = atoi(tokens[3]);
prog->t_refactor = atoi(tokens[4]);
prog->req_compiles = atoi(tokens[5]);
prog->cooldown = atoi(tokens[6]);
if (strcmp(tokens[7], "fifo") == 0)
prog->is_edf = 0;
else if (strcmp(tokens[7], "edf") == 0)
prog->is_edf = 1;
else
return (0);
return (1);
}
int parse_input(int argc, char **argv, t_program *prog)
{
char *joined;
char *tokens[8];
int status;
if (argc < 2)
return (0);
joined = join_args(argc, argv);
if (!joined)
return (0);
if (extract_tokens(joined, tokens) != 8)
{
free(joined);
return (0);
}
status = validate_and_populate(tokens, prog);
free(joined);
return (status);
}