From e8f69d4e566a3e73ff656beebcae9916e9148589 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 21 Feb 2018 18:36:45 +0300 Subject: MSN: C++'11 iterators --- protocols/MSN/src/msn_avatar.cpp | 4 ++-- protocols/MSN/src/msn_chat.cpp | 32 +++++++++++++++----------------- protocols/MSN/src/msn_commands.cpp | 3 +-- protocols/MSN/src/msn_links.cpp | 12 ++++++------ protocols/MSN/src/msn_lists.cpp | 23 +++++++++++------------ protocols/MSN/src/msn_menu.cpp | 6 +++--- protocols/MSN/src/msn_srv.cpp | 7 ++----- protocols/MSN/src/msn_threads.cpp | 28 +++++++++++----------------- 8 files changed, 51 insertions(+), 64 deletions(-) (limited to 'protocols') diff --git a/protocols/MSN/src/msn_avatar.cpp b/protocols/MSN/src/msn_avatar.cpp index 83dce488a8..cf520027a7 100644 --- a/protocols/MSN/src/msn_avatar.cpp +++ b/protocols/MSN/src/msn_avatar.cpp @@ -39,8 +39,8 @@ void CMsnProto::pushAvatarRequest(MCONTACT hContact, LPCSTR pszUrl) if (pszUrl != nullptr && *pszUrl != 0) { mir_cslock lck(csAvatarQueue); - for (int i = 0; i < lsAvatarQueue.getCount(); i++) - if (lsAvatarQueue[i]->hContact == hContact) + for (auto &it : lsAvatarQueue) + if (it->hContact == hContact) return; lsAvatarQueue.insert(new AvatarQueueEntry(hContact, pszUrl)); diff --git a/protocols/MSN/src/msn_chat.cpp b/protocols/MSN/src/msn_chat.cpp index e8f8b87265..2ed9979947 100644 --- a/protocols/MSN/src/msn_chat.cpp +++ b/protocols/MSN/src/msn_chat.cpp @@ -96,12 +96,12 @@ void CMsnProto::MSN_ChatStart(ezxml_t xmli) const char *role = ezxml_txt(ezxml_child(memb, "role")); GCUserItem *gcu = nullptr; - for (int j = 0; j < info->mJoinedContacts.getCount(); j++) { - if (!mir_strcmp(info->mJoinedContacts[j]->WLID, mri)) { - gcu = info->mJoinedContacts[j]; + for (auto &it : info->mJoinedContacts) + if (!mir_strcmp(it->WLID, mri)) { + gcu = it; break; } - } + if (!gcu) { gcu = new GCUserItem; info->mJoinedContacts.insert(gcu); @@ -244,8 +244,7 @@ void CMsnProto::MSN_GCRefreshThreadsInfo(void) MCONTACT hContact; int nThreads = 0; - for (hContact = db_find_first(m_szModuleName); hContact; - hContact = db_find_next(hContact, m_szModuleName)) { + for (hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { if (isChatRoom(hContact) != 0) { DBVARIANT dbv; if (getString(hContact, "ChatRoomID", &dbv) == 0) { @@ -281,12 +280,11 @@ void CMsnProto::MSN_GCAddMessage(wchar_t *mChatID, MCONTACT hContact, char *emai static void ChatInviteUser(ThreadData *thread, GCThreadData* info, const char* wlid) { - if (info->mJoinedContacts.getCount()) { - for (int j = 0; j < info->mJoinedContacts.getCount(); j++) { - if (_stricmp(info->mJoinedContacts[j]->WLID, wlid) == 0) + if (info->mJoinedContacts.getCount()) + for (auto &it : info->mJoinedContacts) + if (!_stricmp(it->WLID, wlid)) return; - } - } + thread->sendPacketPayload("PUT", "MSGR\\THREAD", "%d:%s%suser", info->netId, info->szEmail, wlid); @@ -424,8 +422,8 @@ INT_PTR CALLBACK DlgInviteToChat(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM l ChatInviteSend(nullptr, hwndList, *cont, param->ppro); if (info) { - for (int i = 0; i < cont->getCount(); ++i) - ChatInviteUser(param->ppro->msnNsThread, info, (*cont)[i]); + for (auto &it : *cont) + ChatInviteUser(param->ppro->msnNsThread, info, it); delete cont; } else { @@ -433,9 +431,9 @@ INT_PTR CALLBACK DlgInviteToChat(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM l CMStringA buf; buf.AppendFormat("%d:%sadmin", NETID_SKYPE, param->ppro->GetMyUsername(NETID_SKYPE)); - for (int i = 0; i < cont->getCount(); ++i) { + for (auto &it : *cont) { // TODO: Add support for assigning role in invite dialog maybe? - buf.AppendFormat("%suser", (*cont)[i]); + buf.AppendFormat("%suser", it); } buf.Append(""); param->ppro->msnNsThread->sendPacketPayload("PUT", "MSGR\\THREAD", buf); @@ -462,8 +460,8 @@ int CMsnProto::MSN_GCEventHook(WPARAM, LPARAM lParam) GCThreadData* thread = MSN_GetThreadByChatId(gch->ptszID); if (thread != nullptr) { m_arGCThreads.remove(thread); - for (int i = 0; i < thread->mJoinedContacts.getCount(); i++) - delete thread->mJoinedContacts[i]; + for (auto &it : thread->mJoinedContacts) + delete it; delete thread; } } diff --git a/protocols/MSN/src/msn_commands.cpp b/protocols/MSN/src/msn_commands.cpp index 97bd3e71a9..d9c64ed438 100644 --- a/protocols/MSN/src/msn_commands.cpp +++ b/protocols/MSN/src/msn_commands.cpp @@ -946,8 +946,7 @@ LBL_InvalidCommand: /* We have to traverse the list in reverse order as newest events are on top (which is the opposite direction of Groupchat) */ LIST msgs(10,PtrKeySortT); for (ezxml_t msg = ezxml_get(xmli, "messages", 0, "message", -1); msg != nullptr; msg = ezxml_next(msg)) msgs.insert(msg, 0); - for (int i=0; i ID_STATUS_OFFLINE) { - proto = &g_Instances[i]; + for (auto &it : g_Instances) + if (it->m_iStatus > ID_STATUS_OFFLINE) { + proto = it; break; } - } - if (proto == nullptr) return 1; - + + if (proto == nullptr) + return 1; /* add a contact to the list */ if (wcsnicmp(arg, L"add?", 4) == 0) { diff --git a/protocols/MSN/src/msn_lists.cpp b/protocols/MSN/src/msn_lists.cpp index f0773df159..0953685dde 100644 --- a/protocols/MSN/src/msn_lists.cpp +++ b/protocols/MSN/src/msn_lists.cpp @@ -59,9 +59,9 @@ MsnContact* CMsnProto::Lists_Get(MCONTACT hContact) { mir_cslock lck(m_csLists); - for (int i = 0; i < m_arContacts.getCount(); ++i) - if (m_arContacts[i].hContact == hContact) - return &m_arContacts[i]; + for (auto &it : m_arContacts) + if (it->hContact == hContact) + return it; return nullptr; } @@ -346,17 +346,16 @@ static void AddPrivacyListEntries(HWND hwndList, CMsnProto *proto) } // Add new info - for (int i = 0; i < proto->m_arContacts.getCount(); ++i) { - MsnContact &cont = proto->m_arContacts[i]; - if (!(cont.list & (LIST_FL | LIST_LL))) { - cii.pszText = (wchar_t*)cont.email; + for (auto &cont : proto->m_arContacts) { + if (!(cont->list & (LIST_FL | LIST_LL))) { + cii.pszText = (wchar_t*)cont->email; hItem = (HANDLE)SendMessage(hwndList, CLM_ADDINFOITEMA, 0, (LPARAM)&cii); - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(0, (cont.list & LIST_LL) ? 1 : 0)); - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(1, (cont.list & LIST_FL) ? 2 : 0)); - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(2, (cont.list & LIST_AL) ? 3 : 0)); - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(3, (cont.list & LIST_BL) ? 4 : 0)); - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(4, (cont.list & LIST_RL) ? 5 : 0)); + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(0, (cont->list & LIST_LL) ? 1 : 0)); + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(1, (cont->list & LIST_FL) ? 2 : 0)); + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(2, (cont->list & LIST_AL) ? 3 : 0)); + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(3, (cont->list & LIST_BL) ? 4 : 0)); + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hItem, MAKELPARAM(4, (cont->list & LIST_RL) ? 5 : 0)); } } } diff --git a/protocols/MSN/src/msn_menu.cpp b/protocols/MSN/src/msn_menu.cpp index 9112c3fec0..65807adcc6 100644 --- a/protocols/MSN/src/msn_menu.cpp +++ b/protocols/MSN/src/msn_menu.cpp @@ -192,9 +192,9 @@ static CMsnProto* GetProtoInstanceByHContact(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/MSN/src/msn_srv.cpp b/protocols/MSN/src/msn_srv.cpp index 4d4a239c52..a3e5181f73 100644 --- a/protocols/MSN/src/msn_srv.cpp +++ b/protocols/MSN/src/msn_srv.cpp @@ -84,8 +84,7 @@ void CMsnProto::MSN_DeleteServerGroup(LPCSTR szId) void CMsnProto::MSN_FreeGroups(void) { - for (int i = 0; i < m_arGroups.getCount(); i++) { - ServerGroupItem* p = m_arGroups[i]; + for (auto &p : m_arGroups) { mir_free(p->id); mir_free(p->name); mir_free(p); @@ -107,11 +106,9 @@ LPCSTR CMsnProto::MSN_GetGroupById(const char* pId) LPCSTR CMsnProto::MSN_GetGroupByName(const char* pName) { - for (int i = 0; i < m_arGroups.getCount(); i++) { - const ServerGroupItem* p = m_arGroups[i]; + for (auto &p : m_arGroups) if (mir_strcmp(p->name, pName) == 0) return p->id; - } return nullptr; } diff --git a/protocols/MSN/src/msn_threads.cpp b/protocols/MSN/src/msn_threads.cpp index ff4e6a7915..c75e4b643c 100644 --- a/protocols/MSN/src/msn_threads.cpp +++ b/protocols/MSN/src/msn_threads.cpp @@ -294,16 +294,14 @@ void CMsnProto::MSN_CloseConnections(void) NETLIBSELECTEX nls = {}; - for (int i = 0; i < m_arThreads.getCount(); i++) { - ThreadData &T = m_arThreads[i]; - - switch (T.mType) { + for (auto &it : m_arThreads) { + switch (it->mType) { case SERVER_NOTIFICATION: - if (T.s != nullptr && !T.sessionClosed && !T.termPending) { - nls.hReadConns[0] = T.s; + if (it->s != nullptr && !it->sessionClosed && !it->termPending) { + nls.hReadConns[0] = it->s; int res = Netlib_SelectEx(&nls); if (res >= 0 || nls.hReadStatus[0] == 0) - T.sendTerminate(); + it->sendTerminate(); } break; } @@ -327,11 +325,9 @@ GCThreadData* CMsnProto::MSN_GetThreadByChatId(const wchar_t* chatId) return nullptr; mir_cslock lck(m_csThreads); - for (int i = 0; i < m_arGCThreads.getCount(); i++) { - GCThreadData *T = m_arGCThreads[i]; - if (mir_wstrcmpi(T->mChatID, chatId) == 0) - return T; - } + for (auto &it : m_arGCThreads) + if (mir_wstrcmpi(it->mChatID, chatId) == 0) + return it; return nullptr; } @@ -340,11 +336,9 @@ ThreadData* CMsnProto::MSN_GetThreadByConnection(HANDLE s) { mir_cslock lck(m_csThreads); - for (int i = 0; i < m_arThreads.getCount(); i++) { - ThreadData &T = m_arThreads[i]; - if (T.s == s) - return &T; - } + for (auto &it : m_arThreads) + if (it->s == s) + return it; return nullptr; } -- cgit v1.2.3