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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine" and the "irrXML" project.
// For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h
#ifndef __IRR_XML_H_INCLUDED__
#define __IRR_XML_H_INCLUDED__
#include <stdio.h>
#include "IrrCompileConfig.h"
/** \mainpage irrXML 1.2 API documentation
<div align="center"><img src="logobig.png" ></div>
\section intro Introduction
Welcome to the irrXML API documentation.
Here you'll find any information you'll need to develop applications with
irrXML. If you look for a tutorial on how to start, take a look at the \ref irrxmlexample,
at the homepage of irrXML at <A HREF="http://www.ambiera.com/irrxml/">www.ambiera.com/irrxml/</A>
or into the SDK in the directory example.
irrXML is intended to be a high speed and easy-to-use XML Parser for C++, and
this documentation is an important part of it. If you have any questions or
suggestions, just send a email to the author of the engine, Nikolaus Gebhardt
(niko (at) irrlicht3d.org). For more informations about this parser, see \ref history.
\section features Features
irrXML provides forward-only, read-only
access to a stream of non validated XML data. It was fully implemented by
Nikolaus Gebhardt. Its current features are:
- It it fast as lighting and has very low memory usage. It was
developed with the intention of being used in 3D games, as it already has been.
- irrXML is very small: It only consists of 60 KB of code and can be added easily
to your existing project.
- Of course, it is platform independent and works with lots of compilers.
- It is able to parse ASCII, UTF-8, UTF-16 and UTF-32 text files, both in
little and big endian format.
- Independent of the input file format, the parser can return all strings in ASCII, UTF-8,
UTF-16 and UTF-32 format.
- With its optional file access abstraction it has the advantage that it can read not
only from files but from any type of data (memory, network, ...). For example when
used with the Irrlicht Engine, it directly reads from compressed .zip files.
- Just like the Irrlicht Engine for which it was originally created, it is extremely easy
to use.
- It has no external dependencies, it does not even need the STL.
Although irrXML has some strenghts, it currently also has the following limitations:
- The input xml file is not validated and assumed to be correct.
\section irrxmlexample Example
The following code demonstrates the basic usage of irrXML. A simple xml
file like this is parsed:
\code
<?xml version="1.0"?>
<config>
<!-- This is a config file for the mesh viewer -->
<model file="dwarf.dea" />
<messageText caption="Irrlicht Engine Mesh Viewer">
Welcome to the Mesh Viewer of the "Irrlicht Engine".
</messageText>
</config>
\endcode
The code for parsing this file would look like this:
\code
#include <irrXML.h>
using namespace irr; // irrXML is located in the namespace irr::io
using namespace io;
#include <string> // we use STL strings to store data in this example
void main()
{
// create the reader using one of the factory functions
IrrXMLReader* xml = createIrrXMLReader("config.xml");
// strings for storing the data we want to get out of the file
std::string modelFile;
std::string messageText;
std::string caption;
// parse the file until end reached
while(xml && xml->read())
{
switch(xml->getNodeType())
{
case EXN_TEXT:
// in this xml file, the only text which occurs is the messageText
messageText = xml->getNodeData();
break;
case EXN_ELEMENT:
{
if (!strcmp("model", xml->getNodeName()))
modelFile = xml->getAttributeValue("file");
else
if (!strcmp("messageText", xml->getNodeName()))
caption = xml->getAttributeValue("caption");
}
break;
}
}
// delete the xml parser after usage
delete xml;
}
\endcode
\section howto How to use
Simply add the source files in the /src directory of irrXML to your project. Done.
\section license License
The irrXML license is based on the zlib license. Basicly, this means you can do with
irrXML whatever you want:
Copyright (C) 2002-2012 Nikolaus Gebhardt
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
\section history History
As lots of references in this documentation and the source show, this xml
parser has originally been a part of the
<A HREF="http://irrlicht.sourceforge.net" >Irrlicht Engine</A>. But because
the parser has become very useful with the latest release, people asked for a
separate version of it, to be able to use it in non Irrlicht projects. With
irrXML 1.0, this has now been done.
*/
namespace irr
{
namespace io
{
//! Enumeration of all supported source text file formats
enum ETEXT_FORMAT
{
//! ASCII, file without byte order mark, or not a text file
ETF_ASCII,
//! UTF-8 format
ETF_UTF8,
//! UTF-16 format, big endian
ETF_UTF16_BE,
//! UTF-16 format, little endian
ETF_UTF16_LE,
//! UTF-32 format, big endian
ETF_UTF32_BE,
//! UTF-32 format, little endian
ETF_UTF32_LE
};
//! Enumeration for all xml nodes which are parsed by IrrXMLReader
enum EXML_NODE
{
//! No xml node. This is usually the node if you did not read anything yet.
EXN_NONE,
//! An xml element such as <foo>
EXN_ELEMENT,
//! End of an xml element such as </foo>
EXN_ELEMENT_END,
//! Text within an xml element: <foo> this is the text. </foo>
//! Also text between 2 xml elements: </foo> this is the text. <foo>
EXN_TEXT,
//! An xml comment like <!-- I am a comment --> or a DTD definition.
EXN_COMMENT,
//! An xml cdata section like <![CDATA[ this is some CDATA ]]>
EXN_CDATA,
//! Unknown element.
EXN_UNKNOWN
};
//! Callback class for file read abstraction.
/** With this, it is possible to make the xml parser read in other
things than just files. The Irrlicht engine is using this for example to
read xml from compressed .zip files. To make the parser read in
any other data, derive a class from this interface, implement the
two methods to read your data and give a pointer to an instance of
your implementation when calling createIrrXMLReader(),
createIrrXMLReaderUTF16() or createIrrXMLReaderUTF32() */
class IFileReadCallBack
{
public:
//! Destructor
virtual ~IFileReadCallBack() {}
//! Reads an amount of bytes from the file.
/** \param buffer: Pointer to buffer where to read bytes will be written to.
\param sizeToRead: Amount of bytes to read from the file.
\return Returns how much bytes were read. */
virtual int read(void* buffer, int sizeToRead) = 0;
//! Returns size of file in bytes
virtual long getSize() const = 0;
};
//! Empty class to be used as parent class for IrrXMLReader.
/** If you need another class as base class for the xml reader, you can do this by creating
the reader using for example new CXMLReaderImpl<char, YourBaseClass>(yourcallback);
The Irrlicht Engine for example needs IReferenceCounted as base class for every object to
let it automaticly reference countend, hence it replaces IXMLBase with IReferenceCounted.
See irrXML.cpp on how this can be done in detail. */
class IXMLBase
{
};
//! Interface providing easy read access to a XML file.
/** You can create an instance of this reader using one of the factory functions
createIrrXMLReader(), createIrrXMLReaderUTF16() and createIrrXMLReaderUTF32().
If using the parser from the Irrlicht Engine, please use IFileSystem::createXMLReader()
instead.
For a detailed intro how to use the parser, see \ref irrxmlexample and \ref features.
The typical usage of this parser looks like this:
\code
#include <irrXML.h>
using namespace irr; // irrXML is located in the namespace irr::io
using namespace io;
void main()
{
// create the reader using one of the factory functions
IrrXMLReader* xml = createIrrXMLReader("config.xml");
if (xml == 0)
return; // file could not be opened
// parse the file until end reached
while(xml->read())
{
// based on xml->getNodeType(), do something.
}
// delete the xml parser after usage
delete xml;
}
\endcode
See \ref irrxmlexample for a more detailed example.
*/
template<class char_type, class super_class>
class IIrrXMLReader : public super_class
{
public:
//! Destructor
virtual ~IIrrXMLReader() {}
//! Reads forward to the next xml node.
/** \return Returns false, if there was no further node. */
virtual bool read() = 0;
//! Returns the type of the current XML node.
virtual EXML_NODE getNodeType() const = 0;
//! Returns attribute count of the current XML node.
/** This is usually
non null if the current node is EXN_ELEMENT, and the element has attributes.
\return Returns amount of attributes of this xml node. */
virtual unsigned int getAttributeCount() const = 0;
//! Returns name of an attribute.
/** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
\return Name of the attribute, 0 if an attribute with this index does not exist. */
virtual const char_type* getAttributeName(int idx) const = 0;
//! Returns the value of an attribute.
/** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
\return Value of the attribute, 0 if an attribute with this index does not exist. */
virtual const char_type* getAttributeValue(int idx) const = 0;
//! Returns the value of an attribute.
/** \param name: Name of the attribute.
\return Value of the attribute, 0 if an attribute with this name does not exist. */
virtual const char_type* getAttributeValue(const char_type* name) const = 0;
//! Returns the value of an attribute in a safe way.
/** Like getAttributeValue(), but does not
return 0 if the attribute does not exist. An empty string ("") is returned then.
\param name: Name of the attribute.
\return Value of the attribute, and "" if an attribute with this name does not exist */
virtual const char_type* getAttributeValueSafe(const char_type* name) const = 0;
//! Returns the value of an attribute as integer.
/** \param name Name of the attribute.
\return Value of the attribute as integer, and 0 if an attribute with this name does not exist or
the value could not be interpreted as integer. */
virtual int getAttributeValueAsInt(const char_type* name) const = 0;
//! Returns the value of an attribute as integer.
/** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
\return Value of the attribute as integer, and 0 if an attribute with this index does not exist or
the value could not be interpreted as integer. */
virtual int getAttributeValueAsInt(int idx) const = 0;
//! Returns the value of an attribute as float.
/** \param name: Name of the attribute.
\return Value of the attribute as float, and 0 if an attribute with this name does not exist or
the value could not be interpreted as float. */
virtual float getAttributeValueAsFloat(const char_type* name) const = 0;
//! Returns the value of an attribute as float.
/** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
\return Value of the attribute as float, and 0 if an attribute with this index does not exist or
the value could not be interpreted as float. */
virtual float getAttributeValueAsFloat(int idx) const = 0;
//! Returns the name of the current node.
/** Only valid, if the node type is EXN_ELEMENT.
\return Name of the current node or 0 if the node has no name. */
virtual const char_type* getNodeName() const = 0;
//! Returns data of the current node.
/** Only valid if the node has some
data and it is of type EXN_TEXT, EXN_COMMENT, EXN_CDATA or EXN_UNKNOWN. */
virtual const char_type* getNodeData() const = 0;
//! Returns if an element is an empty element, like <foo />
virtual bool isEmptyElement() const = 0;
//! Returns format of the source xml file.
/** It is not necessary to use
this method because the parser will convert the input file format
to the format wanted by the user when creating the parser. This
method is useful to get/display additional informations. */
virtual ETEXT_FORMAT getSourceFormat() const = 0;
//! Returns format of the strings returned by the parser.
/** This will be UTF8 for example when you created a parser with
IrrXMLReaderUTF8() and UTF32 when it has been created using
IrrXMLReaderUTF32. It should not be necessary to call this
method and only exists for informational purposes. */
virtual ETEXT_FORMAT getParserFormat() const = 0;
};
template <typename T>
struct xmlChar
{
T c;
xmlChar<T>() {}
xmlChar<T>(char in) : c(static_cast<T>(in)) {}
xmlChar<T>(wchar_t in) : c(static_cast<T>(in)) {}
#if defined(__BORLANDC__)
// Note - removing explicit for borland was to get it to even compile.
// There haven't been any kind of tests for that besides that.
xmlChar<T>(unsigned char in) : c(static_cast<T>(in)) {}
xmlChar<T>(unsigned short in) : c(static_cast<T>(in)) {}
xmlChar<T>(unsigned int in) : c(static_cast<T>(in)) {}
xmlChar<T>(unsigned long in) : c(static_cast<T>(in)) {}
#else
explicit xmlChar<T>(unsigned char in) : c(static_cast<T>(in)) {}
explicit xmlChar<T>(unsigned short in) : c(static_cast<T>(in)) {}
explicit xmlChar<T>(unsigned int in) : c(static_cast<T>(in)) {}
explicit xmlChar<T>(unsigned long in) : c(static_cast<T>(in)) {}
#endif
operator T() const { return c; }
void operator=(int t) { c=static_cast<T>(t); }
};
//! defines the utf-16 type.
/** Not using wchar_t for this because
wchar_t has 16 bit on windows and 32 bit on other operating systems. */
typedef xmlChar<unsigned short> char16;
//! defines the utf-32 type.
/** Not using wchar_t for this because
wchar_t has 16 bit on windows and 32 bit on other operating systems. */
typedef xmlChar<unsigned int> char32;
//! A UTF-8 or ASCII character xml parser.
/** This means that all character data will be returned in 8 bit ASCII or UTF-8 by this parser.
The file to read can be in any format, it will be converted to UTF-8 if it is not
in this format.
Create an instance of this with createIrrXMLReader();
See IIrrXMLReader for description on how to use it. */
typedef IIrrXMLReader<char, IXMLBase> IrrXMLReader;
//! A UTF-16 xml parser.
/** This means that all character data will be returned in UTF-16 by this parser.
The file to read can be in any format, it will be converted to UTF-16 if it is not
in this format.
Create an instance of this with createIrrXMLReaderUTF16();
See IIrrXMLReader for description on how to use it. */
typedef IIrrXMLReader<char16, IXMLBase> IrrXMLReaderUTF16;
//! A UTF-32 xml parser.
/** This means that all character data will be returned in UTF-32 by this parser.
The file to read can be in any format, it will be converted to UTF-32 if it is not
in this format.
Create an instance of this with createIrrXMLReaderUTF32();
See IIrrXMLReader for description on how to use it. */
typedef IIrrXMLReader<char32, IXMLBase> IrrXMLReaderUTF32;
//! Creates an instance of an UFT-8 or ASCII character xml parser.
/** This means that all character data will be returned in 8 bit ASCII or UTF-8.
The file to read can be in any format, it will be converted to UTF-8 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReaderUTF8() instead.
\param filename: Name of file to be opened.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(const char* filename);
//! Creates an instance of an UFT-8 or ASCII character xml parser.
/** This means that all character data will be returned in 8 bit ASCII or UTF-8. The file to read can
be in any format, it will be converted to UTF-8 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReaderUTF8() instead.
\param file: Pointer to opened file, must have been opened in binary mode, e.g.
using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(FILE* file);
//! Creates an instance of an UFT-8 or ASCII character xml parser.
/** This means that all character data will be returned in 8 bit ASCII or UTF-8. The file to read can
be in any format, it will be converted to UTF-8 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReaderUTF8() instead.
\param callback: Callback for file read abstraction. Implement your own
callback to make the xml parser read in other things than just files. See
IFileReadCallBack for more information about this.
\param deleteCallback: if true, the callback will be deleted after the file
has been read. Otherwise the caller si responsible for cleaning it up.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(IFileReadCallBack* callback,
bool deleteCallback = false);
//! Creates an instance of an UFT-16 xml parser.
/** This means that
all character data will be returned in UTF-16. The file to read can
be in any format, it will be converted to UTF-16 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param filename: Name of file to be opened.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(const char* filename);
//! Creates an instance of an UFT-16 xml parser.
/** This means that all character data will be returned in UTF-16. The file to read can
be in any format, it will be converted to UTF-16 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param file: Pointer to opened file, must have been opened in binary mode, e.g.
using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(FILE* file);
//! Creates an instance of an UFT-16 xml parser.
/** This means that all character data will be returned in UTF-16. The file to read can
be in any format, it will be converted to UTF-16 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param callback: Callback for file read abstraction. Implement your own
callback to make the xml parser read in other things than just files. See
IFileReadCallBack for more information about this.
\param deleteCallback: if true, the callback will be deleted after the file
has been read. Otherwise the caller si responsible for cleaning it up.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(IFileReadCallBack* callback,
bool deleteCallback = false);
//! Creates an instance of an UFT-32 xml parser.
/** This means that all character data will be returned in UTF-32. The file to read can
be in any format, it will be converted to UTF-32 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param filename: Name of file to be opened.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(const char* filename);
//! Creates an instance of an UFT-32 xml parser.
/** This means that all character data will be returned in UTF-32. The file to read can
be in any format, it will be converted to UTF-32 if it is not in this format.
if you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param file: Pointer to opened file, must have been opened in binary mode, e.g.
using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(FILE* file);
//! Creates an instance of an UFT-32 xml parser.
/** This means that
all character data will be returned in UTF-32. The file to read can
be in any format, it will be converted to UTF-32 if it is not in this format.
If you are using the Irrlicht Engine, it is better not to use this function but
IFileSystem::createXMLReader() instead.
\param callback: Callback for file read abstraction. Implement your own
callback to make the xml parser read in other things than just files. See
IFileReadCallBack for more information about this.
\param deleteCallback: if true, the callback will be deleted after the file
has been read. Otherwise the caller si responsible for cleaning it up.
\return Returns a pointer to the created xml parser. This pointer should be
deleted using 'delete' after no longer needed. Returns 0 if an error occured
and the file could not be opened. */
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(IFileReadCallBack* callback,
bool deleteCallback = false);
/*! \file irrXML.h
\brief Header file of the irrXML, the Irrlicht XML parser.
This file includes everything needed for using irrXML,
the XML parser of the Irrlicht Engine. To use irrXML,
you only need to include this file in your project:
\code
#include <irrXML.h>
\endcode
It is also common to use the two namespaces in which irrXML is included,
directly after including irrXML.h:
\code
#include <irrXML.h>
using namespace irr;
using namespace io;
\endcode
*/
} // end namespace io
} // end namespace irr
#endif // __IRR_XML_H_INCLUDED__
|