-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJigsawParser.cpp
More file actions
282 lines (236 loc) · 8 KB
/
JigsawParser.cpp
File metadata and controls
282 lines (236 loc) · 8 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
//
// Created by okleinfeld on 12/12/17.
//
#include "JigsawParser.h"
JigsawParser::JigsawParser(string& inputFilePath, string& outputFilePath) : outputFile(outputFilePath), cannotComputeSolution(false), fileOperationsError(false){
ifstream inputFile;
inputFile.open(inputFilePath);
ofstream outputFile;
outputFile.open(this->outputFile);
if (!outputFile.is_open()){
cout << CANNOT_WRITE_OUTPUTFILE;
this->fileOperationsError = true;
this->cannotComputeSolution = true;
return;
}
if (!inputFile.is_open()) {
cout << CANNOT_READ_INPUT_FILE;
this->fileOperationsError = true;
this->cannotComputeSolution = true;
outputFile.close();
return;
}
int numElements = JigsawParser::readFirstLine(inputFile);
if (numElements == -1){
outputFile << INVALID_FIRST_LINE;
this->fileOperationsError = true;
this->cannotComputeSolution = true;
outputFile.close();
return;
}
this->numOfElements = numElements;
string line;
while (std::getline(inputFile, line)){
JigsawParser::readPuzzlePieceLine(line);
}
inputFile.close();
for (PuzzlePiece& p : this->correctInputPieces){
this->validatePuzzlePiece(p);
}
// pieces value were valid - but check if all there no missing or redundant ids
updateMissingIDs();
updateRedundantElementsIDs();
if (!this->missingElementsIDs.empty() || !this->redundantElementsIDs.empty() || !this->wrongFormatLines.empty()){
this->cannotComputeSolution = true;
// there are wrong format line/ redundant/missing elements.
// in this case we cannot try to compute a solution for the puzzle
}
}
bool JigsawParser::isInitialized(){
return !this->cannotComputeSolution;
}
bool JigsawParser::fileError(){
return this->fileOperationsError;
}
int JigsawParser::readFirstLine(ifstream &openInputFileStream) {
string line;
std::getline(openInputFileStream, line);
vector<string> lineWords = split(line, '=');
if (lineWords.size() != 2){
return -1;
}
string firstWord = lineWords[0];
firstWord.erase(std::remove(firstWord.begin(), firstWord.end(), ' '), firstWord.end());
if (firstWord != "NumElements"){
return -1;
// File should start with the word "NumElements"
}
string secondWord = lineWords[1];
secondWord.erase(std::remove(secondWord.begin(), secondWord.end(), ' '), secondWord.end());
if (secondWord.empty()) {
return -1;
// The second token in the file's first line must contains a number
}
try {
int numElements = std::stoi(secondWord);
if (numElements <= 0) {
return -1;
// The number representing the number of elements must be a positive integer
}
else {
return numElements;
}
}
catch (std::invalid_argument& ia){
return -1;
// the argument for number of elements is not a valid integer
}
}
void JigsawParser::readPuzzlePieceLine(string& line){
vector<string> pieceInfo = split(line, ' ');
bool isdOK = false;
int ISD;
try {
ISD = std::stoi(pieceInfo[0]);
isdOK = true;
if (pieceInfo.size() != 5){
this->wrongFormatLines.insert({ISD, line});
return;
}
int l = std::stoi(pieceInfo[1]);
int t = std::stoi(pieceInfo[2]);
int r = std::stoi(pieceInfo[3]);
int b = std::stoi(pieceInfo[4]);
PuzzlePiece piece(ISD, l, t, r, b);
this->correctInputPieces.push_back(piece);
// if no fatal error always insert into correctInput list.
// will duplicate to wrongFormat list later.
}
catch (std::invalid_argument& ia){
if(isdOK)
this->wrongFormatLines.insert({ISD, line});
else
this->wrongFormatLines.insert({-1, line});
}
}
void JigsawParser::updateMissingIDs(){
vector<PuzzlePiece>& pieces = this->correctInputPieces;
std::sort(pieces.begin(), pieces.end());
map<int, int> mapIDs;
for (PuzzlePiece& p : pieces){
auto iter = mapIDs.find(p.getISD());
if (iter == mapIDs.end()){
mapIDs.insert({p.getISD(), 1});
}
else{
iter->second = iter->second + 1;
}
}
for (int i = 1; i <= this->numOfElements; i++){
auto iter = mapIDs.find(i);
if (iter == mapIDs.end()){
bool check = true;
for(auto line : wrongFormatLines)
if(line.first == i)
check = false;
if(check)
this->missingElementsIDs.push_back(i);
}
}
}
void JigsawParser::updateRedundantElementsIDs(){
vector<PuzzlePiece>& pieces = this->correctInputPieces;
for (PuzzlePiece& p : pieces){
int pieceID = p.getISD();
if (pieceID > this->numOfElements){
this->redundantElementsIDs.push_back(pieceID);
}
}
}
void JigsawParser::validatePuzzlePiece(PuzzlePiece& piece){
int id = piece.getISD();
int l = piece.getLeftEdge();
int t = piece.getTopEdge();
int r = piece.getRightEdge();
int b = piece.getBottomEdge();
int edges[4] = {l, t, r, b};
for (int e : edges){
if (e < -1 || e > 1){
stringstream line;
piece.print(line);
this->wrongFormatLines.insert({id, line.str()});
break;
}
}
}
void JigsawParser::writeErrorsToOutput(){
ofstream outputFile;
outputFile.open(this->outputFile);
if (!outputFile.is_open()){
cout << OUTPUT_FILE_NOT_OPEN;
this->cannotComputeSolution = true;
return;
}
// write all the needed errors
if (!this->missingElementsIDs.empty()){
list<int>& missingList = this->missingElementsIDs;
long missSize = this->missingElementsIDs.size();
outputFile << "Missing puzzle element(s) with the following IDs: ";
for (int i = 0; i < missSize; i++){
if (i != missSize-1){
auto iter = std::next(missingList.begin(), i);
outputFile << *iter << ", ";
}
else{
auto iter = std::next(missingList.begin(), i);
outputFile << *iter << endl;
}
}
}
if (!this->redundantElementsIDs.empty()){
list<int>& wrongList = this->redundantElementsIDs;
long wrongSize = this->redundantElementsIDs.size();
outputFile << "Puzzle of size " << this->numOfElements << " cannot have the following IDs: ";
for (int i = 0; i < wrongSize; i++){
if (i != wrongSize-1){
auto iter = std::next(wrongList.begin(), i);
outputFile << *iter << ", ";
}
else{
auto iter = std::next(wrongList.begin(), i);
outputFile << *iter << endl;
}
}
}
if (!this->wrongFormatLines.empty()){
map<int, string>& wrongLines = this->wrongFormatLines;
for (auto iter = wrongLines.begin(); iter != wrongLines.end();){
if ((iter->first) != -1 && (iter->first > -1) && ( iter->first <= numOfElements)){
outputFile << "Puzzle ID " << iter->first << " has wrong data: " << iter->second << endl;
}
iter++;
}
for (auto iter = wrongLines.begin(); iter != wrongLines.end();){
if (iter->first == -1){
outputFile << "The following line has wrong data and/or wrong Puzzle ID: " << iter->second << endl;
}
iter++;
}
}
outputFile.close();
}
vector<PuzzlePiece> JigsawParser::getCorrectInputPieces(){
return this->correctInputPieces;
}
vector<string> split(const string &s, char delimiter) {
vector<string> words;
stringstream streamStr(s);
string currentWord;
while (getline(streamStr, currentWord, delimiter)){
if (currentWord == ""){
continue;
}
words.push_back(currentWord);
}
return words;
}