2018-11-22 15:53:32 -05:00
|
|
|
#include "util.h"
|
2018-11-27 15:54:04 -05:00
|
|
|
#include "types.h"
|
|
|
|
#include <Psapi.h>
|
2018-11-22 15:53:32 -05:00
|
|
|
|
|
|
|
namespace Util
|
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
std::optional<std::wstring> GetModuleFileName(DWORD processId, HMODULE module)
|
|
|
|
{
|
|
|
|
if (AutoHandle<> process = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, processId))
|
|
|
|
{
|
|
|
|
std::vector<wchar_t> buffer(MAX_PATH);
|
|
|
|
if (GetModuleFileNameExW(process, module, buffer.data(), MAX_PATH)) return buffer.data();
|
|
|
|
else return {};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::wstring> GetModuleFileName(HMODULE module)
|
|
|
|
{
|
|
|
|
std::vector<wchar_t> buffer(MAX_PATH);
|
|
|
|
if (::GetModuleFileNameW(module, buffer.data(), MAX_PATH)) return buffer.data();
|
|
|
|
else return {};
|
|
|
|
}
|
|
|
|
|
2018-11-22 15:53:32 -05:00
|
|
|
std::optional<std::wstring> GetClipboardText()
|
|
|
|
{
|
|
|
|
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return {};
|
|
|
|
if (!OpenClipboard(NULL)) return {};
|
|
|
|
|
2018-11-27 15:54:04 -05:00
|
|
|
if (HANDLE clipboard = GetClipboardData(CF_UNICODETEXT))
|
2018-11-22 15:53:32 -05:00
|
|
|
{
|
2018-11-27 15:54:04 -05:00
|
|
|
std::wstring ret = (wchar_t*)GlobalLock(clipboard);
|
|
|
|
GlobalUnlock(clipboard);
|
2018-11-22 15:53:32 -05:00
|
|
|
CloseClipboard();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CloseClipboard();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-11-25 16:23:41 -05:00
|
|
|
std::optional<std::wstring> StringToWideString(std::string text, UINT encoding)
|
2018-11-22 15:53:32 -05:00
|
|
|
{
|
2018-11-25 16:23:41 -05:00
|
|
|
std::vector<wchar_t> buffer(text.size() + 1);
|
|
|
|
if (MultiByteToWideChar(encoding, 0, text.c_str(), -1, buffer.data(), buffer.size())) return buffer.data();
|
|
|
|
else return {};
|
2018-11-22 15:53:32 -05:00
|
|
|
}
|
2018-11-22 16:02:45 -05: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)
|
|
|
|
return true | RemoveRepetition(text = end - len);
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-22 15:53:32 -05:00
|
|
|
}
|