From 6e06694f2fdda33eb3237fe1a3aa7f6df94ad61c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 21 Dec 2015 14:52:52 +0000 Subject: - more effective binary search of MUUIDs; - fix to prevent a loop creation if one forgets about UNSET_UID; git-svn-id: http://svn.miranda-ng.org/main/trunk@15926 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- include/newpluginapi.h | 7 +++++ src/mir_app/src/menu_utils.cpp | 64 +++++++++++++++++++++--------------------- src/mir_core/src/utils.cpp | 2 +- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/include/newpluginapi.h b/include/newpluginapi.h index e02d454caf..1226df7ccc 100644 --- a/include/newpluginapi.h +++ b/include/newpluginapi.h @@ -49,6 +49,13 @@ struct MUUID unsigned char d[8]; }; +__forceinline bool operator==(const MUUID &p1, const MUUID &p2) +{ return memcmp(&p1, &p2, sizeof(MUUID)) == 0; +} +__forceinline bool operator!=(const MUUID &p1, const MUUID &p2) +{ return memcmp(&p1, &p2, sizeof(MUUID)) != 0; +} + MIR_APP_DLL(int) GetPluginLangId(const MUUID &uuid, int hLangpack); MIR_APP_DLL(int) IsPluginLoaded(const MUUID &uuid); diff --git a/src/mir_app/src/menu_utils.cpp b/src/mir_app/src/menu_utils.cpp index 3d6d498883..5572157cad 100644 --- a/src/mir_app/src/menu_utils.cpp +++ b/src/mir_app/src/menu_utils.cpp @@ -967,9 +967,7 @@ void ScheduleMenuUpdate() static int sttFindMenuItemByUid(TMO_IntMenuItem *pimi, void *pUid) { - char szUid[33]; - bin2hex(&pimi->mi.uid, sizeof(MUUID), szUid); - return !strcmp(szUid, (char*)pUid); + return 0 == memcmp(&pimi->mi.uid, pUid, sizeof(MUUID)); } int Menu_LoadFromDatabase(TMO_IntMenuItem *pimi, void *szModule) @@ -988,21 +986,25 @@ int Menu_LoadFromDatabase(TMO_IntMenuItem *pimi, void *szModule) TCHAR *ptszToken = szValue, *pDelim = _tcschr(szValue, ';'); int bVisible = true, pos = 0; TCHAR tszCustomName[201]; tszCustomName[0] = 0; - char szCustomRoot[33]; szCustomRoot[0] = 0; + MUUID customRoot = {}; for (int i = 0; i < 4; i++) { if (pDelim) *pDelim = 0; switch (i) { - case 0: bVisible = _ttoi(ptszToken); break; - case 1: pos = _ttoi(ptszToken); break; - case 2: strncpy_s(szCustomRoot, _T2A(ptszToken), _TRUNCATE); break; + case 0: bVisible = _ttoi(ptszToken); break; + case 1: pos = _ttoi(ptszToken); break; + case 2: + hex2binT(ptszToken, &customRoot, sizeof(customRoot)); + if (customRoot == pimi->mi.uid) // prevent a loop + memset(&customRoot, 0, sizeof(customRoot)); + break; } ptszToken = pDelim + 1; if ((pDelim = _tcschr(ptszToken, ';')) == NULL) { if (i == 2 && *ptszToken != 0) - _tcsncpy_s(tszCustomName, ptszToken, _TRUNCATE); break; + _tcsncpy_s(tszCustomName, ptszToken, _TRUNCATE); break; } } @@ -1017,32 +1019,30 @@ int Menu_LoadFromDatabase(TMO_IntMenuItem *pimi, void *szModule) if (tszCustomName[0]) replaceStrT(pimi->ptszCustomName, tszCustomName); - if (szCustomRoot[0]) { - char szCurrentUid[33]; - if (pimi->mi.root == NULL) - szCurrentUid[0] = 0; - else - bin2hex(&pimi->mi.root->mi.uid, sizeof(pimi->mi.root->mi.uid), szCurrentUid); + MUUID currentUid; + if (pimi->mi.root == NULL) + memset(¤tUid, 0, sizeof(currentUid)); + else + memcpy(¤tUid, &pimi->mi.root->mi.uid, sizeof(currentUid)); - if (0 != strcmp(szCurrentUid, szCustomRoot)) { // need to move menu item to another root - TMO_LinkedList *pNew; - if (szCustomRoot[0] != 0) { - TMO_IntMenuItem *p = MO_RecursiveWalkMenu(pmo->m_items.first, sttFindMenuItemByUid, &szCustomRoot); - if (p == NULL) - return NULL; - - pimi->mi.root = p; - pNew = &p->submenu; - } - else { - pimi->mi.root = NULL; - pNew = &pmo->m_items; - } - - // relink menu item - pimi->owner->remove(pimi); - pNew->insert(pimi); + if (currentUid != customRoot) { // need to move menu item to another root + TMO_LinkedList *pNew; + if (customRoot != miid_last) { + TMO_IntMenuItem *p = MO_RecursiveWalkMenu(pmo->m_items.first, sttFindMenuItemByUid, &customRoot); + if (p == NULL) + return 0; + + pimi->mi.root = p; + pNew = &p->submenu; + } + else { + pimi->mi.root = NULL; + pNew = &pmo->m_items; } + + // relink menu item + pimi->owner->remove(pimi); + pNew->insert(pimi); } return 0; diff --git a/src/mir_core/src/utils.cpp b/src/mir_core/src/utils.cpp index a2f1ab0171..d065e8fa1b 100644 --- a/src/mir_core/src/utils.cpp +++ b/src/mir_core/src/utils.cpp @@ -317,7 +317,7 @@ MIR_CORE_DLL(bool) hex2binW(const wchar_t *pSrc, void *pData, size_t len) if (pSrc == NULL || pData == NULL || len == 0) return false; - size_t bufLen = wcslen(pSrc); + size_t bufLen = wcslen(pSrc)/2; if (pSrc[bufLen * 2] != 0 || bufLen > len) return false; -- cgit v1.2.3