summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_xml.h
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2019-02-18 22:32:52 +0300
committerGeorge Hazan <ghazan@miranda.im>2019-02-19 19:24:16 +0300
commit6e9ba17bef418a058e84ff5e248ea479d4836669 (patch)
treed49f0c620d3c06092900ff7374b22ccfa646a0b7 /protocols/JabberG/src/jabber_xml.h
parentd05f7096cd384679e9dcab92a6efc81c68977628 (diff)
Jabber -> TinyXml2
Diffstat (limited to 'protocols/JabberG/src/jabber_xml.h')
-rw-r--r--protocols/JabberG/src/jabber_xml.h227
1 files changed, 85 insertions, 142 deletions
diff --git a/protocols/JabberG/src/jabber_xml.h b/protocols/JabberG/src/jabber_xml.h
index dced7ed334..b1ee52586f 100644
--- a/protocols/JabberG/src/jabber_xml.h
+++ b/protocols/JabberG/src/jabber_xml.h
@@ -26,88 +26,64 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef _JABBER_XML_H_
#define _JABBER_XML_H_
-#include <m_xml.h>
-
-void __fastcall XmlAddChild(HXML, HXML);
-HXML __fastcall XmlAddChild(HXML, const wchar_t *pszName);
-HXML __fastcall XmlAddChild(HXML, const wchar_t *pszName, const wchar_t *ptszValue);
-HXML __fastcall XmlAddChild(HXML, const wchar_t *pszName, int iValue);
-
-const wchar_t *__fastcall XmlGetAttrValue(HXML, const wchar_t *key);
-HXML __fastcall XmlGetChild(HXML, int n = 0);
-HXML __fastcall XmlGetChild(HXML, LPCSTR key);
-HXML __fastcall XmlGetChild(HXML, const wchar_t *key);
-int __fastcall XmlGetChildCount(HXML);
-HXML __fastcall XmlGetChildByTag(HXML, const wchar_t *key, const wchar_t *attrName, const wchar_t *attrValue);
-HXML __fastcall XmlGetChildByTag(HXML, LPCSTR key, LPCSTR attrName, const wchar_t *attrValue);
-HXML __fastcall XmlGetNthChild(HXML, const wchar_t *key, int n = 0);
-
-const wchar_t *__fastcall XmlGetName(HXML);
-const wchar_t *__fastcall XmlGetText(HXML);
-
-void __fastcall XmlAddAttr(HXML, const wchar_t *pszName, const wchar_t *ptszValue);
-void __fastcall XmlAddAttr(HXML, const wchar_t *pszName, int value);
-void __fastcall XmlAddAttr(HXML hXml, const wchar_t *pszName, unsigned __int64 value);
-void __fastcall XmlAddAttrID(HXML, int id);
-
-int __fastcall XmlGetAttrCount(HXML);
-const wchar_t *__fastcall XmlGetAttr(HXML, int n);
-const wchar_t *__fastcall XmlGetAttrName(HXML, int n);
-const wchar_t *__fastcall XmlGetAttrValue(HXML, const wchar_t *key);
-
-struct XmlNode
-{
- __forceinline XmlNode() { m_hXml = nullptr; }
+int XmlGetChildCount(const TiXmlElement*);
- __forceinline XmlNode(const wchar_t *pszString, int* numBytes, const wchar_t *ptszTag)
- {
- m_hXml = xmlParseString(pszString, numBytes, ptszTag);
- }
+TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName);
+TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName, const char *ptszValue);
+TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName, int iValue);
- XmlNode(const XmlNode& n);
- XmlNode(const wchar_t *name);
- XmlNode(const wchar_t *pszName, const wchar_t *ptszText);
- ~XmlNode();
+const TiXmlElement* XmlGetChildByTag(const TiXmlElement*, const char *key, const char *attrName, const char *attrValue);
- XmlNode& operator =(const XmlNode& n);
+void XmlAddAttr(TiXmlElement*, const char *pszName, const char *ptszValue);
+void XmlAddAttrID(TiXmlElement*, int id);
- __forceinline operator HXML() const
+int XmlGetAttrCount(TiXmlElement*);
+const char* XmlGetAttr(TiXmlElement*, int n);
+const char* XmlGetAttrName(TiXmlElement*, int n);
+
+class XmlNode : public TiXmlDocument, private MNonCopyable
+{
+ TiXmlElement *m_hXml;
+
+public:
+ XmlNode(const char *name);
+ XmlNode(const char *pszName, const char *ptszText);
+
+ __forceinline operator TiXmlElement*()
{ return m_hXml;
}
-
-private:
- HXML m_hXml;
};
class CJabberIqInfo;
struct XmlNodeIq : public XmlNode
{
- XmlNodeIq(const wchar_t *type, int id = -1, const wchar_t *to = nullptr);
- XmlNodeIq(const wchar_t *type, const wchar_t *idStr, const wchar_t *to);
- XmlNodeIq(const wchar_t *type, HXML node, const wchar_t *to);
+ XmlNodeIq(const char *type, int id = -1, const char *to = nullptr);
+ XmlNodeIq(const char *type, const char *idStr, const char *to);
+ XmlNodeIq(const char *type, TiXmlElement *node, const char *to);
// new request
XmlNodeIq(CJabberIqInfo *pInfo);
// answer to request
- XmlNodeIq(const wchar_t *type, CJabberIqInfo *pInfo);
+ XmlNodeIq(const char *type, CJabberIqInfo *pInfo);
};
-typedef void (*JABBER_XML_CALLBACK)(HXML, void*);
+typedef void (*JABBER_XML_CALLBACK)(TiXmlElement*, void*);
/////////////////////////////////////////////////////////////////////////////////////////
struct XATTR
{
- const wchar_t *name, *value;
+ const char *name, *value;
- __forceinline XATTR(const wchar_t *_name, const wchar_t *_value) :
+ __forceinline XATTR(const char *_name, const char *_value) :
name(_name),
value(_value)
{}
};
-HXML __forceinline operator<<(HXML node, const XATTR& attr)
-{ XmlAddAttr(node, attr.name, attr.value);
+__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTR& attr)
+{
+ node->SetAttribute(attr.name, attr.value);
return node;
}
@@ -115,17 +91,18 @@ HXML __forceinline operator<<(HXML node, const XATTR& attr)
struct XATTRI
{
- const wchar_t *name;
+ const char *name;
int value;
- __forceinline XATTRI(const wchar_t *_name, int _value) :
+ __forceinline XATTRI(const char *_name, int _value) :
name(_name),
value(_value)
{}
};
-HXML __forceinline operator<<(HXML node, const XATTRI& attr)
-{ XmlAddAttr(node, attr.name, attr.value);
+__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRI& attr)
+{
+ node->SetAttribute(attr.name, attr.value);
return node;
}
@@ -133,17 +110,18 @@ HXML __forceinline operator<<(HXML node, const XATTRI& attr)
struct XATTRI64
{
- const wchar_t *name;
- unsigned __int64 value;
+ const char *name;
+ __int64 value;
- __forceinline XATTRI64(const wchar_t *_name, unsigned __int64 _value) :
+ __forceinline XATTRI64(const char *_name, __int64 _value) :
name(_name),
value(_value)
{}
};
-HXML __forceinline operator<<(HXML node, const XATTRI64& attr)
-{ XmlAddAttr(node, attr.name, attr.value);
+__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRI64& attr)
+{
+ node->SetAttribute(attr.name, attr.value);
return node;
}
@@ -158,8 +136,9 @@ struct XATTRID
{}
};
-HXML __forceinline operator<<(HXML node, const XATTRID& attr)
-{ XmlAddAttrID(node, attr.id);
+__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRID& attr)
+{
+ node->SetAttribute("id", attr.id);
return node;
}
@@ -167,44 +146,45 @@ HXML __forceinline operator<<(HXML node, const XATTRID& attr)
struct XCHILD
{
- const wchar_t *name, *value;
+ const char *name, *value;
- __forceinline XCHILD(const wchar_t *_name, const wchar_t *_value = nullptr) :
+ __forceinline XCHILD(const char *_name, const char *_value = nullptr) :
name(_name),
value(_value)
{}
};
-HXML __forceinline operator<<(HXML node, const XCHILD& child)
-{ return XmlAddChild(node, child.name, child.value);
+__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XCHILD& child)
+{
+ return XmlAddChild(node, child.name, child.value);
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XCHILDNS
{
- const wchar_t *name, *ns;
+ const char *name, *ns;
- __forceinline XCHILDNS(const wchar_t *_name, const wchar_t *_ns = nullptr) :
+ __forceinline XCHILDNS(const char *_name, const char *_ns = nullptr) :
name(_name),
ns(_ns)
{}
};
-HXML __fastcall operator<<(HXML node, const XCHILDNS& child);
+TiXmlElement *__fastcall operator<<(TiXmlElement *node, const XCHILDNS& child);
/////////////////////////////////////////////////////////////////////////////////////////
struct XQUERY
{
- const wchar_t *ns;
+ const char *ns;
- __forceinline XQUERY(const wchar_t *_ns) :
+ __forceinline XQUERY(const char *_ns) :
ns(_ns)
{}
};
-HXML __fastcall operator<<(HXML node, const XQUERY& child);
+TiXmlElement *__fastcall operator<<(TiXmlElement *node, const XQUERY& child);
/////////////////////////////////////////////////////////////////////////////////////////
// Limited XPath support
@@ -213,76 +193,53 @@ HXML __fastcall operator<<(HXML node, const XQUERY& child);
// result may be either "node-spec", or "@attr-name"
//
// Samples:
-// const wchar_t *s = XPathT(node, "child/subchild[@attr='value']"); // get node text
-// const wchar_t *s = XPathT(node, "child/subchild[2]/@attr"); // get attribute value
-// XPathT(node, "child/subchild[@name='test']/@attr") = L"Hello"; // create path if needed and set attribute value
+// const char *s = XPath(node, "child/subchild[@attr='value']"); // get node text
+// XPath(node, "child/subchild[@name='test']/@attr") = L"Hello"; // create path if needed and set attribute value
//
-// XPathT(node, "child/subchild[@name='test']") = L"Hello"; // TODO: create path if needed and set node text
-
-#define XPathT(a,b) XPath(a, _T(b))
+// XPath(node, "child/subchild[@name='test']") = L"Hello"; // TODO: create path if needed and set node text
class XPath
{
public:
- __forceinline XPath(HXML hXml, wchar_t *path):
+ __forceinline XPath(const TiXmlElement *hXml, char *path):
m_type(T_UNKNOWN),
- m_hXml(hXml),
m_szPath(path),
+ m_hXml(hXml),
m_szParam(nullptr)
- {}
+ {}
// Read data
- operator HXML()
+ operator const TiXmlElement*()
{
switch (Lookup())
{
- case T_NODE: return m_hXml;
- case T_NODESET: return XmlGetNthChild(m_hXml, m_szParam, 1);
+ case T_NODE: return m_hXml;
+ case T_NODESET: return m_hXml->FirstChildElement(m_szParam);
}
return nullptr;
}
- operator LPTSTR()
+ operator LPCSTR()
{
switch (Lookup())
{
- case T_ATTRIBUTE: return (wchar_t *)XmlGetAttrValue(m_hXml, m_szParam);
- case T_NODE: return (wchar_t *)XmlGetText(m_hXml);
- case T_NODESET: return (wchar_t *)XmlGetText(XmlGetNthChild(m_hXml, m_szParam, 1));
+ case T_ATTRIBUTE: return m_hXml->Attribute(m_szParam);
+ case T_NODE: return m_hXml->GetText();
+ case T_NODESET: return m_hXml->FirstChildElement()->GetText();
}
return nullptr;
}
operator int()
{
- if (wchar_t *s = *this) return _wtoi(s);
+ if (LPCSTR s = *this) return atoi(s);
return 0;
}
- __forceinline bool operator== (wchar_t *str)
- {
- return !mir_wstrcmp((const wchar_t *)*this, str);
- }
- __forceinline bool operator!= (wchar_t *str)
- {
- return mir_wstrcmp((const wchar_t *)*this, str) ? true : false;
- }
- HXML operator[] (int idx)
+ __forceinline bool operator== (char *str)
{
- return (Lookup() == T_NODESET) ? XmlGetNthChild(m_hXml, m_szParam, idx) : nullptr;
- }
-
- // Write data
- void operator= (const wchar_t *value)
- {
- switch (Lookup(true))
- {
- case T_ATTRIBUTE: XmlAddAttr(m_hXml, m_szParam, value); break;
- case T_NODE: break; // TODO: set node text
- }
+ return !mir_strcmp(*this, str);
}
- void operator= (int value)
+ __forceinline bool operator!= (char *str)
{
- wchar_t buf[16];
- _itow(value, buf, 10);
- *this = buf;
+ return mir_strcmp(*this, str) ? true : false;
}
private:
@@ -295,9 +252,9 @@ private:
T_NODESET
};
- __forceinline PathType Lookup(bool bCreate=false)
+ __forceinline PathType Lookup()
{
- return (m_type == T_UNKNOWN) ? LookupImpl(bCreate) : m_type;
+ return (m_type == T_UNKNOWN) ? LookupImpl() : m_type;
}
enum LookupState
@@ -306,7 +263,6 @@ private:
S_ATTR_STEP,
S_NODE_NAME,
S_NODE_OPENBRACKET,
- S_NODE_INDEX,
S_NODE_ATTRNAME,
S_NODE_ATTREQUALS,
S_NODE_ATTRVALUE,
@@ -322,11 +278,11 @@ private:
struct LookupString
{
- void Begin(const wchar_t *p_) { p = p_; }
- void End(const wchar_t *p_) { length = p_ - p; }
+ void Begin(const char *p_) { p = p_; }
+ void End(const char *p_) { length = p_ - p; }
operator bool() { return p ? true : false; }
- const wchar_t *p;
+ const char *p;
int length;
};
@@ -337,34 +293,22 @@ private:
LookupString nodeName;
LookupString attrName;
LookupString attrValue;
- LookupString nodeIndex;
};
- void ProcessPath(LookupInfo &info, bool bCreate);
- PathType LookupImpl(bool bCreate);
+ void ProcessPath(LookupInfo &info);
+ PathType LookupImpl();
PathType m_type;
- HXML m_hXml;
- const wchar_t *m_szPath;
- const wchar_t *m_szParam;
+ const TiXmlElement *m_hXml;
+ const char *m_szPath;
+ const char *m_szParam;
};
class XPathFmt: public XPath
{
public:
enum { BUFSIZE = 512 };
- XPathFmt(HXML hXml, wchar_t *path, ...): XPath(hXml, m_buf)
- {
- *m_buf = 0;
-
- va_list args;
- va_start(args, path);
- mir_vsnwprintf(m_buf, BUFSIZE, path, args);
- m_buf[BUFSIZE-1] = 0;
- va_end(args);
- }
-
- XPathFmt(HXML hXml, char *path, ...): XPath(hXml, m_buf)
+ XPathFmt(const TiXmlElement *hXml, char *path, ...): XPath(hXml, m_buf)
{
*m_buf = 0;
char buf[BUFSIZE];
@@ -373,12 +317,11 @@ public:
va_start(args, path);
mir_vsnprintf(buf, BUFSIZE, path, args);
buf[BUFSIZE-1] = 0;
- MultiByteToWideChar(CP_ACP, 0, buf, -1, m_buf, BUFSIZE);
va_end(args);
}
private:
- wchar_t m_buf[BUFSIZE];
+ char m_buf[BUFSIZE];
};
#endif