-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.cpp
More file actions
127 lines (101 loc) · 3.29 KB
/
FileHandler.cpp
File metadata and controls
127 lines (101 loc) · 3.29 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
#include "FileHandler.h"
// Finds the line number in which a given data header is located on within a given file
int FindInFile(std::fstream &puzzleFile, std::string dataHeader)
{
std::string line;
int lineNum = 1;
while (getline(puzzleFile, line))
{
if (line == dataHeader)
{
return lineNum;
}
lineNum++;
}
return -1;
}
// Traverses the file stream until we reach the given line number
std::fstream &gotoLine(std::fstream &file, unsigned int num)
{
file.seekg(std::ios::beg);
for (int i = 0; i < num - 1; ++i)
{
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return file;
}
// Returns the chunk of data that correlates with the specificed data header within the file
std::string getDataPieceFromFile(std::string fileName, std::string dataHeader)
{
std::string line;
std::string dataStream;
std::fstream puzzleFile(fileName + ".txt");
int lineNum = FindInFile(puzzleFile, dataHeader);
if (lineNum != -1)
{
if (puzzleFile.is_open())
{
std::fstream &puzzleStream = gotoLine(puzzleFile, lineNum + 1);
getline(puzzleStream, dataStream);
while (getline(puzzleStream, line))
{
if (line.empty())
{
break;
}
dataStream += "\n" + line;
}
return dataStream;
}
}
return "NOT FOUND";
}
// Saves a puzzle 2D array with it's unsolved and solved counterpats aswell as its words to the specificed file name
void savePuzzleToFile(std::vector<std::vector<char>> unsolvedGrid, std::vector<std::vector<char>> solvedGrid, std::string fileName, std::list<std::string> words)
{
std::ofstream puzzleFile;
puzzleFile.open(fileName + ".txt");
puzzleFile << "\nUNSOLVED_GRID\n";
for (int i = 0; i < unsolvedGrid.size(); i++)
{
for (int j = 0; j < unsolvedGrid[i].size(); j++)
{
puzzleFile << unsolvedGrid[i][j] << " ";
}
puzzleFile << "\n";
}
puzzleFile << "\nPUZZLE_WORDS\n";
for (std::list<std::string>::iterator itWord = words.begin(); itWord != words.end(); ++itWord)
{
std::string word = *itWord;
puzzleFile << word << std::endl;
}
puzzleFile << "\nSOLVED_GRID\n";
for (int i = 0; i < solvedGrid.size(); i++)
{
for (int j = 0; j < solvedGrid[i].size(); j++)
{
puzzleFile << solvedGrid[i][j] << " ";
}
puzzleFile << "\n";
}
puzzleFile.close();
}
// Opens and displays an unsolved puzzle from the specificed file
void unsolvedPuzzleFile(std::string fileName)
{
std::string grid = getDataPieceFromFile(fileName, "UNSOLVED_GRID");
std::string puzzleWords = getDataPieceFromFile(fileName, "PUZZLE_WORDS");
std::cout << grid << std::endl;
std::cout << "\n"
<< puzzleWords << std::endl;
}
// Opens and displays a solved puzzle from the specificed file
void solvePuzzleFile(std::string fileName)
{
std::string grid = getDataPieceFromFile(fileName, "SOLVED_GRID");
std::string puzzleWords = getDataPieceFromFile(fileName, "PUZZLE_WORDS");
std::cout << grid << std::endl;
std::cout << "\n"
<< puzzleWords << std::endl;
}