From bc15184c90a7eaf4cf16ea060916346e390a7c3b Mon Sep 17 00:00:00 2001 From: npwoods Date: Wed, 11 Sep 2019 14:16:02 -0400 Subject: 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) --- src/osd/modules/lib/osdlib_macosx.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/osd/modules/lib/osdlib_macosx.cpp') diff --git a/src/osd/modules/lib/osdlib_macosx.cpp b/src/osd/modules/lib/osdlib_macosx.cpp index b476261df41..372293ec1a3 100644 --- a/src/osd/modules/lib/osdlib_macosx.cpp +++ b/src/osd/modules/lib/osdlib_macosx.cpp @@ -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(malloc(length + 1)); - if (result) - { - CFDataGetBytes(data_ref, range, reinterpret_cast(result)); - result[length] = 0; - } + result.resize(length); + CFDataGetBytes(data_ref, range, reinterpret_cast(&result[0])); + has_result = true; CFRelease(data_ref); } -- cgit v1.2.3-70-g09d2