Changeset - r27479:6d2256bd62c8
[Not reviewed]
master
0 5 0
Rubidium - 17 months ago 2023-05-31 18:26:49
rubidium@openttd.org
Codechange: replace buffer + strecpy with std::string for getting clipboard contents
5 files changed with 31 insertions and 49 deletions:
0 comments (0 inline, 0 general)
src/os/macosx/macos.mm
Show inline comments
 
@@ -180,31 +180,27 @@ const char *GetCurrentLocale(const char 
 

	
 

	
 
#ifdef WITH_COCOA
 
/**
 
 * Return the contents of the clipboard (COCOA).
 
 *
 
 * @param buffer Clipboard content.
 
 * @param last The pointer to the last element of the destination buffer
 
 * @return Whether clipboard is empty or not.
 
 * @return The (optional) clipboard contents.
 
 */
 
bool GetClipboardContents(char *buffer, const char *last)
 
std::optional<std::string> GetClipboardContents()
 
{
 
	NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
 
	NSArray *types = [ NSArray arrayWithObject:NSPasteboardTypeString ];
 
	NSString *bestType = [ pb availableTypeFromArray:types ];
 

	
 
	/* Clipboard has no text data available. */
 
	if (bestType == nil) return false;
 
	if (bestType == nil) return std::nullopt;
 

	
 
	NSString *string = [ pb stringForType:NSPasteboardTypeString ];
 
	if (string == nil || [ string length ] == 0) return false;
 
	if (string == nil || [ string length ] == 0) return std::nullopt;
 

	
 
	strecpy(buffer, [ string UTF8String ], last);
 

	
 
	return true;
 
	return [ string UTF8String ];
 
}
 

	
 
/** Set the application's bundle directory.
 
 *
 
 * This is needed since OS X application bundles do not have a
 
 * current directory and the data files are 'somewhere' in the bundle.
src/os/os2/os2.cpp
Show inline comments
 
@@ -151,33 +151,31 @@ void ShowOSErrorBox(const char *buf, boo
 

	
 
	/* terminate PM env. */
 
	WinDestroyMsgQueue(hmq);
 
	WinTerminate(hab);
 
}
 

	
 
bool GetClipboardContents(char *buffer, const char *last)
 
std::optional<std::string> GetClipboardContents()
 
{
 
/* XXX -- Currently no clipboard support implemented with GCC */
 
#ifndef __INNOTEK_LIBC__
 
	HAB hab = 0;
 

	
 
	if (WinOpenClipbrd(hab))
 
	{
 
		const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
 
	if (WinOpenClipbrd(hab)) {
 
		const char *text = (const char *)WinQueryClipbrdData(hab, CF_TEXT);
 

	
 
		if (text != nullptr)
 
		{
 
			strecpy(buffer, text, last);
 
		if (text != nullptr) {
 
			std::string result = text;
 
			WinCloseClipbrd(hab);
 
			return true;
 
			return result;
 
		}
 

	
 
		WinCloseClipbrd(hab);
 
	}
 
#endif
 
	return false;
 
	return std::nullopt;
 
}
 

	
 

	
 
void OSOpenBrowser(const char *url)
 
{
 
	// stub only
src/os/unix/unix.cpp
Show inline comments
 
@@ -214,28 +214,26 @@ void ShowOSErrorBox(const char *buf, boo
 
		fmt::print(stderr, "Error: {}\n", buf);
 
	}
 
}
 
#endif
 

	
 
#ifndef WITH_COCOA
 
bool GetClipboardContents(char *buffer, const char *last)
 
std::optional<std::string> GetClipboardContents()
 
{
 
#ifdef WITH_SDL2
 
	if (SDL_HasClipboardText() == SDL_FALSE) {
 
		return false;
 
	}
 
	if (SDL_HasClipboardText() == SDL_FALSE) return std::nullopt;
 

	
 
	char *clip = SDL_GetClipboardText();
 
	if (clip != nullptr) {
 
		strecpy(buffer, clip, last);
 
		std::string result = clip;
 
		SDL_free(clip);
 
		return true;
 
		return result;
 
	}
 
#endif
 

	
 
	return false;
 
	return std::nullopt;
 
}
 
#endif
 

	
 

	
 
#if defined(__EMSCRIPTEN__)
 
void OSOpenBrowser(const char *url)
src/os/windows/win32.cpp
Show inline comments
 
@@ -430,32 +430,25 @@ void DetermineBasePaths(const char *exe)
 

	
 
	_searchpaths[SP_INSTALLATION_DIR].clear();
 
	_searchpaths[SP_APPLICATION_BUNDLE_DIR].clear();
 
}
 

	
 

	
 
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;
 
}
 

	
 

	
 
/**
 
 * Convert to OpenTTD's encoding from a wide string.
 
 * OpenTTD internal encoding is UTF8.
src/textbuf.cpp
Show inline comments
 
@@ -20,17 +20,15 @@
 
#include "safeguards.h"
 

	
 
/**
 
 * Try to retrieve the current clipboard contents.
 
 *
 
 * @note OS-specific function.
 
 * @param buffer Clipboard content.
 
 * @param last The pointer to the last element of the destination buffer
 
 * @return True if some text could be retrieved.
 
 * @return The (optional) clipboard contents.
 
 */
 
bool GetClipboardContents(char *buffer, const char *last);
 
std::optional<std::string> GetClipboardContents();
 

	
 
int _caret_timer;
 

	
 

	
 
/**
 
 * Checks if it is possible to delete a character.
 
@@ -220,17 +218,16 @@ bool Textbuf::InsertString(const char *s
 
 * and append this up to the maximum length (either absolute or screenlength). If maxlength
 
 * is zero, we don't care about the screenlength but only about the physical length of the string
 
 * @return true on successful change of Textbuf, or false otherwise
 
 */
 
bool Textbuf::InsertClipboard()
 
{
 
	char utf8_buf[512];
 
	auto contents = GetClipboardContents();
 
	if (!contents.has_value()) return false;
 

	
 
	if (!GetClipboardContents(utf8_buf, lastof(utf8_buf))) return false;
 

	
 
	return this->InsertString(utf8_buf, false);
 
	return this->InsertString(contents.value().c_str(), false);
 
}
 

	
 
/**
 
 * Delete a part of the text.
 
 * @param from Start of the text to delete.
 
 * @param to End of the text to delete.
0 comments (0 inline, 0 general)