1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4 
5 #include "CXMLReaderImpl.h"
6 #include "CXMLReader.h"
7 #include "IReadFile.h"
8 
9 namespace irr
10 {
11 namespace io
12 {
13 	//! Irrlicht implementation of the file read callback for the xml parser
14 	class CIrrXMLFileReadCallBack : public IFileReadCallBack
15 	{
16 	public:
17 
18 		//! construct from FILE pointer
CIrrXMLFileReadCallBack(IReadFile * file)19 		CIrrXMLFileReadCallBack(IReadFile* file)
20 			: ReadFile(file)
21 		{
22 			ReadFile->grab();
23 		}
24 
25 		//! destructor
~CIrrXMLFileReadCallBack()26 		virtual ~CIrrXMLFileReadCallBack()
27 		{
28 			ReadFile->drop();
29 		}
30 
31 		//! Reads an amount of bytes from the file.
read(void * buffer,int sizeToRead)32 		virtual int read(void* buffer, int sizeToRead)
33 		{
34 			return ReadFile->read(buffer, sizeToRead);
35 		}
36 
37 		//! Returns size of file in bytes
getSize() const38 		virtual long getSize() const
39 		{
40 			return ReadFile->getSize();
41 		}
42 
43 	private:
44 
45 		IReadFile* ReadFile;
46 	}; // end class CMyXMLFileReadCallBack
47 
48 
49 	// now create an implementation for IXMLReader using irrXML.
50 
51 	//! Creates an instance of a wide character xml parser.
createIXMLReader(IReadFile * file)52 	IXMLReader* createIXMLReader(IReadFile* file)
53 	{
54 		if (!file)
55 			return 0;
56 
57 		return new CXMLReaderImpl<wchar_t, IReferenceCounted>(new CIrrXMLFileReadCallBack(file));
58 	}
59 
60 	//! Creates an instance of an UFT-8 or ASCII character xml parser.
createIXMLReaderUTF8(IReadFile * file)61 	IXMLReaderUTF8* createIXMLReaderUTF8(IReadFile* file)
62 	{
63 		if (!file)
64 			return 0;
65 
66 		return new CXMLReaderImpl<char, IReferenceCounted>(new CIrrXMLFileReadCallBack(file));
67 	}
68 
69 } // end namespace
70 } // end namespace
71