diff --git a/src/Utils/Utils/FileAPI.cpp b/src/Utils/Utils/FileAPI.cpp index b3b7e7d1..067c8a5d 100644 --- a/src/Utils/Utils/FileAPI.cpp +++ b/src/Utils/Utils/FileAPI.cpp @@ -2,31 +2,26 @@ #include #include +#include bool FileAPI::FileExists(const std::string& fileName) { - struct stat st{}; - - return stat(fileName.c_str(), &st) >= 0 && !(st.st_mode & S_IFDIR); + return std::filesystem::exists(fileName); } uint64_t FileAPI::FileSize(const std::string& fileName) { - struct stat st{}; + return std::filesystem::file_size(fileName); +} - if (stat(fileName.c_str(), &st) >= 0 && !(st.st_mode & S_IFDIR)) - { - return st.st_size; - } - - return 0; +bool FileAPI::CreateDirectory(const std::string& directoryPath) +{ + return std::filesystem::create_directories(directoryPath); } bool FileAPI::DirectoryExists(const std::string& directoryName) { - struct stat st{}; - - return stat(directoryName.c_str(), &st) >= 0 && st.st_mode & S_IFDIR; + return std::filesystem::is_directory(directoryName); } FileAPI::File FileAPI::Open(const std::string& filename, const Mode mode) diff --git a/src/Utils/Utils/FileAPI.h b/src/Utils/Utils/FileAPI.h index 4b8a4962..ba543fc3 100644 --- a/src/Utils/Utils/FileAPI.h +++ b/src/Utils/Utils/FileAPI.h @@ -38,6 +38,9 @@ public: static bool FileExists(const std::string& fileName); static uint64_t FileSize(const std::string& fileName); + + static bool CreateDirectory(const std::string& directoryPath); static bool DirectoryExists(const std::string& directoryName); + static File Open(const std::string& filename, Mode mode); }; \ No newline at end of file diff --git a/src/Utils/Utils/PathUtils.cpp b/src/Utils/Utils/PathUtils.cpp index 66093a45..823f0873 100644 --- a/src/Utils/Utils/PathUtils.cpp +++ b/src/Utils/Utils/PathUtils.cpp @@ -1,79 +1,41 @@ #include "PathUtils.h" +#include + namespace utils { - std::string Path::GetFilename(std::string path) + std::string Path::GetFilename(const std::string& pathInput) { - const size_t lastSlashIndex = path.find_last_of("\\/"); - if (std::string::npos != lastSlashIndex) - { - path.erase(0, lastSlashIndex); - } + const std::filesystem::path path(pathInput); - return path; + return path.filename().string(); } - std::string Path::GetFilenameWithoutExtension(std::string path) + std::string Path::GetFilenameWithoutExtension(const std::string& pathInput) { - const size_t lastSlashIndex = path.find_last_of("\\/"); - if (std::string::npos != lastSlashIndex) - { - path.erase(0, lastSlashIndex + 1); - } + const std::filesystem::path path(pathInput); - // Remove extension if present. - const size_t dotIndex = path.rfind('.'); - if (std::string::npos != dotIndex) - { - path.erase(dotIndex); - } - - return path; + return path.filename().replace_extension().string(); } - std::string Path::GetExtension(std::string path) + std::string Path::GetExtension(const std::string& pathInput) { - const size_t lastSlashIndex = path.find_last_of("\\/"); - const size_t lastDotIndex = path.find_last_of('.'); - if (std::string::npos != lastDotIndex - && (lastSlashIndex == std::string::npos || lastDotIndex > lastSlashIndex)) - { - path.erase(0, lastDotIndex); + const std::filesystem::path path(pathInput); - return path; - } - - return ""; + return path.extension().string(); } - std::string Path::GetDirectory(std::string path) + std::string Path::GetDirectory(const std::string& pathInput) { - const size_t lastSlashIndex = path.find_last_of("\\/"); - if (std::string::npos != lastSlashIndex) - { - path.erase(lastSlashIndex); - } - else - { - return "./"; - } + const std::filesystem::path path(pathInput); - return path; + return path.relative_path().string(); } - std::string Path::Combine(std::string p1, std::string p2) + std::string Path::Combine(const std::string& p1, const std::string& p2) { - char c; + std::filesystem::path path(p1); - while (!p1.empty() && (c = p1[p1.size() - 1], c == '\\' || c == '/')) - p1.erase(p1.size() - 1); - - while (!p2.empty() && (c = p2[0], c == '\\' || c == '/')) - p2.erase(0); - - if (!p1.empty()) - p1 += '/'; - - return p1 + p2; + return path.append(p2).string(); } } diff --git a/src/Utils/Utils/PathUtils.h b/src/Utils/Utils/PathUtils.h index 11f4477f..81b8e01f 100644 --- a/src/Utils/Utils/PathUtils.h +++ b/src/Utils/Utils/PathUtils.h @@ -7,10 +7,10 @@ namespace utils class Path { public: - static std::string GetFilename(std::string path); - static std::string GetFilenameWithoutExtension(std::string path); - static std::string GetExtension(std::string path); - static std::string GetDirectory(std::string path); - static std::string Combine(std::string p1, std::string p2); + static std::string GetFilename(const std::string& pathInput); + static std::string GetFilenameWithoutExtension(const std::string& pathInput); + static std::string GetExtension(const std::string& pathInput); + static std::string GetDirectory(const std::string& pathInput); + static std::string Combine(const std::string& p1, const std::string& p2); }; } \ No newline at end of file