diff options
author | boludoz <francomaro@gmail.com> | 2023-10-15 02:02:22 -0300 |
---|---|---|
committer | boludoz <francomaro@gmail.com> | 2023-10-15 02:02:22 -0300 |
commit | 3062a35eb1297067446156c43e9d0df2f684edff (patch) | |
tree | f2ca58e0b8a6c413f3c6783a1501204729e0394c /src/common/fs/fs_util.cpp | |
parent | 1a4abd184f8085f29a18453bdc4aabb3c038907a (diff) |
Improved shortcut: add games in applist for Windows, question for start game at fullscreen & better unicode support for some Windows path funcs.
Diffstat (limited to 'src/common/fs/fs_util.cpp')
-rw-r--r-- | src/common/fs/fs_util.cpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index 813a713c3..442f63728 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp @@ -36,4 +36,63 @@ std::string PathToUTF8String(const std::filesystem::path& path) { return ToUTF8String(path.u8string()); } +std::u8string U8FilenameSantizer(const std::u8string_view u8filename) { + std::u8string u8path_santized{u8filename.begin(), u8filename.end()}; + size_t eSizeSanitized = u8path_santized.size(); + + // Special case for ":", for example: 'Pepe: La secuela' --> 'Pepe - La + // secuela' or 'Pepe : La secuela' --> 'Pepe - La secuela' + for (size_t i = 0; i < eSizeSanitized; i++) { + switch (u8path_santized[i]) { + case u8':': + if (i == 0 || i == eSizeSanitized - 1) { + u8path_santized.replace(i, 1, u8"_"); + } else if (u8path_santized[i - 1] == u8' ') { + u8path_santized.replace(i, 1, u8"-"); + } else { + u8path_santized.replace(i, 1, u8" -"); + eSizeSanitized++; + } + break; + case u8'\\': + case u8'/': + case u8'*': + case u8'?': + case u8'\"': + case u8'<': + case u8'>': + case u8'|': + case u8'\0': + u8path_santized.replace(i, 1, u8"_"); + break; + default: + break; + } + } + + // Delete duplicated spaces || Delete duplicated dots (MacOS i think) + for (size_t i = 0; i < eSizeSanitized - 1; i++) { + if ((u8path_santized[i] == u8' ' && u8path_santized[i + 1] == u8' ') || + (u8path_santized[i] == u8'.' && u8path_santized[i + 1] == u8'.')) { + u8path_santized.erase(i, 1); + i--; + } + } + + // Delete all spaces and dots at the end (Windows almost) + while (u8path_santized.back() == u8' ' || u8path_santized.back() == u8'.') { + u8path_santized.pop_back(); + } + + if (u8path_santized.empty()) { + return u8""; + } + + return u8path_santized; +} + +std::string UTF8FilenameSantizer(const std::string_view filename) { + return ToUTF8String(U8FilenameSantizer(ToU8String(filename))); +} + } // namespace Common::FS |