-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
87 lines (75 loc) · 2.92 KB
/
parser.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
#include <stdio.h>
#include <string.h>
#include "wet.h"
#include <stdlib.h>
#include <stdbool.h>
#define MAX_LINE_SIZE 128
char command_name[MAX_LINE_SIZE + 2];
char command_line[MAX_LINE_SIZE + 2];
char* arguments;
int missing_argc;
int argc;
void goto_next_line(FILE* file)
{
while (!feof(file) && fgetc(file) != '\n');
}
int check_command(char* expected_name)
{
return (!strcmp(command_name,expected_name));
}
int check_arguments(int expected_argc)
{
return ((missing_argc = expected_argc - argc) <= 0);
}
void illegalCommand() {
printf("Illegal command!\n");
}
void parseInput() {
bool exit = false;
while ((!feof(stdin))&&(!exit)) {
if (fgets(command_line,MAX_LINE_SIZE + 2,stdin) == NULL) break;
if (strlen(command_line) > MAX_LINE_SIZE)
{
printf("Command too long, can not parse\n");
goto_next_line(stdin);
}
else
{
missing_argc = 0;
sprintf(command_name,"");
sscanf(command_line,"%s",command_name);
arguments=command_line + strlen(command_name) + 1;
if (check_command(COMMAND_ADD_ACCOUNT)) {
int ID, ANumber, BrNumber;
argc = sscanf(arguments, "%d %d %d", &ID, &ANumber, &BrNumber);
check_arguments(3) ? addAccount(ID, ANumber, BrNumber) : illegalCommand();
} else if (check_command(COMMAND_WITHDRAWAL)) {
int ID, ANumber, BrNumber;
double WAmount;
argc = sscanf(arguments, "%lf %d %d %d", &WAmount, &BrNumber, &ID, &ANumber);
check_arguments(4) ? withdraw(WAmount, BrNumber, ID, ANumber) : illegalCommand();
} else if (check_command(COMMAND_TRANSFER)){
int IDF, IDT, ANumberF, ANumberT;
double TAmount;
argc = sscanf(arguments, "%lf %d %d %d %d", &TAmount, &IDF, &ANumberF, &IDT, &ANumberT);
check_arguments(5) ? transfer(TAmount, IDF, ANumberF, IDT, ANumberT) : illegalCommand();
} else if(check_command(COMMAND_BALANCES)) {
int ID, ANumber;
argc = sscanf(arguments, "%d %d", &ID, &ANumber);
check_arguments(2) ? balances(ID, ANumber) : illegalCommand();
} else if (check_command(COMMAND_ASSOCIATES)) {
int ID;
argc = sscanf(arguments, "%d", &ID);
check_arguments(1) ? associates(ID) : illegalCommand();
} else if (check_command(COMMAND_MONEY_LAUNDERING)) {
moneyLaundering();
} else if (missing_argc > 0) {
printf("Missing %d argument(s) in command %s!\n",missing_argc,command_name);
} else if (argc > 0) {
illegalCommand();
} else {
illegalCommand();
}
}
}
}