diff options
| author | 2023-10-15 21:40:10 -0300 | |
|---|---|---|
| committer | 2023-10-15 21:40:10 -0300 | |
| commit | 74961d4dfb394c098da494f6beacdd994c089566 (patch) | |
| tree | 40cfe9748d427047b758f4e4fe37036010c29026 /src/common/fs | |
| parent | Typing and formatting errors fixed. (diff) | |
| download | yuzu-74961d4dfb394c098da494f6beacdd994c089566.tar.gz yuzu-74961d4dfb394c098da494f6beacdd994c089566.tar.xz yuzu-74961d4dfb394c098da494f6beacdd994c089566.zip | |
Less code, simpler, better.
Diffstat (limited to 'src/common/fs')
| -rw-r--r-- | src/common/fs/fs_util.cpp | 59 | ||||
| -rw-r--r-- | src/common/fs/fs_util.h | 22 |
2 files changed, 1 insertions, 80 deletions
diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index e77958224..813a713c3 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp | |||
| @@ -36,63 +36,4 @@ std::string PathToUTF8String(const std::filesystem::path& path) { | |||
| 36 | return ToUTF8String(path.u8string()); | 36 | return ToUTF8String(path.u8string()); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | std::u8string U8FilenameSanitizer(const std::u8string_view u8filename) { | ||
| 40 | std::u8string u8path_sanitized{u8filename.begin(), u8filename.end()}; | ||
| 41 | size_t eSizeSanitized = u8path_sanitized.size(); | ||
| 42 | |||
| 43 | // The name is improved to make it look more beautiful and prohibited characters and shapes are | ||
| 44 | // removed. Switch is used since it is better with many conditions. | ||
| 45 | for (size_t i = 0; i < eSizeSanitized; i++) { | ||
| 46 | switch (u8path_sanitized[i]) { | ||
| 47 | case u8':': | ||
| 48 | if (i == 0 || i == eSizeSanitized - 1) { | ||
| 49 | u8path_sanitized.replace(i, 1, u8"_"); | ||
| 50 | } else if (u8path_sanitized[i - 1] == u8' ') { | ||
| 51 | u8path_sanitized.replace(i, 1, u8"-"); | ||
| 52 | } else { | ||
| 53 | u8path_sanitized.replace(i, 1, u8" -"); | ||
| 54 | eSizeSanitized++; | ||
| 55 | } | ||
| 56 | break; | ||
| 57 | case u8'\\': | ||
| 58 | case u8'/': | ||
| 59 | case u8'*': | ||
| 60 | case u8'?': | ||
| 61 | case u8'\"': | ||
| 62 | case u8'<': | ||
| 63 | case u8'>': | ||
| 64 | case u8'|': | ||
| 65 | case u8'\0': | ||
| 66 | u8path_sanitized.replace(i, 1, u8"_"); | ||
| 67 | break; | ||
| 68 | default: | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | // Delete duplicated spaces and dots | ||
| 74 | for (size_t i = 0; i < eSizeSanitized - 1; i++) { | ||
| 75 | if ((u8path_sanitized[i] == u8' ' && u8path_sanitized[i + 1] == u8' ') || | ||
| 76 | (u8path_sanitized[i] == u8'.' && u8path_sanitized[i + 1] == u8'.')) { | ||
| 77 | u8path_sanitized.erase(i, 1); | ||
| 78 | i--; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | // Delete all spaces and dots at the end of the name | ||
| 83 | while (u8path_sanitized.back() == u8' ' || u8path_sanitized.back() == u8'.') { | ||
| 84 | u8path_sanitized.pop_back(); | ||
| 85 | } | ||
| 86 | |||
| 87 | if (u8path_sanitized.empty()) { | ||
| 88 | return u8""; | ||
| 89 | } | ||
| 90 | |||
| 91 | return u8path_sanitized; | ||
| 92 | } | ||
| 93 | |||
| 94 | std::string UTF8FilenameSanitizer(const std::string_view filename) { | ||
| 95 | return ToUTF8String(U8FilenameSanitizer(ToU8String(filename))); | ||
| 96 | } | ||
| 97 | |||
| 98 | } // namespace Common::FS | 39 | } // namespace Common::FS |
diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index daec1f8cb..2492a9f94 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h | |||
| @@ -82,24 +82,4 @@ concept IsChar = std::same_as<T, char>; | |||
| 82 | */ | 82 | */ |
| 83 | [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); | 83 | [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); |
| 84 | 84 | ||
| 85 | /** | 85 | } // namespace Common::FS |
| 86 | * Fix filename (remove invalid characters) | ||
| 87 | * | ||
| 88 | * @param u8_string dirty encoded filename string | ||
| 89 | * | ||
| 90 | * @returns utf8_string sanitized filename string | ||
| 91 | * | ||
| 92 | */ | ||
| 93 | [[nodiscard]] std::u8string U8FilenameSanitizer(const std::u8string_view u8filename); | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Fix filename (remove invalid characters) | ||
| 97 | * | ||
| 98 | * @param utf8_string dirty encoded filename string | ||
| 99 | * | ||
| 100 | * @returns utf8_string sanitized filename string | ||
| 101 | * | ||
| 102 | */ | ||
| 103 | [[nodiscard]] std::string UTF8FilenameSanitizer(const std::string_view filename); | ||
| 104 | |||
| 105 | } // namespace Common::FS \ No newline at end of file | ||