irrXML.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // Copyright (C) 2002-2005 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine" and the "irrXML" project.
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h
  4. #ifndef __IRR_XML_H_INCLUDED__
  5. #define __IRR_XML_H_INCLUDED__
  6. #include <stdio.h>
  7. /** \mainpage irrXML 1.2 API documentation
  8. <div align="center"><img src="logobig.png" ></div>
  9. \section intro Introduction
  10. Welcome to the irrXML API documentation.
  11. Here you'll find any information you'll need to develop applications with
  12. irrXML. If you look for a tutorial on how to start, take a look at the \ref irrxmlexample,
  13. at the homepage of irrXML at <A HREF="http://xml.irrlicht3d.org" >xml.irrlicht3d.org</A>
  14. or into the SDK in the directory \example.
  15. irrXML is intended to be a high speed and easy-to-use XML Parser for C++, and
  16. this documentation is an important part of it. If you have any questions or
  17. suggestions, just send a email to the author of the engine, Nikolaus Gebhardt
  18. (niko (at) irrlicht3d.org). For more informations about this parser, see \ref history.
  19. \section features Features
  20. irrXML provides forward-only, read-only
  21. access to a stream of non validated XML data. It was fully implemented by
  22. Nikolaus Gebhardt. Its current features are:
  23. - It it fast as lighting and has very low memory usage. It was
  24. developed with the intention of being used in 3D games, as it already has been.
  25. - irrXML is very small: It only consists of 60 KB of code and can be added easily
  26. to your existing project.
  27. - Of course, it is platform independent and works with lots of compilers.
  28. - It is able to parse ASCII, UTF-8, UTF-16 and UTF-32 text files, both in
  29. little and big endian format.
  30. - Independent of the input file format, the parser can return all strings in ASCII, UTF-8,
  31. UTF-16 and UTF-32 format.
  32. - With its optional file access abstraction it has the advantage that it can read not
  33. only from files but from any type of data (memory, network, ...). For example when
  34. used with the Irrlicht Engine, it directly reads from compressed .zip files.
  35. - Just like the Irrlicht Engine for which it was originally created, it is extremely easy
  36. to use.
  37. - It has no external dependencies, it does not even need the STL.
  38. Although irrXML has some strenghts, it currently also has the following limitations:
  39. - The input xml file is not validated and assumed to be correct.
  40. \section irrxmlexample Example
  41. The following code demonstrates the basic usage of irrXML. A simple xml
  42. file like this is parsed:
  43. \code
  44. <?xml version="1.0"?>
  45. <config>
  46. <!-- This is a config file for the mesh viewer -->
  47. <model file="dwarf.dea" />
  48. <messageText caption="Irrlicht Engine Mesh Viewer">
  49. Welcome to the Mesh Viewer of the &quot;Irrlicht Engine&quot;.
  50. </messageText>
  51. </config>
  52. \endcode
  53. The code for parsing this file would look like this:
  54. \code
  55. #include <irrXML.h>
  56. using namespace irr; // irrXML is located in the namespace irr::io
  57. using namespace io;
  58. #include <string> // we use STL strings to store data in this example
  59. void main()
  60. {
  61. // create the reader using one of the factory functions
  62. IrrXMLReader* xml = createIrrXMLReader("config.xml");
  63. // strings for storing the data we want to get out of the file
  64. std::string modelFile;
  65. std::string messageText;
  66. std::string caption;
  67. // parse the file until end reached
  68. while(xml && xml->read())
  69. {
  70. switch(xml->getNodeType())
  71. {
  72. case EXN_TEXT:
  73. // in this xml file, the only text which occurs is the messageText
  74. messageText = xml->getNodeData();
  75. break;
  76. case EXN_ELEMENT:
  77. {
  78. if (!strcmp("model", xml->getNodeName()))
  79. modelFile = xml->getAttributeValue("file");
  80. else
  81. if (!strcmp("messageText", xml->getNodeName()))
  82. caption = xml->getAttributeValue("caption");
  83. }
  84. break;
  85. }
  86. }
  87. // delete the xml parser after usage
  88. delete xml;
  89. }
  90. \endcode
  91. \section howto How to use
  92. Simply add the source files in the /src directory of irrXML to your project. Done.
  93. \section license License
  94. The irrXML license is based on the zlib license. Basicly, this means you can do with
  95. irrXML whatever you want:
  96. Copyright (C) 2002-2005 Nikolaus Gebhardt
  97. This software is provided 'as-is', without any express or implied
  98. warranty. In no event will the authors be held liable for any damages
  99. arising from the use of this software.
  100. Permission is granted to anyone to use this software for any purpose,
  101. including commercial applications, and to alter it and redistribute it
  102. freely, subject to the following restrictions:
  103. 1. The origin of this software must not be misrepresented; you must not
  104. claim that you wrote the original software. If you use this software
  105. in a product, an acknowledgment in the product documentation would be
  106. appreciated but is not required.
  107. 2. Altered source versions must be plainly marked as such, and must not be
  108. misrepresented as being the original software.
  109. 3. This notice may not be removed or altered from any source distribution.
  110. \section history History
  111. As lots of references in this documentation and the source show, this xml
  112. parser has originally been a part of the
  113. <A HREF="http://irrlicht.sourceforge.net" >Irrlicht Engine</A>. But because
  114. the parser has become very useful with the latest release, people asked for a
  115. separate version of it, to be able to use it in non Irrlicht projects. With
  116. irrXML 1.0, this has now been done.
  117. */
  118. namespace irr
  119. {
  120. namespace io
  121. {
  122. //! Enumeration of all supported source text file formats
  123. enum ETEXT_FORMAT
  124. {
  125. //! ASCII, file without byte order mark, or not a text file
  126. ETF_ASCII,
  127. //! UTF-8 format
  128. ETF_UTF8,
  129. //! UTF-16 format, big endian
  130. ETF_UTF16_BE,
  131. //! UTF-16 format, little endian
  132. ETF_UTF16_LE,
  133. //! UTF-32 format, big endian
  134. ETF_UTF32_BE,
  135. //! UTF-32 format, little endian
  136. ETF_UTF32_LE,
  137. };
  138. //! Enumeration for all xml nodes which are parsed by IrrXMLReader
  139. enum EXML_NODE
  140. {
  141. //! No xml node. This is usually the node if you did not read anything yet.
  142. EXN_NONE,
  143. //! A xml element, like <foo>
  144. EXN_ELEMENT,
  145. //! End of an xml element, like </foo>
  146. EXN_ELEMENT_END,
  147. //! Text within a xml element: <foo> this is the text. </foo>
  148. EXN_TEXT,
  149. //! An xml comment like &lt;!-- I am a comment --&gt; or a DTD definition.
  150. EXN_COMMENT,
  151. //! An xml cdata section like &lt;![CDATA[ this is some CDATA ]]&gt;
  152. EXN_CDATA,
  153. //! Unknown element.
  154. EXN_UNKNOWN
  155. };
  156. //! Callback class for file read abstraction.
  157. /** With this, it is possible to make the xml parser read in other things
  158. than just files. The Irrlicht engine is using this for example to
  159. read xml from compressed .zip files. To make the parser read in
  160. any other data, derive a class from this interface, implement the
  161. two methods to read your data and give a pointer to an instance of
  162. your implementation when calling createIrrXMLReader(),
  163. createIrrXMLReaderUTF16() or createIrrXMLReaderUTF32() */
  164. class IFileReadCallBack
  165. {
  166. public:
  167. //! virtual destructor
  168. virtual ~IFileReadCallBack() {};
  169. //! Reads an amount of bytes from the file.
  170. /** \param buffer: Pointer to buffer where to read bytes will be written to.
  171. \param sizeToRead: Amount of bytes to read from the file.
  172. \return Returns how much bytes were read. */
  173. virtual int read(void* buffer, int sizeToRead) = 0;
  174. //! Returns size of file in bytes
  175. virtual int getSize() = 0;
  176. };
  177. //! Empty class to be used as parent class for IrrXMLReader.
  178. /** If you need another class as base class for the xml reader, you can do this by creating
  179. the reader using for example new CXMLReaderImpl<char, YourBaseClass>(yourcallback);
  180. The Irrlicht Engine for example needs IUnknown as base class for every object to
  181. let it automaticly reference countend, hence it replaces IXMLBase with IUnknown.
  182. See irrXML.cpp on how this can be done in detail. */
  183. class IXMLBase
  184. {
  185. };
  186. //! Interface providing easy read access to a XML file.
  187. /** You can create an instance of this reader using one of the factory functions
  188. createIrrXMLReader(), createIrrXMLReaderUTF16() and createIrrXMLReaderUTF32().
  189. If using the parser from the Irrlicht Engine, please use IFileSystem::createXMLReader()
  190. instead.
  191. For a detailed intro how to use the parser, see \ref irrxmlexample and \ref features.
  192. The typical usage of this parser looks like this:
  193. \code
  194. #include <irrXML.h>
  195. using namespace irr; // irrXML is located in the namespace irr::io
  196. using namespace io;
  197. void main()
  198. {
  199. // create the reader using one of the factory functions
  200. IrrXMLReader* xml = createIrrXMLReader("config.xml");
  201. if (xml == 0)
  202. return; // file could not be opened
  203. // parse the file until end reached
  204. while(xml->read())
  205. {
  206. // based on xml->getNodeType(), do something.
  207. }
  208. // delete the xml parser after usage
  209. delete xml;
  210. }
  211. \endcode
  212. See \ref irrxmlexample for a more detailed example.
  213. */
  214. template<class char_type, class super_class>
  215. class IIrrXMLReader : public super_class
  216. {
  217. public:
  218. //! Destructor
  219. virtual ~IIrrXMLReader() {};
  220. //! Reads forward to the next xml node.
  221. /** \return Returns false, if there was no further node. */
  222. virtual bool read() = 0;
  223. //! Returns the type of the current XML node.
  224. virtual EXML_NODE getNodeType() const = 0;
  225. //! Returns attribute count of the current XML node.
  226. /** This is usually
  227. non null if the current node is EXN_ELEMENT, and the element has attributes.
  228. \return Returns amount of attributes of this xml node. */
  229. virtual int getAttributeCount() const = 0;
  230. //! Returns name of an attribute.
  231. /** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
  232. \return Name of the attribute, 0 if an attribute with this index does not exist. */
  233. virtual const char_type* getAttributeName(int idx) const = 0;
  234. //! Returns the value of an attribute.
  235. /** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
  236. \return Value of the attribute, 0 if an attribute with this index does not exist. */
  237. virtual const char_type* getAttributeValue(int idx) const = 0;
  238. //! Returns the value of an attribute.
  239. /** \param name: Name of the attribute.
  240. \return Value of the attribute, 0 if an attribute with this name does not exist. */
  241. virtual const char_type* getAttributeValue(const char_type* name) const = 0;
  242. //! Returns the value of an attribute in a safe way.
  243. /** Like getAttributeValue(), but does not
  244. return 0 if the attribute does not exist. An empty string ("") is returned then.
  245. \param name: Name of the attribute.
  246. \return Value of the attribute, and "" if an attribute with this name does not exist */
  247. virtual const char_type* getAttributeValueSafe(const char_type* name) const = 0;
  248. //! Returns the value of an attribute as integer.
  249. /** \param name Name of the attribute.
  250. \return Value of the attribute as integer, and 0 if an attribute with this name does not exist or
  251. the value could not be interpreted as integer. */
  252. virtual int getAttributeValueAsInt(const char_type* name) const = 0;
  253. //! Returns the value of an attribute as integer.
  254. /** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
  255. \return Value of the attribute as integer, and 0 if an attribute with this index does not exist or
  256. the value could not be interpreted as integer. */
  257. virtual int getAttributeValueAsInt(int idx) const = 0;
  258. //! Returns the value of an attribute as float.
  259. /** \param name: Name of the attribute.
  260. \return Value of the attribute as float, and 0 if an attribute with this name does not exist or
  261. the value could not be interpreted as float. */
  262. virtual float getAttributeValueAsFloat(const char_type* name) const = 0;
  263. //! Returns the value of an attribute as float.
  264. /** \param idx: Zero based index, should be something between 0 and getAttributeCount()-1.
  265. \return Value of the attribute as float, and 0 if an attribute with this index does not exist or
  266. the value could not be interpreted as float. */
  267. virtual float getAttributeValueAsFloat(int idx) const = 0;
  268. //! Returns the name of the current node.
  269. /** Only non null, if the node type is EXN_ELEMENT.
  270. \return Name of the current node or 0 if the node has no name. */
  271. virtual const char_type* getNodeName() const = 0;
  272. //! Returns data of the current node.
  273. /** Only non null if the node has some
  274. data and it is of type EXN_TEXT or EXN_UNKNOWN. */
  275. virtual const char_type* getNodeData() const = 0;
  276. //! Returns if an element is an empty element, like <foo />
  277. virtual bool isEmptyElement() const = 0;
  278. //! Returns format of the source xml file.
  279. /** It is not necessary to use
  280. this method because the parser will convert the input file format
  281. to the format wanted by the user when creating the parser. This
  282. method is useful to get/display additional informations. */
  283. virtual ETEXT_FORMAT getSourceFormat() const = 0;
  284. //! Returns format of the strings returned by the parser.
  285. /** This will be UTF8 for example when you created a parser with
  286. IrrXMLReaderUTF8() and UTF32 when it has been created using
  287. IrrXMLReaderUTF32. It should not be necessary to call this
  288. method and only exists for informational purposes. */
  289. virtual ETEXT_FORMAT getParserFormat() const = 0;
  290. };
  291. //! defines the utf-16 type.
  292. /** Not using wchar_t for this because
  293. wchar_t has 16 bit on windows and 32 bit on other operating systems. */
  294. typedef unsigned short char16;
  295. //! defines the utf-32 type.
  296. /** Not using wchar_t for this because
  297. wchar_t has 16 bit on windows and 32 bit on other operating systems. */
  298. typedef unsigned long char32;
  299. //! A UTF-8 or ASCII character xml parser.
  300. /** This means that all character data will be returned in 8 bit ASCII or UTF-8 by this parser.
  301. The file to read can be in any format, it will be converted to UTF-8 if it is not
  302. in this format.
  303. Create an instance of this with createIrrXMLReader();
  304. See IIrrXMLReader for description on how to use it. */
  305. typedef IIrrXMLReader<char, IXMLBase> IrrXMLReader;
  306. //! A UTF-16 xml parser.
  307. /** This means that all character data will be returned in UTF-16 by this parser.
  308. The file to read can be in any format, it will be converted to UTF-16 if it is not
  309. in this format.
  310. Create an instance of this with createIrrXMLReaderUTF16();
  311. See IIrrXMLReader for description on how to use it. */
  312. typedef IIrrXMLReader<char16, IXMLBase> IrrXMLReaderUTF16;
  313. //! A UTF-32 xml parser.
  314. /** This means that all character data will be returned in UTF-32 by this parser.
  315. The file to read can be in any format, it will be converted to UTF-32 if it is not
  316. in this format.
  317. Create an instance of this with createIrrXMLReaderUTF32();
  318. See IIrrXMLReader for description on how to use it. */
  319. typedef IIrrXMLReader<char32, IXMLBase> IrrXMLReaderUTF32;
  320. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  321. /** This means that all character data will be returned in 8 bit ASCII or UTF-8.
  322. The file to read can be in any format, it will be converted to UTF-8 if it is not in this format.
  323. If you are using the Irrlicht Engine, it is better not to use this function but
  324. IFileSystem::createXMLReaderUTF8() instead.
  325. \param filename: Name of file to be opened.
  326. \return Returns a pointer to the created xml parser. This pointer should be
  327. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  328. and the file could not be opened. */
  329. IrrXMLReader* createIrrXMLReader(const char* filename);
  330. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  331. /** This means that all character data will be returned in 8 bit ASCII or UTF-8. The file to read can
  332. be in any format, it will be converted to UTF-8 if it is not in this format.
  333. If you are using the Irrlicht Engine, it is better not to use this function but
  334. IFileSystem::createXMLReaderUTF8() instead.
  335. \param file: Pointer to opened file, must have been opened in binary mode, e.g.
  336. using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
  337. \return Returns a pointer to the created xml parser. This pointer should be
  338. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  339. and the file could not be opened. */
  340. IrrXMLReader* createIrrXMLReader(FILE* file);
  341. //! Creates an instance of an UFT-8 or ASCII character xml parser.
  342. /** This means that all character data will be returned in 8 bit ASCII or UTF-8. The file to read can
  343. be in any format, it will be converted to UTF-8 if it is not in this format.
  344. If you are using the Irrlicht Engine, it is better not to use this function but
  345. IFileSystem::createXMLReaderUTF8() instead.
  346. \param callback: Callback for file read abstraction. Implement your own
  347. callback to make the xml parser read in other things than just files. See
  348. IFileReadCallBack for more information about this.
  349. \return Returns a pointer to the created xml parser. This pointer should be
  350. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  351. and the file could not be opened. */
  352. IrrXMLReader* createIrrXMLReader(IFileReadCallBack* callback);
  353. //! Creates an instance of an UFT-16 xml parser.
  354. /** This means that
  355. all character data will be returned in UTF-16. The file to read can
  356. be in any format, it will be converted to UTF-16 if it is not in this format.
  357. If you are using the Irrlicht Engine, it is better not to use this function but
  358. IFileSystem::createXMLReader() instead.
  359. \param filename: Name of file to be opened.
  360. \return Returns a pointer to the created xml parser. This pointer should be
  361. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  362. and the file could not be opened. */
  363. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(const char* filename);
  364. //! Creates an instance of an UFT-16 xml parser.
  365. /** This means that all character data will be returned in UTF-16. The file to read can
  366. be in any format, it will be converted to UTF-16 if it is not in this format.
  367. If you are using the Irrlicht Engine, it is better not to use this function but
  368. IFileSystem::createXMLReader() instead.
  369. \param file: Pointer to opened file, must have been opened in binary mode, e.g.
  370. using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
  371. \return Returns a pointer to the created xml parser. This pointer should be
  372. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  373. and the file could not be opened. */
  374. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(FILE* file);
  375. //! Creates an instance of an UFT-16 xml parser.
  376. /** This means that all character data will be returned in UTF-16. The file to read can
  377. be in any format, it will be converted to UTF-16 if it is not in this format.
  378. If you are using the Irrlicht Engine, it is better not to use this function but
  379. IFileSystem::createXMLReader() instead.
  380. \param callback: Callback for file read abstraction. Implement your own
  381. callback to make the xml parser read in other things than just files. See
  382. IFileReadCallBack for more information about this.
  383. \return Returns a pointer to the created xml parser. This pointer should be
  384. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  385. and the file could not be opened. */
  386. IrrXMLReaderUTF16* createIrrXMLReaderUTF16(IFileReadCallBack* callback);
  387. //! Creates an instance of an UFT-32 xml parser.
  388. /** This means that all character data will be returned in UTF-32. The file to read can
  389. be in any format, it will be converted to UTF-32 if it is not in this format.
  390. If you are using the Irrlicht Engine, it is better not to use this function but
  391. IFileSystem::createXMLReader() instead.
  392. \param filename: Name of file to be opened.
  393. \return Returns a pointer to the created xml parser. This pointer should be
  394. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  395. and the file could not be opened. */
  396. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(const char* filename);
  397. //! Creates an instance of an UFT-32 xml parser.
  398. /** This means that all character data will be returned in UTF-32. The file to read can
  399. be in any format, it will be converted to UTF-32 if it is not in this format.
  400. if you are using the Irrlicht Engine, it is better not to use this function but
  401. IFileSystem::createXMLReader() instead.
  402. \param file: Pointer to opened file, must have been opened in binary mode, e.g.
  403. using fopen("foo.bar", "wb"); The file will not be closed after it has been read.
  404. \return Returns a pointer to the created xml parser. This pointer should be
  405. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  406. and the file could not be opened. */
  407. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(FILE* file);
  408. //! Creates an instance of an UFT-32 xml parser.
  409. /** This means that
  410. all character data will be returned in UTF-32. The file to read can
  411. be in any format, it will be converted to UTF-32 if it is not in this format.
  412. If you are using the Irrlicht Engine, it is better not to use this function but
  413. IFileSystem::createXMLReader() instead.
  414. \param callback: Callback for file read abstraction. Implement your own
  415. callback to make the xml parser read in other things than just files. See
  416. IFileReadCallBack for more information about this.
  417. \return Returns a pointer to the created xml parser. This pointer should be
  418. deleted using 'delete' after no longer needed. Returns 0 if an error occured
  419. and the file could not be opened. */
  420. IrrXMLReaderUTF32* createIrrXMLReaderUTF32(IFileReadCallBack* callback);
  421. /*! \file irrxml.h
  422. \brief Header file of the irrXML, the Irrlicht XML parser.
  423. This file includes everything needed for using irrXML,
  424. the XML parser of the Irrlicht Engine. To use irrXML,
  425. you only need to include this file in your project:
  426. \code
  427. #include <irrXML.h>
  428. \endcode
  429. It is also common to use the two namespaces in which irrXML is included,
  430. directly after #including irrXML.h:
  431. \code
  432. #include <irrXML.h>
  433. using namespace irr;
  434. using namespace io;
  435. \endcode
  436. */
  437. } // end namespace io
  438. } // end namespace irr
  439. #endif // __IRR_XML_H_INCLUDED__