From 59f72fa52bd560003af3c4635f92a885d1c07dbc Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 26 Feb 2019 13:55:31 +0300 Subject: XmlFirstChild / XmlGetAttr - safe wrappers for tinyxml2 --- src/mir_core/src/mir_core.def | 10 +++++ src/mir_core/src/mir_core64.def | 10 +++++ src/mir_core/src/tinyxml2_utils.cpp | 89 +++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/mir_core/src/tinyxml2_utils.cpp (limited to 'src') diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def index 91ca5b7dbd..6039b67290 100644 --- a/src/mir_core/src/mir_core.def +++ b/src/mir_core/src/mir_core.def @@ -1420,3 +1420,13 @@ db_get_utfa @1631 ?GetTextU@CCtrlBase@@QAEPADXZ @1633 NONAME ?BytesParsed@XMLDocument@tinyxml2@@QBEHXZ @1634 NONAME TranslateU_LP @1635 +XmlAddAttr @1636 +XmlAddChild @1637 +XmlAddChildA @1638 +XmlAddChildI @1639 +XmlFirstChild @1640 +XmlGetAttr @1641 +XmlGetChildByTag @1642 +XmlGetChildCount @1643 +XmlGetChildInt @1644 +XmlGetChildText @1645 diff --git a/src/mir_core/src/mir_core64.def b/src/mir_core/src/mir_core64.def index 565fd9bedc..2d73dc7d91 100644 --- a/src/mir_core/src/mir_core64.def +++ b/src/mir_core/src/mir_core64.def @@ -1420,3 +1420,13 @@ db_get_utfa @1631 ?GetTextU@CCtrlBase@@QEAAPEADXZ @1633 NONAME ?BytesParsed@XMLDocument@tinyxml2@@QEBAHXZ @1634 NONAME TranslateU_LP @1635 +XmlAddAttr @1636 +XmlAddChild @1637 +XmlAddChildA @1638 +XmlAddChildI @1639 +XmlFirstChild @1640 +XmlGetAttr @1641 +XmlGetChildByTag @1642 +XmlGetChildCount @1643 +XmlGetChildInt @1644 +XmlGetChildText @1645 diff --git a/src/mir_core/src/tinyxml2_utils.cpp b/src/mir_core/src/tinyxml2_utils.cpp new file mode 100644 index 0000000000..087e9d0230 --- /dev/null +++ b/src/mir_core/src/tinyxml2_utils.cpp @@ -0,0 +1,89 @@ + +#include "stdafx.h" + +///////////////////////////////////////////////////////////////////////////////////////// + +MIR_CORE_DLL(void) XmlAddAttr(TiXmlElement *hXml, const char *name, const char *value) +{ + if (hXml && value) + hXml->SetAttribute(name, value); +} + +MIR_CORE_DLL(const char*) XmlGetAttr(const TiXmlElement *hXml, const char *pszName) +{ + return (hXml == nullptr) ? nullptr : hXml->Attribute(pszName); +} + +///////////////////////////////////////////////////////////////////////////////////////// + +MIR_CORE_DLL(TiXmlElement*) XmlAddChild(TiXmlElement *hXml, const char *name) +{ + if (hXml == nullptr) + return nullptr; + + auto *res = hXml->GetDocument()->NewElement(name); + hXml->InsertEndChild(res); + return res; +} + +MIR_CORE_DLL(TiXmlElement*) XmlAddChildA(TiXmlElement *hXml, const char *name, const char *value) +{ + if (hXml == nullptr) + return nullptr; + + auto *res = hXml->GetDocument()->NewElement(name); + if (value) + res->SetText(value); + hXml->InsertEndChild(res); + return res; +} + +MIR_CORE_DLL(TiXmlElement*) XmlAddChildI(TiXmlElement *hXml, const char *name, int value) +{ + if (hXml == nullptr) + return nullptr; + + auto *res = hXml->GetDocument()->NewElement(name); + if (value) + res->SetText(value); + hXml->InsertEndChild(res); + return res; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +MIR_CORE_DLL(const TiXmlElement*) XmlFirstChild(const TiXmlElement *hXml, const char *key) +{ + return (hXml) ? hXml->FirstChildElement(key) : nullptr; +} + +MIR_CORE_DLL(const char*) XmlGetChildText(const TiXmlElement *hXml, const char *key) +{ + auto *pChild = XmlFirstChild(hXml, key); + return (pChild == nullptr) ? nullptr : pChild->GetText(); +} + +MIR_CORE_DLL(int) XmlGetChildInt(const TiXmlElement *hXml, const char *key) +{ + auto *pChild = XmlFirstChild(hXml, key); + return (pChild == nullptr) ? 0 : atoi(pChild->GetText()); +} + +MIR_CORE_DLL(const TiXmlElement*) XmlGetChildByTag(const TiXmlElement *hXml, const char *key, const char *attrName, const char *attrValue) +{ + for (auto *pChild : TiXmlFilter(hXml, key)) + if (pChild->Attribute(attrName, attrValue)) + return pChild; + + return nullptr; +} + +MIR_CORE_DLL(int) XmlGetChildCount(const TiXmlElement *hXml) +{ + int iCount = 0; + for (auto *it : TiXmlEnum(hXml)) { + UNREFERENCED_PARAMETER(it); + iCount++; + } + return iCount; +} -- cgit v1.2.3