Refactor file loading functions: simplify LoadFile, add LoadConfigFile, and adjust platform handling
This commit is contained in:
@@ -29,8 +29,31 @@ std::string GetFileName(const std::string &filename) {
|
|||||||
return filename.substr(slash + 1);
|
return filename.substr(slash + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string LoadFile(const std::string &filename, Platform platform) {
|
std::string LoadFile(const std::string &filename) {
|
||||||
std::string contents;
|
std::string contents;
|
||||||
|
std::ifstream file(filename);
|
||||||
|
if (file.is_open()) {
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << file.rdbuf();
|
||||||
|
contents = buffer.str();
|
||||||
|
file.close();
|
||||||
|
} else {
|
||||||
|
// Throw an exception
|
||||||
|
throw std::runtime_error("Could not open file: " + filename);
|
||||||
|
}
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LoadConfigFile(const std::string &filename) {
|
||||||
|
std::string contents;
|
||||||
|
Platform platform;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
platform = Platform::kWindows;
|
||||||
|
#elif defined(__APPLE__)
|
||||||
|
platform = Platform::kMacOS;
|
||||||
|
#else
|
||||||
|
platform = Platform::kLinux;
|
||||||
|
#endif
|
||||||
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
|
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
|
||||||
std::ifstream file(filepath);
|
std::ifstream file(filepath);
|
||||||
if (file.is_open()) {
|
if (file.is_open()) {
|
||||||
@@ -55,18 +78,18 @@ void SaveFile(const std::string &filename, const std::string &contents,
|
|||||||
std::string GetConfigDirectory(Platform platform) {
|
std::string GetConfigDirectory(Platform platform) {
|
||||||
std::string config_directory = ".yaze";
|
std::string config_directory = ".yaze";
|
||||||
switch (platform) {
|
switch (platform) {
|
||||||
case Platform::kWindows:
|
case Platform::kWindows:
|
||||||
config_directory = "~/AppData/Roaming/yaze";
|
config_directory = "~/AppData/Roaming/yaze";
|
||||||
break;
|
break;
|
||||||
case Platform::kMacOS:
|
case Platform::kMacOS:
|
||||||
case Platform::kLinux:
|
case Platform::kLinux:
|
||||||
config_directory = "~/.config/yaze";
|
config_directory = "~/.config/yaze";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return config_directory;
|
return config_directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace core
|
} // namespace core
|
||||||
} // namespace yaze
|
} // namespace yaze
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ enum class Platform { kUnknown, kMacOS, kiOS, kWindows, kLinux };
|
|||||||
|
|
||||||
std::string GetFileExtension(const std::string &filename);
|
std::string GetFileExtension(const std::string &filename);
|
||||||
std::string GetFileName(const std::string &filename);
|
std::string GetFileName(const std::string &filename);
|
||||||
std::string LoadFile(const std::string &filename, Platform platform);
|
std::string LoadFile(const std::string &filename);
|
||||||
|
std::string LoadConfigFile(const std::string &filename);
|
||||||
std::string GetConfigDirectory(Platform platform);
|
std::string GetConfigDirectory(Platform platform);
|
||||||
|
|
||||||
void SaveFile(const std::string &filename, const std::string &data,
|
void SaveFile(const std::string &filename, const std::string &data,
|
||||||
|
|||||||
Reference in New Issue
Block a user