From 2b0fedca7ad38d8ddd4af8e9ccfd23e9c21dc3b5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 25 Jun 2024 15:23:03 +0300 Subject: fixes #4484 (CyrTranslit) --- plugins/CyrTranslit/src/TransliterationMap.cpp | 301 ++++++--------------- plugins/CyrTranslit/src/TransliterationMap.h | 73 ----- .../CyrTranslit/src/TransliterationProtocol.cpp | 66 ++--- plugins/CyrTranslit/src/TransliterationProtocol.h | 5 - plugins/CyrTranslit/src/stdafx.h | 3 +- 5 files changed, 108 insertions(+), 340 deletions(-) delete mode 100644 plugins/CyrTranslit/src/TransliterationMap.h (limited to 'plugins/CyrTranslit/src') diff --git a/plugins/CyrTranslit/src/TransliterationMap.cpp b/plugins/CyrTranslit/src/TransliterationMap.cpp index 6ec327d84f..7ccd8bbbf4 100644 --- a/plugins/CyrTranslit/src/TransliterationMap.cpp +++ b/plugins/CyrTranslit/src/TransliterationMap.cpp @@ -19,228 +19,99 @@ #include "stdafx.h" -namespace CyrTranslit +struct TransliterationMap { - -TransliterationMap::Guard TransliterationMap::guard; -const TransliterationMap *TransliterationMap::pInstance = nullptr; - -//------------------------------------------------------------------------------ - -TransliterationMap::Guard::~Guard() -{ - delete TransliterationMap::pInstance; -} - -//------------------------------------------------------------------------------ - -const TransliterationMap& TransliterationMap::getInstance() -{ - if (!pInstance) - { - pInstance = new TransliterationMap(); - } - return *pInstance; -} - -//------------------------------------------------------------------------------ - -TransliterationMap::TransliterationMap() -{ - theMap['а'] = "a"; - theMap['б'] = "b"; - theMap['в'] = "v"; - theMap['г'] = "g"; - theMap['д'] = "d"; - theMap['е'] = "e"; - theMap['ё'] = "e"; - theMap['ж'] = "zh"; - theMap['з'] = "z"; - theMap['и'] = "i"; - theMap['й'] = "i"; - theMap['к'] = "k"; - theMap['л'] = "l"; - theMap['м'] = "m"; - theMap['н'] = "n"; - theMap['о'] = "o"; - theMap['п'] = "p"; - theMap['р'] = "r"; - theMap['с'] = "s"; - theMap['т'] = "t"; - theMap['у'] = "u"; - theMap['ф'] = "f"; - theMap['х'] = "kh"; - theMap['ц'] = "ts"; - theMap['ч'] = "ch"; - theMap['ш'] = "sh"; - theMap['щ'] = "sch"; - theMap['ъ'] = "`"; - theMap['ы'] = "y"; - theMap['ь'] = "'"; - theMap['э'] = "e"; - theMap['ю'] = "yu"; - theMap['я'] = "ya"; - - theMap['А'] = "A"; - theMap['Б'] = "B"; - theMap['В'] = "V"; - theMap['Г'] = "G"; - theMap['Д'] = "D"; - theMap['Е'] = "E"; - theMap['Ё'] = "E"; - theMap['Ж'] = "ZH"; - theMap['З'] = "Z"; - theMap['И'] = "I"; - theMap['Й'] = "I"; - theMap['К'] = "K"; - theMap['Л'] = "L"; - theMap['М'] = "M"; - theMap['Н'] = "N"; - theMap['О'] = "O"; - theMap['П'] = "P"; - theMap['Р'] = "R"; - theMap['С'] = "S"; - theMap['Т'] = "T"; - theMap['У'] = "U"; - theMap['Ф'] = "F"; - theMap['Х'] = "KH"; - theMap['Ц'] = "TS"; - theMap['Ч'] = "CH"; - theMap['Ш'] = "SH"; - theMap['Щ'] = "SCH"; - theMap['Ъ'] = "`"; - theMap['Ы'] = "Y"; - theMap['Ь'] = "'"; - theMap['Э'] = "E"; - theMap['Ю'] = "YU"; - theMap['Я'] = "YA"; - - - - - theMapW[L'а'] = L"a"; - theMapW[L'б'] = L"b"; - theMapW[L'в'] = L"v"; - theMapW[L'г'] = L"g"; - theMapW[L'д'] = L"d"; - theMapW[L'е'] = L"e"; - theMapW[L'ё'] = L"e"; - theMapW[L'ж'] = L"zh"; - theMapW[L'з'] = L"z"; - theMapW[L'и'] = L"i"; - theMapW[L'й'] = L"i"; - theMapW[L'к'] = L"k"; - theMapW[L'л'] = L"l"; - theMapW[L'м'] = L"m"; - theMapW[L'н'] = L"n"; - theMapW[L'о'] = L"o"; - theMapW[L'п'] = L"p"; - theMapW[L'р'] = L"r"; - theMapW[L'с'] = L"s"; - theMapW[L'т'] = L"t"; - theMapW[L'у'] = L"u"; - theMapW[L'ф'] = L"f"; - theMapW[L'х'] = L"kh"; - theMapW[L'ц'] = L"ts"; - theMapW[L'ч'] = L"ch"; - theMapW[L'ш'] = L"sh"; - theMapW[L'щ'] = L"sch"; - theMapW[L'ъ'] = L"`"; - theMapW[L'ы'] = L"y"; - theMapW[L'ь'] = L"'"; - theMapW[L'э'] = L"e"; - theMapW[L'ю'] = L"yu"; - theMapW[L'я'] = L"ya"; - - theMapW[L'А'] = L"A"; - theMapW[L'Б'] = L"B"; - theMapW[L'В'] = L"V"; - theMapW[L'Г'] = L"G"; - theMapW[L'Д'] = L"D"; - theMapW[L'Е'] = L"E"; - theMapW[L'Ё'] = L"E"; - theMapW[L'Ж'] = L"ZH"; - theMapW[L'З'] = L"Z"; - theMapW[L'И'] = L"I"; - theMapW[L'Й'] = L"I"; - theMapW[L'К'] = L"K"; - theMapW[L'Л'] = L"L"; - theMapW[L'М'] = L"M"; - theMapW[L'Н'] = L"N"; - theMapW[L'О'] = L"O"; - theMapW[L'П'] = L"P"; - theMapW[L'Р'] = L"R"; - theMapW[L'С'] = L"S"; - theMapW[L'Т'] = L"T"; - theMapW[L'У'] = L"U"; - theMapW[L'Ф'] = L"F"; - theMapW[L'Х'] = L"KH"; - theMapW[L'Ц'] = L"TS"; - theMapW[L'Ч'] = L"CH"; - theMapW[L'Ш'] = L"SH"; - theMapW[L'Щ'] = L"SCH"; - theMapW[L'Ъ'] = L"`"; - theMapW[L'Ы'] = L"Y"; - theMapW[L'Ь'] = L"'"; - theMapW[L'Э'] = L"E"; - theMapW[L'Ю'] = L"YU"; - theMapW[L'Я'] = L"YA"; + TransliterationMap() + { + theMapW["а"] = "a"; + theMapW["б"] = "b"; + theMapW["в"] = "v"; + theMapW["г"] = "g"; + theMapW["д"] = "d"; + theMapW["е"] = "e"; + theMapW["ё"] = "e"; + theMapW["ж"] = "zh"; + theMapW["з"] = "z"; + theMapW["и"] = "i"; + theMapW["й"] = "i"; + theMapW["к"] = "k"; + theMapW["л"] = "l"; + theMapW["м"] = "m"; + theMapW["н"] = "n"; + theMapW["о"] = "o"; + theMapW["п"] = "p"; + theMapW["р"] = "r"; + theMapW["с"] = "s"; + theMapW["т"] = "t"; + theMapW["у"] = "u"; + theMapW["ф"] = "f"; + theMapW["х"] = "kh"; + theMapW["ц"] = "ts"; + theMapW["ч"] = "ch"; + theMapW["ш"] = "sh"; + theMapW["щ"] = "sch"; + theMapW["ъ"] = "`"; + theMapW["ы"] = "y"; + theMapW["ь"] = "'"; + theMapW["э"] = "e"; + theMapW["ю"] = "yu"; + theMapW["я"] = "ya"; + + theMapW["А"] = "A"; + theMapW["Б"] = "B"; + theMapW["В"] = "V"; + theMapW["Г"] = "G"; + theMapW["Д"] = "D"; + theMapW["Е"] = "E"; + theMapW["Ё"] = "E"; + theMapW["Ж"] = "ZH"; + theMapW["З"] = "Z"; + theMapW["И"] = "I"; + theMapW["Й"] = "I"; + theMapW["К"] = "K"; + theMapW["Л"] = "L"; + theMapW["М"] = "M"; + theMapW["Н"] = "N"; + theMapW["О"] = "O"; + theMapW["П"] = "P"; + theMapW["Р"] = "R"; + theMapW["С"] = "S"; + theMapW["Т"] = "T"; + theMapW["У"] = "U"; + theMapW["Ф"] = "F"; + theMapW["Х"] = "KH"; + theMapW["Ц"] = "TS"; + theMapW["Ч"] = "CH"; + theMapW["Ш"] = "SH"; + theMapW["Щ"] = "SCH"; + theMapW["Ъ"] = "`"; + theMapW["Ы"] = "Y"; + theMapW["Ь"] = "'"; + theMapW["Э"] = "E"; + theMapW["Ю"] = "YU"; + theMapW["Я"] = "YA"; + } + + std::map theMapW; } +static m; //------------------------------------------------------------------------------ -std::string TransliterationMap::cyrillicToLatin(char c) const -{ - std::string ret(1, c); - - CharMap::const_iterator it = theMap.find(c); - if (it != theMap.end()) - { - ret = (*it).second; - } - - return ret; -} -//------------------------------------------------------------------------------ - -std::wstring TransliterationMap::cyrillicToLatin(wchar_t c) const +CMStringA cyrillicToLatin(const char *src) { - std::wstring ret(1, c); + CMStringA ret; - WCharMap::const_iterator it = theMapW.find(c); - if (it != theMapW.end()) - { - ret = (*it).second; - } - - return ret; -} - -//------------------------------------------------------------------------------ + for (auto *p = src; *p; p++) { + std::string str(p, 2); -std::string TransliterationMap::cyrillicToLatin(const std::string &src) const -{ - std::string ret; - - for(std::string::const_iterator i = src.begin(); i != src.end(); ++i) - { - ret += cyrillicToLatin(*i); - } - - return ret; -} -//------------------------------------------------------------------------------ + auto f = m.theMapW.find(str); + if (f != m.theMapW.end()) { + ret += f->second; + p++; + } + else ret += *p; + } -std::wstring TransliterationMap::cyrillicToLatin(const std::wstring &src) const -{ - std::wstring ret; - - for(std::wstring::const_iterator i = src.begin(); i != src.end(); ++i) - { - ret += cyrillicToLatin(*i); - } - - return ret; + return ret; } - -} \ No newline at end of file diff --git a/plugins/CyrTranslit/src/TransliterationMap.h b/plugins/CyrTranslit/src/TransliterationMap.h deleted file mode 100644 index 960cb6ceac..0000000000 --- a/plugins/CyrTranslit/src/TransliterationMap.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * CyrTranslit: the Cyrillic transliteration plug-in for Miranda IM. - * Copyright 2005 Ivan Krechetov. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#pragma once - -#include "stdafx.h" - -namespace CyrTranslit -{ - -/** - * Maps the Cyrillic letters to the phonetically equivalent latin character - * sequences. That's what transliteration is. - */ -class TransliterationMap -{ -public: - /** - * Gives an access to the singleton object of this class. - * - * @return One and only instance of TransliterationMap class. - */ - static const TransliterationMap& getInstance(); - - /** - * Performs the transliteration from Cyrillic to Latin. - * - * @param src The source string that may contain Cyrillic characters. - * - * @return The transliterated source string in which all the occurences of - * Cyrillic letters are replaced by the phonetically equivalent Latin - * sequences (1-3 letters for a single Cyrillic leter). - */ - std::string cyrillicToLatin(const std::string &src) const; - std::wstring cyrillicToLatin(const std::wstring &src) const; - -private: - class Guard - { - public: - ~Guard(); - }; - - TransliterationMap(); - std::string cyrillicToLatin(char c) const; - std::wstring cyrillicToLatin(wchar_t c) const; - - static const TransliterationMap *pInstance; - static Guard guard; - - typedef std::map CharMap; - typedef std::map WCharMap; - CharMap theMap; - WCharMap theMapW; -}; - -} \ No newline at end of file diff --git a/plugins/CyrTranslit/src/TransliterationProtocol.cpp b/plugins/CyrTranslit/src/TransliterationProtocol.cpp index 516d2257d3..436b68918f 100644 --- a/plugins/CyrTranslit/src/TransliterationProtocol.cpp +++ b/plugins/CyrTranslit/src/TransliterationProtocol.cpp @@ -19,60 +19,34 @@ #include "stdafx.h" -namespace CyrTranslit -{ - -char *TransliterationProtocol::MODULENAME = "ProtoCyrTranslitByIKR"; - -//------------------------------------------------------------------------------ - -void TransliterationProtocol::initialize() -{ - Proto_RegisterModule(PROTOTYPE_TRANSLATION, MODULENAME); - - CreateProtoServiceFunction(MODULENAME, PSS_MESSAGE, sendMessage); -} +#define MODULENAME "ProtoCyrTranslitByIKR" -//------------------------------------------------------------------------------ -void TransliterationProtocol::TranslateMessageUTF(WPARAM, LPARAM lParam) +namespace CyrTranslit { - CCSDATA *ccs = reinterpret_cast(lParam); - - wchar_t* txtWdecoded = mir_utf8decodeW(reinterpret_cast(ccs->lParam)); - std::wstring txtW = txtWdecoded; - mir_free(txtWdecoded); + //------------------------------------------------------------------------------ - txtW = TransliterationMap::getInstance().cyrillicToLatin(txtW); + void TransliterationProtocol::initialize() + { + Proto_RegisterModule(PROTOTYPE_TRANSLATION, MODULENAME); - char* txtUTFencoded = mir_utf8encodeW(txtW.c_str()); - std::string txtUTF = txtUTFencoded; - mir_free(txtUTFencoded); - - ccs->lParam = reinterpret_cast(mir_alloc(txtUTF.length())); - mir_strcpy(reinterpret_cast(ccs->lParam), txtUTF.c_str()); -} - -//------------------------------------------------------------------------------ - -INT_PTR TransliterationProtocol::sendMessage(WPARAM wParam, LPARAM lParam) -{ - CCSDATA *ccs = reinterpret_cast(lParam); - if ( !MirandaContact::bIsActive(ccs->hContact)) - return Proto_ChainSend(wParam, ccs); + CreateProtoServiceFunction(MODULENAME, PSS_MESSAGE, sendMessage); + } - LPARAM oldlParam = ccs->lParam; - bool msgProcessed = true; + //------------------------------------------------------------------------------ - TranslateMessageUTF(wParam, lParam); + INT_PTR TransliterationProtocol::sendMessage(WPARAM wParam, LPARAM lParam) + { + CCSDATA *ccs = (CCSDATA *)lParam; + if (!MirandaContact::bIsActive(ccs->hContact)) + return Proto_ChainSend(wParam, ccs); - int ret = Proto_ChainSend(wParam, ccs); + LPARAM oldlParam = ccs->lParam; + + CMStringA szEncoded(cyrillicToLatin((char *)ccs->lParam)); + ccs->lParam = (LPARAM)szEncoded.c_str(); - if (msgProcessed) { - mir_free(reinterpret_cast(ccs->lParam)); + int ret = Proto_ChainSend(wParam, ccs); ccs->lParam = oldlParam; + return ret; } - - return ret; -} - } diff --git a/plugins/CyrTranslit/src/TransliterationProtocol.h b/plugins/CyrTranslit/src/TransliterationProtocol.h index 164debbfd4..5c7861f145 100644 --- a/plugins/CyrTranslit/src/TransliterationProtocol.h +++ b/plugins/CyrTranslit/src/TransliterationProtocol.h @@ -46,13 +46,8 @@ public: */ static INT_PTR sendMessage(WPARAM wParam, LPARAM lParam); - static void TranslateMessageUTF(WPARAM wParam, LPARAM lParam); - private: - static char *MODULENAME; - TransliterationProtocol(); - ~TransliterationProtocol(); }; } \ No newline at end of file diff --git a/plugins/CyrTranslit/src/stdafx.h b/plugins/CyrTranslit/src/stdafx.h index c5eb7f39e7..a69c4f8df9 100644 --- a/plugins/CyrTranslit/src/stdafx.h +++ b/plugins/CyrTranslit/src/stdafx.h @@ -32,9 +32,10 @@ #include "version.h" #include "MirandaContact.h" -#include "TransliterationMap.h" #include "TransliterationProtocol.h" +CMStringA cyrillicToLatin(const char *src); + struct CMPlugin : public PLUGIN { CMPlugin(); -- cgit v1.2.3