-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
564 lines (465 loc) · 16 KB
/
project.cpp
File metadata and controls
564 lines (465 loc) · 16 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include "project.h"
#include <QFile>
#include <QIODevice>
#include <QStringList>
#include <QDebug>
#include <QDir>
#include <QSaveFile>
#include <QtMath>
Error::Error(ErrorCode errCode) : errCode(errCode)
{
switch (errCode) {
case ErrorCode::NoErrors:
message = QCoreApplication::translate("Error", "No Errors.");
break;
case ErrorCode::InvalidIdentifier:
message = QCoreApplication::translate("Error", "Invalid Identifier: Identifier must consist only of letters, digits and underscores, and must not begin with a digit.");
break;
case ErrorCode::IdentifierAlreadyExists:
message = QCoreApplication::translate("Error", "Identifier already exists.");
break;
case ErrorCode::UnknownVariableId:
message = QCoreApplication::translate("Error", "Unknown Variable Id.");
break;
case ErrorCode::UnknownVariableName:
message = QCoreApplication::translate("Error", "Unknown Variable Name.");
break;
case ErrorCode::UnknownValueId:
message = QCoreApplication::translate("Error", "Unknown Value Id.");
break;
case ErrorCode::UnknownValueName:
message = QCoreApplication::translate("Error", "Unknown Value Name.");
break;
case ErrorCode::UnknownRuleId:
message = QCoreApplication::translate("Error", "Unknown Rule Id.");
break;
case ErrorCode::UnknownPairId:
message = QCoreApplication::translate("Error", "Unknown Pair Id.");
break;
case ErrorCode::PairAlreadyExists:
message = QCoreApplication::translate("Error", "Pair already exists.");
break;
}
}
Pair::Pair()
{
}
Pair::Pair(const QString &var, const QString &value) : var(var), value(value)
{
}
QString Rule::stringify() const
{
QString result;
result.append("IF " + stringifyIfBlock() + " ");
result.append("THEN " + stringifyThenBlock());
return result;
}
QString Rule::stringifyIfBlock() const
{
QString result;
int last = ifBlock.length() - 1;
if (last >= 0)
{
for (int i = 0; i < last; i++)
{
result.append(ifBlock.at(i).stringify(true) + " & ");
}
result.append(ifBlock.at(last).stringify(true));
}
return result;
}
QString Rule::stringifyThenBlock() const
{
QString result;
int last = thenBlock.length() - 1;
if (last >= 0)
{
for (int i = 0; i < last; i++)
{
result.append(thenBlock.at(i).stringify(false) + "; ");
}
result.append(thenBlock.at(last).stringify(false) + ".");
}
return result;
}
// Constructor for Existing Project; we pass path to ".esp" file
Project::Project(const QString &projFilePath)
: projFilePath(projFilePath), regexpIdentifier("[_a-zA-Z][_a-zA-Z0-9]*")
{
QFile projFile(projFilePath);
projFile.open(QFile::ReadOnly);
QTextStream projFileStream(&projFile);
QString projFolder(projFilePath.mid(0, projFilePath.lastIndexOf("/", -1) + 1));
projName = projFileStream.readLine();
// Start loading variables
varFilePath = projFolder + projFileStream.readLine();
QFile varFile(varFilePath);
varFile.open(QFile::ReadOnly);
QTextStream varFileStream(&varFile);
while (!varFileStream.atEnd())
{
QStringList line = varFileStream.readLine().split(".");
varNames.append(line.at(0));
QStringList valueBlock;
for (int i = 1; i < line.length(); i++)
{
valueBlock.append(line.at(i));
}
varValues.append(valueBlock);
}
varFile.close();
// End loading variables
// Start loading rules
rulFilePath = projFolder + projFileStream.readLine();
projFile.close();
QFile rulFile(rulFilePath);
rulFile.open(QFile::ReadOnly);
QTextStream rulFileStream(&rulFile);
while (!rulFileStream.atEnd())
{
Rule r;
QStringList line = rulFileStream.readLine().split("-");
QStringList ifPart = line.at(0).split("&");
for (int i = 0; i < ifPart.length(); i++)
{
QStringList pair = ifPart.at(i).split("=");
if (pair.length() < 2) continue;
Pair p{pair.at(0), pair.at(1)};
r.ifBlock.append(p);
}
QStringList thenPart = line.at(1).split("&");
for (int i = 0; i < thenPart.length(); i++)
{
QStringList pair = thenPart.at(i).split("=");
if (pair.length() < 2) continue;
Pair p{pair.at(0), pair.at(1)};
r.thenBlock.append(p);
}
rules.append(r);
}
rulFile.close();
// End loading rules
saved = true;
}
// Constructor for New Project; we pass path to desired project folder
Project::Project(const QString &folderPath, const QString &projName)
: projName(projName), regexpIdentifier("[_a-zA-Z][_a-zA-Z0-9]*")
{
QString newProjFolderPath = folderPath + "/" + projName;
QDir().mkpath(newProjFolderPath);
QString tempStr = newProjFolderPath + "/" + projName;
projFilePath = tempStr + ".esp";
QFile projFile(projFilePath);
projFile.open(QFile::WriteOnly | QFile::Truncate);
QTextStream projFileStream(&projFile);
projFileStream << projName + "\n";
projFileStream << projName + ".var" + "\n";
projFileStream << projName + ".rul" + "\n";
projFile.close();
varFilePath = tempStr + ".var";
QFile varFile(varFilePath);
varFile.open(QFile::WriteOnly | QFile::Truncate);
varFile.close();
rulFilePath = tempStr + ".rul";
QFile rulFile(rulFilePath);
rulFile.open(QFile::WriteOnly | QFile::Truncate);
rulFile.close();
saved = true;
}
void Project::saveProject() const
{
// Start saving variables
QSaveFile varFile(varFilePath);
varFile.open(QSaveFile::WriteOnly | QSaveFile::Truncate);
QTextStream varFileStream(&varFile);
for (int i = 0; i < varNames.length(); i++)
{
varFileStream << varNames.at(i);
for (int j = 0; j < varValues.at(i).length(); j++)
{
varFileStream << "." + varValues.at(i).at(j);
}
varFileStream << "\n";
}
varFile.commit();
// End saving variables
// Start saving rules
QSaveFile rulFile(rulFilePath);
rulFile.open(QSaveFile::WriteOnly | QSaveFile::Truncate);
QTextStream rulFileStream(&rulFile);
int last;
for (int i = 0; i < rules.length(); i++)
{
// Output If-Block
last = rules.at(i).ifBlock.length() - 1;
if (last >= 0)
{
for (int j = 0; j < last; j++)
{
rulFileStream << rules.at(i).ifBlock.at(j).var + "=" + rules.at(i).ifBlock.at(j).value + "&";
}
rulFileStream << rules.at(i).ifBlock.at(last).var + "=" + rules.at(i).ifBlock.at(last).value;
}
rulFileStream << "-";
// Output Then-Block
last = rules.at(i).thenBlock.length() - 1;
if (last >= 0)
{
for (int j = 0; j < last; j++)
{
rulFileStream << rules.at(i).thenBlock.at(j).var + "=" + rules.at(i).thenBlock.at(j).value + "&";
}
rulFileStream << rules.at(i).thenBlock.at(last).var + "=" + rules.at(i).thenBlock.at(last).value;
}
rulFileStream << "\n";
}
rulFile.commit();
// End saving rules
saved = true;
}
const QString &Project::getProjName() const
{
return projName;
}
const QStringList &Project::getVarNames() const
{
return varNames;
}
const QList<QStringList> &Project::getAllVarValues() const
{
return varValues;
}
const QStringList *Project::getVarValues(int varId) const
{
normalizeVarId(&varId);
if (!varExists(varId)) return nullptr;
return (&varValues.at(varId));
}
const QStringList *Project::getVarValues(const QString &varName) const
{
int varId = getVarId(varName);
if (varId == -1) return nullptr;
return getVarValues(varId);
}
const QList<Rule> &Project::getRules() const
{
return rules;
}
const Rule *Project::getRule(int ruleId) const
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return nullptr;
return (&rules.at(ruleId));
}
const Rule *Project::getRule(const QString &ruleStringified) const
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return nullptr;
return getRule(ruleId);
}
QStringList Project::getRulezzStringified() const
{
QStringList result;
int length = rules.length();
for (int i = 0; i < length; i++)
{
result.append(getRuleStringified(i));
}
return result;
}
QString Project::getRuleStringified(int ruleId) const
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return QString();
return QString(zeros(ruleId + 1, rules.length()) + QString::number(ruleId + 1) + ") " + rules.at(ruleId).stringify());
}
QString Project::getRuleStringified(const QString &ruleStringified) const
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return QString();
return getRuleStringified(ruleId);
}
bool Project::isSaved() const
{
return saved;
}
Error Project::addVar(const QString &varName, const QStringList &values)
{
if (!isValid(varName)) return Error(ErrorCode::InvalidIdentifier);
if (varExists(varName)) return Error(ErrorCode::IdentifierAlreadyExists);
for (const QString &s : values)
{
if (!isValid(s)) return Error(ErrorCode::InvalidIdentifier);
}
varNames.append(varName);
varValues.append(values);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::addVarValue(const QString &newValue, int varId)
{
normalizeVarId(&varId);
if (!varExists(varId)) return Error(ErrorCode::UnknownVariableId);
if (!isValid(newValue)) return Error(ErrorCode::InvalidIdentifier);
if (valueExists(varId, newValue)) return Error(ErrorCode::IdentifierAlreadyExists);
varValues[varId].append(newValue);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::addVarValue(const QString &newValue, const QString &varName)
{
int varId = getVarId(varName);
if (varId == -1) return Error(ErrorCode::UnknownVariableName);
return addVarValue(newValue, varId);
}
Error Project::deleteVar(int varId)
{
normalizeVarId(&varId);
if (!varExists(varId)) return Error(ErrorCode::UnknownVariableId);
varNames.removeAt(varId);
varValues.removeAt(varId);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::deleteVar(const QString &varName)
{
int varId = getVarId(varName);
if (varId == -1) return Error(ErrorCode::UnknownVariableName);
return deleteVar(varId);
}
Error Project::deleteVarValue(int varId, int valueId)
{
normalizeVarId(&varId);
if (!varExists(varId)) return Error(ErrorCode::UnknownVariableId);
normalizeValueId(varId, &valueId);
if (!valueExists(varId, valueId)) return Error(ErrorCode::UnknownValueId);
varValues[varId].removeAt(valueId);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::deleteVarValue(const QString &varName, const QString &valueName)
{
int varId = getVarId(varName);
if (varId == -1) return Error(ErrorCode::UnknownVariableName);
int valueId = getValueId(varId, valueName);
if (valueId == -1) return Error(ErrorCode::UnknownValueName);
return deleteVarValue(varId, valueId);
}
Error Project::setVarName(const QString &newName, int varId)
{
normalizeVarId(&varId);
if (!varExists(varId)) return Error(ErrorCode::UnknownVariableId);
if (!isValid(newName)) return Error(ErrorCode::InvalidIdentifier);
if (varExists(newName)) return Error(ErrorCode::IdentifierAlreadyExists);
varNames[varId] = newName;
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::setVarName(const QString &newName, const QString &oldName)
{
int varId = getVarId(oldName);
if (varId == -1) return Error(ErrorCode::UnknownVariableName);
return setVarName(newName, varId);
}
Error Project::setVarValue(const QString &newValue, int varId, int valueId)
{
normalizeVarId(&varId);
if (!varExists(varId)) return Error(ErrorCode::UnknownVariableId);
normalizeValueId(varId, &valueId);
if (!valueExists(varId, valueId)) return Error(ErrorCode::UnknownValueId);
if (!isValid(newValue)) return Error(ErrorCode::InvalidIdentifier);
if (valueExists(varId, newValue)) return Error(ErrorCode::IdentifierAlreadyExists);
varValues[varId][valueId] = newValue;
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::setVarValue(const QString &newValue, const QString &varName, const QString &oldValue)
{
int varId = getVarId(varName);
if (varId == -1) return Error(ErrorCode::UnknownVariableName);
int valueId = getValueId(varId, oldValue);
if (valueId == -1) return Error(ErrorCode::UnknownValueName);
return setVarValue(newValue, varId, valueId);
}
Error Project::addRule(const Rule &rule)
{
rules.append(rule);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::addIfPair(const Pair &ifPair, int ruleId)
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
if (ifPairExists(ruleId, ifPair)) Error(ErrorCode::PairAlreadyExists);
rules[ruleId].ifBlock.append(ifPair);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::addIfPair(const Pair &ifPair, const QString &ruleStringified)
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
return addIfPair(ifPair, ruleId);
}
Error Project::addThenPair(const Pair &thenPair, int ruleId)
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
if (thenPairExists(ruleId, thenPair)) Error(ErrorCode::PairAlreadyExists);
rules[ruleId].thenBlock.append(thenPair);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::addThenPair(const Pair &thenPair, const QString &ruleStringified)
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
return addThenPair(thenPair, ruleId);
}
Error Project::deleteRule(int ruleId)
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
rules.removeAt(ruleId);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::deleteRule(const QString &ruleStringified)
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
return deleteRule(ruleId);
}
Error Project::deleteIfPair(int ruleId, int ifPairId)
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
normalizeIfPairId(ruleId, &ifPairId);
if (!ifPairExists(ruleId, ifPairId)) return Error(ErrorCode::UnknownPairId);
rules[ruleId].ifBlock.removeAt(ifPairId);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::deleteIfPair(const QString &ruleStringified, int ifPairId)
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
return deleteIfPair(ruleId, ifPairId);
}
Error Project::deleteThenPair(int ruleId, int thenPairId)
{
normalizeRuleId(&ruleId);
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
normalizeThenPairId(ruleId, &thenPairId);
if (!thenPairExists(ruleId, thenPairId)) return Error(ErrorCode::UnknownPairId);
rules[ruleId].thenBlock.removeAt(thenPairId);
saved = false;
return Error(ErrorCode::NoErrors);
}
Error Project::deleteThenPair(const QString &ruleStringified, int thenPairId)
{
int ruleId = ruleStringified.section(')', 0, 0).toInt() - 1;
if (!ruleExists(ruleId)) return Error(ErrorCode::UnknownRuleId);
return deleteThenPair(ruleId, thenPairId);
}