diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/file_util.cpp | 82 | ||||
| -rw-r--r-- | src/common/file_util.h | 13 |
2 files changed, 55 insertions, 40 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index a427372c9..1bc291cf9 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -792,66 +792,80 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 792 | } | 792 | } |
| 793 | } | 793 | } |
| 794 | 794 | ||
| 795 | std::vector<std::string> SplitPathComponents(const std::string& filename) { | 795 | std::vector<std::string> SplitPathComponents(std::string_view filename) { |
| 796 | auto copy(filename); | 796 | std::string copy(filename); |
| 797 | std::replace(copy.begin(), copy.end(), '\\', '/'); | 797 | std::replace(copy.begin(), copy.end(), '\\', '/'); |
| 798 | std::vector<std::string> out; | 798 | std::vector<std::string> out; |
| 799 | 799 | ||
| 800 | std::stringstream stream(filename); | 800 | std::stringstream stream(copy); |
| 801 | std::string item; | 801 | std::string item; |
| 802 | while (std::getline(stream, item, '/')) | 802 | while (std::getline(stream, item, '/')) { |
| 803 | out.push_back(std::move(item)); | 803 | out.push_back(std::move(item)); |
| 804 | } | ||
| 804 | 805 | ||
| 805 | return out; | 806 | return out; |
| 806 | } | 807 | } |
| 807 | 808 | ||
| 808 | std::string GetParentPath(const std::string& path) { | 809 | std::string_view GetParentPath(std::string_view path) { |
| 809 | auto out = path; | 810 | const auto name_bck_index = path.rfind('\\'); |
| 810 | const auto name_bck_index = out.find_last_of('\\'); | 811 | const auto name_fwd_index = path.rfind('/'); |
| 811 | const auto name_fwd_index = out.find_last_of('/'); | ||
| 812 | size_t name_index; | 812 | size_t name_index; |
| 813 | if (name_bck_index == std::string::npos || name_fwd_index == std::string::npos) | ||
| 814 | name_index = std::min<size_t>(name_bck_index, name_fwd_index); | ||
| 815 | else | ||
| 816 | name_index = std::max<size_t>(name_bck_index, name_fwd_index); | ||
| 817 | 813 | ||
| 818 | return out.erase(name_index); | 814 | if (name_bck_index == std::string_view::npos || name_fwd_index == std::string_view::npos) { |
| 815 | name_index = std::min(name_bck_index, name_fwd_index); | ||
| 816 | } else { | ||
| 817 | name_index = std::max(name_bck_index, name_fwd_index); | ||
| 818 | } | ||
| 819 | |||
| 820 | return path.substr(0, name_index); | ||
| 819 | } | 821 | } |
| 820 | 822 | ||
| 821 | std::string GetPathWithoutTop(std::string path) { | 823 | std::string_view GetPathWithoutTop(std::string_view path) { |
| 822 | if (path.empty()) | 824 | if (path.empty()) { |
| 823 | return ""; | 825 | return path; |
| 826 | } | ||
| 827 | |||
| 824 | while (path[0] == '\\' || path[0] == '/') { | 828 | while (path[0] == '\\' || path[0] == '/') { |
| 825 | path = path.substr(1); | 829 | path.remove_suffix(1); |
| 826 | if (path.empty()) | 830 | if (path.empty()) { |
| 827 | return ""; | 831 | return path; |
| 832 | } | ||
| 828 | } | 833 | } |
| 829 | const auto name_bck_index = path.find_first_of('\\'); | 834 | |
| 830 | const auto name_fwd_index = path.find_first_of('/'); | 835 | const auto name_bck_index = path.find('\\'); |
| 836 | const auto name_fwd_index = path.find('/'); | ||
| 831 | return path.substr(std::min(name_bck_index, name_fwd_index) + 1); | 837 | return path.substr(std::min(name_bck_index, name_fwd_index) + 1); |
| 832 | } | 838 | } |
| 833 | 839 | ||
| 834 | std::string GetFilename(std::string path) { | 840 | std::string_view GetFilename(std::string_view path) { |
| 835 | std::replace(path.begin(), path.end(), '\\', '/'); | 841 | const auto name_index = path.find_last_of("\\/"); |
| 836 | auto name_index = path.find_last_of('/'); | 842 | |
| 837 | if (name_index == std::string::npos) | 843 | if (name_index == std::string_view::npos) { |
| 838 | return ""; | 844 | return {}; |
| 845 | } | ||
| 846 | |||
| 839 | return path.substr(name_index + 1); | 847 | return path.substr(name_index + 1); |
| 840 | } | 848 | } |
| 841 | 849 | ||
| 842 | std::string GetExtensionFromFilename(const std::string& name) { | 850 | std::string_view GetExtensionFromFilename(std::string_view name) { |
| 843 | size_t index = name.find_last_of('.'); | 851 | const size_t index = name.rfind('.'); |
| 844 | if (index == std::string::npos) | 852 | |
| 845 | return ""; | 853 | if (index == std::string_view::npos) { |
| 854 | return {}; | ||
| 855 | } | ||
| 846 | 856 | ||
| 847 | return name.substr(index + 1); | 857 | return name.substr(index + 1); |
| 848 | } | 858 | } |
| 849 | 859 | ||
| 850 | std::string RemoveTrailingSlash(const std::string& path) { | 860 | std::string_view RemoveTrailingSlash(std::string_view path) { |
| 851 | if (path.empty()) | 861 | if (path.empty()) { |
| 852 | return path; | 862 | return path; |
| 853 | if (path.back() == '\\' || path.back() == '/') | 863 | } |
| 854 | return path.substr(0, path.size() - 1); | 864 | |
| 865 | if (path.back() == '\\' || path.back() == '/') { | ||
| 866 | path.remove_suffix(1); | ||
| 867 | return path; | ||
| 868 | } | ||
| 855 | 869 | ||
| 856 | return path; | 870 | return path; |
| 857 | } | 871 | } |
diff --git a/src/common/file_util.h b/src/common/file_util.h index 090907c03..abfa79eae 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include <fstream> | 9 | #include <fstream> |
| 10 | #include <functional> | 10 | #include <functional> |
| 11 | #include <string> | 11 | #include <string> |
| 12 | #include <string_view> | ||
| 12 | #include <type_traits> | 13 | #include <type_traits> |
| 13 | #include <vector> | 14 | #include <vector> |
| 14 | #include "common/common_types.h" | 15 | #include "common/common_types.h" |
| @@ -151,22 +152,22 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 151 | 152 | ||
| 152 | // Splits the path on '/' or '\' and put the components into a vector | 153 | // Splits the path on '/' or '\' and put the components into a vector |
| 153 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } | 154 | // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" } |
| 154 | std::vector<std::string> SplitPathComponents(const std::string& filename); | 155 | std::vector<std::string> SplitPathComponents(std::string_view filename); |
| 155 | 156 | ||
| 156 | // Gets all of the text up to the last '/' or '\' in the path. | 157 | // Gets all of the text up to the last '/' or '\' in the path. |
| 157 | std::string GetParentPath(const std::string& path); | 158 | std::string_view GetParentPath(std::string_view path); |
| 158 | 159 | ||
| 159 | // Gets all of the text after the first '/' or '\' in the path. | 160 | // Gets all of the text after the first '/' or '\' in the path. |
| 160 | std::string GetPathWithoutTop(std::string path); | 161 | std::string_view GetPathWithoutTop(std::string_view path); |
| 161 | 162 | ||
| 162 | // Gets the filename of the path | 163 | // Gets the filename of the path |
| 163 | std::string GetFilename(std::string path); | 164 | std::string_view GetFilename(std::string_view path); |
| 164 | 165 | ||
| 165 | // Gets the extension of the filename | 166 | // Gets the extension of the filename |
| 166 | std::string GetExtensionFromFilename(const std::string& name); | 167 | std::string_view GetExtensionFromFilename(std::string_view name); |
| 167 | 168 | ||
| 168 | // Removes the final '/' or '\' if one exists | 169 | // Removes the final '/' or '\' if one exists |
| 169 | std::string RemoveTrailingSlash(const std::string& path); | 170 | std::string_view RemoveTrailingSlash(std::string_view path); |
| 170 | 171 | ||
| 171 | // Creates a new vector containing indices [first, last) from the original. | 172 | // Creates a new vector containing indices [first, last) from the original. |
| 172 | template <typename T> | 173 | template <typename T> |