summaryrefslogtreecommitdiff
path: root/src/modules/xml/xmlApi.cpp
blob: b010d182d91b6b95f275d71e8c603db14a632c60 (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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-12 Miranda IM, 2012-13 Miranda NG project, 
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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.
*/

#include "..\..\core\commonheaders.h"
#include "xmlParser.h"

static HXML xmlapiCreateNode(LPCTSTR name, LPCTSTR text, char isDeclaration)
{
	XMLNode result = XMLNode::createXMLTopNode(name, isDeclaration);
	if (text)
		result.updateText(text);
	return result.detach();
}

static void xmlapiDestroyNode(HXML n)
{
	XMLNode tmp; tmp.attach(n);
}

static HXML xmlapiParseFile(LPCTSTR str, int* datalen, LPCTSTR tag)
{
	if (str == NULL) return NULL;

	XMLResults res;
	XMLNode result = XMLNode::parseFile(str, tag, &res);

	if (datalen != NULL)
		datalen[0] += res.nChars;

	return (res.error == eXMLErrorNone || (tag != NULL && res.error == eXMLErrorMissingEndTag)) ? result.detach() : NULL;
}

static HXML xmlapiParseString(LPCTSTR str, int* datalen, LPCTSTR tag)
{
	if (str == NULL) return NULL;

	XMLResults res;
	XMLNode result = XMLNode::parseString(str, tag, &res);

	if (datalen != NULL)
		datalen[0] += res.nChars;

	return (res.error == eXMLErrorNone || (tag != NULL && res.error == eXMLErrorMissingEndTag)) ? result.detach() : NULL;
}

static HXML xmlapiAddChild(HXML _n, LPCTSTR name, LPCTSTR text)
{
	XMLNode result = XMLNode(_n).addChild(name);
	if (text != NULL)
		result.updateText(text);
	return result;
}

static void xmlapiAddChild2(HXML _child, HXML _parent)
{
	XMLNode child(_child), parent(_parent);
	parent.addChild(child);
}

static HXML xmlapiCopyNode(HXML _n)
{
	XMLNode result = XMLNode(_n);
	return result.detach();
}

static LPCTSTR xmlapiGetAttr(HXML _n, int i)
{
	return XMLNode(_n).getAttributeValue(i);
}

static int xmlapiGetAttrCount(HXML _n)
{
	return XMLNode(_n).nAttribute();
}

static LPCTSTR xmlapiGetAttrName(HXML _n, int i)
{
	return XMLNode(_n).getAttributeName(i);
}

static HXML xmlapiGetChild(HXML _n, int i)
{
	return XMLNode(_n).getChildNode(i);
}

static HXML xmlapiGetChildByAttrValue(HXML _n, LPCTSTR name, LPCTSTR attrName, LPCTSTR attrValue)
{
	return XMLNode(_n).getChildNodeWithAttribute(name, attrName, attrValue);
}

static int xmlapiGetChildCount(HXML _n)
{
	return XMLNode(_n).nChildNode();
}

static HXML xmlapiGetFirstChild(HXML _n)
{
	return XMLNode(_n).getChildNode(0);
}

static HXML xmlapiGetNthChild(HXML _n, LPCTSTR name, int i)
{
	return XMLNode(_n).getChildNode(name, i);
}

static HXML xmlapiGetNextChild(HXML _n, LPCTSTR name, int* i)
{
	return XMLNode(_n).getChildNode(name, i);
}

static HXML xmlapiGetNextNode(HXML _n)
{
	return XMLNode(_n).getNextNode();
}

static HXML xmlapiGetChildByPath(HXML _n, LPCTSTR path, char createNodeIfMissing)
{
	return XMLNode(_n).getChildNodeByPath(path, createNodeIfMissing);
}

static LPCTSTR xmlapiGetName(HXML _n)
{
	return XMLNode(_n).getName();
}

static HXML xmlapiGetParent(HXML _n)
{
	return XMLNode(_n).getParentNode();
}

static LPCTSTR xmlapiGetText(HXML _n)
{
	return XMLNode(_n).getInnerText();
}

static LPCTSTR xmlapiGetAttrValue(HXML _n, LPCTSTR attrName)
{
	return XMLNode(_n).getAttribute(attrName);
}

static void xmlapiSetText(HXML _n, LPCTSTR _text)
{
	XMLNode(_n).updateText(_text);
}

static LPTSTR xmlapiToString(HXML _n, int* datalen)
{
	return XMLNode(_n).createXMLString(0, datalen);
}

static XMLError xmlapiToFile(HXML _n, LPCTSTR filename, int withformatting)
{
	return XMLNode(_n).writeToFile(filename, NULL, withformatting);
}

static void xmlapiAddAttr(HXML _n, LPCTSTR attrName, LPCTSTR attrValue)
{
	if (attrName != NULL && attrValue != NULL)
		XMLNode(_n).addAttribute(attrName, attrValue);
}

static void xmlapiAddAttrInt(HXML _n, LPCTSTR attrName, int attrValue)
{
	TCHAR buf[40];
	_itot(attrValue, buf, 10);
	XMLNode(_n).addAttribute(attrName, buf);
}

static void xmlapiFree(void* p)
{
	free(p);
}

// XML API v2 methods
static int xmlapiGetTextCount(HXML _n)
{
	return XMLNode(_n).nText();
}

static LPCTSTR xmlapiGetTextByIndex(HXML _n, int i)
{
	return XMLNode(_n).getText(i);
}

static void xmlapiSetTextByIndex(HXML _n, int i, LPCTSTR value)
{
	XMLNode(_n).updateText(value, i);
}

static void xmlapiAddText(HXML _n, LPCTSTR value, XML_ELEMENT_POS pos)
{
	XMLNode(_n).addText(value, (XMLElementPosition)pos);
}

static LPTSTR xmlapiToStringWithFormatting(HXML _n, int* datalen)
{
	return XMLNode(_n).createXMLString(1, datalen);
}

static int xmlapiGetClearCount(HXML _n)
{
	return XMLNode(_n).nClear();
}

static LPCTSTR xmlapiGetClear(HXML _n, int i, LPCTSTR *openTag, LPCTSTR *closeTag)
{
	XMLClear c = XMLNode(_n).getClear(i);
	if (openTag)
		*openTag = c.lpszOpenTag;
	if (closeTag)
		*closeTag = c.lpszCloseTag;
	return c.lpszValue;
}

static void xmlapiAddClear(HXML _n, LPCTSTR lpszValue, LPCTSTR openTag, LPCTSTR closeTag, XML_ELEMENT_POS pos)
{
	XMLNode(_n).addClear(lpszValue, openTag, closeTag, (XMLElementPosition)pos);
}

static void xmlapiSetClear(HXML _n, int i, LPCTSTR lpszValue)
{
	XMLNode(_n).updateClear(lpszValue, i);
}

static int xmlapiGetElement(HXML _n, XML_ELEMENT_POS pos, XML_ELEMENT_TYPE *type, HXML *child, LPCTSTR *value, LPCTSTR *name, LPCTSTR *openTag, LPCTSTR *closeTag)
{
	// reset all values
	if (child)
		*child = NULL;
	if (value)
		*value = NULL;
	if (name)
		*name = NULL;
	if (openTag)
		*openTag = NULL;
	if (closeTag)
		*closeTag = NULL;

	if ( !type || pos >= XMLNode(_n).nElement())
		return false;
	XMLNodeContents c(XMLNode(_n).enumContents((XMLElementPosition)pos));
	switch (c.etype) {
		case eNodeChild:
		{
			*type = XML_ELEM_TYPE_CHILD;
			if (child)
				*child = c.child;
		} break;
		case eNodeAttribute:
		{
			*type = XML_ELEM_TYPE_ATTRIBUTE;
			if (name)
				*name = c.attrib.lpszName;
			if (value)
				*value = c.attrib.lpszValue;
		} break;
		case eNodeText:
		{
			*type = XML_ELEM_TYPE_TEXT;
			if (value)
				*value = c.text;
		} break;
		case eNodeClear:
		{
			*type = XML_ELEM_TYPE_CLEAR;
			if (value)
				*value = c.clear.lpszValue;
			if (openTag)
				*openTag = c.clear.lpszOpenTag;
			if (closeTag)
				*closeTag = c.clear.lpszCloseTag;
		} break;
		case eNodeNULL:
		{
			return false;
		} break;
	}
	return true;
}

static int xmlapiGetElementCount(HXML _n)
{
	return XMLNode(_n).nElement();
}

static char xmlapiIsDeclaration(HXML _n)
{
	return XMLNode(_n).isDeclaration();
}

static HXML xmlapiDeepCopy(HXML _n)
{
	return XMLNode(_n).deepCopy().detach();
}

static HXML xmlapiAddChildEx(HXML _n, LPCTSTR name, char isDeclaration, XML_ELEMENT_POS pos)
{
	return XMLNode(_n).addChild(name, isDeclaration, (XMLElementPosition)pos);
}

static void xmlapiAddChildEx2(HXML _n, HXML parent, XML_ELEMENT_POS pos)
{
	XMLNode(_n).addChild(parent, (XMLElementPosition)pos);
}

static void xmlapiSetAttrByIndex(HXML _n, int i, LPCTSTR value)
{
	XMLNode(_n).updateAttribute(value, NULL, i);
}

static void xmlapiSetAttrByName(HXML _n, LPCTSTR name, LPCTSTR value)
{
	XMLNode(_n).updateAttribute(value, NULL, name);
}

static void xmlapiDeleteNodeContent(HXML _n)
{
	XMLNode(_n).deleteNodeContent();
}

static void xmlapiDeleteAttrByIndex(HXML _n, int i)
{
	XMLNode(_n).deleteAttribute(i);
}

static void xmlapiDeleteAttrByName(HXML _n, LPCTSTR name)
{
	XMLNode(_n).deleteAttribute(name);
}

static void xmlapiDeleteText(HXML _n, int i)
{
	XMLNode(_n).deleteText(i);
}

static void xmlapiDeleteClear(HXML _n, int i)
{
	XMLNode(_n).deleteClear(i);
}

static XML_ELEMENT_POS xmlapiPositionOfText(HXML _n, int i)
{
	return (XML_ELEMENT_POS)XMLNode(_n).positionOfText(i);
}

static XML_ELEMENT_POS xmlapiPositionOfClear(HXML _n, int i)
{
	return (XML_ELEMENT_POS)XMLNode(_n).positionOfClear(i);
}

static XML_ELEMENT_POS xmlapiPositionOfChildByIndex(HXML _n, int i)
{
	return (XML_ELEMENT_POS)XMLNode(_n).positionOfChildNode(i);
}

static XML_ELEMENT_POS xmlapiPositionOfChildByNode(HXML _n, HXML child)
{
	return (XML_ELEMENT_POS)XMLNode(_n).positionOfChildNode(child);
}

static XML_ELEMENT_POS xmlapiPositionOfChildByName(HXML _n, LPCTSTR name, int i)
{
	return (XML_ELEMENT_POS)XMLNode(_n).positionOfChildNode(name, i);
}

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

static INT_PTR GetXmlApi(WPARAM, LPARAM lParam)
{
	XML_API* xi = (XML_API*)lParam;
	if (xi == NULL)
		return FALSE;

	if (xi->cbSize != XML_API_SIZEOF_V1 && xi->cbSize != sizeof(XML_API))
		return FALSE;

	xi->createNode = xmlapiCreateNode;
	xi->destroyNode = xmlapiDestroyNode;

	xi->parseString = xmlapiParseString;
	xi->toString = xmlapiToString;
	xi->freeMem = xmlapiFree;
	xi->parseFile = xmlapiParseFile;
	xi->toFile = xmlapiToFile;

	xi->addChild = xmlapiAddChild;
	xi->addChild2 = xmlapiAddChild2;
	xi->copyNode = xmlapiCopyNode;
	xi->getChild = xmlapiGetChild;
	xi->getChildByAttrValue = xmlapiGetChildByAttrValue;
	xi->getChildCount = xmlapiGetChildCount;
	xi->getFirstChild = xmlapiGetFirstChild;
	xi->getNthChild = xmlapiGetNthChild;
	xi->getNextChild = xmlapiGetNextChild;
	xi->getNextNode = xmlapiGetNextNode;
	xi->getChildByPath = xmlapiGetChildByPath;
	xi->getName = xmlapiGetName;
	xi->getParent = xmlapiGetParent;
	xi->getText = xmlapiGetText;
	xi->setText = xmlapiSetText;

	xi->getAttr = xmlapiGetAttr;
	xi->getAttrCount = xmlapiGetAttrCount;
	xi->getAttrName = xmlapiGetAttrName;
	xi->getAttrValue = xmlapiGetAttrValue;
	xi->addAttr = xmlapiAddAttr;
	xi->addAttrInt = xmlapiAddAttrInt;

	if (xi->cbSize > XML_API_SIZEOF_V1) {
		xi->isDeclaration = xmlapiIsDeclaration;
		xi->toStringWithFormatting = xmlapiToStringWithFormatting;
		xi->deepCopy = xmlapiDeepCopy;
		xi->setAttrByIndex = xmlapiSetAttrByIndex;
		xi->setAttrByName = xmlapiSetAttrByName;
		xi->addChildEx = xmlapiAddChildEx;
		xi->addChildEx2 = xmlapiAddChildEx2;
		xi->getTextCount = xmlapiGetTextCount;
		xi->getTextByIndex = xmlapiGetTextByIndex;
		xi->addText = xmlapiAddText;
		xi->setTextByIndex = xmlapiSetTextByIndex;
		xi->getClearCount = xmlapiGetClearCount;
		xi->getClear = xmlapiGetClear;
		xi->addClear = xmlapiAddClear;
		xi->setClear = xmlapiSetClear;
		xi->getElementCount = xmlapiGetElementCount;
		xi->getElement = xmlapiGetElement;

		xi->deleteNodeContent = xmlapiDeleteNodeContent;
		xi->deleteAttrByIndex = xmlapiDeleteAttrByIndex;
		xi->deleteAttrByName = xmlapiDeleteAttrByName;
		xi->deleteText = xmlapiDeleteText;
		xi->deleteClear = xmlapiDeleteClear;

		xi->positionOfChildByIndex = xmlapiPositionOfChildByIndex;
		xi->positionOfChildByNode = xmlapiPositionOfChildByNode;
		xi->positionOfChildByName = xmlapiPositionOfChildByName;
		xi->positionOfText = xmlapiPositionOfText;
		xi->positionOfClear = xmlapiPositionOfClear;
	}
	return TRUE;
}

void InitXmlApi(void)
{
	CreateServiceFunction(MS_SYSTEM_GET_XI, GetXmlApi);
}