diff --git a/src/app/core/utils/file_util.cc b/src/app/core/utils/file_util.cc index b0436d65..a803133a 100644 --- a/src/app/core/utils/file_util.cc +++ b/src/app/core/utils/file_util.cc @@ -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 \ No newline at end of file +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 diff --git a/src/app/core/utils/file_util.h b/src/app/core/utils/file_util.h index a8bfd49b..67dd7ff1 100644 --- a/src/app/core/utils/file_util.h +++ b/src/app/core/utils/file_util.h @@ -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 \ No newline at end of file +#endif // YAZE_APP_CORE_UTILS_FILE_UTIL_H