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()) {
|
||||||
|
|||||||
@@ -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