-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave.cpp
205 lines (177 loc) · 6 KB
/
Save.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#define _HAS_STD_BYTE 0
#define _CRT_SECURE_NO_WARNINGS
#include "Save.h"
#include <fstream>
#include <filesystem>
#include <wx/wx.h>
std::string Save::EscapeForCSV(const std::string& field) {
std::string escapedField = "\"";
for (char c : field) {
if (c == '"') {
escapedField += "\"\"";
}
else {
escapedField += c;
}
}
escapedField += "\"";
return escapedField;
}
void Save::SaveItems(const std::vector<Item>& items, const std::string& filePath, char separator) {
std::error_code ec;
if (!std::filesystem::exists("save")) {
std::filesystem::create_directory("save", ec);
if (ec) {
wxMessageBox("Failed to create save directory", "Error", wxICON_ERROR);
return;
}
}
std::ofstream file(filePath);
if (!file.is_open()) {
wxMessageBox("Error: Could not open file for saving.", "Save Error", wxICON_ERROR);
return;
}
for (const auto& item : items) {
file << EscapeForCSV(item.getName()) << separator
<< EscapeForCSV(item.getDescription()) << separator
<< item.getAmount() << separator
<< EscapeForCSV(item.getImage()) << separator
<< EscapeForCSV(item.getCategory()) << "\n";
}
file.close();
wxMessageBox("Items saved successfully!", "Save Complete", wxICON_INFORMATION);
}
void Save::SaveToCfg(const std::vector<Item>& items, const std::vector<std::string>& categories, const std::string& filePath) {
std::ofstream file(filePath);
if (!file.is_open()) {
wxMessageBox("Error: Could not open file for saving.", "Save Error", wxICON_ERROR);
return;
}
// Save categories
file << "[Categories]\n";
for (const auto& category : categories) {
file << category << "\n";
}
// Save items
file << "[Items]\n";
for (const auto& item : items) {
file << EscapeForCSV(item.getName()) << ","
<< EscapeForCSV(item.getDescription()) << ","
<< item.getAmount() << ","
<< EscapeForCSV(item.getImage()) << ","
<< EscapeForCSV(item.getCategory()) << "\n";
}
file.close();
wxMessageBox("Items and categories saved successfully!", "Save Complete", wxICON_INFORMATION);
}
void Save::LoadFromCfg(std::vector<Item>& items, std::vector<std::string>& categories, const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
wxMessageBox("Error: Could not open file for loading.", "Load Error", wxICON_ERROR);
return;
}
items.clear();
categories.clear();
std::string line;
std::string currentSection;
while (std::getline(file, line)) {
if (line.empty()) continue;
if (line == "[Categories]") {
currentSection = "Categories";
continue;
}
else if (line == "[Items]") {
currentSection = "Items";
continue;
}
if (currentSection == "Categories") {
categories.push_back(line);
}
else if (currentSection == "Items") {
std::vector<std::string> fields = ParseCSVLine(line);
if (fields.size() == 5) {
try {
Item item(fields[0], fields[1], std::stoi(fields[2]), fields[3]);
item.setCategory(fields[4]);
items.push_back(item);
}
catch (const std::invalid_argument& e) {
wxMessageBox("Warning: Invalid number format in line. Skipping.", "Load Warning", wxICON_WARNING);
}
}
}
}
file.close();
}
std::vector<std::string> Save::ParseCSVLine(const std::string& line) {
std::vector<std::string> fields;
std::string field;
bool inQuotes = false;
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
if (c == '"') {
inQuotes = !inQuotes;
}
else if (c == ',' && !inQuotes) {
fields.push_back(field);
field.clear();
}
else {
field += c;
}
}
fields.push_back(field);
return fields;
}
void Save::LoadItems(std::vector<Item>& items, const std::string& filePath) {
std::ifstream file(filePath);
if (!file.is_open()) {
wxMessageBox("Error: Could not open file for loading.", "Load Error", wxICON_ERROR);
return;
}
items.clear();
std::string line;
while (std::getline(file, line)) {
std::vector<std::string> fields = ParseCSVLine(line);
if (fields.size() == 5) { // Updated to check for 5 fields
try {
Item item(fields[0], fields[1], std::stoi(fields[2]), fields[3]);
item.setCategory(fields[4]); // Set the category
items.push_back(item);
}
catch (const std::invalid_argument& e) {
wxMessageBox("Warning: Invalid number format in line. Skipping. Error: " + std::string(e.what()), "Load Warning", wxICON_WARNING);
}
}
}
file.close();
wxMessageBox("Items loaded successfully!", "Load Complete", wxICON_INFORMATION);
}
bool Save::FileDoesNotExist(const std::string& filePath) {
std::ifstream file(filePath);
return !file.is_open();
}
void Save::SaveSettings(const std::list<std::string>& settings) {
std::ofstream file("save/settings.cfg");
if (!file.is_open()) {
wxMessageBox("Error: Could not open settings file for saving.", "Settings Error", wxICON_ERROR);
return;
}
for (const auto& setting : settings) {
file << setting << std::endl;
}
file.close();
}
std::list<std::string> Save::LoadSettings() {
std::list<std::string> settings;
if (FileDoesNotExist("save/settings.cfg")) {
SaveSettings(std::list<std::string>{","});
}
std::ifstream file("save/settings.cfg");
std::string line;
while (std::getline(file, line)) {
settings.push_back(line);
}
file.close();
return settings;
}