-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetCommand.cpp
More file actions
59 lines (56 loc) · 2.27 KB
/
SetCommand.cpp
File metadata and controls
59 lines (56 loc) · 2.27 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
//
// Created by chen96 on 27/12/2019.
//
#include "SetCommand.h"
#include "globalVariables.h"
#include "ExpressionCalculate.h"
#include <cstring>
#include <netinet/in.h>
#include <string>
#include <algorithm>
int SetCommand::execute(vector<string> params) {
Interpreter *i = new Interpreter();
Expression *e = nullptr;
try {
// Remove spaces from the left expression
string::iterator end_pos = remove(params[2].begin(), params[2].end(), ' ');
params[2].erase(end_pos, params[2].end());
// Remove tabs from the left expression
end_pos = remove(params[2].begin(), params[2].end(), '\t');
params[2].erase(end_pos, params[2].end());
// Remove spaces from the right expression
end_pos = remove(params[0].begin(), params[0].end(), ' ');
params[0].erase(end_pos, params[0].end());
// Remove tabs from the right expression
end_pos = remove(params[0].begin(), params[0].end(), '\t');
params[0].erase(end_pos, params[0].end());
e = i->interpret(params[2]);
// Calculate the left expression
double answer = e->calculate();
/* Convert the result of the calculation into float in order to insert to the symbolTable map
(it's value is defined as a float) */
float floatAnswer = (float) answer;
string stringAnswer = to_string(floatAnswer);
// Insert the calculated value into the symbolTable map
globalVariables::symbolTableClientToSim[params[0]].first = floatAnswer;
// Insert the set command to the simulator according to the wanted syntax
string setCommandToSim = "set ";
// Insert the path to the simulator
setCommandToSim += globalVariables::symbolTableClientToSim[params[0]].second;
setCommandToSim += " ";
setCommandToSim += stringAnswer;
setCommandToSim += "\r\n";
const char *setCommandToSend = setCommandToSim.c_str();
// Send the resulting command to the simulator through the socket
send(globalVariables::socketId, setCommandToSend, strlen(setCommandToSend), 0);
// Delete memory
delete e;
delete i;
} catch (const char *e) {
if (e != nullptr) {
delete e;
delete i;
}
}
return 3;
}