Changed osd_get_clipboard_text() to return std::string (#5615)

* Changed osd_get_clipboard_text() to return std::string

This change has only been tested on Windows.  The Un*x/Mac versions were
made blindly; they might not even build.  This needs to be checked prior
to merging.

* Fixing Mac OS X build (hopefully)
This commit is contained in:
npwoods 2019-09-11 14:16:02 -04:00 committed by R. Belmont
parent 4f8928432a
commit bc15184c90
8 changed files with 45 additions and 71 deletions

View File

@ -585,17 +585,10 @@ void natural_keyboard::post_coded(const std::string &text, const attotime &rate)
void natural_keyboard::paste()
{
// retrieve the clipboard text
char *text = osd_get_clipboard_text();
std::string text = osd_get_clipboard_text();
// was a result returned?
if (text != nullptr)
{
// post the text
post_utf8(text);
// free the string
free(text);
}
// post the text
post_utf8(text);
}

View File

@ -917,15 +917,8 @@ void mame_ui_manager::decrease_frameskip()
bool mame_ui_manager::can_paste()
{
// retrieve the clipboard text
char *text = osd_get_clipboard_text();
// free the string if allocated
if (text != nullptr)
free(text);
// did we have text?
return text != nullptr;
// check to see if the clipboard is not empty
return !osd_get_clipboard_text().empty();
}

View File

@ -54,14 +54,9 @@ int osd_setenv(const char *name, const char *value, int overwrite);
/*-----------------------------------------------------------------------------
osd_get_clipboard_text: retrieves text from the clipboard
Return value:
the returned string needs to be free-ed!
osd_get_clipboard_text: retrieves text from the clipboard
-----------------------------------------------------------------------------*/
char *osd_get_clipboard_text(void);
std::string osd_get_clipboard_text(void);
/*-----------------------------------------------------------------------------
dynamic_module: load functions from optional shared libraries

View File

@ -116,22 +116,23 @@ void osd_break_into_debugger(const char *message)
// osd_get_clipboard_text
//============================================================
char *osd_get_clipboard_text(void)
std::string osd_get_clipboard_text(void)
{
std::string result;
bool has_result = false;
OSStatus err;
PasteboardRef pasteboard_ref;
err = PasteboardCreate(kPasteboardClipboard, &pasteboard_ref);
if (err)
return nullptr;
return result;
PasteboardSynchronize(pasteboard_ref);
ItemCount item_count;
err = PasteboardGetItemCount(pasteboard_ref, &item_count);
char *result = nullptr; // core expects a malloced C string of uft8 data
for (UInt32 item_index = 1; (item_index <= item_count) && !result; item_index++)
for (UInt32 item_index = 1; (item_index <= item_count) && !has_result; item_index++)
{
PasteboardItemID item_id;
err = PasteboardGetItemIdentifier(pasteboard_ref, item_index, &item_id);
@ -144,7 +145,7 @@ char *osd_get_clipboard_text(void)
continue;
CFIndex const flavor_count = CFArrayGetCount(flavor_type_array);
for (CFIndex flavor_index = 0; (flavor_index < flavor_count) && !result; flavor_index++)
for (CFIndex flavor_index = 0; (flavor_index < flavor_count) && !has_result; flavor_index++)
{
CFStringRef const flavor_type = (CFStringRef)CFArrayGetValueAtIndex(flavor_type_array, flavor_index);
@ -171,12 +172,9 @@ char *osd_get_clipboard_text(void)
CFIndex const length = CFDataGetLength(data_ref);
CFRange const range = CFRangeMake(0, length);
result = reinterpret_cast<char *>(malloc(length + 1));
if (result)
{
CFDataGetBytes(data_ref, range, reinterpret_cast<unsigned char *>(result));
result[length] = 0;
}
result.resize(length);
CFDataGetBytes(data_ref, range, reinterpret_cast<unsigned char *>(&result[0]));
has_result = true;
CFRelease(data_ref);
}

View File

@ -100,24 +100,23 @@ void osd_break_into_debugger(const char *message)
}
#ifdef SDLMAME_ANDROID
char *osd_get_clipboard_text(void)
std::string osd_get_clipboard_text(void)
{
return nullptr;
return std::string();
}
#else
//============================================================
// osd_get_clipboard_text
//============================================================
char *osd_get_clipboard_text(void)
std::string osd_get_clipboard_text(void)
{
char *result = nullptr;
std::string result;
if (SDL_HasClipboardText())
{
char *temp = SDL_GetClipboardText();
result = (char *) malloc(strlen(temp) + 1);
strcpy(result, temp);
result.assign(temp);
SDL_free(temp);
}
return result;

View File

@ -129,7 +129,7 @@ void osd_break_into_debugger(const char *message)
// get_clipboard_text_by_format
//============================================================
static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LPCVOID data))
static bool get_clipboard_text_by_format(std::string &result_text, UINT format, std::string (*convert)(LPCVOID data))
{
DataPackageView^ dataPackageView;
IAsyncOperation<String^>^ getTextOp;
@ -139,7 +139,8 @@ static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LP
getTextOp = dataPackageView->GetTextAsync();
clipboardText = getTextOp->GetResults();
return (char *)convert(clipboardText->Data()).c_str();
result_text = convert(clipboardText->Data());
return !result_text.empty();
}
//============================================================
@ -164,14 +165,16 @@ static std::string convert_ansi(LPCVOID data)
// osd_get_clipboard_text
//============================================================
char *osd_get_clipboard_text(void)
std::string osd_get_clipboard_text(void)
{
// try to access unicode text
char *result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide);
std::string result;
// try to access ANSI text
if (result == nullptr)
result = get_clipboard_text_by_format(CF_TEXT, convert_ansi);
// try to access unicode text
if (!get_clipboard_text_by_format(result, CF_UNICODETEXT, convert_wide))
{
// try to access ANSI text
get_clipboard_text_by_format(result, CF_TEXT, convert_ansi);
}
return result;
}

View File

@ -134,9 +134,9 @@ void osd_break_into_debugger(const char *message)
// get_clipboard_text_by_format
//============================================================
static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LPCVOID data))
static bool get_clipboard_text_by_format(std::string &result_text, UINT format, std::string (*convert)(LPCVOID data))
{
char *result = nullptr;
bool result = false;
HANDLE data_handle;
LPVOID data;
@ -155,12 +155,8 @@ static char *get_clipboard_text_by_format(UINT format, std::string (*convert)(LP
if (data != nullptr)
{
// invoke the convert
std::string s = (*convert)(data);
// copy the string
result = (char *) malloc(s.size() + 1);
if (result != nullptr)
memcpy(result, s.data(), (s.size() + 1) * sizeof(*result));
result_text = (*convert)(data);
result = true;
// unlock the data
GlobalUnlock(data_handle);
@ -196,14 +192,16 @@ static std::string convert_ansi(LPCVOID data)
// osd_get_clipboard_text
//============================================================
char *osd_get_clipboard_text(void)
std::string osd_get_clipboard_text(void)
{
// try to access unicode text
char *result = get_clipboard_text_by_format(CF_UNICODETEXT, convert_wide);
std::string result;
// try to access ANSI text
if (result == nullptr)
result = get_clipboard_text_by_format(CF_TEXT, convert_ansi);
// try to access unicode text
if (!get_clipboard_text_by_format(result, CF_UNICODETEXT, convert_wide))
{
// try to access ANSI text
get_clipboard_text_by_format(result, CF_TEXT, convert_ansi);
}
return result;
}

View File

@ -752,13 +752,8 @@ void osd_break_into_debugger(const char *message);
/*-----------------------------------------------------------------------------
osd_get_clipboard_text: retrieves text from the clipboard
Return value:
the returned string needs to be osd_free()-ed!
-----------------------------------------------------------------------------*/
char *osd_get_clipboard_text(void);
std::string osd_get_clipboard_text(void);
/***************************************************************************