Repository navigation
Expand file tree
/
Copy pathutils.cpp
More file actions
157 lines (146 loc) · 4.76 KB
/
Copy pathutils.cpp
File metadata and controls
157 lines (146 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "pch.h"
#include "utils.h"
// C:/dir/name
std::wstring convert_path_from_win_to_winslash(const std::wstring& win_path) {
std::wstring winslash;
for (WCHAR c : win_path) {
if (c == L'\\') {
winslash.push_back(L'/');
} else {
winslash.push_back(c);
}
}
return winslash;
}
// file:///C:/dir/name
std::wstring convert_path_from_win_to_fileprotocal(const std::wstring& win_path) {
std::wstring file_url = L"file:///";
for (WCHAR c : win_path) {
if (c == L'\\') {
file_url.push_back(L'/');
} else {
file_url.push_back(c);
}
}
DWORD size = static_cast<DWORD>(file_url.size() * 3 + 1);
std::wstring escaped(size, L'\0');
if (S_OK != UrlEscapeW(file_url.c_str(), &escaped[0], &size, URL_ESCAPE_SPACES_ONLY)) {
escaped.resize(size);
if (S_OK != UrlEscapeW(file_url.c_str(), &escaped[0], &size, URL_ESCAPE_SPACES_ONLY)) {
return file_url;
}
}
escaped.resize(size);
return escaped;
}
// C:\\dir\\name
std::wstring convert_path_from_win_to_winescaped(const std::wstring& win_path) {
std::wstring winescaped;
for (WCHAR c : win_path) {
if (c == L'\\') {
winescaped.append(L"\\\\");
} else {
winescaped.push_back(c);
}
}
return winescaped;
}
// /c/dir/name
std::wstring convert_path_from_win_to_unix(const std::wstring& win_path) {
std::wstring unix_path;
size_t len = win_path.length();
size_t i2 = 0;
if (len >= 2 && win_path[1] == L':') {
unix_path.push_back(L'/');
unix_path.push_back(win_path[0]);
i2 = 2;
}
for (; i2 < len; i2++) {
WCHAR c = win_path[i2];
if (c == L'\\') unix_path.push_back(L'/');
else unix_path.push_back(c);
}
return unix_path;
}
// name
std::wstring convert_path_from_win_to_name(const std::wstring& win_path) {
size_t pos = win_path.find_last_of(L'\\');
std::wstring name_part = (pos == std::wstring::npos) ? win_path : win_path.substr(pos + 1);
return name_part;
}
// /mnt/c/dir/name
// Only converts local drive-letter paths; UNC not specially handled.
std::wstring convert_path_from_win_to_wsl(const std::wstring &win_path) {
if (win_path.empty()) return win_path;
if (win_path.size() >= 2 && win_path[1] == L':') {
wchar_t drive = towlower(win_path[0]);
std::wstring rest;
size_t idx = 2;
if (win_path.size() > 2 && (win_path[2] == L'\\' || win_path[2] == L'/')) idx = 3;
for (size_t i = idx; i < win_path.size(); ++i) {
wchar_t c = win_path[i];
if (c == L'\\') rest.push_back(L'/');
else if (c == L' ') { rest.push_back(L'\\'); rest.push_back(L' '); }
else rest.push_back(c);
}
std::wstring out = L"/mnt/";
out.push_back(drive);
if (!rest.empty()) {
if (rest.front() != L'/') out.push_back(L'/');
out += rest;
}
return out;
}
// Fallback: replace backslashes with slashes and ensure leading slash
std::wstring s = win_path;
for (wchar_t &c : s) if (c == L'\\') c = L'/';
if (!s.empty() && s[0] != L'/') s.insert(s.begin(), L'/');
return s;
}
std::wstring ConvertPaths(std::vector<std::wstring> paths, std::wstring (*converter)(const std::wstring&), std::wstring wrapper) {
std::wstring res;
size_t size = paths.size();
for (size_t i = 0; i < size; i++) {
if (converter != nullptr) {
std::wstring converted = converter(paths[i]);
res += wrapper + converted + wrapper;
} else {
res += wrapper + paths[i] + wrapper;
}
res.push_back(L'\n');
}
res.pop_back();
return res;
}
// Helper: truncate a wide string in the middle with ellipsis so total length <= maxLen
std::wstring TruncateMiddle(const std::wstring &s, size_t maxLen) {
if (s.size() <= maxLen) return s;
if (maxLen <= 1) return s.substr(0, maxLen);
size_t keepLeft = (maxLen - 1) / 2;
size_t keepRight = (maxLen - 1) - keepLeft;
return s.substr(0, keepLeft) + L'\u2026' + s.substr(s.size() - keepRight);
}
BOOL SetClipboardTextW(const WCHAR *text, SIZE_T cch) {
if (!OpenClipboard(NULL)) {
return FALSE;
}
if (!EmptyClipboard()) {
CloseClipboard();
return FALSE;
}
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR) * cch);
if (hGlobal == NULL) {
CloseClipboard();
return FALSE;
}
WCHAR *lpText = (WCHAR *)GlobalLock(hGlobal);
if (lpText == NULL) {
CloseClipboard();
return FALSE;
}
wmemcpy_s(lpText, cch, text, cch);
GlobalUnlock(hGlobal);
SetClipboardData(CF_UNICODETEXT, hGlobal);
CloseClipboard();
return TRUE;
}