-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.cpp
More file actions
152 lines (126 loc) · 4.53 KB
/
Copy pathHelper.cpp
File metadata and controls
152 lines (126 loc) · 4.53 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
#include "Helper.h"
namespace kukdh1 {
HTREEITEM AddTreeItem(HWND hTree, HTREEITEM hParent, HTREEITEM hInsertAfter, LPWSTR pszText, LPARAM lParam) {
TVINSERTSTRUCT tvis;
tvis.hParent = hParent;
tvis.hInsertAfter = hInsertAfter;
tvis.item.mask = TVIF_TEXT | TVIF_PARAM;
tvis.item.pszText = pszText;
tvis.item.lParam = lParam;
return TreeView_InsertItem(hTree, &tvis);
}
BOOL BrowseFolder(HWND hParent, LPCWSTR szTitle, LPCWSTR szStartPath, std::wstring &outFolder) {
IFileOpenDialog *pfd = nullptr;
if (FAILED(CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
return FALSE;
DWORD dwOptions = 0;
pfd->GetOptions(&dwOptions);
pfd->SetOptions(dwOptions | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
if (szTitle) pfd->SetTitle(szTitle);
if (szStartPath && szStartPath[0]) {
IShellItem *psi = nullptr;
if (SUCCEEDED(SHCreateItemFromParsingName(szStartPath, nullptr, IID_PPV_ARGS(&psi)))) {
pfd->SetFolder(psi);
psi->Release();
}
}
HRESULT hr = pfd->Show(hParent);
if (FAILED(hr)) { pfd->Release(); return FALSE; }
IShellItem *psiResult = nullptr;
hr = pfd->GetResult(&psiResult);
pfd->Release();
if (FAILED(hr)) return FALSE;
PWSTR pszPath = nullptr;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath);
psiResult->Release();
if (SUCCEEDED(hr)) {
outFolder = pszPath;
CoTaskMemFree(pszPath);
}
return SUCCEEDED(hr) ? TRUE : FALSE;
}
BOOL SaveFileDialog(HWND hParent, LPCWSTR szTitle, LPCWSTR szDefaultName,
const COMDLG_FILTERSPEC *pFilters, UINT uFilterCount,
LPCWSTR szStartPath, UINT &outFilterIndex,
std::wstring &outPath) {
IFileSaveDialog *pfd = nullptr;
if (FAILED(CoCreateInstance(CLSID_FileSaveDialog, nullptr, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pfd))))
return FALSE;
DWORD dwOptions = 0;
pfd->GetOptions(&dwOptions);
pfd->SetOptions(dwOptions | FOS_FORCEFILESYSTEM | FOS_OVERWRITEPROMPT);
if (szTitle) pfd->SetTitle(szTitle);
if (szDefaultName) pfd->SetFileName(szDefaultName);
if (pFilters && uFilterCount) {
pfd->SetFileTypes(uFilterCount, pFilters);
pfd->SetFileTypeIndex(outFilterIndex ? outFilterIndex : 1);
}
if (szStartPath && szStartPath[0]) {
IShellItem *psi = nullptr;
if (SUCCEEDED(SHCreateItemFromParsingName(szStartPath, nullptr, IID_PPV_ARGS(&psi)))) {
pfd->SetFolder(psi);
psi->Release();
}
}
HRESULT hr = pfd->Show(hParent);
if (FAILED(hr)) { pfd->Release(); return FALSE; }
UINT idx = 1;
pfd->GetFileTypeIndex(&idx);
IShellItem *psiResult = nullptr;
hr = pfd->GetResult(&psiResult);
pfd->Release();
if (FAILED(hr)) return FALSE;
PWSTR pszPath = nullptr;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath);
psiResult->Release();
if (SUCCEEDED(hr)) {
outPath = pszPath;
CoTaskMemFree(pszPath);
outFilterIndex = idx;
}
return SUCCEEDED(hr) ? TRUE : FALSE;
}
void ParsePath(std::string path, std::vector<std::string> &folders) {
std::stringstream ss(path);
folders.clear();
while (!ss.eof()) {
std::string token;
std::getline(ss, token, '/');
folders.push_back(token);
}
}
void ConvertWidechar(const std::string &in, std::wstring &out) {
int len = MultiByteToWideChar(CP_ACP, 0, in.c_str(), static_cast<int>(in.length()), nullptr, 0);
out.resize(len);
MultiByteToWideChar(CP_ACP, 0, in.c_str(), static_cast<int>(in.length()), out.data(), len);
}
void ConvertCapacity(const LARGE_INTEGER &values, std::wstring &out) {
LONGLONG qpart = values.QuadPart;
std::wstringstream wss;
int unit = 0;
while (true) {
qpart >>= 10;
if (qpart > 0) {
unit++;
}
else
break;
}
switch (unit) {
case 1:
wss << std::to_wstring(values.QuadPart / 1024.0) << L" KB (" << std::to_wstring(values.QuadPart) << " bytes)";
break;
case 2:
wss << std::to_wstring(values.QuadPart / 1024.0 / 1024.0) << L" MB (" << std::to_wstring(values.QuadPart) << " bytes)";
break;
case 3:
wss << std::to_wstring(values.QuadPart / 1024.0 / 1024.0 / 1024.0) << L" GB (" << std::to_wstring(values.QuadPart) << " bytes)";
break;
default:
wss << std::to_wstring(values.QuadPart) << " bytes";
break;
}
out = wss.str();
}
}