diff options
author | George Hazan <ghazan@miranda.im> | 2019-02-14 16:52:56 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2019-02-14 16:52:56 +0300 |
commit | 9039a597d913aaeadb8090622d06b6425dc34125 (patch) | |
tree | 0aca047709fd0e3383bc10ee065ed87d0c8ae02f | |
parent | f682bca52b2b1811a823eeac9768f00e850908e7 (diff) |
SmileyAdd -> TinyXml
-rw-r--r-- | plugins/SmileyAdd/src/smileys.cpp | 132 | ||||
-rw-r--r-- | plugins/SmileyAdd/src/smileys.h | 4 | ||||
-rw-r--r-- | plugins/SmileyAdd/src/version.h | 2 |
3 files changed, 77 insertions, 61 deletions
diff --git a/plugins/SmileyAdd/src/smileys.cpp b/plugins/SmileyAdd/src/smileys.cpp index 7ecfe38486..56e0c144b7 100644 --- a/plugins/SmileyAdd/src/smileys.cpp +++ b/plugins/SmileyAdd/src/smileys.cpp @@ -352,10 +352,7 @@ bool SmileyPackType::LoadSmileyFile(const CMStringW &filename, const CMStringW & CMStringW modpath; pathToAbsolute(filename, modpath); - - // Load file - int fh = _wopen(modpath.c_str(), _O_BINARY | _O_RDONLY); - if (fh == -1) { + if (_waccess(filename, 4) != 0) { if (!noerr) { static const wchar_t errmsg[] = LPGENW("Smiley pack %s for category \"%s\" not found.\nSelect correct smiley pack in the Options -> Customize -> Smileys."); wchar_t msgtxt[1024]; @@ -369,34 +366,12 @@ bool SmileyPackType::LoadSmileyFile(const CMStringW &filename, const CMStringW & m_Filename = filename; - // Find file size - const long flen = _filelength(fh); - - // Allocate file buffer - char *buf = new char[flen + sizeof(wchar_t)]; - - // Read file in - int len = _read(fh, buf, flen); - *(wchar_t*)(buf + len) = 0; - - // Close file - _close(fh); - - CMStringW tbuf; - if (len > 2 && *(wchar_t*)buf == 0xfeff) - tbuf = ((wchar_t*)buf + 1); - else if (len > 3 && buf[0] == '\xef' && buf[1] == '\xbb' && buf[2] == '\xbf') - tbuf = _A2T(buf + 3, CP_UTF8); - else - tbuf = _A2T(buf); - - delete[] buf; - + // Load file bool res; if (filename.Find(L".xep") == -1) - res = LoadSmileyFileMSL(tbuf, onlyInfo, modpath); + res = LoadSmileyFileMSL(modpath, onlyInfo, modpath); else - res = LoadSmileyFileXEP(tbuf, onlyInfo); + res = LoadSmileyFileXEP(modpath, onlyInfo); if (errorFound) ReportError(TranslateT("There were problems loading smiley pack (it should be corrected).\nSee network log for details.")); @@ -404,10 +379,13 @@ bool SmileyPackType::LoadSmileyFile(const CMStringW &filename, const CMStringW & return res; } -static IStream* DecodeBase64Data(const wchar_t *pString) +static IStream* DecodeBase64Data(const char *pString) { + if (pString == nullptr) + return nullptr; + size_t dataLen; - ptrA data((char*)mir_base64_decode(_T2A(pString), &dataLen)); + ptrA data((char*)mir_base64_decode(pString, &dataLen)); if (data == nullptr) return nullptr; @@ -425,7 +403,7 @@ static IStream* DecodeBase64Data(const wchar_t *pString) return pStream; } -static CMStringW FilterQuotes(const wchar_t *pStr) +static CMStringW FilterQuotes(const char *pStr) { CMStringW res(pStr); int iStart = res.Find('\"', 0); @@ -435,24 +413,38 @@ static CMStringW FilterQuotes(const wchar_t *pStr) res = res.Mid(iStart, iEnd - iStart); } - return res; + return res.Trim(); } -bool SmileyPackType::LoadSmileyFileXEP(CMStringW &tbuf, bool onlyInfo) +bool SmileyPackType::LoadSmileyFileXEP(const CMStringW &fileName, bool onlyInfo) { - HXML node, xmlRoot = xmlParseString(tbuf, nullptr, nullptr); - if (!xmlRoot) + FILE *in = _wfopen(fileName, L"rb"); + if (in == nullptr) + return false; + + TiXmlDocument doc; + int ret = doc.LoadFile(in); + fclose(in); + if (ret != 0) return false; - if (node = xmlGetChildByPath(xmlRoot, L"settings/DataBaseName", 0)) - m_Name = xmlGetText(node); - if (node = xmlGetChildByPath(xmlRoot, L"settings/PackageAuthor", 0)) - m_Author = xmlGetText(node); + auto *pSettings = doc.FirstChildElement("settings"); + if (pSettings != nullptr) { + if (auto *pNode = pSettings->FirstChildElement("DataBaseName")) + m_Name = CMStringW(Utf2T(pNode->GetText())).Trim(); + + if (auto *pNode = pSettings->FirstChildElement("PackageAuthor")) + m_Author = CMStringW(Utf2T(pNode->GetText())).Trim(); + } if (!onlyInfo) { - const wchar_t *pStr = xmlGetClear(xmlGetChildByPath(xmlRoot, L"lists/images", 0), 0, 0, 0); - if (pStr) { - IStream *pStream = DecodeBase64Data(pStr); + auto *pDataRoot = doc.FirstChildElement("dataroot"); + if (pDataRoot == nullptr) + return false; + + auto *pImages = tinyxml2::XMLConstHandle(&doc).FirstChildElement("lists").FirstChildElement("images").ToElement(); + if (pImages) { + IStream *pStream = DecodeBase64Data(pImages->GetText()); if (pStream) { if (m_hSmList != nullptr) ImageList_Destroy(m_hSmList); @@ -461,26 +453,25 @@ bool SmileyPackType::LoadSmileyFileXEP(CMStringW &tbuf, bool onlyInfo) } } - HXML nRec, dataRoot = xmlGetChildByPath(xmlRoot, L"dataroot", 0); - for (int i = 0; (nRec = xmlGetNthChild(dataRoot, L"record", i)) != 0; i++) { - pStr = xmlGetAttrValue(nRec, L"ImageIndex"); - if (pStr == nullptr) + for (auto *nRec : TiXmlFilter(pDataRoot, "record")) { + int idx = nRec->IntAttribute("ImageIndex", -1); + if (idx == -1) continue; SmileyType *dat = new SmileyType; dat->SetRegEx(true); - dat->SetImList(m_hSmList, _wtoi(pStr)); - dat->m_ToolText = FilterQuotes(xmlGetText(nRec)); + dat->SetImList(m_hSmList, idx); + dat->m_ToolText = FilterQuotes(nRec->GetText()); - if (node = xmlGetChildByPath(nRec, L"Expression", 0)) - dat->m_TriggerText = FilterQuotes(xmlGetText(node)); - if (node = xmlGetChildByPath(nRec, L"PasteText", 0)) - dat->m_InsertText = FilterQuotes(xmlGetText(node)); + if (auto *pNode = nRec->FirstChildElement("Expression")) + dat->m_TriggerText = FilterQuotes(pNode->GetText()); + if (auto *pNode = nRec->FirstChildElement("PasteText")) + dat->m_InsertText = FilterQuotes(pNode->GetText()); dat->SetHidden(dat->m_InsertText.IsEmpty()); - if (node = xmlGetChildByPath(nRec, L"Image", 0)) { - IStream *pStream = DecodeBase64Data(xmlGetText(node)); + if (auto *pNode = nRec->FirstChildElement("Image")) { + IStream *pStream = DecodeBase64Data(pNode->GetText()); if (pStream) { dat->LoadFromImage(pStream); pStream->Release(); @@ -491,8 +482,6 @@ bool SmileyPackType::LoadSmileyFileXEP(CMStringW &tbuf, bool onlyInfo) } } - xmlDestroyNode(xmlRoot); - m_VisibleCount = m_SmileyList.getCount(); AddTriggersToSmileyLookup(); @@ -500,8 +489,35 @@ bool SmileyPackType::LoadSmileyFileXEP(CMStringW &tbuf, bool onlyInfo) return true; } -bool SmileyPackType::LoadSmileyFileMSL(CMStringW &tbuf, bool onlyInfo, CMStringW &modpath) +bool SmileyPackType::LoadSmileyFileMSL(const CMStringW &filename, bool onlyInfo, CMStringW &modpath) { + int fh = _wopen(filename.c_str(), _O_BINARY | _O_RDONLY); + if (fh == -1) + return false; + + // Find file size + const long flen = _filelength(fh); + + // Allocate file buffer + char *buf = new char[flen + sizeof(wchar_t)]; + + // Read file in + int len = _read(fh, buf, flen); + *(wchar_t*)(buf + len) = 0; + + // Close file + _close(fh); + + CMStringW tbuf; + if (len > 2 && *(wchar_t*)buf == 0xfeff) + tbuf = ((wchar_t*)buf + 1); + else if (len > 3 && buf[0] == '\xef' && buf[1] == '\xbb' && buf[2] == '\xbf') + tbuf = _A2T(buf + 3, CP_UTF8); + else + tbuf = _A2T(buf); + + delete[] buf; + CMStringW pathstr, packstr; { MRegexp16 pathsplit(L"(.*\\\\)(.*)\\.|$"); diff --git a/plugins/SmileyAdd/src/smileys.h b/plugins/SmileyAdd/src/smileys.h index 5f41640960..dfcde52468 100644 --- a/plugins/SmileyAdd/src/smileys.h +++ b/plugins/SmileyAdd/src/smileys.h @@ -203,8 +203,8 @@ public: bool LoadSmileyFile(const CMStringW &filename, const CMStringW &packname, bool onlyInfo, bool noerr = false);
- bool LoadSmileyFileMSL(CMStringW &body, bool bOnlyInfo, CMStringW &modPath);
- bool LoadSmileyFileXEP(CMStringW &body, bool bOnlyInfo);
+ bool LoadSmileyFileMSL(const CMStringW &filename, bool bOnlyInfo, CMStringW &modPath);
+ bool LoadSmileyFileXEP(const CMStringW &filename, bool bOnlyInfo);
void Clear(void);
};
diff --git a/plugins/SmileyAdd/src/version.h b/plugins/SmileyAdd/src/version.h index a7721013ee..9f256db3d2 100644 --- a/plugins/SmileyAdd/src/version.h +++ b/plugins/SmileyAdd/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0
#define __MINOR_VERSION 3
#define __RELEASE_NUM 1
-#define __BUILD_NUM 0
+#define __BUILD_NUM 1
#include <stdver.h>
|