diff options
Diffstat (limited to 'src/common/fs')
| -rw-r--r-- | src/common/fs/path_util.cpp | 44 | ||||
| -rw-r--r-- | src/common/fs/path_util.h | 8 |
2 files changed, 37 insertions, 15 deletions
diff --git a/src/common/fs/path_util.cpp b/src/common/fs/path_util.cpp index c3a81f9a9..4f69db6f5 100644 --- a/src/common/fs/path_util.cpp +++ b/src/common/fs/path_util.cpp | |||
| @@ -354,18 +354,36 @@ std::string_view RemoveTrailingSlash(std::string_view path) { | |||
| 354 | return path; | 354 | return path; |
| 355 | } | 355 | } |
| 356 | 356 | ||
| 357 | std::vector<std::string> SplitPathComponents(std::string_view filename) { | 357 | template <typename F> |
| 358 | std::string copy(filename); | 358 | static void ForEachPathComponent(std::string_view filename, F&& cb) { |
| 359 | std::replace(copy.begin(), copy.end(), '\\', '/'); | 359 | const char* component_begin = filename.data(); |
| 360 | std::vector<std::string> out; | 360 | const char* const end = component_begin + filename.size(); |
| 361 | 361 | for (const char* it = component_begin; it != end; ++it) { | |
| 362 | std::stringstream stream(copy); | 362 | const char c = *it; |
| 363 | std::string item; | 363 | if (c == '\\' || c == '/') { |
| 364 | while (std::getline(stream, item, '/')) { | 364 | if (component_begin != it) { |
| 365 | out.push_back(std::move(item)); | 365 | cb(std::string_view{component_begin, it}); |
| 366 | } | ||
| 367 | component_begin = it + 1; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | if (component_begin != end) { | ||
| 371 | cb(std::string_view{component_begin, end}); | ||
| 366 | } | 372 | } |
| 373 | } | ||
| 374 | |||
| 375 | std::vector<std::string_view> SplitPathComponents(std::string_view filename) { | ||
| 376 | std::vector<std::string_view> components; | ||
| 377 | ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); | ||
| 367 | 378 | ||
| 368 | return out; | 379 | return components; |
| 380 | } | ||
| 381 | |||
| 382 | std::vector<std::string> SplitPathComponentsCopy(std::string_view filename) { | ||
| 383 | std::vector<std::string> components; | ||
| 384 | ForEachPathComponent(filename, [&](auto component) { components.emplace_back(component); }); | ||
| 385 | |||
| 386 | return components; | ||
| 369 | } | 387 | } |
| 370 | 388 | ||
| 371 | std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { | 389 | std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) { |
| @@ -400,9 +418,9 @@ std::string SanitizePath(std::string_view path_, DirectorySeparator directory_se | |||
| 400 | return std::string(RemoveTrailingSlash(path)); | 418 | return std::string(RemoveTrailingSlash(path)); |
| 401 | } | 419 | } |
| 402 | 420 | ||
| 403 | std::string_view GetParentPath(std::string_view path) { | 421 | std::string GetParentPath(std::string_view path) { |
| 404 | if (path.empty()) { | 422 | if (path.empty()) { |
| 405 | return path; | 423 | return std::string(path); |
| 406 | } | 424 | } |
| 407 | 425 | ||
| 408 | #ifdef ANDROID | 426 | #ifdef ANDROID |
| @@ -421,7 +439,7 @@ std::string_view GetParentPath(std::string_view path) { | |||
| 421 | name_index = std::max(name_bck_index, name_fwd_index); | 439 | name_index = std::max(name_bck_index, name_fwd_index); |
| 422 | } | 440 | } |
| 423 | 441 | ||
| 424 | return path.substr(0, name_index); | 442 | return std::string(path.substr(0, name_index)); |
| 425 | } | 443 | } |
| 426 | 444 | ||
| 427 | std::string_view GetPathWithoutTop(std::string_view path) { | 445 | std::string_view GetPathWithoutTop(std::string_view path) { |
diff --git a/src/common/fs/path_util.h b/src/common/fs/path_util.h index 2874ea738..59301e7ed 100644 --- a/src/common/fs/path_util.h +++ b/src/common/fs/path_util.h | |||
| @@ -289,7 +289,11 @@ enum class DirectorySeparator { | |||
| 289 | 289 | ||
| 290 | // Splits the path on '/' or '\' and put the components into a vector | 290 | // Splits the path on '/' or '\' and put the components into a vector |
| 291 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } | 291 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } |
| 292 | [[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename); | 292 | [[nodiscard]] std::vector<std::string_view> SplitPathComponents(std::string_view filename); |
| 293 | |||
| 294 | // Splits the path on '/' or '\' and put the components into a vector | ||
| 295 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } | ||
| 296 | [[nodiscard]] std::vector<std::string> SplitPathComponentsCopy(std::string_view filename); | ||
| 293 | 297 | ||
| 294 | // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' | 298 | // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' |
| 295 | // depending if directory_separator is BackwardSlash or PlatformDefault and running on windows | 299 | // depending if directory_separator is BackwardSlash or PlatformDefault and running on windows |
| @@ -298,7 +302,7 @@ enum class DirectorySeparator { | |||
| 298 | DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); | 302 | DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash); |
| 299 | 303 | ||
| 300 | // Gets all of the text up to the last '/' or '\' in the path. | 304 | // Gets all of the text up to the last '/' or '\' in the path. |
| 301 | [[nodiscard]] std::string_view GetParentPath(std::string_view path); | 305 | [[nodiscard]] std::string GetParentPath(std::string_view path); |
| 302 | 306 | ||
| 303 | // Gets all of the text after the first '/' or '\' in the path. | 307 | // Gets all of the text after the first '/' or '\' in the path. |
| 304 | [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path); | 308 | [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path); |