-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileHandling.hpp
201 lines (185 loc) · 5.96 KB
/
fileHandling.hpp
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
//fileHandling.h
/* handleFile slass*/
#pragma once
#include <string>
#include <fstream>
#include <limits> //to use numeric_limit
namespace BackWork
{
class handleFile
{
private:
int mTime;
std::string mLevel;
std::string mBestTime;
std::string mPlayerName;
int entryCount = 0; // Counter for number of entries read
public:
void setData(int timeElapshed, std::string bestTime, std::string username, Buttons Level);
int getData(std::string (&myData)[], Buttons home);
void getData(Buttons Level, std::string &bestTime, std::string &bestPleyer, int &Time);
void writeToFile(handleFile hf);
};
}
//Write things to file
void BackWork::handleFile::writeToFile(handleFile hf)
{
// Open the file in read mode to read existing data
std::ifstream inputFile("Dependencies/myData.txt");
if (!inputFile)
{
std::cerr << "Failed to open file for reading." << std::endl;
return;
}
// Create a temporary file to write modified data
std::ofstream tempFile("Dependencies/tempData.txt");
if (!tempFile)
{
std::cerr << "Failed to open temp file for writing." << std::endl;
inputFile.close();
return;
}
std::string line;
bool found = false;
// Copy data to temp file, updating Level: Hard data
while (std::getline(inputFile, line))
{
tempFile << line << std::endl;
if (line == ("Level: " + hf.mLevel))
{
found = true;
tempFile << "Shortest Time Elapsed: " << hf.mBestTime << std::endl;
tempFile << "By: " << hf.mPlayerName << std::endl;
tempFile << hf.mTime << std::endl;
tempFile << " "; //to avoid Could not create TTF SDL_Surface! Error: Text has zero width
for(int i = 0; i< 3; i++)
inputFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore 3 lines
}
if (found && line.empty())
{
break; // Stop after updating the Level: Hard data
}
}
// Copy the remaining data
while (std::getline(inputFile, line))
{
tempFile << line << std::endl;
}
// Close the input and temporary files
inputFile.close();
tempFile.close();
// Rename the temporary file to the original file name
std::remove("Dependencies/myData.txt");
std::rename("Dependencies/tempData.txt", "Dependencies/myData.txt");
}
//Set Datas if new best score
void BackWork::handleFile::setData(int timeElapshed, std::string bestTime, std::string username, Buttons Level)
{
mTime = timeElapshed;
mBestTime = bestTime;
mPlayerName = username;
if(Level == Buttons::HARD)
mLevel = "Hard";
else if(Level == Buttons::MEDIUM)
mLevel = "Medium";
else if(Level == Buttons::EASY)
mLevel = "Easy";
writeToFile(*this);
}
int BackWork::handleFile::getData(std::string (&myData)[], Buttons home)
{
// Open the file
std::ifstream scoreBoard("Dependencies/myData.txt", std::ios::in | std::ios::binary);
if (!scoreBoard)
{
std::cerr << "Failed to open file for reading." << std::endl;
return -1;
}
std::string findString[2];
if(home == Buttons::BEST)
{
findString[0] = "BEST";
findString[1] = "//end of best";
}
else if(home == Buttons::ABOUT)
{
findString[0] = "ABOUT";
findString[1] = "//end of about";
}
else if(home == Buttons::HELP)
{
findString[0] = "HELP";
findString[1] = "//end of help";
}
std::string line;
entryCount = 0;
int lineCount = 0;
bool readingSection = false;
while(std::getline(scoreBoard, line))
{
lineCount++;
if (line.find(findString[0]) != std::string::npos)
{
readingSection = true;
continue;
}
else if (line.find(findString[1]) != std::string::npos)
{
readingSection = false;
break; // Exit loop after reaching end of best section
}
if (readingSection && !line.empty() && home != Buttons::BEST)
{
myData[entryCount] = line;
// Pop last element i.e newline character else it will be displayed
myData[entryCount].pop_back();
entryCount++;
}
else if(readingSection && !line.empty() && home == Buttons::BEST)
{
//Ignore integer time
if ((lineCount%5) != 0)
{
myData[entryCount] = line;
//Pop last element i.e newline character else it will be displayed
myData[entryCount].pop_back();
entryCount++;
}
}
}
//Close the file
scoreBoard.close();
return entryCount;
}
void BackWork::handleFile::getData(Buttons Level, std::string &bestTime, std::string &bestPleyer, int &Time)
{
if (Level == Buttons::HARD)
mLevel = "Hard";
else if (Level == Buttons::MEDIUM)
mLevel = "Medium";
else if (Level == Buttons::EASY)
mLevel = "Easy";
// Open the file in read mode to read existing data
std::ifstream inputFile("Dependencies/myData.txt");
if (!inputFile)
{
std::cerr << "Failed to open file for reading." << std::endl;
return;
}
bool found = false;
std::string line;
// Copy data to temp file, updating Level: Hard data
while (std::getline(inputFile, line))
{
if (line == ("Level: " + mLevel))
{
found = true;
// Read and store lines
std::getline(inputFile, bestTime);
std::getline(inputFile, bestPleyer);
inputFile >> Time;
break;
}
}
inputFile.close();
}