-
Notifications
You must be signed in to change notification settings - Fork 0
/
appointment.c
331 lines (303 loc) · 20.4 KB
/
appointment.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
|----------------------------------------------------------|
| This file contains the functions |
| used on the appointments |
|----------------------------------------------------------|
*/
// -------------------------- Includes --------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "appointment.h"
// -------------------------- Functions --------------------------
p_appointment createEmptyAppointment() {
p_appointment new = (p_appointment) malloc (sizeof(t_appointment)); // Allocate memory
t_date date; // Create date structure
t_time length; // Create length structure
t_time hours; // Create hours structure
// Initialize all fields
date.day = -1;
date.month = -1;
date.years = -1;
length.hours = -1;
length.minutes = -1;
hours.hours = -1;
hours.minutes = -1;
new->date = date; // Attribute date
new->hour = hours; // Attribute hours
new->length = length; // Attribute length
new->object = NULL; // Set object to NULL
new->next = NULL; // Set next to NULL
return new;
}
char* deleteLastChar(char* input) {
char* res = (char*) malloc(100*sizeof(char)); // Str variable to stock the result
char* temp = (char*) malloc(100*sizeof(char)); // Str variable to transform character into strings
for (int i = 0 ; i < (int) strlen(input)-1 ; i++) { // Loop to add 1 by 1 all character-1 of the current string (avoid the\n)
temp[0]=input[i]; // Convert from character to string
strcat(res, temp); // Cat to the new result string
}
free(temp); // Free memory
return res;
}
int checkDateFormat(p_appointment new_appointment) {
char* input = (char*) malloc(100*sizeof(char)); // --> Comment to the right // Create a str variable to stock the input
char* day = (char*) malloc(100*sizeof(char));
char* month = (char*) malloc(100*sizeof(char));
char* years = (char*) malloc(100*sizeof(char));
char* temp = (char*) malloc(100*sizeof(char)); // Create some str variable to manipulate the input
char** convert = (char**) malloc(100*sizeof(char*));
int i = 0; // Set an index to 0
printf("~> ");
fgets(input, 100, stdin); // Get the input
if (strlen(input)==11) { // Check if the input is the correct length
while (i!=10) { // Loop to check all element
if (i != 2 && i != 5) { // Avoid checking if the separator is a number
if (input[i] >= 48 && input[i] <= 57) { // Check if caracter is a number (avoid atoi error)
temp[0] = input[i]; // modify the temporary string so we can strcat() it just after
} else { // Case where it's not a number
printf("Not a number\n");
free(month);
free(day);
free(years);
free(temp);
free(convert);
free(input);
return -1;
}
if (i < 2) { // If i<2 mean it's the day
strcat(day, temp);
} else if (i < 5) { // If i<5 mean it's the month
strcat(month, temp);
} else { // If i<8 mean it's the day
strcat(years, temp);
}
} else if (input[2]!='/' || input[5]!='/') { // Check it's the correct separator
printf("Not the right separator\n");
free(month);
free(day);
free(years);
free(temp);
free(convert);
free(input);
return -1;
}
i++;
}
new_appointment->date.day = (int) strtol (day, convert, 10); // Convert and attribute the day value
new_appointment->date.month = (int) strtol (month, convert, 10); // Convert and attribute the month value
new_appointment->date.years = (int) strtol (years, convert, 10); // Convert and attribute the years value
free(input);
free(month);
free(day);
free(years);
free(temp);
free(convert);
if (new_appointment->date.month > 12 || new_appointment->date.day > 31) { // Overall Check if there isn't big mistake in the month and day value
printf("The day / month isn't possible\n");
return -1;
} else if ((new_appointment->date.month %2 != 0 && new_appointment->date.month<8) || (new_appointment->date.month %2 == 0 && new_appointment->date.month> 7)) { // Case where month supposed to have 31 day max
if (new_appointment->date.day > 31) {
printf("Month supposed to have maximum 31 days\n");
return -1;
}
} else {
if (new_appointment->date.month == 2 && new_appointment->date.day > 28) { // Case where month supposed to have 28 day max
printf("Month supposed to have maximum 28 days\n");
return -1;
} else if (new_appointment->date.day > 30) { // Case where month supposed to have 30 day max
printf("Month supposed to have maximum 30 days\n");
return -1;
}
}
return 0;
} else {
printf("Input too long to be a correct date\n");
return -1;
}
}
int checkHourFormat(p_appointment new_appointment) {
char* input = (char*) malloc(100*sizeof(char)); // str variables to stock the input
char* hours = (char*) malloc(100*sizeof(char)); // str variables to stock the hours
char* minutes = (char*) malloc(100*sizeof(char)); // str variables to stock the minutes
char* temp = (char*) malloc(100*sizeof(char)); // str variables to stock the temp
char** convert = (char**) malloc(100*sizeof(char*));
int i = 0, skip = 2; // Set a index to 0 and the skip variable to the skip if length of the input is 6
printf("~> ");
fgets(input, 100, stdin);
if (strlen(input)==5) { // If the length of the input is 5, set the skip to 1 (line 85 avoid an else condition)
skip = 1;
}
if (strlen(input) == 5 || strlen(input) == 6) { // Check the input is the correct possible length
while (input[i+1]!='\0') { // Loop to go through all caracter except the end one
if (i!=skip) { // Avoid checking the delimiter
if (input[i] >= 48 && input[i] <= 57) { // Check if it's a number
temp[0] = input[i]; // Adding the number to the argument temporary variable
if (strlen(input)==5 && i<1) { // Case where the argument is the hour when there is a 1 digit hour
strcat(hours, temp);
} else if (strlen(input)==5 && i > 1) { // Case where the argument is the minute when there is a 1 digit hour
strcat(minutes, temp);
} else if (strlen(input)==6 && i<2) { // Case where the argument is the hour when there is a 2 digits hour
strcat(hours, temp);
} else if (strlen(input)==6 && i>2) { // Case where the argument is the minute when there is a 2 digits hour
strcat(minutes, temp);
}
} else {
printf("Not a number\n");
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
return -1;
}
} else if (input[i]!='h') { // Check if it's the correct delimiter
printf("not the correct separator\n");
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
return -1;
}
i++; // Incrementing the index
}
new_appointment->hour.hours = (int) strtol(hours, convert, 10); // Convert and attribute the hours value
new_appointment->hour.minutes = (int) strtol(minutes, convert, 10); // Convert and attribute the minutes value
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
if (new_appointment->hour.hours> 23 || new_appointment->hour.minutes > 59) { // Check that the hours and minute variable are possible
printf("Not a valid hours / minute\n");
return -1;
}
return 0;
} else {
printf("Not the correct length\n");
return -1;
}
}
int checkLengthAppointmentFormat(p_appointment new_appointment) {
char* input = (char*) malloc(100*sizeof(char)); // str vcariable to stock the input
char* hours = (char*) malloc(100*sizeof(char));
char* minutes = (char*) malloc(100*sizeof(char));
char* temp = (char*) malloc(100*sizeof(char));
char** convert = (char**) malloc(100*sizeof(char*)); // str variables to manipulate the arguments
int i = 0, skip = 2; // Set a index to 0 and the skip variable to the skip if length of the input is 6
printf("~> ");
fgets(input, 100, stdin);
if (strlen(input)==5) { // If the length of the input is 5, set the skip to 1 (line 85 avoid an else condition)
skip = 1;
}
if (strlen(input) == 5 || strlen(input) == 6) { // Check the input is the correct possible length
while (input[i+1]!='\0') { // Loop to go through all character except the end one
if (i!=skip) { // Avoid checking the delimiter
if (input[i] >= 48 && input[i] <= 57) { // Check if it's a number
temp[0] = input[i]; // Adding the number to the argument temporary variable
if (strlen(input)==5 && i<1) { // Case where the argument is the hour when there is a 1 digit hour
strcat(hours, temp);
} else if (strlen(input)==5 && i> 1) { // Case where the argument is the minute when there is a 1 digit hour
strcat(minutes, temp);
} else if (strlen(input)==6 && i<2) { // Case where the argument is the hour when there is a 2 digits hour
strcat(hours, temp);
} else if (strlen(input)==6 && i>2) { // Case where the argument is the minute when there is a 2 digits hour
strcat(minutes, temp);
}
} else {
printf("Not a number\n");
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
return -1;
}
} else if (input[i]!='h') { // Check if it's the correct delimiter
printf("not the correct separator\n");
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
return -1;
}
i++; // Incrementing the index
}
new_appointment->length.hours = (int) strtol(hours, convert, 10); // Convert and attribute the hours value
new_appointment->length.minutes = (int) strtol(minutes, convert, 10); // Convert and attribute the minutes value
free(hours);
free(minutes);
free(temp);
free(convert);
free(input);
if (new_appointment->length.minutes > 59) { // Check that minute variable is possible
printf("Not a valid hours / minute\n");
return -1;
}
return 0;
} else {
printf("Not the correct length\n");
return -1;
}
}
int checkLengthObject(p_appointment new_appointment) {
char* input = (char*) malloc(100*sizeof(char)); // Str variable to stock the input
char* res = (char*) malloc(100*sizeof(char)); // Str variable to stock the result
char* temp = (char*) malloc(100*sizeof(char)); // Str variable to transform character into strings
printf("~> ");
fgets(input, 100, stdin); // Get the input
if (strlen(input)<=1) { // If input as no character
return -1;
}
new_appointment->object = deleteLastChar(input); // Attribute the input
free(input); // Free memory
free(temp); // Free memory
return 0;
}
int compareDate(p_appointment toplace, p_appointment check) { // We use negative return to return and identify it has to be placed before and positive to identify and insert after
if (toplace->date.years < check->date.years) { // Compare the year of an appointment
return -1;
} else if (toplace->date.years > check->date.years) {
return 1;
} else { // Compare the month of an appointment
if (toplace->date.month < check->date.month) {
return -2;
} else if (toplace->date.month > check->date.month) {
return 2;
} else { // Compare the day of an appointment
if (toplace->date.day < check->date.day) {
return -3;
} else if (toplace->date.day > check->date.day) {
return 3;
} else { // Compare the time hour of an appointment
if (toplace->hour.hours < check->hour.hours) {
return -4;
} else if (toplace->hour.hours > check->hour.hours) {
return 4;
} else { // Compare the time minute of an appointment
if (toplace->hour.minutes < check->hour.minutes ) {
return -5;
} else if (toplace->hour.minutes > check->hour.minutes ) {
return 5;
} else { // Compare the length hour of the appointment
if (toplace->length.hours < check->length.hours ) { // Place the short appointment before
return -6;
} else if (toplace->length.hours > check->length.hours ) {
return 6;
} else { // Compare the length minute of the appointment
if (toplace->length.minutes < check->length.minutes ) { // Place the short appointment before
return -7;
} else if (toplace->length.minutes > check->length.minutes ) {
return 7;
} else {
return 0;
}
}
}
}
}
}
}
}