Update LoadFile, GetFileExtension, GetFileName

This commit is contained in:
scawful
2024-11-02 10:25:30 -05:00
parent 665d58aa97
commit 0d5618a5ba
2 changed files with 67 additions and 24 deletions

View File

@@ -14,12 +14,63 @@ namespace yaze {
namespace app {
namespace core {
std::string LoadFile(const std::string& filename) {
std::string GetFileExtension(const std::string &filename) {
size_t dot = filename.find_last_of(".");
if (dot == std::string::npos) {
return "";
}
return filename.substr(dot + 1);
}
std::string GetFileName(const std::string &filename) {
size_t slash = filename.find_last_of("/");
if (slash == std::string::npos) {
return filename;
}
return filename.substr(slash + 1);
}
std::string LoadFile(const std::string &filename, Platform platform) {
std::string contents;
// TODO: Load a file based on the platform and add error handling
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ifstream file(filepath);
if (file.is_open()) {
std::stringstream buffer;
buffer << file.rdbuf();
contents = buffer.str();
file.close();
}
return contents;
}
} // namespace core
} // namespace app
} // namespace yaze
void SaveFile(const std::string &filename, const std::string &contents,
Platform platform) {
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::ofstream file(filepath);
if (file.is_open()) {
file << contents;
file.close();
}
}
std::string GetConfigDirectory(Platform platform) {
std::string config_directory = ".yaze";
switch (platform) {
case Platform::kMacOS:
config_directory = "~/Library/Application Support/yaze";
break;
case Platform::kWindows:
config_directory = "~/AppData/Roaming/yaze";
break;
case Platform::kLinux:
config_directory = "~/.config/yaze";
break;
default:
break;
}
return config_directory;
}
} // namespace core
} // namespace app
} // namespace yaze

View File

@@ -7,26 +7,18 @@ namespace yaze {
namespace app {
namespace core {
std::string GetFileExtension(const std::string& filename) {
size_t dot = filename.find_last_of(".");
if (dot == std::string::npos) {
return "";
}
return filename.substr(dot + 1);
}
enum class Platform { kUnknown, kMacOS, kiOS, kWindows, kLinux };
std::string GetFileName(const std::string& filename) {
size_t slash = filename.find_last_of("/");
if (slash == std::string::npos) {
return filename;
}
return filename.substr(slash + 1);
}
std::string GetFileExtension(const std::string &filename);
std::string GetFileName(const std::string &filename);
std::string LoadFile(const std::string &filename, Platform platform);
std::string GetConfigDirectory(Platform platform);
std::string LoadFile(const std::string& filename);
void SaveFile(const std::string &filename, const std::string &data,
Platform platform);
} // namespace core
} // namespace app
} // namespace yaze
} // namespace core
} // namespace app
} // namespace yaze
#endif // YAZE_APP_CORE_UTILS_FILE_UTIL_H
#endif // YAZE_APP_CORE_UTILS_FILE_UTIL_H