-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRWFile.cpp
314 lines (249 loc) · 5.17 KB
/
RWFile.cpp
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
#include "RWFile.h"
vector<string> RWFile::Tokenlizer(string const& line, char SEPERATOR)
{
vector<string> out;
if (line.length() <= 0) return out;
int start = 0;
int pos;
string temp = "";
pos = line.find_first_of(SEPERATOR, start);
while (pos >= 0) {
temp = line.substr(+start, pos - start);
out.push_back(temp);
start = pos + 1;
pos = line.find_first_of(SEPERATOR, start);
}
if (start < line.length()) out.push_back(line.substr(start, line.length()));
return out;
}
RWFile::RWFile(const string& in, const string& out)
{
this->filein = in;
this->fileout = out;
}
RWFile::RWFile(const char* in, const char* out)
{
this->filein = in;
this->fileout = out;
}
bool RWFile::Start()
{
ifstream fin(this->filein);
if (!fin.is_open()) return false;
results.clear();
string line;
int count = 0;
while (getline(fin, line))
{
count++;
if (line.length() <= 0 || line[0] == '#') continue;
// remove 'r' at end of line if break line is CRLF (only affect on unix/linux because them use CR instead)
if (line.at(line.length() - 1) == '\r') line = line.substr(0, line.length() - 1);
string result;
bool check = ExecALine(line, result);
if (check)
{
this->results.push_back(result);
}
else
{
stringstream exeption;
exeption << "ERROR Line " << count << ": " << result;
//this->results.push_back(exeption.str());
this->results.push_back(string("0"));
}
}
fin.close();
return true;
}
bool RWFile::ExecALine(const string& line, string& result)
{
bool res = true;
vector<string> tokens = Tokenlizer(line, SEPERATOR);
QInt* t1 = nullptr;
QInt* t2 = nullptr;
QInt* kq = nullptr;
try {
// first token is I/O base
int basein = parseInt(tokens[0]);
// baseout is needed only when transform base
int baseout = 0;
string opt;
string opr1, opr2;
// use this var because c/c++ doesn't support switch case with string
bool isValidOpt = false;
if (tokens.size() == 3) {
// here just 1 opr
if (tokens[1] == "2" || tokens[1] == "10" || tokens[1] == "16")
{
// this only transform with known base
baseout = parseInt(tokens[1]);
opr1 = tokens[2];
}
else
{
if (isOperator(tokens[1], basein))
{
opt = tokens[1];
opr1 = tokens[2];
}
else
{
// transform with unknown base
}
}
}
else {
// opt stored at tokens[2], tokens size == 4
if (isOperator(tokens[2], basein))
{
opt = tokens[2];
opr1 = tokens[1];
opr2 = tokens[3];
}
else {
// is there any else case?
}
}
// init oprs
t1 = new QInt(opr1, basein);
// run opt
if (opt == "ror") {
isValidOpt = true;
t1->ROR();
kq = new QInt(*t1);
}
if (opt == "rol") {
isValidOpt = true;
t1->ROL();
kq = new QInt(*t1);
}
if (opt == ">>") {
isValidOpt = true;
int count = parseInt(opr2);
kq = new QInt(*t1 >> count);
}
if (opt == "<<") {
isValidOpt = true;
int count = parseInt(opr2);
kq = new QInt(*t1 << count);
}
if (opt == "|") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 | *t2);
}
if (opt == "&") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 & *t2);
}
if (opt == "^") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 ^ *t2);
}
if (opt == "~") {
isValidOpt = true;
kq = new QInt(~*t1);
}
if (opt == "+") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 + *t2);
}
if (opt == "-") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 - *t2);
}
if (opt == "*") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 * *t2);
}
if (opt == "/") {
isValidOpt = true;
t2 = new QInt(opr2, basein);
kq = new QInt(*t1 / *t2);
}
if (baseout != 0) {
// transform base
isValidOpt = true;
kq = new QInt(*t1);
}
// result object was stored at kq
if (isValidOpt) {
// export as basein by default, but when baseout dercaled, use baseout (transform)
result = kq->exportData((baseout == 0) ? basein : baseout);
}
else
{
result = "Operator not found!";
res = false;
}
}
catch (const char* msg)
{
result = msg;
res = false;
}
if (!t1) {
delete t1;
t1 = nullptr;
}
if (!t2) {
delete t2;
t2 = nullptr;
}
if (!kq) {
delete kq;
kq = nullptr;
}
return res;
}
bool RWFile::WriteFile()
{
ofstream fout(fileout);
if (!fout.is_open()) return false;
for (string& line : results)
{
fout << line << endl;
}
fout.close();
return true;
}
int RWFile::parseInt(string const& intStr)
{
try
{
int i = std::stoi(intStr);
return i;
}
catch (std::invalid_argument const& e)
{
throw "This is not a number";
}
catch (std::out_of_range const& e)
{
throw "Number out of range";
}
}
bool RWFile::isOperator(string const& str, int base)
{
for (const char& c : str)
{
// if at least 1 char is a valid value in base, return false
if (base == 10) {
if (c >= '0' && c <= '9') return false;
}
if (base == 16) {
if (((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
return false;
}
if (base == 2) {
if (c == '0' || c == '1') return false;
}
}
return true;
}