From 391980ce1e890445542441eeb5d9f9cc18ae1baf Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 26 Jan 2018 22:31:20 +0300 Subject: code optimization --- src/core/stdaway/src/sendmsg.cpp | 24 ++++++++++++------------ src/core/stdclist/src/clcopts.cpp | 14 +++++--------- src/core/stdclist/src/contact.cpp | 8 ++++---- src/core/stdidle/src/options.cpp | 4 ++-- src/core/stdmsg/src/msglog.cpp | 4 ++-- src/core/stdmsg/src/msgoptions.cpp | 8 ++++---- src/mir_app/src/DefaultExtraIcons.cpp | 11 +++-------- src/mir_app/src/chat_tools.cpp | 4 ++-- src/mir_app/src/clistmod.cpp | 4 ++-- src/mir_app/src/findadd.cpp | 4 ++-- src/mir_app/src/ignore.cpp | 5 +++-- src/mir_app/src/netliblog.cpp | 8 ++++---- src/mir_app/src/newplugins.cpp | 16 ++++++++-------- src/mir_app/src/proto_accs.cpp | 4 ++-- src/mir_app/src/skinicons.cpp | 6 +++--- src/mir_app/src/utils.cpp | 12 ++++++------ src/mir_app/src/visibility.cpp | 8 ++++---- 17 files changed, 68 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/core/stdaway/src/sendmsg.cpp b/src/core/stdaway/src/sendmsg.cpp index f7357e4f08..14ec7baffb 100644 --- a/src/core/stdaway/src/sendmsg.cpp +++ b/src/core/stdaway/src/sendmsg.cpp @@ -374,28 +374,28 @@ static INT_PTR CALLBACK DlgProcAwayMsgOpts(HWND hwndDlg, UINT msg, WPARAM wParam dat = (AwayMsgDlgData*)mir_alloc(sizeof(AwayMsgDlgData)); SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)dat); dat->oldPage = -1; - for (int i = 0; i < _countof(statusModes); i++) { - if (!(protoModeMsgFlags & Proto_Status2Flag(statusModes[i]))) + for (auto &it : statusModes) { + if (!(protoModeMsgFlags & Proto_Status2Flag(it))) continue; int j; if (hLst) { - j = SendDlgItemMessage(hwndDlg, IDC_LST_STATUS, LB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(statusModes[i], 0)); - SendDlgItemMessage(hwndDlg, IDC_LST_STATUS, LB_SETITEMDATA, j, statusModes[i]); + j = SendDlgItemMessage(hwndDlg, IDC_LST_STATUS, LB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(it, 0)); + SendDlgItemMessage(hwndDlg, IDC_LST_STATUS, LB_SETITEMDATA, j, it); } else { - j = SendDlgItemMessage(hwndDlg, IDC_STATUS, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(statusModes[i], 0)); - SendDlgItemMessage(hwndDlg, IDC_STATUS, CB_SETITEMDATA, j, statusModes[i]); + j = SendDlgItemMessage(hwndDlg, IDC_STATUS, CB_ADDSTRING, 0, (LPARAM)pcli->pfnGetStatusModeDescription(it, 0)); + SendDlgItemMessage(hwndDlg, IDC_STATUS, CB_SETITEMDATA, j, it); } - dat->info[j].ignore = GetStatusModeByte(statusModes[i], "Ignore"); - dat->info[j].noDialog = GetStatusModeByte(statusModes[i], "NoDlg", true); - dat->info[j].usePrevious = GetStatusModeByte(statusModes[i], "UsePrev"); + dat->info[j].ignore = GetStatusModeByte(it, "Ignore"); + dat->info[j].noDialog = GetStatusModeByte(it, "NoDlg", true); + dat->info[j].usePrevious = GetStatusModeByte(it, "UsePrev"); DBVARIANT dbv; - if (db_get_ws(NULL, "SRAway", StatusModeToDbSetting(statusModes[i], "Default"), &dbv)) - if (db_get_ws(NULL, "SRAway", StatusModeToDbSetting(statusModes[i], "Msg"), &dbv)) - dbv.ptszVal = mir_wstrdup(GetDefaultMessage(statusModes[i])); + if (db_get_ws(NULL, "SRAway", StatusModeToDbSetting(it, "Default"), &dbv)) + if (db_get_ws(NULL, "SRAway", StatusModeToDbSetting(it, "Msg"), &dbv)) + dbv.ptszVal = mir_wstrdup(GetDefaultMessage(it)); mir_wstrcpy(dat->info[j].msg, dbv.ptszVal); mir_free(dbv.ptszVal); } diff --git a/src/core/stdclist/src/clcopts.cpp b/src/core/stdclist/src/clcopts.cpp index 7b889c2a21..932e546e52 100644 --- a/src/core/stdclist/src/clcopts.cpp +++ b/src/core/stdclist/src/clcopts.cpp @@ -149,12 +149,9 @@ static INT_PTR CALLBACK DlgProcClcMainOpts(HWND hwndDlg, UINT msg, WPARAM wParam SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_HIDEOFFLINEOPTS), GWL_STYLE, GetWindowLongPtr(GetDlgItem(hwndDlg, IDC_HIDEOFFLINEOPTS), GWL_STYLE) | TVS_NOHSCROLL | TVS_CHECKBOXES); { - int i; DWORD exStyle = db_get_dw(NULL, "CLC", "ExStyle", pcli->pfnGetDefaultExStyle()); - for (i = 0; i < _countof(checkBoxToStyleEx); i++) - CheckDlgButton(hwndDlg, checkBoxToStyleEx[i].id, - (exStyle & checkBoxToStyleEx[i].flag) ^ (checkBoxToStyleEx[i].flag * - checkBoxToStyleEx[i].not) ? BST_CHECKED : BST_UNCHECKED); + for (auto &it : checkBoxToStyleEx) + CheckDlgButton(hwndDlg, it.id, (exStyle & it.flag) ^ (it.flag * it.not) ? BST_CHECKED : BST_UNCHECKED); } { UDACCEL accel[2] = { { 0, 10 }, { 2, 50 } }; @@ -242,11 +239,10 @@ static INT_PTR CALLBACK DlgProcClcMainOpts(HWND hwndDlg, UINT msg, WPARAM wParam case 0: if (((LPNMHDR)lParam)->code == PSN_APPLY) { - int i; DWORD exStyle = 0; - for (i = 0; i < _countof(checkBoxToStyleEx); i++) - if ((IsDlgButtonChecked(hwndDlg, checkBoxToStyleEx[i].id) == 0) == checkBoxToStyleEx[i].not) - exStyle |= checkBoxToStyleEx[i].flag; + for (auto &it : checkBoxToStyleEx) + if ((IsDlgButtonChecked(hwndDlg, it.id) == 0) == it.not) + exStyle |= it.flag; db_set_dw(NULL, "CLC", "ExStyle", exStyle); { diff --git a/src/core/stdclist/src/contact.cpp b/src/core/stdclist/src/contact.cpp index b3b46976a5..d03630e45b 100644 --- a/src/core/stdclist/src/contact.cpp +++ b/src/core/stdclist/src/contact.cpp @@ -44,10 +44,10 @@ static statusModeOrder[] = { int GetStatusModeOrdering(int statusMode) { - int i; - for (i = 0; i < _countof(statusModeOrder); i++) - if (statusModeOrder[i].status == statusMode) - return statusModeOrder[i].order; + for (auto &it : statusModeOrder) + if (it.status == statusMode) + return it.order; + return 1000; } diff --git a/src/core/stdidle/src/options.cpp b/src/core/stdidle/src/options.cpp index a3753db6d1..e9e670fe51 100644 --- a/src/core/stdidle/src/options.cpp +++ b/src/core/stdidle/src/options.cpp @@ -94,8 +94,8 @@ public: spinIdle.SetRange(60, 1); spinIdle.SetPosition(S.iIdleTime1st); - for (int j = 0; j < _countof(aa_Status); j++) - cmbAAStatus.AddString(pcli->pfnGetStatusModeDescription(aa_Status[j], 0)); + for (auto &it : aa_Status) + cmbAAStatus.AddString(pcli->pfnGetStatusModeDescription(it, 0)); cmbAAStatus.SetCurSel(IdleGetStatusIndex(S.bAAStatus)); ShowHide(); diff --git a/src/core/stdmsg/src/msglog.cpp b/src/core/stdmsg/src/msglog.cpp index 1ec5bbbd1c..58d6ecf2a7 100644 --- a/src/core/stdmsg/src/msglog.cpp +++ b/src/core/stdmsg/src/msglog.cpp @@ -500,6 +500,6 @@ void LoadMsgLogIcons(void) void FreeMsgLogIcons(void) { - for (int i = 0; i < _countof(pLogIconBmpBits); i++) - mir_free(pLogIconBmpBits[i]); + for (auto &it : pLogIconBmpBits) + mir_free(it); } diff --git a/src/core/stdmsg/src/msgoptions.cpp b/src/core/stdmsg/src/msgoptions.cpp index 79cae04ddc..34bb77028c 100644 --- a/src/core/stdmsg/src/msgoptions.cpp +++ b/src/core/stdmsg/src/msgoptions.cpp @@ -166,11 +166,11 @@ class COptionMainDlg : public CPluginDlgBase tvis.hParent = nullptr; tvis.hInsertAfter = TVI_LAST; tvis.item.mask = TVIF_PARAM | TVIF_TEXT | TVIF_IMAGE; - for (int i = 0; i < _countof(statusValues); i++) { - tvis.item.lParam = statusValues[i].style; - tvis.item.pszText = TranslateW(statusValues[i].szDescr); + for (auto &it : statusValues) { + tvis.item.lParam = it.style; + tvis.item.pszText = TranslateW(it.szDescr); tvis.item.stateMask = TVIS_STATEIMAGEMASK; - tvis.item.iImage = (style & statusValues[i].style) != 0; + tvis.item.iImage = (style & it.style) != 0; tree.InsertItem(&tvis); } } diff --git a/src/mir_app/src/DefaultExtraIcons.cpp b/src/mir_app/src/DefaultExtraIcons.cpp index f98f44fe11..26e9ae9d1f 100644 --- a/src/mir_app/src/DefaultExtraIcons.cpp +++ b/src/mir_app/src/DefaultExtraIcons.cpp @@ -144,9 +144,7 @@ static void SetExtraIcons(MCONTACT hContact) if ( IsEmpty(proto)) return; - for (unsigned int i = 0; i < _countof(infos); i++) { - Info &p = infos[i]; - + for (auto &p : infos) { for (unsigned int j = 0; j < _countof(p.db); j += 2) { if (p.db[j + 1] == nullptr) break; @@ -181,9 +179,7 @@ static int SettingChanged(WPARAM hContact, LPARAM lParam) return 0; } - for (int i = 0; i < _countof(infos); i++) { - Info &p = infos[i]; - + for (auto &p : infos) { for (int j = 0; j < _countof(p.db); j += 2) { if (p.db[j + 1] == nullptr) break; @@ -308,8 +304,7 @@ void DefaultExtraIcons_Load() hExtraProto = ExtraIcon_RegisterCallback("protocol", "Account", Skin_GetIconName(SKINICON_OTHER_ACCMGR), &ProtocolRebuildIcons, &ProtocolApplyIcon, &ProtocolOnClick, 0, EIF_DISABLED_BY_DEFAULT); - for (int i = 0; i < _countof(infos); i++) { - Info &p = infos[i]; + for (auto &p : infos) { p.hIcolib = Skin_GetIconHandle(p.iSkinIcon); if (p.OnClick) p.hExtraIcon = ExtraIcon_RegisterIcolib(p.name, p.desc, Skin_GetIconName(p.iSkinIcon), DefaultOnClick, (LPARAM)&p, p.flags); diff --git a/src/mir_app/src/chat_tools.cpp b/src/mir_app/src/chat_tools.cpp index 35e35a0f29..a3240c2ef3 100644 --- a/src/mir_app/src/chat_tools.cpp +++ b/src/mir_app/src/chat_tools.cpp @@ -681,8 +681,8 @@ wchar_t* GetChatLogsFilename(SESSION_INFO *si, time_t tTime) PathToAbsoluteW(tszParsedName, si->pszLogFileName); mir_free(tszParsedName); - for (int i = 0; i < _countof(rva); i++) - mir_free(rva[i].value.w); + for (auto &it : rva) + mir_free(it.value.w); for (wchar_t *p = si->pszLogFileName + 2; *p; ++p) if (*p == ':' || *p == '*' || *p == '?' || *p == '"' || *p == '<' || *p == '>' || *p == '|') diff --git a/src/mir_app/src/clistmod.cpp b/src/mir_app/src/clistmod.cpp index 30ffa63b80..2f4c7876a4 100644 --- a/src/mir_app/src/clistmod.cpp +++ b/src/mir_app/src/clistmod.cpp @@ -435,8 +435,8 @@ int LoadContactListModule2(void) ImageList_AddIcon_NotShared(hCListImages, MAKEINTRESOURCE(IDI_BLANK)); // now all core skin icons are loaded via icon lib. so lets release them - for (int i = 0; i < _countof(statusModeList); i++) - ImageList_AddIcon_IconLibLoaded(hCListImages, skinIconStatusList[i]); + for (auto &it : statusModeList) + ImageList_AddIcon_IconLibLoaded(hCListImages, it); // see IMAGE_GROUP... in clist.h if you add more images above here ImageList_AddIcon_IconLibLoaded(hCListImages, SKINICON_OTHER_GROUPOPEN); diff --git a/src/mir_app/src/findadd.cpp b/src/mir_app/src/findadd.cpp index 58132b95b9..bc1e252a0c 100644 --- a/src/mir_app/src/findadd.cpp +++ b/src/mir_app/src/findadd.cpp @@ -284,8 +284,8 @@ static const int controls[] = { IDC_BYPROTOID, IDC_BYEMAIL, IDC_BYNAME, IDC_BYAD static void CheckSearchTypeRadioButton(HWND hwndDlg, int idControl) { - for (int i = 0; i < _countof(controls); i++) - CheckDlgButton(hwndDlg, controls[i], idControl == controls[i] ? BST_CHECKED : BST_UNCHECKED); + for (auto &it : controls) + CheckDlgButton(hwndDlg, it, idControl == it ? BST_CHECKED : BST_UNCHECKED); } #define sttErrMsg TranslateT("You haven't filled in the search field. Please enter a search term and try again.") diff --git a/src/mir_app/src/ignore.cpp b/src/mir_app/src/ignore.cpp index f5dfb0f254..f0c9032a0a 100644 --- a/src/mir_app/src/ignore.cpp +++ b/src/mir_app/src/ignore.cpp @@ -314,8 +314,9 @@ static INT_PTR CALLBACK DlgProcIgnoreOpts(HWND hwndDlg, UINT msg, WPARAM, LPARAM break; case WM_DESTROY: - for (int i = 0; i < _countof(hIcons); i++) - DestroyIcon(hIcons[i]); + for (auto &it : hIcons) + DestroyIcon(it); + HIMAGELIST hIml = (HIMAGELIST)SendDlgItemMessage(hwndDlg, IDC_LIST, CLM_GETEXTRAIMAGELIST, 0, 0); ImageList_Destroy(hIml); break; diff --git a/src/mir_app/src/netliblog.cpp b/src/mir_app/src/netliblog.cpp index 226da520bf..6bb9fc32a3 100644 --- a/src/mir_app/src/netliblog.cpp +++ b/src/mir_app/src/netliblog.cpp @@ -103,10 +103,10 @@ static INT_PTR CALLBACK LogOptionsDlgProc(HWND hwndDlg, UINT message, WPARAM wPa CheckDlgButton(hwndDlg, IDC_DUMPSSL, logOptions.dumpSsl ? BST_CHECKED : BST_UNCHECKED); CheckDlgButton(hwndDlg, IDC_TEXTDUMPS, logOptions.textDumps ? BST_CHECKED : BST_UNCHECKED); CheckDlgButton(hwndDlg, IDC_AUTODETECTTEXT, logOptions.autoDetectText ? BST_CHECKED : BST_UNCHECKED); - { - for (int i=0; i < _countof(szTimeFormats); i++) - SendDlgItemMessage(hwndDlg, IDC_TIMEFORMAT, CB_ADDSTRING, 0, (LPARAM)TranslateW(szTimeFormats[i])); - } + + for (auto &it : szTimeFormats) + SendDlgItemMessage(hwndDlg, IDC_TIMEFORMAT, CB_ADDSTRING, 0, (LPARAM)TranslateW(it)); + SendDlgItemMessage(hwndDlg, IDC_TIMEFORMAT, CB_SETCURSEL, logOptions.timeFormat, 0); CheckDlgButton(hwndDlg, IDC_SHOWNAMES, logOptions.showUser ? BST_CHECKED : BST_UNCHECKED); CheckDlgButton(hwndDlg, IDC_TOOUTPUTDEBUGSTRING, logOptions.toOutputDebugString ? BST_CHECKED : BST_UNCHECKED); diff --git a/src/mir_app/src/newplugins.cpp b/src/mir_app/src/newplugins.cpp index 2ab86f710d..f16ba12347 100644 --- a/src/mir_app/src/newplugins.cpp +++ b/src/mir_app/src/newplugins.cpp @@ -121,8 +121,8 @@ static const MUUID pluginBannedList[] = static bool isPluginBanned(const MUUID& u1) { - for (int i = 0; i < _countof(pluginBannedList); i++) - if (pluginBannedList[i] == u1) + for (auto &it : pluginBannedList) + if (it == u1) return true; return false; @@ -156,11 +156,11 @@ int getDefaultPluginIdx(const MUUID &muuid) int LoadStdPlugins() { - for (int i = 0; i < _countof(pluginDefault); i++) { - if (pluginDefault[i].pImpl) + for (auto &it : pluginDefault) { + if (it.pImpl) continue; - if (!LoadCorePlugin(pluginDefault[i])) + if (!LoadCorePlugin(it)) return 1; } @@ -380,9 +380,9 @@ int Plugin_UnloadDyn(pluginEntry *p) // mark default plugins to be loaded if (!(p->pclass & PCLASS_CORE)) - for (int i = 0; i < _countof(pluginDefault); i++) - if (pluginDefault[i].pImpl == p) - pluginDefault[i].pImpl = nullptr; + for (auto &it : pluginDefault) + if (it.pImpl == p) + it.pImpl = nullptr; return TRUE; } diff --git a/src/mir_app/src/proto_accs.cpp b/src/mir_app/src/proto_accs.cpp index ef8c5d4e86..269027909d 100644 --- a/src/mir_app/src/proto_accs.cpp +++ b/src/mir_app/src/proto_accs.cpp @@ -413,8 +413,8 @@ void UnloadAccountsModule() } accounts.destroy(); - for (int i = 0; i < _countof(hHooks); i++) - UnhookEvent(hHooks[i]); + for (auto &it : hHooks) + UnhookEvent(it); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/mir_app/src/skinicons.cpp b/src/mir_app/src/skinicons.cpp index c27b8386a0..c54dea3dcd 100644 --- a/src/mir_app/src/skinicons.cpp +++ b/src/mir_app/src/skinicons.cpp @@ -341,9 +341,9 @@ MIR_APP_DLL(HICON) Skin_LoadProtoIcon(const char *szProto, int status, bool big) MIR_APP_DLL(HANDLE) Skin_GetIconHandle(int idx) { - for (int i = 0; i < _countof(mainIcons); i++) - if (idx == mainIcons[i].id) - return mainIcons[i].hIcolib; + for (auto &it : mainIcons) + if (idx == it.id) + return it.hIcolib; return nullptr; } diff --git a/src/mir_app/src/utils.cpp b/src/mir_app/src/utils.cpp index 167a397a58..170c7db7c2 100644 --- a/src/mir_app/src/utils.cpp +++ b/src/mir_app/src/utils.cpp @@ -292,18 +292,18 @@ static CountryListEntry countries[] = { static INT_PTR GetCountryByNumber(WPARAM wParam, LPARAM) { - for (int i = 0; i < _countof(countries); i++) - if ((int)wParam == countries[i].id) - return (INT_PTR)countries[i].szName; + for (auto &it : countries) + if ((int)wParam == it.id) + return (INT_PTR)it.szName; return 0; } static INT_PTR GetCountryByISOCode(WPARAM wParam, LPARAM) { - for (int i = 0; i < _countof(countries); i++) - if ( mir_strcmpi((char*)wParam, countries[i].ISOcode) == 0) - return (INT_PTR)countries[i].szName; + for (auto &it : countries) + if ( mir_strcmpi((char*)wParam, it.ISOcode) == 0) + return (INT_PTR)it.szName; return 0; } diff --git a/src/mir_app/src/visibility.cpp b/src/mir_app/src/visibility.cpp index 8abdfabaf5..5946ca3159 100644 --- a/src/mir_app/src/visibility.cpp +++ b/src/mir_app/src/visibility.cpp @@ -50,7 +50,7 @@ static void SetListGroupIcons(HWND hwndList, HANDLE hFirstItem, HANDLE hParentIt else hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hFirstItem); while (hItem) { - for (int i=0; i < _countof(iconOn); i++) { + for (int i = 0; i < _countof(iconOn); i++) { int iImage = SendMessage(hwndList, CLM_GETEXTRAIMAGE, (WPARAM)hItem, i); if (iconOn[i] && iImage == 0) iconOn[i] = 0; if (iImage != EMPTY_EXTRA_ICON) @@ -59,9 +59,9 @@ static void SetListGroupIcons(HWND hwndList, HANDLE hFirstItem, HANDLE hParentIt hItem = (HANDLE)SendMessage(hwndList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, (LPARAM)hItem); } //set icons - for (int i=0; i < _countof(iconOn); i++) { - SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hParentItem, MAKELPARAM(i, childCount[i]?(iconOn[i]?i+1:0):EMPTY_EXTRA_ICON)); - if (groupChildCount) groupChildCount[i]+=childCount[i]; + for (int i = 0; i < _countof(iconOn); i++) { + SendMessage(hwndList, CLM_SETEXTRAIMAGE, (WPARAM)hParentItem, MAKELPARAM(i, childCount[i] ? (iconOn[i] ? i + 1 : 0) : EMPTY_EXTRA_ICON)); + if (groupChildCount) groupChildCount[i] += childCount[i]; } } -- cgit v1.2.3