-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsz7pipeline.cpp
383 lines (320 loc) · 8.17 KB
/
sz7pipeline.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "targetver.h"
#include <memory>
#include <vector>
#include <fstream>
#include <codecvt>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#define STRICT
#define STRICT_CONST
#define STRICT_TYPED_ITEMIDS
#define NOOPENFILE
#define NOMINMAX
#include <Windows.h>
#include <Shlobj.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <strsafe.h>
#include <Unknwn.h>
#include <comdef.h>
#include <Propkey.h>
_COM_SMARTPTR_TYPEDEF(IFolderView2, __uuidof(IFolderView2));
_COM_SMARTPTR_TYPEDEF(IShellItemArray, __uuidof(IShellItemArray));
_COM_SMARTPTR_TYPEDEF(IShellItem, __uuidof(IShellItem));
#include <assert.h>
#include <tchar.h>
#pragma comment (lib, "shlwapi.lib")
#pragma comment( lib, "Rpcrt4.lib" ) // RpcStringFree
#define CPPLINQ_INLINEMETHOD __forceinline
#include "cpplinq.hpp"
#ifdef _DEBUG
#define SZ7PP_TEST 1
#endif
inline std::wstring guidToString(const GUID *pGuid)
{
std::wstring wrk;
RPC_WSTR waString = nullptr;
if (RPC_S_OK == ::UuidToString(pGuid, &waString))
{
wrk = reinterpret_cast<const WCHAR*>(waString);
if (RPC_S_OK != ::RpcStringFree(&waString))
{
assert(0);
}
}
return std::move(wrk);
}
class CComHeapDeleter
{
public:
void operator()(void* p)
{
::CoTaskMemFree(p);
}
};
class ComHeapString : public std::unique_ptr<WCHAR[], CComHeapDeleter>
{
typedef std::unique_ptr<WCHAR[], CComHeapDeleter> base;
public:
constexpr ComHeapString() noexcept
: base()
{
}
explicit ComHeapString(WCHAR *ptr) noexcept
: base(ptr)
{
}
operator const WCHAR *() const
{
return get();
}
};
//typedef std::unique_ptr<WCHAR[], CComHeapDeleter> ComHeapString;
class CCoInitialize {
public:
CCoInitialize() : m_hr(::CoInitialize(NULL))
{
if (FAILED(m_hr)) _com_raise_error(m_hr);
}
~CCoInitialize() { if (SUCCEEDED(m_hr)) ::CoUninitialize(); }
operator HRESULT() const { return m_hr; }
HRESULT m_hr;
};
inline void check(HRESULT hr)
{
if (FAILED(hr))
{
_com_issue_error(hr);
}
}
inline void checkWin32(BOOL success)
{
if (!success)
{
_com_issue_error(HRESULT_FROM_WIN32(::GetLastError()));
}
}
inline VARIANT ToVariant(int iVal)
{
VARIANT v;
v.vt = VT_I4;
v.lVal = iVal;
return v;
}
IShellBrowserPtr getForegroundShellBrowser()
{
auto hWndFg = ::GetAncestor(::GetForegroundWindow(), GA_ROOT);
if (hWndFg == nullptr)
return nullptr;
IShellWindowsPtr pShellWindows;
check(::CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pShellWindows)));
long count;
check(pShellWindows->get_Count(&count));
for (int i = 0; i < count; i++)
{
IDispatchPtr pDispatch;
check(pShellWindows->Item(ToVariant(i), &pDispatch));
{
IShellBrowserPtr pShellBrowser;
check(IUnknown_QueryService(pDispatch, SID_STopLevelBrowser, IID_PPV_ARGS(&pShellBrowser)));
HWND hwnd;
check(IUnknown_GetWindow(pShellBrowser, &hwnd));
#if SZ7PP_TEST
return pShellBrowser;
#else
if (::GetAncestor(hwnd, GA_ROOT) == hWndFg)
{
return pShellBrowser;
}
#endif
}
}
return nullptr;
}
#ifdef NDEBUG
#define debugPrint __noop
#else
void debugPrint(const TCHAR *fmt, ...)
{
TCHAR buf[512];
va_list args;
va_start(args, fmt);
size_t size = _vsnwprintf_s(buf, _countof(buf), fmt, args);
assert(size != -1);
::OutputDebugString(buf);
}
#endif
bool canConvert(LPCWSTR str)
{
BOOL UsedDefaultChar = FALSE;
checkWin32(::WideCharToMultiByte(CP_THREAD_ACP, WC_NO_BEST_FIT_CHARS, str, -1, NULL, 0, NULL, &UsedDefaultChar));
return !UsedDefaultChar;
}
class Config
{
static std::wstring getString(LPCWSTR iniFile, LPCWSTR appName, LPCWSTR key, LPCWSTR default = L"")
{
DWORD cap = 256;
auto str = std::make_unique<wchar_t[]>(cap);
do
{
auto ret = ::GetPrivateProfileString(appName, key, default, str.get(), cap, iniFile);
if (ret == cap - 1)
{
cap += 256;
str = std::make_unique<wchar_t[]>(cap);
}
else
{
break;
}
} while (true);
return str.get();
}
public:
const std::wstring m_pattern;
const std::wstring m_workDir;
const std::wstring m_filename;
const bool useShortPath = true;
public:
explicit Config(LPCWSTR iniFile, LPCWSTR sectionName = L"sz7pipeline")
: m_pattern(std::move(getString(iniFile, sectionName, L"PATTERN", L"*")))
, m_workDir(std::move(getString(iniFile, sectionName, L"PATH", L"%TEMP%")))
, m_filename(std::move(getString(iniFile, sectionName, L"FILENAME", L"")))
{
}
};
std::wofstream openText(LPCWSTR path)
{
std::wofstream stream;
stream.open(path, std::ios::out);
stream.exceptions(std::ios::badbit | std::ios::eofbit | std::ios::failbit);
stream.imbue(std::locale(std::locale(""), new std::codecvt_utf8_utf16<wchar_t, 0x10ffff, std::generate_header>()));
return stream;
}
void main(int argc, WCHAR *argv[])
{
#ifndef SZ7PP_TEST
if (argc < 2)
{
::MessageBox(nullptr, L"Shell:Sendto にショートカットを登録してください。", argv[0], MB_OK | MB_ICONINFORMATION);
return;
}
#endif
WCHAR iniFile[MAX_PATH];
wcscpy_s(iniFile, argv[0]);
::PathRenameExtension(iniFile, L".ini");
const Config config(iniFile);
using namespace std;
auto pShellBrowser = getForegroundShellBrowser();
if (!pShellBrowser)
{
::MessageBox(nullptr, L"エクスプローラーが見つかりませんでした。", argv[0], MB_OK | MB_ICONEXCLAMATION);
return;
}
IShellViewPtr pShellView;
check(pShellBrowser->QueryActiveShellView(&pShellView));
IFolderView2Ptr pFolderView = pShellView;
//IFolderViewPtr pFolderView = pShellView;
IShellFolderPtr pShellFolder;
HRESULT hr = pFolderView->GetFolder(IID_PPV_ARGS(&pShellFolder));
if (SUCCEEDED(hr))
{
WCHAR dir[MAX_PATH] = L"";
checkWin32(::ExpandEnvironmentStrings(config.m_workDir.c_str(), dir, _countof(dir)));
WCHAR fname[MAX_PATH];
if (config.m_filename.length() == 0)
{
::PathCombine(fname, dir, L"XXXXXX");
_wmktemp_s(fname, _countof(dir));
wcscat_s(fname, L".sz7");
}
else
{
::PathCombine(fname, dir, config.m_filename.c_str());
}
int selectionCount;
check(pFolderView->ItemCount(SVGIO_SELECTION, &selectionCount));
UINT flag = (selectionCount > 1) ? SVGIO_SELECTION : SVGIO_ALLVIEW;
IShellItemArrayPtr pShellItemArray;
check(pFolderView->Items(flag | SVGIO_FLAG_VIEWORDER, IID_PPV_ARGS(&pShellItemArray)));
DWORD shellItemCount;
check(pShellItemArray->GetCount(&shellItemCount));
std::vector<ComHeapString> list;
list.reserve(shellItemCount);
for (DWORD dwIdx = 0; dwIdx < shellItemCount; dwIdx++)
{
IShellItemPtr pShellItem;
check(pShellItemArray->GetItemAt(dwIdx, &pShellItem));
LPWSTR pszName = NULL;
check(pShellItem->GetDisplayName(SIGDN_FILESYSPATH, &pszName));
list.emplace_back(ComHeapString(pszName));
}
{
auto stream = openText(fname);
using namespace cpplinq;
from(list)
>> where([&](const auto &i) { return ::PathMatchSpec(i.get(), config.m_pattern.c_str()); })
>> for_each([&](const auto &i)
{
WCHAR shortPath[MAX_PATH];
LPCWSTR path = i.get();
assert(path);
if (!canConvert(path) && config.useShortPath)
{
checkWin32(::GetShortPathName(path, shortPath, _countof(shortPath)));
path = shortPath;
}
debugPrint(L"%s\n", path);
stream << path << std::endl;
});
stream.close();
}
SHELLEXECUTEINFO sei = { sizeof(sei), };
sei.lpVerb = L"open";
sei.lpFile = fname;
sei.nShow = SW_SHOWNORMAL;
::ShellExecuteEx(&sei);
}
}
std::wstring str2wstr(LPCSTR source, size_t size = _TRUNCATE, bool throwIfError = false)
{
WCHAR buf[256];
size_t conv;
auto result = mbstowcs_s(&conv, buf, source, size);
if (throwIfError && result != 0)
{
throw std::system_error(result, std::system_category());
}
return std::wstring{ buf, conv };
}
int APIENTRY _tWinMain(HINSTANCE,
HINSTANCE,
LPTSTR /*lpCmdLine*/,
int /*nCmdShow*/)
{
std::locale::global(std::locale("", LC_CTYPE));
_tsetlocale(LC_CTYPE, _T(""));
LPWSTR *szArglist;
int nArgs;
szArglist = ::CommandLineToArgvW(::GetCommandLineW(), &nArgs);
try
{
CCoInitialize init;
main(nArgs, szArglist);
}
catch (const _com_error& err)
{
::MessageBox(nullptr, err.ErrorMessage(), szArglist[0], MB_OK | MB_ICONERROR);
}
catch (const std::system_error& err)
{
auto msg = str2wstr(err.what());
::MessageBox(nullptr, msg.c_str(), szArglist[0], MB_OK | MB_ICONERROR);
}
return 0;
}