Refactor project management: create Project class for handling project file operations and remove message handling code
This commit is contained in:
@@ -4,7 +4,7 @@ set(
|
|||||||
app/core/controller.cc
|
app/core/controller.cc
|
||||||
app/core/labeling.cc
|
app/core/labeling.cc
|
||||||
app/emu/emulator.cc
|
app/emu/emulator.cc
|
||||||
app/core/message.cc
|
app/core/project.cc
|
||||||
app/core/utils/file_util.cc
|
app/core/utils/file_util.cc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
58
src/app/core/project.cc
Normal file
58
src/app/core/project.cc
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#include "project.h"
|
||||||
|
|
||||||
|
namespace yaze {
|
||||||
|
namespace app {
|
||||||
|
|
||||||
|
absl::Status Project::Open(const std::string &project_path) {
|
||||||
|
filepath = project_path;
|
||||||
|
name = project_path.substr(project_path.find_last_of("/") + 1);
|
||||||
|
|
||||||
|
std::ifstream in(project_path);
|
||||||
|
|
||||||
|
if (!in.good()) {
|
||||||
|
return absl::InternalError("Could not open project file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
std::getline(in, name);
|
||||||
|
std::getline(in, filepath);
|
||||||
|
std::getline(in, rom_filename_);
|
||||||
|
std::getline(in, code_folder_);
|
||||||
|
std::getline(in, labels_filename_);
|
||||||
|
std::getline(in, keybindings_file);
|
||||||
|
|
||||||
|
while (std::getline(in, line)) {
|
||||||
|
if (line == kEndOfProjectFile) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
absl::Status Project::Save() {
|
||||||
|
RETURN_IF_ERROR(CheckForEmptyFields());
|
||||||
|
|
||||||
|
std::ofstream out(filepath + "/" + name + ".yaze");
|
||||||
|
if (!out.good()) {
|
||||||
|
return absl::InternalError("Could not open project file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
out << name << std::endl;
|
||||||
|
out << filepath << std::endl;
|
||||||
|
out << rom_filename_ << std::endl;
|
||||||
|
out << code_folder_ << std::endl;
|
||||||
|
out << labels_filename_ << std::endl;
|
||||||
|
out << keybindings_file << std::endl;
|
||||||
|
|
||||||
|
out << kEndOfProjectFile << std::endl;
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
return absl::OkStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace app
|
||||||
|
} // namespace yaze
|
||||||
@@ -37,54 +37,8 @@ struct Project : public core::ExperimentFlags {
|
|||||||
return absl::OkStatus();
|
return absl::OkStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
absl::Status Open(const std::string &project_path) {
|
absl::Status Open(const std::string &project_path);
|
||||||
filepath = project_path;
|
absl::Status Save();
|
||||||
name = project_path.substr(project_path.find_last_of("/") + 1);
|
|
||||||
|
|
||||||
std::ifstream in(project_path);
|
|
||||||
|
|
||||||
if (!in.good()) {
|
|
||||||
return absl::InternalError("Could not open project file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string line;
|
|
||||||
std::getline(in, name);
|
|
||||||
std::getline(in, filepath);
|
|
||||||
std::getline(in, rom_filename_);
|
|
||||||
std::getline(in, code_folder_);
|
|
||||||
std::getline(in, labels_filename_);
|
|
||||||
|
|
||||||
while (std::getline(in, line)) {
|
|
||||||
if (line == kEndOfProjectFile) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
return absl::OkStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::Status Save() {
|
|
||||||
RETURN_IF_ERROR(CheckForEmptyFields());
|
|
||||||
|
|
||||||
std::ofstream out(filepath + "/" + name + ".yaze");
|
|
||||||
if (!out.good()) {
|
|
||||||
return absl::InternalError("Could not open project file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
out << name << std::endl;
|
|
||||||
out << filepath << std::endl;
|
|
||||||
out << rom_filename_ << std::endl;
|
|
||||||
out << code_folder_ << std::endl;
|
|
||||||
out << labels_filename_ << std::endl;
|
|
||||||
|
|
||||||
out << kEndOfProjectFile << std::endl;
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
return absl::OkStatus();
|
|
||||||
}
|
|
||||||
|
|
||||||
absl::Status CheckForEmptyFields() {
|
absl::Status CheckForEmptyFields() {
|
||||||
if (name.empty() || filepath.empty() || rom_filename_.empty() ||
|
if (name.empty() || filepath.empty() || rom_filename_.empty() ||
|
||||||
@@ -104,6 +58,7 @@ struct Project : public core::ExperimentFlags {
|
|||||||
std::string rom_filename_ = "";
|
std::string rom_filename_ = "";
|
||||||
std::string code_folder_ = "";
|
std::string code_folder_ = "";
|
||||||
std::string labels_filename_ = "";
|
std::string labels_filename_ = "";
|
||||||
|
std::string keybindings_file = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace app
|
} // namespace app
|
||||||
|
|||||||
Reference in New Issue
Block a user