2018-11-23 04:53:32 +08:00
|
|
|
#include "util.h"
|
2018-11-28 04:54:04 +08:00
|
|
|
#include "types.h"
|
|
|
|
#include <Psapi.h>
|
2018-11-23 04:53:32 +08:00
|
|
|
|
|
|
|
namespace Util
|
|
|
|
{
|
2018-12-23 02:05:01 +08:00
|
|
|
std::optional<std::wstring> GetModuleFilename(DWORD processId, HMODULE module)
|
2018-11-28 04:54:04 +08:00
|
|
|
{
|
2019-01-20 22:52:35 +08:00
|
|
|
std::vector<wchar_t> buffer(MAX_PATH);
|
|
|
|
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
|
2018-11-28 04:54:04 +08:00
|
|
|
if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-12-23 02:05:01 +08:00
|
|
|
std::optional<std::wstring> GetModuleFilename(HMODULE module)
|
2018-11-28 04:54:04 +08:00
|
|
|
{
|
|
|
|
std::vector<wchar_t> buffer(MAX_PATH);
|
2019-01-10 11:35:01 +08:00
|
|
|
if (GetModuleFileNameW(module, buffer.data(), MAX_PATH)) return buffer.data();
|
|
|
|
return {};
|
2018-11-28 04:54:04 +08:00
|
|
|
}
|
|
|
|
|
2018-11-23 04:53:32 +08:00
|
|
|
std::optional<std::wstring> GetClipboardText()
|
|
|
|
{
|
|
|
|
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {};
|
|
|
|
if (!OpenClipboard(NULL)) return {};
|
|
|
|
|
2019-01-20 22:52:35 +08:00
|
|
|
std::optional<std::wstring> text;
|
|
|
|
if (AutoHandle<Functor<GlobalUnlock>> clipboard = GetClipboardData(CF_UNICODETEXT)) text = (wchar_t*)GlobalLock(clipboard);
|
2018-11-23 04:53:32 +08:00
|
|
|
CloseClipboard();
|
2019-01-20 22:52:35 +08:00
|
|
|
return text;
|
2018-11-23 04:53:32 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 05:23:41 +08:00
|
|
|
std::optional<std::wstring> StringToWideString(std::string text, UINT encoding)
|
2018-11-23 04:53:32 +08:00
|
|
|
{
|
2018-11-26 05:23:41 +08:00
|
|
|
std::vector<wchar_t> buffer(text.size() + 1);
|
|
|
|
if (MultiByteToWideChar(encoding, 0, text.c_str(), -1, buffer.data(), buffer.size())) return buffer.data();
|
2019-01-10 11:35:01 +08:00
|
|
|
return {};
|
2018-11-23 04:53:32 +08:00
|
|
|
}
|
2018-11-23 05:02:45 +08:00
|
|
|
|
|
|
|
bool RemoveRepetition(std::wstring& text)
|
|
|
|
{
|
|
|
|
wchar_t* end = text.data() + text.size();
|
|
|
|
for (int len = text.size() / 3; len > 6; --len)
|
|
|
|
if (wcsncmp(end - len * 3, end - len * 2, len) == 0 && wcsncmp(end - len * 3, end - len * 1, len) == 0)
|
2019-01-20 22:52:35 +08:00
|
|
|
return RemoveRepetition(text = end - len), true;
|
2018-11-23 05:02:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-19 05:55:07 +08:00
|
|
|
}
|