From fdaadab8d86f29bf4f27ba1e6dfe07269ccffaff Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 13 Nov 2017 21:48:09 +0300 Subject: IRC: code cleaning for some crazy moments related to #1010 --- protocols/IRCG/src/services.cpp | 172 ++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 85 deletions(-) (limited to 'protocols/IRCG/src/services.cpp') diff --git a/protocols/IRCG/src/services.cpp b/protocols/IRCG/src/services.cpp index d7c9228f97..7c3443a98a 100644 --- a/protocols/IRCG/src/services.cpp +++ b/protocols/IRCG/src/services.cpp @@ -375,92 +375,96 @@ INT_PTR __cdecl CIrcProto::OnChangeNickMenuCommand(WPARAM, LPARAM) return 0; } -static void DoChatFormatting(wchar_t* pszText) -{ - wchar_t* p1 = pszText; - int iFG = -1; - int iRemoveChars; - wchar_t InsertThis[50]; - - while (*p1 != '\0') { - iRemoveChars = 0; - InsertThis[0] = 0; - - if (*p1 == '%') { - switch (p1[1]) { - case 'B': - case 'b': - mir_wstrcpy(InsertThis, L"\002"); - iRemoveChars = 2; - break; - case 'I': - case 'i': - mir_wstrcpy(InsertThis, L"\026"); - iRemoveChars = 2; - break; - case 'U': - case 'u': - mir_wstrcpy(InsertThis, L"\037"); - iRemoveChars = 2; - break; - case 'c': - { - mir_wstrcpy(InsertThis, L"\003"); - iRemoveChars = 2; - - wchar_t szTemp[3]; - mir_wstrncpy(szTemp, p1 + 2, 3); - iFG = _wtoi(szTemp); - } - break; - case 'C': - if (p1[2] == '%' && p1[3] == 'F') { - mir_wstrcpy(InsertThis, L"\x0399,99"); - iRemoveChars = 4; - } - else { - mir_wstrcpy(InsertThis, L"\x0399"); - iRemoveChars = 2; - } - iFG = -1; - break; - case 'f': - if (p1 - 3 >= pszText && p1[-3] == '\003') - mir_wstrcpy(InsertThis, L","); - else if (iFG >= 0) - mir_snwprintf(InsertThis, L"\x03%u,", iFG); - else - mir_wstrcpy(InsertThis, L"\x0399,"); +static int mapSrmm2irc[] = { 1, 2, 10, 6, 3, 7, 5, 14, 15, 12, 11, 13, 9, 8, 4, 0 }; - iRemoveChars = 2; - break; +static wchar_t* DoPrintColor(wchar_t *pDest, int iFG, int iBG) +{ + *pDest = 3; + if (iFG == -1) { + if (iBG == -1) + pDest[1] = 0; + else + mir_snwprintf(pDest+1, 49, L",%d", iBG); + } + else { + if (iBG == -1) + mir_snwprintf(pDest+1, 49, L"%d", iFG); + else + mir_snwprintf(pDest+1, 49, L"%d,%d", iFG, iBG); + } - case 'F': - if (iFG >= 0) - mir_snwprintf(InsertThis, L"\x03%u,99", iFG); - else - mir_wstrcpy(InsertThis, L"\x0399,99"); - iRemoveChars = 2; - break; + return pDest; +} - case '%': - mir_wstrcpy(InsertThis, L"%"); - iRemoveChars = 2; - break; +static void DoChatFormatting(CMStringW &wszText) +{ + int iFG = -1, iBG = -1; + wchar_t InsertThis[50]; - default: - iRemoveChars = 2; - break; + for (int i = 0; i < wszText.GetLength(); i++) { + if (wszText[i] != '%') + continue; + + switch (wszText[i + 1]) { + case 'B': + case 'b': + wszText.Delete(i, 2); + wszText.Insert(i, L"\002"); + break; + case 'I': + case 'i': + wszText.Delete(i, 2); + wszText.Insert(i, L"\026"); + break; + case 'U': + case 'u': + wszText.Delete(i, 2); + wszText.Insert(i, L"\037"); + break; + + case 'c': + wszText.Delete(i, 2); + iFG = _wtoi(wszText.GetString() + i); + wszText.Delete(i, (iFG < 10) ? 1 : 2); + + iFG = (iFG < 0 || iFG >= _countof(mapSrmm2irc)) ? -1 : mapSrmm2irc[iFG]; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'C': + if (wszText[i + 2] == '%' && wszText[i + 3] == 'F') { + wszText.Delete(i, 4); + iBG = -1; } - - memmove(p1 + mir_wstrlen(InsertThis), p1 + iRemoveChars, sizeof(wchar_t)*(mir_wstrlen(p1) - iRemoveChars + 1)); - memcpy(p1, InsertThis, sizeof(wchar_t)*mir_wstrlen(InsertThis)); - if (iRemoveChars || mir_wstrlen(InsertThis)) - p1 += mir_wstrlen(InsertThis); - else - p1++; + else wszText.Delete(i, 2); + iFG = -1; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'f': + wszText.Delete(i, 2); + iBG = _wtoi(wszText.GetString() + i); + wszText.Delete(i, (iBG < 10) ? 1 : 2); + + iBG = (iBG < 0 || iBG >= _countof(mapSrmm2irc)) ? -1 : mapSrmm2irc[iBG]; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case 'F': + wszText.Delete(i, 2); + iBG = -1; + wszText.Insert(i, DoPrintColor(InsertThis, iFG, iBG)); + break; + + case '%': + wszText.Delete(i, 1); + i++; + break; + + default: + wszText.Delete(i, 2); + break; } - else p1++; } } @@ -486,11 +490,9 @@ int __cdecl CIrcProto::GCEventHook(WPARAM, LPARAM lParam) case GC_USER_MESSAGE: if (gch && gch->ptszText && *gch->ptszText) { - wchar_t* pszText = new wchar_t[mir_wstrlen(gch->ptszText) + 1000]; - mir_wstrcpy(pszText, gch->ptszText); - DoChatFormatting(pszText); - PostIrcMessageWnd(p1, NULL, pszText); - delete[]pszText; + CMStringW wszText(gch->ptszText); + DoChatFormatting(wszText); + PostIrcMessageWnd(p1, NULL, wszText); } break; -- cgit v1.2.3