From df4bde2fb5075725f7d813f7dc4407cadb398784 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 5 Jun 2021 18:35:11 +0300 Subject: AutoShutdown: code cleaning --- plugins/AutoShutdown/src/settingsdlg.cpp | 2 +- plugins/AutoShutdown/src/shutdownsvc.cpp | 21 ++++++++-------- plugins/AutoShutdown/src/utils.cpp | 43 -------------------------------- plugins/AutoShutdown/src/utils.h | 5 ---- plugins/AutoShutdown/src/watcher.cpp | 33 +++--------------------- 5 files changed, 14 insertions(+), 90 deletions(-) diff --git a/plugins/AutoShutdown/src/settingsdlg.cpp b/plugins/AutoShutdown/src/settingsdlg.cpp index bb39c0fc18..999eeb0d98 100644 --- a/plugins/AutoShutdown/src/settingsdlg.cpp +++ b/plugins/AutoShutdown/src/settingsdlg.cpp @@ -339,7 +339,7 @@ static INT_PTR CALLBACK SettingsDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, L int len = GetWindowTextLength(hwndEdit) + 1; wchar_t *pszText = (wchar_t*)mir_alloc(len*sizeof(wchar_t)); if (pszText != nullptr && GetWindowText(hwndEdit, pszText, len + 1)) { - TrimString(pszText); + ltrimw(rtrimw(pszText)); g_plugin.setWString("Message", pszText); } mir_free(pszText); /* does NULL check */ diff --git a/plugins/AutoShutdown/src/shutdownsvc.cpp b/plugins/AutoShutdown/src/shutdownsvc.cpp index d8f6df0dac..c225d63e17 100644 --- a/plugins/AutoShutdown/src/shutdownsvc.cpp +++ b/plugins/AutoShutdown/src/shutdownsvc.cpp @@ -475,18 +475,17 @@ INT_PTR ServiceGetTypeDescription(WPARAM wParam, LPARAM lParam) /* shutdownType range check */ if (!wParam || (BYTE)wParam > SDSDT_MAX) return 0; /* select description */ - wchar_t *pszDesc = (wchar_t*)((lParam&GSTDF_LONGDESC) ? apszLong : apszShort)[wParam - 1]; - if (!(lParam&GSTDF_UNTRANSLATED)) pszDesc = TranslateW(pszDesc); + wchar_t *pszDesc = (wchar_t*)((lParam & GSTDF_LONGDESC) ? apszLong : apszShort)[wParam - 1]; + if (!(lParam & GSTDF_UNTRANSLATED)) + pszDesc = TranslateW(pszDesc); + /* convert as needed */ - if (!(lParam&GSTDF_UNICODE)) { - static char szConvBuf[128]; - char *buf = u2a(pszDesc); - if (buf == nullptr) return 0; - mir_strncpy(szConvBuf, buf, sizeof(szConvBuf)); - mir_free(buf); - return (INT_PTR)szConvBuf; - } - return (INT_PTR)pszDesc; + if (lParam & GSTDF_UNICODE) + return (INT_PTR)pszDesc; + + static char szConvBuf[128]; + mir_strncpy(szConvBuf, _T2A(pszDesc), sizeof(szConvBuf)); + return (INT_PTR)szConvBuf; } /************************* Misc ***************************************/ diff --git a/plugins/AutoShutdown/src/utils.cpp b/plugins/AutoShutdown/src/utils.cpp index aceb66cc18..4a9eb583b0 100644 --- a/plugins/AutoShutdown/src/utils.cpp +++ b/plugins/AutoShutdown/src/utils.cpp @@ -21,49 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "stdafx.h" -/************************* String *********************************/ - -// mir_free() the return value -char* u2a(const WCHAR *pszUnicode) -{ - if (pszUnicode == nullptr) - return nullptr; - - int codepage = Langpack_GetDefaultCodePage(); - - /* without WC_COMPOSITECHECK some characters might get out strange (see MS blog) */ - DWORD flags; - int cch = WideCharToMultiByte(codepage, flags = WC_COMPOSITECHECK, pszUnicode, -1, nullptr, 0, nullptr, nullptr); - if (!cch) - cch = WideCharToMultiByte(codepage, flags = 0, pszUnicode, -1, nullptr, 0, nullptr, nullptr); - if (!cch) - return nullptr; - - char *psz = (char*)mir_alloc(cch); - if (psz != nullptr && !WideCharToMultiByte(codepage, flags, pszUnicode, -1, psz, cch, nullptr, nullptr)) { - mir_free(psz); - return nullptr; - } - return psz; -} - -void TrimString(wchar_t *pszStr) -{ - wchar_t *psz, szChars[] = L" \r\n\t"; - for (int i = 0; i < _countof(szChars); ++i) { - /* trim end */ - psz = &pszStr[mir_wstrlen(pszStr) - 1]; - while (pszStr[0] && *psz == szChars[i]) { - *psz = 0; - psz = CharPrev(pszStr, psz); - } - /* trim beginning */ - for (psz = pszStr; (*psz && *psz == szChars[i]); psz = CharNext(psz)) - ; - memmove(pszStr, psz, (mir_wstrlen(psz) + 1)*sizeof(wchar_t)); - } -} - /************************* Error Output ***************************/ static void MessageBoxIndirectFree(MSGBOXPARAMSA *mbp) diff --git a/plugins/AutoShutdown/src/utils.h b/plugins/AutoShutdown/src/utils.h index 8ae39f0d9e..e64dc87086 100644 --- a/plugins/AutoShutdown/src/utils.h +++ b/plugins/AutoShutdown/src/utils.h @@ -21,11 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #pragma once -/* String */ -WCHAR *a2u(const char *pszAnsi); -char *u2a(const WCHAR *pszUnicode); -void TrimString(wchar_t *pszStr); - /* Error Output */ void ShowInfoMessage(BYTE flags,const char *pszTitle,const char *pszTextFmt,...); char* GetWinErrorDescription(DWORD dwLastError); diff --git a/plugins/AutoShutdown/src/watcher.cpp b/plugins/AutoShutdown/src/watcher.cpp index b01cb95735..f3a3f76578 100644 --- a/plugins/AutoShutdown/src/watcher.cpp +++ b/plugins/AutoShutdown/src/watcher.cpp @@ -61,33 +61,6 @@ static void __inline ShutdownAndStopWatcher(void) /************************* Msg Shutdown *******************************/ -// ppBlob might get reallocated, must have been allocated using mir_alloc() -static wchar_t* GetMessageText(BYTE **ppBlob, DWORD *pcbBlob) -{ - (*ppBlob)[*pcbBlob] = 0; - size_t cb = mir_strlen((char*)*ppBlob); - /* use Unicode data if present */ - if (*pcbBlob > (cb + 3)) { - (*ppBlob)[*pcbBlob - 1] = 0; - return (WCHAR*)&(*ppBlob)[cb]; - } - /* no Unicode data present, convert from ANSI */ - int len = MultiByteToWideChar(CP_ACP, 0, (char*)*ppBlob, -1, nullptr, 0); - if (!len) - return nullptr; - - BYTE *buf = (BYTE*)mir_realloc(*ppBlob, (*pcbBlob) + (len*sizeof(WCHAR))); - if (buf == nullptr) - return nullptr; - - *pcbBlob += len*sizeof(WCHAR); - *ppBlob = buf; - buf = &(*ppBlob)[cb]; - MultiByteToWideChar(CP_ACP, 0, (char*)*ppBlob, -1, (WCHAR*)buf, len); - ((WCHAR*)buf)[len - 1] = 0; - return (WCHAR*)buf; -} - static int MsgEventAdded(WPARAM, LPARAM hDbEvent) { if (currentWatcherType & SDWTF_MESSAGE) { @@ -99,9 +72,9 @@ static int MsgEventAdded(WPARAM, LPARAM hDbEvent) if (dbei.eventType == EVENTTYPE_MESSAGE && !(dbei.flags & DBEF_SENT)) { DBVARIANT dbv; if (!g_plugin.getWString("Message", &dbv)) { - TrimString(dbv.pwszVal); - wchar_t *pszMsg = GetMessageText(&dbei.pBlob, &dbei.cbBlob); - if (pszMsg != nullptr && wcsstr(pszMsg, dbv.pwszVal) != nullptr) + ltrimw(rtrimw(dbv.pwszVal)); + ptrW wszMsg(DbEvent_GetTextW(&dbei, CP_ACP)); + if (wszMsg != nullptr && wcsstr(wszMsg, dbv.pwszVal) != nullptr) ShutdownAndStopWatcher(); /* msg with specified text recvd */ mir_free(dbv.pwszVal); /* does NULL check */ } -- cgit v1.2.3