summaryrefslogtreecommitdiff
path: root/protocols/JabberG/src/jabber_xml.h
blob: b21738e870a3203f6af0515bf3865dbb8c6a6231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*

Jabber Protocol Plugin for Miranda NG

Copyright (c) 2002-04  Santithorn Bunchua
Copyright (c) 2005-12  George Hazan
Copyright (c) 2007     Maxim Mluhov
Copyright (C) 2012-19 Miranda NG team

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.

*/

#ifndef _JABBER_XML_H_
#define _JABBER_XML_H_

int XmlGetChildCount(const TiXmlElement*);

TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName);
TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName, const char *ptszValue);
TiXmlElement* XmlAddChild(TiXmlElement*, const char *pszName, int iValue);

int XmlGetChildInt(const TiXmlElement *hXml, const char *key);
const char* XmlGetChildText(const TiXmlElement *hXml, const char *key);
const TiXmlElement* XmlGetChildByTag(const TiXmlElement *hXml, const char *key, const char *attrName, const char *attrValue);

void XmlAddAttr(TiXmlElement*, const char *pszName, const char *ptszValue);
void XmlAddAttrID(TiXmlElement*, int id);

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;
	}
};

class CJabberIqInfo;

struct XmlNodeIq : public XmlNode
{
	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 char *type, CJabberIqInfo *pInfo);
};

typedef void (*JABBER_XML_CALLBACK)(TiXmlElement*, void*);

/////////////////////////////////////////////////////////////////////////////////////////

struct XATTR
{
	const char *name, *value;

	__forceinline XATTR(const char *_name, const char *_value) :
		name(_name),
		value(_value)
		{}
};

__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTR& attr)
{
	node->SetAttribute(attr.name, attr.value);
	return node;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct XATTRI
{
	const char *name;
	int value;

	__forceinline XATTRI(const char *_name, int _value) :
		name(_name),
		value(_value)
		{}
};

__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRI& attr)
{
	node->SetAttribute(attr.name, attr.value);
	return node;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct XATTRI64
{
	const char *name;
	__int64 value;

	__forceinline XATTRI64(const char *_name, __int64 _value) :
		name(_name),
		value(_value)
		{}
};

__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRI64& attr)
{
	node->SetAttribute(attr.name, attr.value);
	return node;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct XATTRID
{
	int id;

	__forceinline XATTRID(int _value) :
		id(_value)
		{}
};

__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XATTRID& attr)
{
	XmlAddAttrID(node, attr.id);
	return node;
}

/////////////////////////////////////////////////////////////////////////////////////////

struct XCHILD
{
	const char *name, *value;

	__forceinline XCHILD(const char *_name, const char *_value = nullptr) :
		name(_name),
		value(_value)
		{}
};

__forceinline TiXmlElement *operator<<(TiXmlElement *node, const XCHILD &child)
{
	return XmlAddChild(node, child.name, child.value);
}

/////////////////////////////////////////////////////////////////////////////////////////

struct XCHILDNS
{
	const char *name, *ns;

	__forceinline XCHILDNS(const char *_name, const char *_ns = nullptr) :
		name(_name),
		ns(_ns)
		{}
};

TiXmlElement* __fastcall operator<<(TiXmlElement *node, const XCHILDNS &child);

/////////////////////////////////////////////////////////////////////////////////////////

struct XQUERY
{
	const char *ns;

	__forceinline XQUERY(const char *_ns) :
		ns(_ns)
		{}
};

TiXmlElement* __fastcall operator<<(TiXmlElement *node, const XQUERY& child);

/////////////////////////////////////////////////////////////////////////////////////////
// Limited XPath support
//     path should look like: "node-spec/node-spec/.../result-spec"
//     where "node-spec" can be "node-name", "node-name[@attr-name='attr-value']" or "node-name[node-index]"
//     result may be either "node-spec", or "@attr-name"
//
// Samples:
//    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
//
//    XPath(node, "child/subchild[@name='test']") = L"Hello";         // TODO: create path if needed and set node text

class XPath
{
public:
	__forceinline XPath(const TiXmlElement *hXml, char *path):
		m_type(T_UNKNOWN),
		m_szPath(path),
		m_hXml(hXml),
		m_szParam(nullptr)
	{}

	// Read data
	operator const TiXmlElement*()
	{
		switch (Lookup())
		{
			case T_NODE:    return m_hXml;
			case T_NODESET: return m_hXml->FirstChildElement(m_szParam);
		}
		return nullptr;
	}
	operator LPCSTR()
	{
		switch (Lookup())
		{
			case T_ATTRIBUTE: return m_hXml->Attribute(m_szParam);
			case T_NODE:      return m_hXml->GetText();
			case T_NODESET:   return (m_hXml->FirstChildElement()) ? m_hXml->FirstChildElement()->GetText() : 0;
		}
		return nullptr;
	}
	__forceinline bool operator== (char *str)
	{
		return !mir_strcmp(*this, str);
	}
	__forceinline bool operator!= (char *str)
	{
		return mir_strcmp(*this, str) ? true : false;
	}

private:
	enum PathType
	{
		T_UNKNOWN,
		T_ERROR,
		T_NODE,
		T_ATTRIBUTE,
		T_NODESET
	};

	__forceinline PathType Lookup()
	{
		return (m_type == T_UNKNOWN) ? LookupImpl() : m_type;
	}

	enum LookupState
	{
		S_START,
		S_ATTR_STEP,
		S_NODE_NAME,
		S_NODE_OPENBRACKET,
		S_NODE_ATTRNAME,
		S_NODE_ATTREQUALS,
		S_NODE_ATTRVALUE,
		S_NODE_ATTRCLOSEVALUE,
		S_NODE_CLOSEBRACKET,

		S_FINAL,
		S_FINAL_ERROR,
		S_FINAL_ATTR,
		S_FINAL_NODESET,
		S_FINAL_NODE
	};

	struct LookupString
	{
		void Begin(const char *p_) { p = p_; }
		void End(const char *p_) { length = p_ - p; }
		operator bool() { return p ? true : false; }

		const char *p;
		int length;

	};

	struct LookupInfo
	{
		void Reset() { memset(this, 0, sizeof(*this)); }
		LookupString nodeName;
		LookupString attrName;
		LookupString attrValue;
	};

	void ProcessPath(LookupInfo &info);
	PathType LookupImpl();

	PathType m_type;
	const TiXmlElement *m_hXml;
	const char *m_szPath;
	const char *m_szParam;
};

class XPathFmt: public XPath
{
public:
	enum { BUFSIZE = 512 };
	XPathFmt(const TiXmlElement *hXml, char *path, ...): XPath(hXml, m_buf)
	{
		*m_buf = 0;
		char buf[BUFSIZE];

		va_list args;
		va_start(args, path);
		mir_vsnprintf(buf, BUFSIZE, path, args);
		buf[BUFSIZE-1] = 0;
		va_end(args);
	}

private:
	char m_buf[BUFSIZE];
};

#endif