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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
Jabber Protocol Plugin for Miranda NG
Copyright (c) 2002-04 Santithorn Bunchua
Copyright (c) 2005-12 George Hazan
Copyright (c) 2007 Maxim Mluhov
Copyright (ñ) 2012-16 Miranda NG project
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_
#include <m_xml.h>
void __fastcall XmlAddChild(HXML, HXML);
HXML __fastcall XmlAddChild(HXML, LPCTSTR pszName);
HXML __fastcall XmlAddChild(HXML, LPCTSTR pszName, LPCTSTR ptszValue);
HXML __fastcall XmlAddChild(HXML, LPCTSTR pszName, int iValue);
LPCTSTR __fastcall XmlGetAttrValue(HXML, LPCTSTR key);
HXML __fastcall XmlGetChild(HXML, int n = 0);
HXML __fastcall XmlGetChild(HXML, LPCSTR key);
HXML __fastcall XmlGetChild(HXML, LPCTSTR key);
int __fastcall XmlGetChildCount(HXML);
HXML __fastcall XmlGetChildByTag(HXML, LPCTSTR key, LPCTSTR attrName, LPCTSTR attrValue);
HXML __fastcall XmlGetChildByTag(HXML, LPCSTR key, LPCSTR attrName, LPCTSTR attrValue);
HXML __fastcall XmlGetNthChild(HXML, LPCTSTR key, int n = 0);
LPCTSTR __fastcall XmlGetName(HXML);
LPCTSTR __fastcall XmlGetText(HXML);
void __fastcall XmlAddAttr(HXML, LPCTSTR pszName, LPCTSTR ptszValue);
void __fastcall XmlAddAttr(HXML, LPCTSTR pszName, int value);
void __fastcall XmlAddAttr(HXML hXml, LPCTSTR pszName, unsigned __int64 value);
void __fastcall XmlAddAttrID(HXML, int id);
int __fastcall XmlGetAttrCount(HXML);
LPCTSTR __fastcall XmlGetAttr(HXML, int n);
LPCTSTR __fastcall XmlGetAttrName(HXML, int n);
LPCTSTR __fastcall XmlGetAttrValue(HXML, LPCTSTR key);
struct XmlNode
{
__forceinline XmlNode() { m_hXml = NULL; }
__forceinline XmlNode(LPCTSTR pszString, int* numBytes, LPCTSTR ptszTag)
{
m_hXml = xmlParseString(pszString, numBytes, ptszTag);
}
XmlNode(const XmlNode& n);
XmlNode(LPCTSTR name);
XmlNode(LPCTSTR pszName, LPCTSTR ptszText);
~XmlNode();
XmlNode& operator =(const XmlNode& n);
__forceinline operator HXML() const
{ return m_hXml;
}
private:
HXML m_hXml;
};
class CJabberIqInfo;
struct XmlNodeIq : public XmlNode
{
XmlNodeIq(const TCHAR *type, int id = -1, const TCHAR *to = NULL);
XmlNodeIq(const TCHAR *type, const TCHAR *idStr, const TCHAR *to);
XmlNodeIq(const TCHAR *type, HXML node, const TCHAR *to);
// new request
XmlNodeIq(CJabberIqInfo *pInfo);
// answer to request
XmlNodeIq(const TCHAR *type, CJabberIqInfo *pInfo);
};
typedef void (*JABBER_XML_CALLBACK)(HXML, void*);
/////////////////////////////////////////////////////////////////////////////////////////
struct XATTR
{
LPCTSTR name, value;
__forceinline XATTR(LPCTSTR _name, LPCTSTR _value) :
name(_name),
value(_value)
{}
};
HXML __forceinline operator<<(HXML node, const XATTR& attr)
{ XmlAddAttr(node, attr.name, attr.value);
return node;
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XATTRI
{
LPCTSTR name;
int value;
__forceinline XATTRI(LPCTSTR _name, int _value) :
name(_name),
value(_value)
{}
};
HXML __forceinline operator<<(HXML node, const XATTRI& attr)
{ XmlAddAttr(node, attr.name, attr.value);
return node;
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XATTRI64
{
LPCTSTR name;
unsigned __int64 value;
__forceinline XATTRI64(LPCTSTR _name, unsigned __int64 _value) :
name(_name),
value(_value)
{}
};
HXML __forceinline operator<<(HXML node, const XATTRI64& attr)
{ XmlAddAttr(node, attr.name, attr.value);
return node;
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XATTRID
{
int id;
__forceinline XATTRID(int _value) :
id(_value)
{}
};
HXML __forceinline operator<<(HXML node, const XATTRID& attr)
{ XmlAddAttrID(node, attr.id);
return node;
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XCHILD
{
LPCTSTR name, value;
__forceinline XCHILD(LPCTSTR _name, LPCTSTR _value = NULL) :
name(_name),
value(_value)
{}
};
HXML __forceinline operator<<(HXML node, const XCHILD& child)
{ return XmlAddChild(node, child.name, child.value);
}
/////////////////////////////////////////////////////////////////////////////////////////
struct XCHILDNS
{
LPCTSTR name, ns;
__forceinline XCHILDNS(LPCTSTR _name, LPCTSTR _ns = NULL) :
name(_name),
ns(_ns)
{}
};
HXML __fastcall operator<<(HXML node, const XCHILDNS& child);
/////////////////////////////////////////////////////////////////////////////////////////
struct XQUERY
{
LPCTSTR ns;
__forceinline XQUERY(LPCTSTR _ns) :
ns(_ns)
{}
};
HXML __fastcall operator<<(HXML 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:
// LPCTSTR s = XPathT(node, "child/subchild[@attr='value']"); // get node text
// LPCTSTR s = XPathT(node, "child/subchild[2]/@attr"); // get attribute value
// XPathT(node, "child/subchild[@name='test']/@attr") = _T("Hello"); // create path if needed and set attribute value
//
// XPathT(node, "child/subchild[@name='test']") = _T("Hello"); // TODO: create path if needed and set node text
#define XPathT(a,b) XPath(a, _T(b))
class XPath
{
public:
__forceinline XPath(HXML hXml, TCHAR *path):
m_type(T_UNKNOWN),
m_hXml(hXml),
m_szPath(path),
m_szParam(NULL)
{}
// Read data
operator HXML()
{
switch (Lookup())
{
case T_NODE: return m_hXml;
case T_NODESET: return XmlGetNthChild(m_hXml, m_szParam, 1);
}
return NULL;
}
operator LPTSTR()
{
switch (Lookup())
{
case T_ATTRIBUTE: return (TCHAR *)XmlGetAttrValue(m_hXml, m_szParam);
case T_NODE: return (TCHAR *)XmlGetText(m_hXml);
case T_NODESET: return (TCHAR *)XmlGetText(XmlGetNthChild(m_hXml, m_szParam, 1));
}
return NULL;
}
operator int()
{
if (TCHAR *s = *this) return _ttoi(s);
return 0;
}
__forceinline bool operator== (TCHAR *str)
{
return !mir_tstrcmp((LPCTSTR)*this, str);
}
__forceinline bool operator!= (TCHAR *str)
{
return mir_tstrcmp((LPCTSTR)*this, str) ? true : false;
}
HXML operator[] (int idx)
{
return (Lookup() == T_NODESET) ? XmlGetNthChild(m_hXml, m_szParam, idx) : NULL;
}
// Write data
void operator= (LPCTSTR value)
{
switch (Lookup(true))
{
case T_ATTRIBUTE: XmlAddAttr(m_hXml, m_szParam, value); break;
case T_NODE: break; // TODO: set node text
}
}
void operator= (int value)
{
TCHAR buf[16];
_itot(value, buf, 10);
*this = buf;
}
private:
enum PathType
{
T_UNKNOWN,
T_ERROR,
T_NODE,
T_ATTRIBUTE,
T_NODESET
};
__forceinline PathType Lookup(bool bCreate=false)
{
return (m_type == T_UNKNOWN) ? LookupImpl(bCreate) : m_type;
}
enum LookupState
{
S_START,
S_ATTR_STEP,
S_NODE_NAME,
S_NODE_OPENBRACKET,
S_NODE_INDEX,
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 TCHAR *p_) { p = p_; }
void End(const TCHAR *p_) { length = p_ - p; }
operator bool() { return p ? true : false; }
const TCHAR *p;
int length;
};
struct LookupInfo
{
void Reset() { memset(this, 0, sizeof(*this)); }
LookupString nodeName;
LookupString attrName;
LookupString attrValue;
LookupString nodeIndex;
};
void ProcessPath(LookupInfo &info, bool bCreate);
PathType LookupImpl(bool bCreate);
PathType m_type;
HXML m_hXml;
LPCTSTR m_szPath;
LPCTSTR m_szParam;
};
class XPathFmt: public XPath
{
public:
enum { BUFSIZE = 512 };
XPathFmt(HXML hXml, TCHAR *path, ...): XPath(hXml, m_buf)
{
*m_buf = 0;
va_list args;
va_start(args, path);
mir_vsntprintf(m_buf, BUFSIZE, path, args);
m_buf[BUFSIZE-1] = 0;
va_end(args);
}
XPathFmt(HXML 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;
MultiByteToWideChar(CP_ACP, 0, buf, -1, m_buf, BUFSIZE);
va_end(args);
}
private:
TCHAR m_buf[BUFSIZE];
};
#endif
|