make StringToWideString pure

This commit is contained in:
Akash Mozumdar 2018-11-25 16:23:41 -05:00
parent 49c4af8c2c
commit c50e2992bf
4 changed files with 11 additions and 20 deletions

View File

@ -165,7 +165,7 @@ namespace
case HOST_NOTIFICATION_TEXT: case HOST_NOTIFICATION_TEXT:
{ {
auto info = *(ConsoleOutputNotif*)buffer; auto info = *(ConsoleOutputNotif*)buffer;
Host::AddConsoleOutput(Util::StringToWideString(info.message)); Host::AddConsoleOutput(Util::StringToWideString(info.message).value());
} }
break; break;
default: default:
@ -308,7 +308,7 @@ namespace Host
std::wstring GetHookName(DWORD processId, uint64_t addr) std::wstring GetHookName(DWORD processId, uint64_t addr)
{ {
LOCK(hostMutex); LOCK(hostMutex);
return Util::StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName); return Util::StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName).value();
} }
std::shared_ptr<TextThread> GetThread(ThreadParam tp) std::shared_ptr<TextThread> GetThread(ThreadParam tp)

View File

@ -1,5 +1,6 @@
#include "textthread.h" #include "textthread.h"
#include "const.h" #include "const.h"
#include "text.h"
#include "host.h" #include "host.h"
#include "util.h" #include "util.h"
@ -38,9 +39,9 @@ void TextThread::Push(const BYTE* data, int len)
{ {
if (len < 0) return; if (len < 0) return;
LOCK(bufferMutex); LOCK(bufferMutex);
buffer += hp.type & USING_UNICODE if (hp.type & USING_UNICODE) buffer += std::wstring((wchar_t*)data, len / 2);
? std::wstring((wchar_t*)data, len / 2) else if (auto converted = Util::StringToWideString(std::string((char*)data, len), hp.codepage ? hp.codepage : defaultCodepage)) buffer += converted.value();
: Util::StringToWideString(std::string((char*)data, len), hp.codepage ? hp.codepage : defaultCodepage); else Host::AddConsoleOutput(INVALID_CODEPAGE);
if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear(); if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear();
lastPushTime = GetTickCount(); lastPushTime = GetTickCount();
} }

View File

@ -1,6 +1,4 @@
#include "util.h" #include "util.h"
#include "text.h"
#include "host.h"
namespace Util namespace Util
{ {
@ -20,19 +18,11 @@ namespace Util
return {}; return {};
} }
std::wstring StringToWideString(std::string text, UINT encoding) std::optional<std::wstring> StringToWideString(std::string text, UINT encoding)
{ {
std::wstring ret(text.size() + 1, 0); std::vector<wchar_t> buffer(text.size() + 1);
if (int len = MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.size())) if (MultiByteToWideChar(encoding, 0, text.c_str(), -1, buffer.data(), buffer.size())) return buffer.data();
{ else return {};
ret.resize(len - 1);
return ret;
}
else
{
Host::AddConsoleOutput(INVALID_CODEPAGE);
return L"";
}
} }
bool RemoveRepetition(std::wstring& text) bool RemoveRepetition(std::wstring& text)

View File

@ -5,7 +5,7 @@
namespace Util namespace Util
{ {
std::optional<std::wstring> GetClipboardText(); std::optional<std::wstring> GetClipboardText();
std::wstring StringToWideString(std::string text, UINT encoding = CP_UTF8); std::optional<std::wstring> StringToWideString(std::string text, UINT encoding = CP_UTF8);
// return true if repetition found (see https://github.com/Artikash/Textractor/issues/40) // return true if repetition found (see https://github.com/Artikash/Textractor/issues/40)
bool RemoveRepetition(std::wstring& text); bool RemoveRepetition(std::wstring& text);
} }