-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationQueue.cpp
More file actions
132 lines (119 loc) · 2.19 KB
/
OperationQueue.cpp
File metadata and controls
132 lines (119 loc) · 2.19 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
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
#include "OperationQueue.h"
Operation::Operation(string operationText)
{
parse(operationText);
}
string Operation::scanUntil(string s, char sign, unsigned int &pos)
{
stringstream ss;
string ret;
while(pos != s.length())
{
if(s[pos] == sign)
{
++pos;
break;
}
else
{
ss << s[pos];
++pos;
}
}
ret = ss.str();
return ret;
}
void Operation::parse(string operationText)
{
size_t equalPos = operationText.find('=');
unsigned int pos = 0;
if(equalPos == string::npos)
{
//get the operation type;
name = scanUntil(operationText, '(', pos);
//get the parameters;
string rawParameters = scanUntil(operationText, ')', pos);
pos = 0;
while(pos != rawParameters.length())
{
string parameter = scanUntil(rawParameters, ',', pos);
parameters.push_back(parameter);
}
}
else
{
name = scanUntil(operationText, '=', pos);
exp = operationText.substr(pos);
}
}
string Operation::getExpression()
{
return exp;
}
string Operation::getParameter(int i)
{
return parameters[i];
}
string Operation::getName()
{
return name;
}
int Operation::parameterCount()
{
return parameters.size();
}
bool Operation::isExpression()
{
return parameters.size() == 0;
}
string Operation::toString()
{
stringstream ss;
ss << "op.getName() = " << getName() << endl;
ss << "op.isExpression() = " << (isExpression() ? "true" : "false") << endl;
if(isExpression())
{
ss << "op.getExpression() = " << getExpression() << endl;
}
else
{
ss << "op.parameterCount() = " << parameterCount() << endl;
for(int i=0; i<parameterCount(); ++i)
{
ss << "op.getParameter(" << i << ") = " << getParameter(i) << endl;
}
}
return ss.str();
}
ostream& operator << (ostream &out, Operation &op)
{
out << op.toString();
return out;
}
OperationQueue::OperationQueue(string filename)
{
ifstream in(filename.c_str());
if(in)
{
string scriptLine;
while(getline(in, scriptLine))
{
Operation newOperation(scriptLine);
operQueue.push(newOperation);
}
}
else
{
printf("Error: %s not found.\n",filename.c_str());
}
}
bool OperationQueue::isEmpty()
{
return operQueue.empty();
}
Operation OperationQueue::pop()
{
Operation ret = operQueue.front();
operQueue.pop();
return ret;
}