From 933aae4fc25f007ce4ca5375be2ddf2b2bd0a191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20J=C3=B6nsson?= Date: Mon, 1 Jun 2015 18:47:24 +0200 Subject: [PATCH] Improve str2Clipboard. Make it take generic_string instead of TCHAR*, since at most callsites we already have a generic_string. Improve error handling. Depending on where we are in the function when we get an error, we need to free the memory, unlock the memory, or close the clipboard. Note that if SetClipboardData succeeds then we should not do anything more to the memory. --- PowerEditor/src/MISC/Common/Common.cpp | 52 ++++++++++++++++---------- PowerEditor/src/MISC/Common/Common.h | 2 +- PowerEditor/src/Notepad_plus.cpp | 6 +-- PowerEditor/src/Notepad_plus.h | 2 +- PowerEditor/src/NppCommands.cpp | 2 +- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index eb6e8ece..3b97a041 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -779,35 +779,49 @@ double stodLocale(const generic_string& str, _locale_t loc, size_t* idx) return ans; } -bool str2Clipboard(const TCHAR *str2cpy, HWND hwnd) +bool str2Clipboard(const generic_string &str2cpy, HWND hwnd) { - if (!str2cpy) - return false; - - int len2Allocate = lstrlen(str2cpy) + 1; - len2Allocate *= sizeof(TCHAR); - unsigned int cilpboardFormat = CF_TEXT; - - cilpboardFormat = CF_UNICODETEXT; - + int len2Allocate = (str2cpy.size() + 1) * sizeof(TCHAR); HGLOBAL hglbCopy = ::GlobalAlloc(GMEM_MOVEABLE, len2Allocate); if (hglbCopy == NULL) { return false; } - - if (!::OpenClipboard(hwnd)) //_pPublicInterface->getHSelf())) + if (!::OpenClipboard(hwnd)) + { + ::GlobalFree(hglbCopy); + ::CloseClipboard(); return false; - - ::EmptyClipboard(); - + } + if (!::EmptyClipboard()) + { + ::GlobalFree(hglbCopy); + ::CloseClipboard(); + return false; + } // Lock the handle and copy the text to the buffer. TCHAR *pStr = (TCHAR *)::GlobalLock(hglbCopy); - lstrcpy(pStr, str2cpy); + if (pStr == NULL) + { + ::GlobalUnlock(hglbCopy); + ::GlobalFree(hglbCopy); + ::CloseClipboard(); + return false; + } + _tcscpy_s(pStr, len2Allocate / sizeof(TCHAR), str2cpy.c_str()); ::GlobalUnlock(hglbCopy); - // Place the handle on the clipboard. - ::SetClipboardData(cilpboardFormat, hglbCopy); - ::CloseClipboard(); + unsigned int clipBoardFormat = CF_UNICODETEXT; + if (::SetClipboardData(clipBoardFormat, hglbCopy) == NULL) + { + ::GlobalUnlock(hglbCopy); + ::GlobalFree(hglbCopy); + ::CloseClipboard(); + return false; + } + if (!::CloseClipboard()) + { + return false; + } return true; } \ No newline at end of file diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index 22572d9c..80171755 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -198,6 +198,6 @@ generic_string stringJoin(const std::vector& strings, const gene generic_string stringTakeWhileAdmissable(const generic_string& input, const generic_string& admissable); double stodLocale(const generic_string& str, _locale_t loc, size_t* idx = NULL); -bool str2Clipboard(const TCHAR *str2cpy, HWND hwnd); +bool str2Clipboard(const generic_string &str2cpy, HWND hwnd); #endif //M30_IDE_COMMUN_H diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 7b63f764..c81d48a5 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -1933,7 +1933,7 @@ void Notepad_plus::copyMarkedLines() globalStr = currentStr; } } - str2Cliboard(globalStr.c_str()); + str2Cliboard(globalStr); } void Notepad_plus::cutMarkedLines() @@ -1953,7 +1953,7 @@ void Notepad_plus::cutMarkedLines() } } _pEditView->execute(SCI_ENDUNDOACTION); - str2Cliboard(globalStr.c_str()); + str2Cliboard(globalStr); } void Notepad_plus::deleteMarkedLines(bool isMarked) @@ -4555,7 +4555,7 @@ void Notepad_plus::getCurrentOpenedFiles(Session & session, bool includUntitledD _invisibleEditView.execute(SCI_SETDOCPOINTER, 0, oldDoc); } -bool Notepad_plus::str2Cliboard(const TCHAR *str2cpy) +bool Notepad_plus::str2Cliboard(const generic_string & str2cpy) { return str2Clipboard(str2cpy, _pPublicInterface->getHSelf()); } diff --git a/PowerEditor/src/Notepad_plus.h b/PowerEditor/src/Notepad_plus.h index f0d4d607..d209080f 100644 --- a/PowerEditor/src/Notepad_plus.h +++ b/PowerEditor/src/Notepad_plus.h @@ -599,7 +599,7 @@ private: void doSynScorll(HWND hW); void setWorkingDir(const TCHAR *dir); - bool str2Cliboard(const TCHAR *str2cpy); + bool str2Cliboard(const generic_string & str2cpy); bool bin2Cliboard(const UCHAR *uchar2cpy, size_t length); bool getIntegralDockingData(tTbData & dockData, int & iCont, bool & isVisible); diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index a75a8186..2df44ee7 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -676,7 +676,7 @@ void Notepad_plus::command(int id) { generic_string dir(buf->getFullPathName()); PathRemoveFileSpec(dir); - str2Cliboard(dir.c_str()); + str2Cliboard(dir); } else if (id == IDM_EDIT_FILENAMETOCLIP) {