forked from AUTOMATIC1111/3ds-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellUtils.cc
More file actions
205 lines (166 loc) · 5.24 KB
/
ShellUtils.cc
File metadata and controls
205 lines (166 loc) · 5.24 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "ShellUtils.h"
#include <shlobj.h>
unsigned long long swapLongLong(unsigned long long x) {
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
std::tstring format(const TCHAR *fmt,va_list ap){
int size = 100;
std::tstring str;
while (1) {
str.resize(size);
int n = _vsntprintf((TCHAR *)str.c_str(), size, fmt, ap);
if (n > -1 && n < size) {
str.resize(n);
return str;
}
if (n > -1)
size = n + 1;
else
size *= 2;
}
return str;
}
std::tstring format(const TCHAR *fmt, ...) {
va_list ap;
va_start(ap, fmt);
std::tstring res=format(fmt,ap);
va_end(ap);
return res;
}
std::string formatA(const char *fmt, ...){
int size = 100;
std::string str;
va_list ap;
while (1) {
str.resize(size);
va_start(ap, fmt);
int n = _vsnprintf((char *)str.c_str(), size, fmt, ap);
va_end(ap);
if (n > -1 && n < size) {
str.resize(n);
return str;
}
if (n > -1)
size = n + 1;
else
size *= 2;
}
return str;
}
std::wstring s2ws(const std::string& s){
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, 0, 0);
std::wstring r(len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), slength, &r[0], len);
r.resize(len-1);
return r;
}
std::string ws2s(const std::wstring& s){
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, 0, 0, 0, 0);
std::string r(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, s.c_str(), slength, &r[0], len, 0, 0);
r.resize(len-1);
return r;
}
std::wstring cs2ws(const char *s){
int len;
int slength = (int)strlen(s) + 1;
len = MultiByteToWideChar(CP_UTF8, 0, s, slength, 0, 0);
std::wstring r(len, L'\0');
MultiByteToWideChar(CP_UTF8, 0, s, slength, &r[0], len);
r.resize(len-1);
return r;
}
std::string cws2s(const TCHAR *s){
int len;
int slength = (int)_tcslen(s) + 1;
len = WideCharToMultiByte(CP_UTF8, 0, s, slength, 0, 0, 0, 0);
std::string r(len, '\0');
WideCharToMultiByte(CP_UTF8, 0, s, slength, &r[0], len, 0, 0);
r.resize(len-1);
return r;
}
std::wstring js2ws(const std::string& s){
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(932, 0, s.c_str(), slength, 0, 0);
std::wstring r(len, L'\0');
MultiByteToWideChar(932, 0, s.c_str(), slength, &r[0], len);
r.resize(len-1);
return r;
}
std::string ws2js(const std::wstring& s){
int len;
int slength = (int)s.length() + 1;
len = WideCharToMultiByte(932, 0, s.c_str(), slength, 0, 0, 0, 0);
std::string r(len, '\0');
WideCharToMultiByte(932, 0, s.c_str(), slength, &r[0], len, 0, 0);
r.resize(len-1);
return r;
}
std::wstring jcs2ws(const char *s){
int len;
int slength = (int)strlen(s) + 1;
len = MultiByteToWideChar(932, 0, s, slength, 0, 0);
std::wstring r(len, L'\0');
MultiByteToWideChar(932, 0, s, slength, &r[0], len);
r.resize(len-1);
return r;
}
std::string cws2js(const TCHAR *s){
int len;
int slength = (int)_tcslen(s) + 1;
len = WideCharToMultiByte(932, 0, s, slength, 0, 0, 0, 0);
std::string r(len, '\0');
WideCharToMultiByte(932, 0, s, slength, &r[0], len, 0, 0);
r.resize(len-1);
return r;
}
std::tstring lcfirst(const std::tstring& s){
std::tstring res=std::tstring(_T(""))+(TCHAR)tolower(s.at(0))+s.substr(1,999999);
return res;
}
std::tstring ucfirst(const std::tstring& s){
std::tstring res=std::tstring(_T(""))+(TCHAR)toupper(s.at(0))+s.substr(1,999999);
return res;
}
std::tstring filesize(size_t size){
std::tstring res;
double s=(double)size;
if(s<1024) return format(_T("%.0fb"),s); s/=1024;
if(s<1024) return format(_T("%.1fkB"),s); s/=1024;
if(s<1024) return format(_T("%.2fMb"),s); s/=1024;
if(s<1024) return format(_T("%.2fGb"),s); s/=1024;
if(s<1024) return format(_T("%.2fTb"),s);
return _T("HUGE!");
}
void listFiles(TCHAR *path, std::list<String>& files){
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
TCHAR sPath[4096];
_sntprintf(sPath,4096, _T("%s\\*.*"), path);
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
return;
do{
if(_tcscmp(fdFile.cFileName, _T(".")) == 0) continue;
if(_tcscmp(fdFile.cFileName, _T("..")) == 0) continue;
_sntprintf(sPath,4096,_T("%s\\%s"), path, fdFile.cFileName);
if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
listFiles(sPath, files);
} else{
files.push_back(sPath);
}
}
while(FindNextFile(hFind, &fdFile));
FindClose(hFind);
}
void reloadExplorer(){
SHChangeNotify(SHCNE_ASSOCCHANGED,SHCNF_IDLIST,NULL,NULL);
SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,0,NULL,SMTO_ABORTIFHUNG,1000,NULL);
}