-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_arguments.c
executable file
·280 lines (202 loc) · 6.03 KB
/
check_arguments.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* @file check_arguments.c
* @brief Verification des trames à envoyer au panneau à LED
*
* @author Stéphanie Ouillon
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "board_control.h"
#include "parse_command_line.h"
#include "check_arguments.h"
/* Par défaut, l'ID est mis à 1. */
#define ID "01"
/* Check and save display options and text. */
int check_arguments( struct_arg_options arg_options, char* text ){
/* On enregistre l'ID par défaut.*/
if ( ( strlen( ID ) == ( LENGTH_ID - 1 ) ) && ( atoi( ID ) == 1 ) )
strcpy( options.id, ID );
else{
printf( "Bad value of ID in check_arguments.\n" );
return EXIT_FAILURE;
}
/* Check each display option */
if ( check_option_page( arg_options.page ) != 0 )
return EXIT_FAILURE;
if ( check_option_lead_cmd( arg_options.lead_cmd ) != 0 )
return EXIT_FAILURE;
if ( check_option_display_meth( arg_options.display_meth ) !=0 )
return EXIT_FAILURE;
if ( check_option_wait_time( arg_options.wait_time ) != 0 )
return EXIT_FAILURE;
if ( check_option_lag_cmd( arg_options.lag_cmd ) != 0 )
return EXIT_FAILURE;
/* Check priority and save it */
if ( ( arg_options.priority != '0' ) && ( arg_options.priority != '1' ) ){
printf("[!!]ERROR the option 'priority' is not correct.\n");
return EXIT_FAILURE;
}
/* Save options to be written in the framework */
if ( save_option( options.page, arg_options.page ) != 0 )
return EXIT_FAILURE;
if ( save_option( options.lead_cmd, arg_options.lead_cmd ) != 0 )
return EXIT_FAILURE;
if ( save_option( options.display_meth, arg_options.display_meth ) != 0 )
return EXIT_FAILURE;
if ( save_option( options.wait_time, arg_options.wait_time ) != 0 )
return EXIT_FAILURE;
if ( save_option( options.lag_cmd, arg_options.lag_cmd ) != 0 )
return EXIT_FAILURE;
options.priority[0] = arg_options.priority;
/* Check test message doesn't have any non-supported characters, or replace them */
if ( check_text( text ) != 0 )
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/* Check validity of text */
int check_text( char* text ){
//debug
//printf("We enter check_text.\n");
if ( remove_spec_char( text ) != 0 ){
printf("[!!]ERROR remove_spec_char() has failed.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/* Check display options are capital letters. */
int check_option_page( char argv ){
char letter_begin = 'A';
char letter_end = 'Z';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
printf("[!!]ERROR the option 'page' is not correct.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int check_option_lead_cmd( char argv ){
char letter_begin = 'A';
char letter_end = 'S';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
printf("[!!]ERROR the option 'lead command' is not correct.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int check_option_display_meth( char argv ){
int var = 0;
char letter_begin = 'A';
char letter_end = 'B';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
var++ ;
}
letter_begin = 'Q';
letter_end = 'R';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
var++ ;
}
letter_begin = 'a';
letter_end = 'b';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
var++ ;
}
letter_begin = 'q';
letter_end = 'r';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
var++ ;
}
if ( var == 4 ){
printf("ERROR the option 'lead command' is not correct.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int check_option_wait_time( char argv ){
char letter_begin = 'A';
char letter_end = 'K';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
printf("[!!]ERROR the option 'waiting time' is not correct.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int check_option_lag_cmd( char argv ){
char letter_begin = 'A';
char letter_end = 'K';
if ( check_option( argv, letter_begin, letter_end ) != 0 ){
printf("[!!]ERROR the option 'lag command' is not correct.\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int check_option( char argv, char letter_begin, char letter_end ){
if ( is_letter( argv ) != 0 ){
printf("[!!]ERROR %c is not a letter.\n", argv );
return EXIT_FAILURE;
}
else{
if ( is_letter_between( argv, letter_begin, letter_end ) != 0 ){
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
/* Save an option if correct to be written in the framework. */
int save_option( char* argv1, char argv2){
printf("The saved option is: \n");
*argv1 = argv2;
printf("*argv1 : %c\n", *argv1);
return EXIT_SUCCESS;
}
int is_letter_between( char letter_tested, char letter_begin, char letter_end ){
if ( ( letter_tested < letter_begin || letter_tested > letter_end ) )
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
/* Check option arguments are letters only */
int is_letter( char argv ){
if ( isalpha( argv ) == 0 ){
perror( "isalpha() false! Character is not a letter.\n" );
return EXIT_FAILURE;
}
return EXIT_SUCCESS ;
}
/* Remove accents and special characters off the text. */
int remove_spec_char( char* text ){
int j = 0;
int length = strlen( text );
/* char* table[2][11];
table[0][0] = "é";
table[0][1] = "è";
table[0][2] = "ê";
table[0][3] = "ç";
table[0][4] = "î";
table[0][5] = "ô";
table[0][6] = "ù";
table[0][7] = "â";
table[0][8] = "ä";
table[0][9] = "à";
table[0][10] = "€";
table[1][0] = "e";
table[1][1] = "e";
table[1][2] = "e";
table[1][3] = "c";
table[1][4] = "i";
table[1][5] = "o";
table[1][6] = "u";
table[1][7] = "a";
table[1][8] = "a";
table[1][9] = "a";
table[1][10] = "E"; */
while ( j < length ){
if ( ( ( isalnum( text[j] ) == 0 ) && ( ispunct( text[j] ) == 0 ) ) ||
( ( text[j] == '<' ) || ( text[j] == '>' ) ) ){
printf("Le carcatère %c n'est pas accepté.\n", text[j]);
return EXIT_FAILURE;
}
j++;
}
return EXIT_SUCCESS;
}