-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunelection.c
445 lines (321 loc) · 9.37 KB
/
runelection.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./election.h"
#include "./utils/file.h"
void commands(); // Print commands
void help(); // Print help
void clearScreen(); // Clear screen with newlines
void fInsRecords(electionPtr el, FILE *finput); // Insert records from input file
int main(int argc, char *argv[]) {
electionPtr el = NULL;
char *input = NULL;
char *output = NULL;
int numofupdates = -1;
FILE *finput = NULL;
FILE *foutput = NULL;
if (argc < 2) { // No arguments
el = electionInit4();
}
else if (argc > 7) { // Too many arguments
printf("Too many arguments.\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n");
return 1;
}
else {
for (int i = 1; i < argc; i += 2) { // Check all flags and parameters
if (argv[i][0] == '-') { // If argument is flag
switch (argv[i][1]) { // Check which option it is
case 'i':
if (input != NULL) {
printf("Duplicate flag: '%s'\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n", argv[i]);
return 1;
}
else {
input = argv[i+1];
finput = fopen(input, "r");
if (finput == NULL) {
printf("Invalid input file: File does not exist.\n");
return 2;
}
}
break;
case 'o':
if (output != NULL) {
printf("Duplicate flag: '%s'\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n", argv[i]);
return 1;
}
else {
output = argv[i+1];
foutput = fopen(output, "w");
if (foutput == NULL) {
printf("Invalid output file: File cannot be opened.\n");
return 2;
}
}
break;
case 'n':
if (numofupdates != -1) {
printf("Duplicate flag: '%s'\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n", argv[i]);
return 1;
}
else {
int num = atoi(argv[i+1]);
if (num <= 0) {
printf("Invalid parameter: numofupdates must be greater than or equal to 1.\n");
return 1;
}
numofupdates = num;
}
break;
default:
printf("Invalid flag: '%s'\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n", argv[i]);
return 1;
}
}
else {
printf("Invalid argument: '%s'\nUsage: './runelection -i inputfile -o outfile -n numofupdates'\n", argv[i]);
return 1;
}
}
if ((finput != NULL) && (numofupdates != -1)) {
int predictedNumOfRecords = fCountLines(&finput);
fseek(finput, 0, SEEK_SET); // Move file pointer to the start
if (predictedNumOfRecords != 0) { // If input file is not empty
el = electionInit(predictedNumOfRecords, numofupdates);
}
else { // If input file is empty
if (fclose(finput) == EOF) { // Check fclose failure
printf("Error closing input file.\n");
return 3;
}
finput = NULL;
input = NULL;
el = electionInit3(numofupdates);
}
}
else if ((finput != NULL) && (numofupdates == -1)) {
int predictedNumOfRecords = fCountLines(&finput);
fseek(finput, 0, SEEK_SET); // Move file pointer to the start
if (predictedNumOfRecords != 0) { // If input file is not empty
el = electionInit2(predictedNumOfRecords);
}
else { // If input file is empty
if (fclose(finput) == EOF) { // Check fclose failure
printf("Error closing input file.\n");
return 3;
}
finput = NULL;
input = NULL;
el = electionInit4();
}
}
else if ((finput == NULL) && (numofupdates != -1)) { //
el = electionInit3(numofupdates);
}
else { // ((input == NULL) && (numofupdates == -1))
el = electionInit4();
}
}
if (finput != NULL) {
fInsRecords(el, finput); // Insert records from input file
}
commands();
char *command = NULL;
char *parameter = NULL;
char buf[128];
fgets(buf, 128, stdin);
command = strtok(buf, " \n");
parameter = strtok(NULL, "\n");
do {
if (!strcmp(command, "lbf")) {
if (parameter != NULL) { // If parameter is not empty
electionLbf(el, parameter);
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "lrb")) {
if (parameter != NULL) { // If parameter is not empty
electionLrb(el, parameter);
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "ins")) {
if (parameter != NULL) { // If parameter is not empty
char *id, *name, *surname;
int age;
char *agechar;
char *gender;
int postcode;
char *postcodechar;
id = strtok(parameter, " ");
surname = strtok(NULL, " ");
name = strtok(NULL, " ");
agechar = strtok(NULL, " ");
gender = strtok(NULL, " ");
postcodechar = strtok(NULL, "\n");
if ((id == NULL) || (surname == NULL) || (name == NULL) || (agechar == NULL) || (gender == NULL) || (postcodechar == NULL)) { // Check for empty tokens
printf("Invalid parameters. Try 'help' for available commands.\n");
}
else {
age = atoi(agechar);
postcode = atoi(postcodechar);
if (electionIns(el, id, name, surname, age, gender[0], postcode, 1)) { // Check if returns error
printf(" - REC-WITH %s EXISTS\n", id);
}
}
}
else {
printf("Invalid parameters. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "find")) {
if (parameter != NULL) { // If parameter is not empty
electionFind(el, parameter);
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "delete")) {
if (parameter != NULL) { // If parameter is not empty
if (electionDelete(el, parameter)) { // Check if returns error
printf(" - KEY %s NOT-in-structs\n", parameter);
}
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "vote")) {
if (parameter != NULL) { // If parameter is not empty
if (electionVote(el, parameter)) { // Check if returns error
printf(" - REC-WITH %s NOT-in-structs\n", parameter);
}
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "load")) {
if (parameter != NULL) { // If parameter is not empty
FILE *fvote = fopen(parameter, "r");
if (fvote == NULL) {
printf("Invalid vote file: File does not exist.\n");
return 2;
}
char *id;
char buf[512];
while (fgets(buf, 512, fvote) != NULL) {
id = strtok(buf, "\n");
if (electionVote(el, id)) { // Check if returns error
printf(" - REC-WITH %s NOT-in-structs\n", id);
}
}
if (fclose(fvote) == EOF) { // Check fclose failure
printf("Error closing input file.\n");
return 3;
}
fvote = NULL;
}
else {
printf("Invalid parameter. Try 'help' for available commands.\n");
}
}
else if (!strcmp(command, "voted")) {
if (parameter != NULL) { // If parameter is not empty
electionVotedpc(el, atoi(parameter));
}
else {
electionVoted(el);
}
}
else if (!strcmp(command, "votedperpc")) {
electionVotedperpc(el);
}
else if ((!strcmp(command, "exit")) || (!strcmp(command, "e"))) {
if (finput != NULL) {
if (fclose(finput) == EOF) { // Check fclose failure
printf("Error closing input file.\n");
return 3;
}
finput = NULL;
input = NULL;
}
if (foutput != NULL) {
electionFPrint(el, foutput); // Print output file
if (fclose(foutput) == EOF) { // Check fclose failure
printf("Error closing input file.\n");
return 3;
}
foutput = NULL;
output = NULL;
}
electionFree(el);
printf(" - exit program\n");
break;
}
else if (!strcmp(command, "help")) {
help();
}
else {
printf("Invalid command. Try 'help' for available commands.\n");
}
fgets(buf, 128, stdin);
command = strtok(buf, " \n");
parameter = strtok(NULL, "\n");
}
while (1);
return 0;
}
void fInsRecords(electionPtr el, FILE *finput) { // Insert records from input file
char *id, *name, *surname;
int age;
char *gender;
int postcode;
char buf[512];
while (fgets(buf, 512, finput) != NULL) {
id = strtok(buf, " ");
surname = strtok(NULL, " ");
name = strtok(NULL, " ");
age = atoi(strtok(NULL, " "));
gender = strtok(NULL, " ");
postcode = atoi(strtok(NULL, "\n"));
if (electionIns(el, id, name, surname, age, gender[0], postcode, 0)) { // Check if returns error
printf(" - REC-WITH %s EXISTS\n", id);
}
}
}
void commands() { // Print commands
printf("\n");
printf("COMMANDS\n");
printf(" List of available commands.\n");
printf(" • lbf key (lookup bloom-filter)\n");
printf(" • lrb key (lookup red-black tree)\n");
printf(" • ins record\n");
printf(" • find key\n");
printf(" • delete key\n");
printf(" • vote key\n");
printf(" • load fileofkeys (to vote)\n");
printf(" • voted\n");
printf(" • voted postcode\n");
printf(" • votedperpc\n");
printf(" • help\n");
printf(" • exit\n\n");
}
void clearScreen() { // Clear screen with newlines
for (int cs = 0; cs < 100; cs++) { // 100 newlines
printf("\n");
}
}
void help() { // Print help
clearScreen();
commands();
printf("AUTHOR\n");
printf(" Written by Efstathios Siatras - sdi1600152.\n\n");
printf("SEE ALSO\n");
printf(" Readme available.\n\n");
}