From 905484a073f1c61ec4113533dddd087b68910192 Mon Sep 17 00:00:00 2001 From: CFrank Date: Tue, 24 Apr 2018 23:28:12 +0200 Subject: [PATCH 1/2] fixes issue #34 --- PythonScript/src/PythonScript.cpp | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/PythonScript/src/PythonScript.cpp b/PythonScript/src/PythonScript.cpp index e2e6025c..8d396d1f 100644 --- a/PythonScript/src/PythonScript.cpp +++ b/PythonScript/src/PythonScript.cpp @@ -621,43 +621,37 @@ static void ensurePathExists(const tstring& path) static void newScript() { - - OPENFILENAMEA ofn; - memset(&ofn, 0, sizeof(OPENFILENAMEA)); + OPENFILENAMEW ofn; + memset(&ofn, 0, sizeof(OPENFILENAMEW)); - ofn.lStructSize = sizeof(OPENFILENAMEA); + ofn.lStructSize = sizeof(OPENFILENAMEW); ofn.hwndOwner = nppData._nppHandle; ensurePathExists(ConfigFile::getInstance()->getUserScriptsDir()); - std::shared_ptr userScriptsDir = WcharMbcsConverter::tchar2char(ConfigFile::getInstance()->getUserScriptsDir().c_str()); - ofn.lpstrInitialDir = userScriptsDir.get(); - //ofn.lpstrFileTitle = "Choose filename for new script"; - ofn.lpstrFile = new char[MAX_PATH]; + + ofn.lpstrInitialDir = ConfigFile::getInstance()->getUserScriptsDir().c_str(); + ofn.lpstrFile = new TCHAR[MAX_PATH]; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = MAX_PATH; - ofn.lpstrDefExt = "py"; + ofn.lpstrDefExt = L"py"; //lint -e840 Use of nul character in a string literal // This is how it's meant to be used. - ofn.lpstrFilter = "Python Source Files (*.py)\0*.py\0All Files (*.*)\0*.*\0"; + ofn.lpstrFilter = L"Python Source Files (*.py)\0*.py\0All Files (*.*)\0*.*\0"; //lint +e840 ofn.nFilterIndex = 1; ofn.Flags = OFN_OVERWRITEPROMPT; - if (GetSaveFileNameA(&ofn)) + if (GetSaveFileNameW(&ofn)) { - - HANDLE hFile = CreateFileA(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE hFile = CreateFileW(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); CloseHandle(hFile); - SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast(WcharMbcsConverter::char2tchar(ofn.lpstrFile).get())); + SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast(ofn.lpstrFile)); intptr_t bufferID = SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0); SendMessage(nppData._nppHandle, NPPM_SETBUFFERLANGTYPE, L_PYTHON, bufferID); } - delete [] ofn.lpstrFile; - - } From 6bf33ddfaa8b76f9a6ce3835b34d9bf8e53625f1 Mon Sep 17 00:00:00 2001 From: CFrank Date: Sat, 28 Apr 2018 14:12:58 +0200 Subject: [PATCH 2/2] further changes to support unicode pathes --- PythonScript/src/MenuManager.cpp | 2 +- PythonScript/src/PythonConsole.cpp | 19 +++++++---- PythonScript/src/PythonScript.cpp | 18 ++++++----- PythonScript/src/WcharMbcsConverter.cpp | 43 +++++++++++++++++++++++++ PythonScript/src/WcharMbcsConverter.h | 2 ++ 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/PythonScript/src/MenuManager.cpp b/PythonScript/src/MenuManager.cpp index ae275df3..f57f456d 100644 --- a/PythonScript/src/MenuManager.cpp +++ b/PythonScript/src/MenuManager.cpp @@ -1041,7 +1041,7 @@ void MenuManager::updatePreviousScript(const char *filename) m_previousRunFilename = filename; tstring tdisplayName(_T("Run Previous Script (")); - tdisplayName.append(WcharMbcsConverter::char2tchar(displayName).get()); + tdisplayName.append(WcharMbcsConverter::char2acp(displayName).get()); tdisplayName.append(_T(")")); if (!m_runLastScriptShortcut.empty()) diff --git a/PythonScript/src/PythonConsole.cpp b/PythonScript/src/PythonConsole.cpp index 14810fb2..5aa85d32 100644 --- a/PythonScript/src/PythonConsole.cpp +++ b/PythonScript/src/PythonConsole.cpp @@ -121,6 +121,7 @@ void PythonConsole::initPython(PythonHandler *pythonHandler) void PythonConsole::pythonShowDialog() { assert(mp_consoleDlg); + GILRelease release; if (mp_consoleDlg) { // Post the message to ourselves (on the right thread) to create the window @@ -144,6 +145,7 @@ void PythonConsole::showDialog() assert(mp_consoleDlg); if (mp_consoleDlg) { + GILRelease release; mp_consoleDlg->doDialog(); } } @@ -153,6 +155,7 @@ void PythonConsole::hideDialog() assert(mp_consoleDlg); if (mp_consoleDlg) { + GILRelease release; mp_consoleDlg->hide(); } } @@ -162,6 +165,7 @@ void PythonConsole::message(const char *msg) assert(mp_consoleDlg); if (mp_consoleDlg) { + GILRelease release; mp_consoleDlg->writeText(strlen(msg), msg); } } @@ -171,6 +175,7 @@ void PythonConsole::clear() assert(mp_consoleDlg); if (mp_consoleDlg) { + GILRelease release; mp_consoleDlg->clearText(); } } @@ -210,14 +215,15 @@ void PythonConsole::writeError(boost::python::object text) if (PyUnicode_Check(text.ptr())) { boost::python::object utf8String(boost::python::handle(PyUnicode_AsUTF8String(text.ptr()))); - - std::string textToWrite((const char *)boost::python::extract(utf8String)); + std::string textToWrite((const char *)boost::python::extract(utf8String)); GILRelease release; mp_consoleDlg->writeError(textToWrite.size(), textToWrite.c_str()); } else { - std::string textToWrite((const char *)boost::python::extract(text.attr("__str__")())); + std::shared_ptr wacpString = WcharMbcsConverter::char2acp(boost::python::extract(text.attr("__str__")())); + std::string textToWrite(WcharMbcsConverter::tchar2char(wacpString.get()).get()); + //std::string textToWrite((const char *)boost::python::extract(text.attr("__str__")()), _len(text)); GILRelease release; mp_consoleDlg->writeError(textToWrite.size(),textToWrite.c_str()); } @@ -272,9 +278,8 @@ void PythonConsole::queueComplete() void PythonConsole::consume(std::shared_ptr statement) { - GILLock gilLock; - - bool continuePrompt = false; + bool continuePrompt = false; + GILLock gilLock; try { boost::python::object oldStdout = m_sys.attr("stdout"); @@ -327,7 +332,7 @@ void export_console() void PythonConsole::openFile(const char *filename, idx_t lineNo) { - std::shared_ptr tFilename = WcharMbcsConverter::char2tchar(filename); + std::shared_ptr tFilename = WcharMbcsConverter::char2wchar(filename); if (!SendMessage(m_hNotepad, NPPM_SWITCHTOFILE, 0, reinterpret_cast(tFilename.get()))) { SendMessage(m_hNotepad, NPPM_DOOPEN, 0, reinterpret_cast(tFilename.get())); diff --git a/PythonScript/src/PythonScript.cpp b/PythonScript/src/PythonScript.cpp index 8d396d1f..98fd459f 100644 --- a/PythonScript/src/PythonScript.cpp +++ b/PythonScript/src/PythonScript.cpp @@ -489,7 +489,9 @@ static void runScript(idx_t number) MenuManager::s_menuItemClicked = true; } - runScript(ConfigFile::getInstance()->getMenuScript(number).c_str(), false); + std::shared_ptr wscript = WcharMbcsConverter::char2tchar(ConfigFile::getInstance()->getMenuScript(number).c_str()); + std::shared_ptr script = WcharMbcsConverter::acp2char(wscript.get()); + runScript(script.get(), false); } @@ -539,9 +541,9 @@ static void runScript(const char *filename, bool synchronous, HANDLE completedEv && ((keyState[VK_SHIFT] & 0x80) == 0) && ((keyState[VK_MENU] & 0x80) == 0)) { - if (!SendMessage(nppData._nppHandle, NPPM_SWITCHTOFILE, 0, reinterpret_cast(WcharMbcsConverter::char2tchar(filename).get()))) + if (!SendMessage(nppData._nppHandle, NPPM_SWITCHTOFILE, 0, reinterpret_cast(WcharMbcsConverter::char2acp(filename).get()))) { - SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast(WcharMbcsConverter::char2tchar(filename).get())); + SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast(WcharMbcsConverter::char2acp(filename).get())); } } else @@ -621,10 +623,10 @@ static void ensurePathExists(const tstring& path) static void newScript() { - OPENFILENAMEW ofn; - memset(&ofn, 0, sizeof(OPENFILENAMEW)); + OPENFILENAME ofn; + memset(&ofn, 0, sizeof(OPENFILENAME)); - ofn.lStructSize = sizeof(OPENFILENAMEW); + ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = nppData._nppHandle; ensurePathExists(ConfigFile::getInstance()->getUserScriptsDir()); @@ -642,9 +644,9 @@ static void newScript() ofn.Flags = OFN_OVERWRITEPROMPT; - if (GetSaveFileNameW(&ofn)) + if (GetSaveFileName(&ofn)) { - HANDLE hFile = CreateFileW(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); + HANDLE hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL); CloseHandle(hFile); SendMessage(nppData._nppHandle, NPPM_DOOPEN, 0, reinterpret_cast(ofn.lpstrFile)); intptr_t bufferID = SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0); diff --git a/PythonScript/src/WcharMbcsConverter.cpp b/PythonScript/src/WcharMbcsConverter.cpp index 7adc5c12..2da3e13e 100644 --- a/PythonScript/src/WcharMbcsConverter.cpp +++ b/PythonScript/src/WcharMbcsConverter.cpp @@ -66,6 +66,49 @@ std::shared_ptr WcharMbcsConverter::wchar2char(const wchar_t* wcStr) return multiByteStr; } +std::shared_ptr WcharMbcsConverter::char2acp(const char* mbStr) +{ + + std::shared_ptr wideCharStr; + + size_t len = (size_t)MultiByteToWideChar(CP_ACP, 0, mbStr, -1, NULL, 0); + + + if (len > 0) + { + wideCharStr.reset(new wchar_t[len]); + MultiByteToWideChar(CP_ACP, 0, mbStr, -1, wideCharStr.get(), (int)len); + } + else + { + wideCharStr.reset(new wchar_t[1]); + wideCharStr.get()[0] = 0; + } + + return wideCharStr; +} + +std::shared_ptr WcharMbcsConverter::acp2char(const wchar_t* wcStr) +{ + + std::shared_ptr multiByteStr; + + size_t len = (size_t)WideCharToMultiByte(CP_ACP, 0, wcStr, -1, NULL, 0, NULL, NULL); + + if (len > 0) + { + multiByteStr.reset(new char[len]); + WideCharToMultiByte(CP_ACP, 0, wcStr, -1, multiByteStr.get(), (int)len, NULL, NULL); + } + else + { + multiByteStr.reset(new char[1]); + multiByteStr.get()[0] = 0; + } + + return multiByteStr; +} + //static boost::std::shared_ptr char2tchar(const char* mbStr); //static boost::std::shared_ptr tchar2char(const TCHAR* tStr); std::shared_ptr WcharMbcsConverter::char2tchar(const char* mbStr) diff --git a/PythonScript/src/WcharMbcsConverter.h b/PythonScript/src/WcharMbcsConverter.h index 401f7f8f..5937fa22 100644 --- a/PythonScript/src/WcharMbcsConverter.h +++ b/PythonScript/src/WcharMbcsConverter.h @@ -27,6 +27,8 @@ class WcharMbcsConverter { static std::shared_ptr char2wchar(const char* mbStr); static std::shared_ptr wchar2char(const wchar_t* wcStr); + static std::shared_ptr char2acp(const char* mbStr); + static std::shared_ptr acp2char(const wchar_t* wcStr); static std::shared_ptr char2tchar(const char* mbStr); static std::shared_ptr tchar2char(const TCHAR* tStr);