diff --git a/src/Utils/Utils.vcxproj b/src/Utils/Utils.vcxproj index b6ea4fc5..2f96dc40 100644 --- a/src/Utils/Utils.vcxproj +++ b/src/Utils/Utils.vcxproj @@ -21,9 +21,11 @@ + + 15.0 diff --git a/src/Utils/Utils/PathUtils.cpp b/src/Utils/Utils/PathUtils.cpp new file mode 100644 index 00000000..66093a45 --- /dev/null +++ b/src/Utils/Utils/PathUtils.cpp @@ -0,0 +1,79 @@ +#include "PathUtils.h" + +namespace utils +{ + std::string Path::GetFilename(std::string path) + { + const size_t lastSlashIndex = path.find_last_of("\\/"); + if (std::string::npos != lastSlashIndex) + { + path.erase(0, lastSlashIndex); + } + + return path; + } + + std::string Path::GetFilenameWithoutExtension(std::string path) + { + const size_t lastSlashIndex = path.find_last_of("\\/"); + if (std::string::npos != lastSlashIndex) + { + path.erase(0, lastSlashIndex + 1); + } + + // Remove extension if present. + const size_t dotIndex = path.rfind('.'); + if (std::string::npos != dotIndex) + { + path.erase(dotIndex); + } + + return path; + } + + std::string Path::GetExtension(std::string path) + { + 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); + + return path; + } + + return ""; + } + + std::string Path::GetDirectory(std::string path) + { + const size_t lastSlashIndex = path.find_last_of("\\/"); + if (std::string::npos != lastSlashIndex) + { + path.erase(lastSlashIndex); + } + else + { + return "./"; + } + + return path; + } + + std::string Path::Combine(std::string p1, std::string p2) + { + char c; + + 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; + } +} diff --git a/src/Utils/Utils/PathUtils.h b/src/Utils/Utils/PathUtils.h new file mode 100644 index 00000000..11f4477f --- /dev/null +++ b/src/Utils/Utils/PathUtils.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +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); + }; +} \ No newline at end of file