/
filemanager.cpp
184 lines (150 loc) · 6.07 KB
/
filemanager.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
//--------------------------------------------------------------
// File: filemanager.cpp
// Author: Jordan Fleck Team: Barnabas Class: CpS 111
// Desc: This file is in charge of keeping track of the highscore
// and save files for the game.
//--------------------------------------------------------------
#include "filemanager.h"
#include "levelmanager.h"
#include "userinformation.h"
FileManager::FileManager(QObject *parent): QObject(parent) {
}
FileManager FileManager::instance;
FileManager& FileManager::getInstance() {
return instance;
}
//writes userName and lastUnlockedLevel to a file. Accounts for previous saves.
void FileManager::saveFile() {
if(UserInformation::getInstance().getUserName() == "") {
UserInformation::getInstance().setUserName("UNKNOWN");
}
//c_string to read bytes into
char c[20];
//open the current saveFile
ifstream in("saveFile.txt");
//create a new file to write data into
ofstream out("tempFile.txt");
//write new info into temp.txt
QString s = UserInformation::getInstance().getUserName();
out << s.toStdString().c_str() << endl;
out << UserInformation::getLastUnlockedLevel() << endl;
do {
//read data and put into a QString for comparison
in.getline(c,20);
QString info = QString(c);
//if line not the userName
if(info != UserInformation::getInstance().getUserName()) {
//write this line and the next one
out << c << endl;
in.getline(c,20);
out << c << endl;
//if line is userName, then do not write this line
} else if(info == UserInformation::getInstance().getUserName()) {
//read next line, but do not write
in.getline(c,20);
}
} while(in.peek() != EOF);
in.close();
out.close();
remove("saveFile.txt");
rename("tempFile.txt","saveFile.txt");
}
//saves new HighScore; assumes an existing HighScore.txt
void FileManager::saveUserHighScore() {
if(UserInformation::getInstance().getUserName() == "") {
UserInformation::getInstance().setUserName("UNKNOWN");
}
char c[20];
ofstream outStream("temp.txt");
QString filename = QString("highscore") + QString::number(UserInformation::getInstance().getLevel()) + QString(".txt");
ifstream inStream(filename.toStdString().c_str());
inStream.getline(c,20);//disregard first name
int counter = 0; //counts how many levels in the userHighScore will be put
bool isInserted = false; //determines whether highscore was inserted
while(inStream.peek() != EOF) {
inStream.getline(c,20); //grab number
//acurately change c_string to number
string stringValue = string(c);
stringstream conversion (stringValue);
int number = 0;
conversion >> number;
if (UserInformation::getInstance().getUserHighScore() > number) {
isInserted = true;
//reset stream state to start from clean slate
inStream.close();
inStream.open(filename.toStdString().c_str());
int i = 0; //this value keeps track of how many sets of insertions have been made to temp.txt
while (i < counter) {//while temp does not have as many sets of insertions as highscore.txt keep inputing
for(int integer = 0; integer < 3; integer++) {//input a set {username, highscore, blank space}
inStream.getline(c,20);
outStream << c << endl; //I assume this will write the whole line, no more no less
}
i++;
}
//write new highScore
outStream << UserInformation::getInstance().getUserName().toStdString().c_str() << endl;
// userHighScore = Interface::getInstance().getTimeLeft();
outStream << UserInformation::getInstance().getUserHighScore();
outStream << endl << endl;
i++;
while(i < 5) {
for(int integer = 0; integer < 3; integer++) {
inStream.getline(c,20);
outStream << c << endl;
}
i++;
}
break;
} else {
inStream.getline(c,20); //remove empty whitespace
inStream.getline(c, 20); //remove the next name so we can get the number later
}
counter++; //this goes up every time a set of data is read. {name, highscore, whiteSpace}
}
inStream.close();
outStream.close();
if (isInserted == true) {
remove("HighScore.txt");
rename("temp.txt", filename.toStdString().c_str());
} else {
remove("temp.txt");
}
}
void FileManager::loadFile() {
//flag if name not found
bool nameFound = false;
//open file for reading
ifstream fs("saveFile.txt");
char a[20];
//check if file open
if(fs.is_open() == true) {
//keep going till end of file
while (fs.peek() != EOF) {
//read line of file, which should be a name
fs.getline(a, 20);
//if name equals userName, then load lastUnlockedLevelber and end loop
if(UserInformation::getInstance().getUserName() == QString(a)) {
nameFound = true;
//since we found user name, then read the user's lastUnlockedLevel
fs.getline(a, 20);
//acurately change c_string to number
string stringValue = string(a);
stringstream conversion (stringValue);
int i = 0;
conversion >> i;
//load number into
UserInformation::setLastUnlockedLevel(i);
//close file
fs.close();
break;
//if the name in the file is not what i want, then throw away the lastUnlockedLevel
} else if (UserInformation::getInstance().getUserName() != QString(a)){
fs.getline(a, 20);
}
}
}
if(nameFound == false){
//set lastUnlockedLevel
UserInformation::setLastUnlockedLevel(1);
}
}