Codechange: replace buffer + strecpy with std::string for getting clipboard contents

This commit is contained in:
Rubidium
2023-05-31 20:26:49 +02:00
committed by rubidium42
parent 35f7f7e8dc
commit d68b5c9162
5 changed files with 31 additions and 49 deletions

View File

@@ -433,26 +433,19 @@ void DetermineBasePaths(const char *exe)
}
bool GetClipboardContents(char *buffer, const char *last)
std::optional<std::string> GetClipboardContents()
{
HGLOBAL cbuf;
const char *ptr;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT)) return std::nullopt;
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
OpenClipboard(nullptr);
cbuf = GetClipboardData(CF_UNICODETEXT);
OpenClipboard(nullptr);
HGLOBAL cbuf = GetClipboardData(CF_UNICODETEXT);
ptr = (const char*)GlobalLock(cbuf);
int out_len = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)ptr, -1, buffer, (last - buffer) + 1, nullptr, nullptr);
GlobalUnlock(cbuf);
CloseClipboard();
std::string result = FS2OTTD(static_cast<LPCWSTR>(GlobalLock(cbuf)));
GlobalUnlock(cbuf);
CloseClipboard();
if (out_len == 0) return false;
} else {
return false;
}
return true;
if (result.empty()) return std::nullopt;
return result;
}