From c50e2992bfb3241489a1c61c6446476f8fed3e07 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sun, 25 Nov 2018 16:23:41 -0500 Subject: [PATCH] make StringToWideString pure --- GUI/host/host.cpp | 4 ++-- GUI/host/textthread.cpp | 7 ++++--- GUI/host/util.cpp | 18 ++++-------------- GUI/host/util.h | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/GUI/host/host.cpp b/GUI/host/host.cpp index b477e0c..b23e58e 100644 --- a/GUI/host/host.cpp +++ b/GUI/host/host.cpp @@ -165,7 +165,7 @@ namespace case HOST_NOTIFICATION_TEXT: { auto info = *(ConsoleOutputNotif*)buffer; - Host::AddConsoleOutput(Util::StringToWideString(info.message)); + Host::AddConsoleOutput(Util::StringToWideString(info.message).value()); } break; default: @@ -308,7 +308,7 @@ namespace Host std::wstring GetHookName(DWORD processId, uint64_t addr) { LOCK(hostMutex); - return Util::StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName); + return Util::StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName).value(); } std::shared_ptr GetThread(ThreadParam tp) diff --git a/GUI/host/textthread.cpp b/GUI/host/textthread.cpp index 43dd255..ec6e7cd 100644 --- a/GUI/host/textthread.cpp +++ b/GUI/host/textthread.cpp @@ -1,5 +1,6 @@ #include "textthread.h" #include "const.h" +#include "text.h" #include "host.h" #include "util.h" @@ -38,9 +39,9 @@ void TextThread::Push(const BYTE* data, int len) { if (len < 0) return; LOCK(bufferMutex); - buffer += hp.type & USING_UNICODE - ? std::wstring((wchar_t*)data, len / 2) - : Util::StringToWideString(std::string((char*)data, len), hp.codepage ? hp.codepage : defaultCodepage); + if (hp.type & USING_UNICODE) buffer += 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(); + else Host::AddConsoleOutput(INVALID_CODEPAGE); if (std::all_of(buffer.begin(), buffer.end(), [&](wchar_t c) { return repeatingChars.count(c) > 0; })) buffer.clear(); lastPushTime = GetTickCount(); } diff --git a/GUI/host/util.cpp b/GUI/host/util.cpp index 6816f8a..ad2065e 100644 --- a/GUI/host/util.cpp +++ b/GUI/host/util.cpp @@ -1,6 +1,4 @@ #include "util.h" -#include "text.h" -#include "host.h" namespace Util { @@ -20,19 +18,11 @@ namespace Util return {}; } - std::wstring StringToWideString(std::string text, UINT encoding) + std::optional StringToWideString(std::string text, UINT encoding) { - std::wstring ret(text.size() + 1, 0); - if (int len = MultiByteToWideChar(encoding, 0, text.c_str(), -1, ret.data(), ret.size())) - { - ret.resize(len - 1); - return ret; - } - else - { - Host::AddConsoleOutput(INVALID_CODEPAGE); - return L""; - } + std::vector buffer(text.size() + 1); + if (MultiByteToWideChar(encoding, 0, text.c_str(), -1, buffer.data(), buffer.size())) return buffer.data(); + else return {}; } bool RemoveRepetition(std::wstring& text) diff --git a/GUI/host/util.h b/GUI/host/util.h index 1a3ebad..b703fa5 100644 --- a/GUI/host/util.h +++ b/GUI/host/util.h @@ -5,7 +5,7 @@ namespace Util { std::optional GetClipboardText(); - std::wstring StringToWideString(std::string text, UINT encoding = CP_UTF8); + std::optional StringToWideString(std::string text, UINT encoding = CP_UTF8); // return true if repetition found (see https://github.com/Artikash/Textractor/issues/40) bool RemoveRepetition(std::wstring& text); }