summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar boludoz2023-10-15 21:40:10 -0300
committerGravatar boludoz2023-10-15 21:40:10 -0300
commit74961d4dfb394c098da494f6beacdd994c089566 (patch)
tree40cfe9748d427047b758f4e4fe37036010c29026 /src
parentTyping and formatting errors fixed. (diff)
downloadyuzu-74961d4dfb394c098da494f6beacdd994c089566.tar.gz
yuzu-74961d4dfb394c098da494f6beacdd994c089566.tar.xz
yuzu-74961d4dfb394c098da494f6beacdd994c089566.zip
Less code, simpler, better.
Diffstat (limited to 'src')
-rw-r--r--src/common/fs/fs_util.cpp59
-rw-r--r--src/common/fs/fs_util.h22
-rw-r--r--src/yuzu/main.cpp16
3 files changed, 15 insertions, 82 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
39std::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
94std::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
diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp
index c3c12eeec..f2b91a7f3 100644
--- a/src/yuzu/main.cpp
+++ b/src/yuzu/main.cpp
@@ -2849,9 +2849,21 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
2849 2849
2850 bool shortcut_succeeded = false; 2850 bool shortcut_succeeded = false;
2851 2851
2852 // Copy characters if they are not illegal in Windows filenames
2853 std::string filename = "";
2854 const std::string illegal_chars = "<>:\"/\\|?*";
2855 filename.reserve(name.size());
2856 std::copy_if(name.begin(), name.end(), std::back_inserter(filename),
2857 [&illegal_chars](char c) { return illegal_chars.find(c) == std::string::npos; });
2858
2859 if (filename.empty()) {
2860 LOG_ERROR(Frontend, "Filename is empty");
2861 shortcut_succeeded = false;
2862 return shortcut_succeeded;
2863 }
2864
2852 // Replace characters that are illegal in Windows filenames 2865 // Replace characters that are illegal in Windows filenames
2853 std::filesystem::path shortcut_path_full = 2866 std::filesystem::path shortcut_path_full = shortcut_path / filename;
2854 shortcut_path / Common::FS::UTF8FilenameSanitizer(name);
2855 2867
2856#if defined(__linux__) || defined(__FreeBSD__) 2868#if defined(__linux__) || defined(__FreeBSD__)
2857 shortcut_path_full += ".desktop"; 2869 shortcut_path_full += ".desktop";