From a635e84d822b701916528f37d1fd13b7cacad7d0 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 12 Apr 2022 21:06:46 +1000 Subject: [PATCH] FileSystem: Add path splitting helpers --- common/FileSystem.cpp | 33 +++++++++++++++++++++++++++++++++ common/FileSystem.h | 6 ++++++ 2 files changed, 39 insertions(+) diff --git a/common/FileSystem.cpp b/common/FileSystem.cpp index 77cd17697b..b13b056bfd 100644 --- a/common/FileSystem.cpp +++ b/common/FileSystem.cpp @@ -214,6 +214,39 @@ std::string_view FileSystem::GetFileTitleFromPath(const std::string_view& path) return filename.substr(0, pos); } +std::vector FileSystem::SplitWindowsPath(const std::string_view& path) +{ + std::vector parts; + + std::string::size_type start = 0; + std::string::size_type pos = 0; + while (pos < path.size()) + { + if (path[pos] != '/' && path[pos] != '\\') + { + pos++; + continue; + } + + // skip consecutive separators + if (pos != start) + parts.push_back(path.substr(start, pos - start)); + + pos++; + start = pos; + } + + if (start != pos) + parts.push_back(path.substr(start)); + + return parts; +} + +std::vector FileSystem::SplitNativePath(const std::string_view& path) +{ + return StringUtil::SplitString(path, FS_OSPATH_SEPARATOR_CHARACTER, true); +} + std::vector FileSystem::GetRootDirectoryList() { std::vector results; diff --git a/common/FileSystem.h b/common/FileSystem.h index c37d59a597..1ea3cd6208 100644 --- a/common/FileSystem.h +++ b/common/FileSystem.h @@ -102,6 +102,12 @@ namespace FileSystem /// Returns the file title (less the extension and path) from a filename. std::string_view GetFileTitleFromPath(const std::string_view& path); + /// Splits a path into its components, handling both Windows and Unix separators. + std::vector SplitWindowsPath(const std::string_view& path); + + /// Splits a path into its components, only handling native separators. + std::vector SplitNativePath(const std::string_view& path); + /// Returns a list of "root directories" (i.e. root/home directories on Linux, drive letters on Windows). std::vector GetRootDirectoryList();