-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings_Profile_Manager.cpp
More file actions
193 lines (182 loc) · 6.46 KB
/
Copy pathSettings_Profile_Manager.cpp
File metadata and controls
193 lines (182 loc) · 6.46 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
#include "Settings_Profile_Manager.h"
#include <QFileDialog>
#include <QTextStream>
#include <QDir>
#include <QFile>
#include <assert.h>
Settings_Profile_Manager::Settings_Profile_Manager(QWidget *parent, const QString &applicationPath) {
this->parent = parent;
this->applicationPath = applicationPath;
}
Settings_Profile_Manager::~Settings_Profile_Manager() { }
int Settings_Profile_Manager::Save_Settings(Settings *settings, const QString &saveLocation) {
assert(settings);
QDir dir(this->applicationPath);
if (!dir.exists()) return false;
if (!dir.cd("Profiles")) {
if (!dir.mkdir("Profiles")) return 3;
}
//Write the file to the buffer
QFile file(saveLocation);
if (!file.open(QFile::ReadWrite | QFile::Truncate)) return 2;
QTextStream stream(&file);
if (stream.status() != QTextStream::Ok) {
stream.flush();
file.close();
file.remove();
return 2;
}
stream << HEADER << "\n"; //use Unix end lines for cross platform compatability
stream << settings->startingOffset << "\n";
stream << settings->endingOffset << "\n";
stream << settings->incrementMinNum << "\n";
stream << settings->incrementMaxNum << "\n";
stream << settings->increment << "\n";
stream << settings->random << "\n";
stream << settings->add << "\n";
stream << settings->shiftLeft << "\n";
stream << settings->replace << "\n";
stream << settings->addNum << "\n";
stream << settings->shiftLeftNum << "\n";
stream << settings->replaceOldNum << "\n";
stream << settings->replaceNewNum << "\n";
stream.flush(); //flush the buffer to the disk
file.close();
if (stream.status() != QTextStream::Ok) {
file.remove();
return 2;
}
return 0;
}
int Settings_Profile_Manager::Save_Settings(Settings *settings) {
assert(this->parent);
QString profileLocation = this->applicationPath + "/Profiles";
QString saveLocation = QFileDialog::getSaveFileName(this->parent, "Save Location", profileLocation, "ROM Poison Settings File (*.rps)");
if (saveLocation == nullptr || saveLocation.isEmpty()) return 1; //the user canceled the save
if (!saveLocation.endsWith(".rps")) saveLocation += ".rps";
return this->Save_Settings(settings, saveLocation);
}
int Settings_Profile_Manager::Load_Settings(Settings *settings) {
assert(this->parent);
assert(settings);
QDir dir(this->applicationPath);
if (!dir.exists()) return false;
if (!dir.cd("Profiles")) {
if (!dir.mkdir("Profiles")) return 3;
}
QString profileLocation = this->applicationPath + "/Profiles";
QString loadLocation = QFileDialog::getOpenFileName(this->parent, "Open a ROM Poison Settings File", profileLocation, "ROM Poison Settings File (*.rps)");
if (loadLocation == nullptr || loadLocation.isEmpty()) return 1; //the user canceled the load
return this->Read_Settings(settings, loadLocation);
}
int Settings_Profile_Manager::Read_Settings(Settings *settings, const QString &fileLocation) {
assert(settings);
assert(fileLocation != nullptr && !fileLocation.isEmpty());
QFile file(fileLocation);
if (!file.exists() || !file.open(QFile::ReadWrite)) return 2;
QTextStream stream(&file);
if (stream.status() != QTextStream::Ok) {
file.close();
return 2;
}
//Check the header
QString line = file.readLine().trimmed();
if (line != HEADER) {
file.close();
return 4;
}
//Create a new settings struct just in case this data ends up being invalid
Settings newSettings;
stream >> newSettings.startingOffset;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.endingOffset;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.incrementMinNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.incrementMaxNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
int value = 0;
stream >> value;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
newSettings.increment = static_cast<bool>(value);
stream >> value;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
newSettings.random = static_cast<bool>(value);
stream >> value;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
newSettings.add = static_cast<bool>(value);
stream >> value;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
newSettings.shiftLeft = static_cast<bool>(value);
stream >> value;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
newSettings.replace = static_cast<bool>(value);
stream >> newSettings.addNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.shiftLeftNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.replaceOldNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> newSettings.replaceNewNum;
if (stream.status() != QTextStream::Ok) {
file.close();
return 4;
}
stream >> line; //ignore the last newline
if (!stream.atEnd()) {
file.close();
return 4;
}
//Copy the data over to the settings now that it is confirmed to be valid
settings->startingOffset = newSettings.startingOffset;
settings->endingOffset = newSettings.endingOffset;
settings->incrementMinNum = newSettings.incrementMinNum;
settings->incrementMaxNum = newSettings.incrementMaxNum;
settings->increment = newSettings.increment;
settings->random = newSettings.random;
settings->add = newSettings.add;
settings->shiftLeft = newSettings.shiftLeft;
settings->replace = newSettings.replace;
settings->addNum = newSettings.addNum;
settings->shiftLeftNum = newSettings.shiftLeftNum;
settings->replaceOldNum = newSettings.replaceOldNum;
settings->replaceNewNum = newSettings.replaceNewNum;
file.close();
return 0;
}