From fd23a292afc4aa760a0ff8ab646eebe5942fb06d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 21 Feb 2018 18:36:16 +0300 Subject: ICQ: C++'11 iterators --- protocols/IcqOscarJ/src/cookies.cpp | 39 +++++++------- protocols/IcqOscarJ/src/db.cpp | 51 ------------------- protocols/IcqOscarJ/src/fam_01service.cpp | 5 +- protocols/IcqOscarJ/src/icq_avatar.cpp | 3 +- protocols/IcqOscarJ/src/icq_direct.cpp | 70 ++++++++++++-------------- protocols/IcqOscarJ/src/icq_menu.cpp | 6 +-- protocols/IcqOscarJ/src/icq_proto.cpp | 4 +- protocols/IcqOscarJ/src/icq_proto.h | 7 --- protocols/IcqOscarJ/src/icq_rates.cpp | 4 +- protocols/IcqOscarJ/src/oscar_filetransfer.cpp | 6 +-- protocols/IcqOscarJ/src/utilities.cpp | 20 +++----- 11 files changed, 69 insertions(+), 146 deletions(-) (limited to 'protocols') diff --git a/protocols/IcqOscarJ/src/cookies.cpp b/protocols/IcqOscarJ/src/cookies.cpp index 4e0c8bada0..51004e0915 100644 --- a/protocols/IcqOscarJ/src/cookies.cpp +++ b/protocols/IcqOscarJ/src/cookies.cpp @@ -101,13 +101,12 @@ int CIcqProto::FindCookieByData(void *pvExtra, DWORD *pdwCookie, MCONTACT *phCon { mir_cslock l(cookieMutex); - for (int i = 0; i < cookies.getCount(); i++) { - icq_cookie_info &cookie = cookies[i]; - if (pvExtra == cookie.pvExtra) { + for (auto &it : cookies) { + if (pvExtra == it->pvExtra) { if (phContact) - *phContact = cookie.hContact; + *phContact = it->hContact; if (pdwCookie) - *pdwCookie = cookie.dwCookie; + *pdwCookie = it->dwCookie; // Cookie found return 1; @@ -121,16 +120,14 @@ int CIcqProto::FindCookieByType(BYTE bType, DWORD *pdwCookie, MCONTACT *phContac { mir_cslock l(cookieMutex); - for (int i = 0; i < cookies.getCount(); i++) { - icq_cookie_info &cookie = cookies[i]; - - if (bType == cookie.bType) { + for (auto &it : cookies) { + if (bType == it->bType) { if (pdwCookie) - *pdwCookie = cookie.dwCookie; + *pdwCookie = it->dwCookie; if (phContact) - *phContact = cookie.hContact; + *phContact = it->hContact; if (ppvExtra) - *ppvExtra = cookie.pvExtra; + *ppvExtra = it->pvExtra; // Cookie found return 1; @@ -144,17 +141,16 @@ int CIcqProto::FindMessageCookie(DWORD dwMsgID1, DWORD dwMsgID2, DWORD *pdwCooki { mir_cslock l(cookieMutex); - for (int i = 0; i < cookies.getCount(); i++) { - icq_cookie_info &cookie = cookies[i]; - if (cookie.bType == CKT_MESSAGE || cookie.bType == CKT_FILE || cookie.bType == CKT_REVERSEDIRECT) { + for (auto &it : cookies) { + if (it->bType == CKT_MESSAGE || it->bType == CKT_FILE || it->bType == CKT_REVERSEDIRECT) { // message cookie found - cookie_message_data *pCookie = (cookie_message_data*)cookie.pvExtra; + cookie_message_data *pCookie = (cookie_message_data*)it->pvExtra; if (pCookie->dwMsgID1 == dwMsgID1 && pCookie->dwMsgID2 == dwMsgID2) { if (phContact) - *phContact = cookie.hContact; + *phContact = it->hContact; if (pdwCookie) - *pdwCookie = cookie.dwCookie; + *pdwCookie = it->dwCookie; if (ppvExtra) *ppvExtra = pCookie; @@ -182,11 +178,10 @@ void CIcqProto::FreeCookieByData(BYTE bType, void *pvExtra) { mir_cslock l(cookieMutex); - for (int i = 0; i < cookies.getCount(); i++) { - icq_cookie_info &cookie = cookies[i]; - if (bType == cookie.bType && pvExtra == cookie.pvExtra) { + for (auto &it : cookies) { + if (bType == it->bType && pvExtra == it->pvExtra) { // Cookie found, remove from list - cookies.remove(i); + cookies.remove(it); break; } } diff --git a/protocols/IcqOscarJ/src/db.cpp b/protocols/IcqOscarJ/src/db.cpp index 5daf759ed6..105566c102 100644 --- a/protocols/IcqOscarJ/src/db.cpp +++ b/protocols/IcqOscarJ/src/db.cpp @@ -111,57 +111,6 @@ void ChangeInfoData::ClearChangeFlags(void) settingData[i].changed = 0; } -///////////////////////////////////////////////////////////////////////////////////////// - -struct PwConfirmDlgParam -{ - CIcqProto* ppro; - char* Pass; -}; - -static INT_PTR CALLBACK PwConfirmDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) -{ - PwConfirmDlgParam* dat = (PwConfirmDlgParam*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); - - switch (msg) { - case WM_INITDIALOG: - TranslateDialogDefault(hwndDlg); - SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam); - SendDlgItemMessage(hwndDlg, IDC_PASSWORD, EM_LIMITTEXT, PASSWORDMAXLEN, 0); - return TRUE; - - case WM_COMMAND: - switch (LOWORD(wParam)) { - case IDOK: - { - char szTest[16]; - - GetDlgItemTextA(hwndDlg, IDC_OLDPASS, szTest, _countof(szTest)); - - if (mir_strcmp(szTest, dat->ppro->GetUserPassword(TRUE))) { - MessageBox(hwndDlg, TranslateT("The password does not match your current password. Check Caps Lock and try again."), TranslateT("Change ICQ Details"), MB_OK); - SendDlgItemMessage(hwndDlg, IDC_OLDPASS, EM_SETSEL, 0, (LPARAM)-1); - SetFocus(GetDlgItem(hwndDlg, IDC_OLDPASS)); - break; - } - - GetDlgItemTextA(hwndDlg, IDC_PASSWORD, szTest, _countof(szTest)); - if (mir_strcmp(szTest, dat->Pass)) { - MessageBox(hwndDlg, TranslateT("The password does not match the password you originally entered. Check Caps Lock and try again."), TranslateT("Change ICQ Details"), MB_OK); - SendDlgItemMessage(hwndDlg, IDC_PASSWORD, EM_SETSEL, 0, (LPARAM)-1); - SetFocus(GetDlgItem(hwndDlg, IDC_PASSWORD)); - break; - } - } - case IDCANCEL: - EndDialog(hwndDlg, wParam); - break; - } - break; - } - return FALSE; -} - int ChangeInfoData::SaveSettingsToDb() { int ret = 1; diff --git a/protocols/IcqOscarJ/src/fam_01service.cpp b/protocols/IcqOscarJ/src/fam_01service.cpp index ad6ff78bd3..d6d6e821dc 100644 --- a/protocols/IcqOscarJ/src/fam_01service.cpp +++ b/protocols/IcqOscarJ/src/fam_01service.cpp @@ -661,9 +661,8 @@ void CIcqProto::setUserInfo() db_free(&dbv); } - if (CustomCapList.getCount()) - for (int i = 0; i < CustomCapList.getCount(); i++) - packBuffer(&packet, (PBYTE)CustomCapList[i].caps, 0x10); + for (auto &it : CustomCapList) + packBuffer(&packet, (PBYTE)it->caps, 0x10); sendServPacket(&packet); } diff --git a/protocols/IcqOscarJ/src/icq_avatar.cpp b/protocols/IcqOscarJ/src/icq_avatar.cpp index 211546a757..28ef778680 100644 --- a/protocols/IcqOscarJ/src/icq_avatar.cpp +++ b/protocols/IcqOscarJ/src/icq_avatar.cpp @@ -622,8 +622,7 @@ int CIcqProto::SetAvatarData(MCONTACT hContact, WORD wRef, const BYTE *data, siz // we failed to send request, or avatar thread not ready // check if any request for this user is not already in the queue - for (int i = 0; i < m_arAvatars.getCount(); i++) { - avatars_request *ar = m_arAvatars[i]; + for (auto ar : m_arAvatars) { if (ar->hContact == hContact && ar->type == ART_UPLOAD) { // we found it, return error alck.unlock(); debugLogA("Avatars: Ignoring duplicate upload avatar request."); diff --git a/protocols/IcqOscarJ/src/icq_direct.cpp b/protocols/IcqOscarJ/src/icq_direct.cpp index 345bf763a6..3ecbbe8d92 100644 --- a/protocols/IcqOscarJ/src/icq_direct.cpp +++ b/protocols/IcqOscarJ/src/icq_direct.cpp @@ -48,11 +48,11 @@ void CIcqProto::CloseContactDirectConns(MCONTACT hContact) { mir_cslock l(directConnListMutex); - for (int i = 0; i < directConns.getCount(); i++) { - if (!hContact || directConns[i]->hContact == hContact) { - HNETLIBCONN hConnection = directConns[i]->hConnection; + for (auto &it : directConns) { + if (!hContact || it->hContact == hContact) { + HNETLIBCONN hConnection = it->hConnection; - directConns[i]->hConnection = nullptr; // do not allow reuse + it->hConnection = nullptr; // do not allow reuse NetLib_CloseConnection(&hConnection, FALSE); } } @@ -61,34 +61,27 @@ void CIcqProto::CloseContactDirectConns(MCONTACT hContact) directconnect* CIcqProto::FindFileTransferDC(filetransfer* ft) { - directconnect* dc = nullptr; mir_cslock l(directConnListMutex); - for (int i = 0; i < directConns.getCount(); i++) { - if (directConns[i]->ft == ft) { - dc = directConns[i]; - break; - } - } + for (auto &it : directConns) + if (it->ft == ft) + return it; - return dc; + return nullptr; } filetransfer* CIcqProto::FindExpectedFileRecv(DWORD dwUin, DWORD dwTotalSize) { - filetransfer* pFt = nullptr; mir_cslock l(expectedFileRecvMutex); - for (int i = 0; i < expectedFileRecvs.getCount(); i++) { - if (expectedFileRecvs[i]->dwUin == dwUin && expectedFileRecvs[i]->dwTotalSize == dwTotalSize) { - pFt = expectedFileRecvs[i]; - expectedFileRecvs.remove(i); - break; + for (auto &it : expectedFileRecvs) + if (it->dwUin == dwUin && it->dwTotalSize == dwTotalSize) { + expectedFileRecvs.remove(it); + return it; } - } - return pFt; + return nullptr; } @@ -123,22 +116,21 @@ directthreadstartinfo* CreateDTSI(MCONTACT hContact, HNETLIBCONN hConnection, in BOOL CIcqProto::IsDirectConnectionOpen(MCONTACT hContact, int type, int bPassive) { BOOL bIsOpen = FALSE, bIsCreated = FALSE; - { mir_cslock l(directConnListMutex); - for (int i = 0; i < directConns.getCount(); i++) { - if (directConns[i] && (directConns[i]->type == type)) { - if (directConns[i]->hContact == hContact) - if (directConns[i]->initialised) { - // Connection is OK - bIsOpen = TRUE; - // we are going to use the conn, so prevent timeout - directConns[i]->packetPending = 1; - break; + for (auto &it : directConns) { + if (it && it->type == type) { + if (it->hContact == hContact) { + if (it->initialised) { + // Connection is OK + bIsOpen = TRUE; + // we are going to use the conn, so prevent timeout + it->packetPending = 1; + break; } - else - bIsCreated = TRUE; // we found pending connection + else bIsCreated = TRUE; // we found pending connection + } } } } @@ -823,20 +815,20 @@ int CIcqProto::SendDirectMessage(MCONTACT hContact, icq_packet *pkt) { mir_cslock l(directConnListMutex); - for (int i = 0; i < directConns.getCount(); i++) { - if (directConns[i] == nullptr) + for (auto &it : directConns) { + if (it == nullptr) continue; - if (directConns[i]->hContact == hContact) { - if (directConns[i]->initialised) { + if (it->hContact == hContact) { + if (it->initialised) { // This connection can be reused, send packet and exit NetLog_Direct("Sending direct message"); if (pkt->pData[2] == 2) - EncryptDirectPacket(directConns[i], pkt); + EncryptDirectPacket(it, pkt); - sendDirectPacket(directConns[i], pkt); - directConns[i]->packetPending = 0; // packet done + sendDirectPacket(it, pkt); + it->packetPending = 0; // packet done return TRUE; // Success } diff --git a/protocols/IcqOscarJ/src/icq_menu.cpp b/protocols/IcqOscarJ/src/icq_menu.cpp index f4450d0095..97fa93ab52 100644 --- a/protocols/IcqOscarJ/src/icq_menu.cpp +++ b/protocols/IcqOscarJ/src/icq_menu.cpp @@ -43,9 +43,9 @@ static CIcqProto* IcqGetInstanceByHContact(MCONTACT hContact) if (szProto == nullptr) return nullptr; - for (int i = 0; i < g_Instances.getCount(); i++) - if (!mir_strcmp(szProto, g_Instances[i]->m_szModuleName)) - return g_Instances[i]; + for (auto &it : g_Instances) + if (!mir_strcmp(szProto, it->m_szModuleName)) + return it; return nullptr; } diff --git a/protocols/IcqOscarJ/src/icq_proto.cpp b/protocols/IcqOscarJ/src/icq_proto.cpp index 8cc92b3f96..16efe23929 100644 --- a/protocols/IcqOscarJ/src/icq_proto.cpp +++ b/protocols/IcqOscarJ/src/icq_proto.cpp @@ -208,8 +208,8 @@ CIcqProto::~CIcqProto() SAFE_FREE((void**)&servlistQueueList); // Finalize avatars - for (int i = 0; i < m_arAvatars.getCount(); i++) - delete m_arAvatars[i]; + for (auto &it : m_arAvatars) + delete it; // NetLib clean-up Netlib_CloseHandle(m_hDirectNetlibUser); diff --git a/protocols/IcqOscarJ/src/icq_proto.h b/protocols/IcqOscarJ/src/icq_proto.h index 1c46ce3b3b..5599fc409e 100644 --- a/protocols/IcqOscarJ/src/icq_proto.h +++ b/protocols/IcqOscarJ/src/icq_proto.h @@ -122,18 +122,11 @@ struct CIcqProto : public PROTO INT_PTR __cdecl OnCreateAccMgrUI(WPARAM, LPARAM); //====| Events |====================================================================== - void __cdecl OnAddContactForever(DBCONTACTWRITESETTING *cws, MCONTACT hContact); int __cdecl OnIdleChanged(WPARAM, LPARAM); int __cdecl OnModulesLoaded(WPARAM, LPARAM); int __cdecl OnOptionsInit(WPARAM, LPARAM); int __cdecl OnPreShutdown(WPARAM, LPARAM); int __cdecl OnPreBuildContactMenu(WPARAM, LPARAM); - int __cdecl OnMsgUserTyping(WPARAM, LPARAM); - int __cdecl OnProcessSrmmIconClick(WPARAM, LPARAM); - int __cdecl OnProcessSrmmEvent(WPARAM, LPARAM); - int __cdecl OnReloadIcons(WPARAM, LPARAM); - void __cdecl OnRenameContact(DBCONTACTWRITESETTING *cws, MCONTACT hContact); - void __cdecl OnRenameGroup( DBCONTACTWRITESETTING *cws, MCONTACT hContact); int __cdecl OnUserInfoInit(WPARAM, LPARAM); int __cdecl OnPreBuildStatusMenu(WPARAM, LPARAM); diff --git a/protocols/IcqOscarJ/src/icq_rates.cpp b/protocols/IcqOscarJ/src/icq_rates.cpp index 17b4db82cd..b2d3ee2823 100644 --- a/protocols/IcqOscarJ/src/icq_rates.cpp +++ b/protocols/IcqOscarJ/src/icq_rates.cpp @@ -325,8 +325,8 @@ void rates_queue::handleDelay() void rates_queue::cleanup() { mir_cslock l(csLists); - for (int i = 0; i < lstPending.getCount(); i++) - delete lstPending[i]; + for (auto &it : lstPending) + delete it; lstPending.destroy(); } diff --git a/protocols/IcqOscarJ/src/oscar_filetransfer.cpp b/protocols/IcqOscarJ/src/oscar_filetransfer.cpp index f4f5222df6..6da9246a8e 100644 --- a/protocols/IcqOscarJ/src/oscar_filetransfer.cpp +++ b/protocols/IcqOscarJ/src/oscar_filetransfer.cpp @@ -122,9 +122,9 @@ oscar_filetransfer* CIcqProto::FindOscarTransfer(MCONTACT hContact, DWORD dwID1, { mir_cslock l(oftMutex); - for (int i = 0; i < m_arFileTransfers.getCount(); i++) { - if (m_arFileTransfers[i]->ft_magic == FT_MAGIC_OSCAR) { - oscar_filetransfer *oft = (oscar_filetransfer*)m_arFileTransfers[i]; + for (auto &it : m_arFileTransfers) { + if (it->ft_magic == FT_MAGIC_OSCAR) { + oscar_filetransfer *oft = (oscar_filetransfer*)it; if (oft->hContact == hContact && oft->pMessage.dwMsgID1 == dwID1 && oft->pMessage.dwMsgID2 == dwID2) return oft; } diff --git a/protocols/IcqOscarJ/src/utilities.cpp b/protocols/IcqOscarJ/src/utilities.cpp index 1a22839013..db1af286c9 100644 --- a/protocols/IcqOscarJ/src/utilities.cpp +++ b/protocols/IcqOscarJ/src/utilities.cpp @@ -373,11 +373,9 @@ void CIcqProto::UninitContactsCache(void) mir_cslock l(contactsCacheMutex); // cleanup the cache - for (int i = 0; i < contactsCache.getCount(); i++) { - icq_contacts_cache *cache_item = contactsCache[i]; - - SAFE_FREE((void**)&cache_item->szUid); - SAFE_FREE((void**)&cache_item); + for (auto &it : contactsCache) { + SAFE_FREE((void**)&it->szUid); + SAFE_FREE((void**)&it); } contactsCache.destroy(); @@ -388,14 +386,12 @@ void CIcqProto::DeleteFromContactsCache(MCONTACT hContact) { mir_cslock l(contactsCacheMutex); - for (int i = 0; i < contactsCache.getCount(); i++) { - icq_contacts_cache *cache_item = contactsCache[i]; - - if (cache_item->hContact == hContact) { - contactsCache.remove(i); + for (auto &it : contactsCache) { + if (it->hContact == hContact) { + contactsCache.remove(it); // Release memory - SAFE_FREE((void**)&cache_item->szUid); - SAFE_FREE((void**)&cache_item); + SAFE_FREE((void**)&it->szUid); + SAFE_FREE((void**)&it); break; } } -- cgit v1.2.3