nuclear@0: // Copyright (C) 2002-2005 Nikolaus Gebhardt nuclear@0: // This file is part of the "Irrlicht Engine" and the "irrXML" project. nuclear@0: // For conditions of distribution and use, see copyright notice in irrlicht.h and/or irrXML.h nuclear@0: nuclear@0: // Need to include Assimp, too. We're using Assimp's version of fast_atof nuclear@0: // so we need stdint.h. But no PCH. nuclear@0: nuclear@0: #include "AssimpPCH.h" nuclear@0: nuclear@0: #include "irrXML.h" nuclear@0: #include "irrString.h" nuclear@0: #include "irrArray.h" nuclear@0: #include "fast_atof.h" nuclear@0: #include "CXMLReaderImpl.h" nuclear@0: nuclear@0: namespace irr nuclear@0: { nuclear@0: namespace io nuclear@0: { nuclear@0: nuclear@0: //! Implementation of the file read callback for ordinary files nuclear@0: class CFileReadCallBack : public IFileReadCallBack nuclear@0: { nuclear@0: public: nuclear@0: nuclear@0: //! construct from filename nuclear@0: CFileReadCallBack(const char* filename) nuclear@0: : File(0), Size(0), Close(true) nuclear@0: { nuclear@0: // open file nuclear@0: File = fopen(filename, "rb"); nuclear@0: nuclear@0: if (File) nuclear@0: getFileSize(); nuclear@0: } nuclear@0: nuclear@0: //! construct from FILE pointer nuclear@0: CFileReadCallBack(FILE* file) nuclear@0: : File(file), Size(0), Close(false) nuclear@0: { nuclear@0: if (File) nuclear@0: getFileSize(); nuclear@0: } nuclear@0: nuclear@0: //! destructor nuclear@0: virtual ~CFileReadCallBack() nuclear@0: { nuclear@0: if (Close && File) nuclear@0: fclose(File); nuclear@0: } nuclear@0: nuclear@0: //! Reads an amount of bytes from the file. nuclear@0: virtual int read(void* buffer, int sizeToRead) nuclear@0: { nuclear@0: if (!File) nuclear@0: return 0; nuclear@0: nuclear@0: return (int)fread(buffer, 1, sizeToRead, File); nuclear@0: } nuclear@0: nuclear@0: //! Returns size of file in bytes nuclear@0: virtual int getSize() nuclear@0: { nuclear@0: return Size; nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: //! retrieves the file size of the open file nuclear@0: void getFileSize() nuclear@0: { nuclear@0: fseek(File, 0, SEEK_END); nuclear@0: Size = ftell(File); nuclear@0: fseek(File, 0, SEEK_SET); nuclear@0: } nuclear@0: nuclear@0: FILE* File; nuclear@0: int Size; nuclear@0: bool Close; nuclear@0: nuclear@0: }; // end class CFileReadCallBack nuclear@0: nuclear@0: nuclear@0: nuclear@0: // FACTORY FUNCTIONS: nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UFT-8 or ASCII character xml parser. nuclear@0: IrrXMLReader* createIrrXMLReader(const char* filename) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(filename)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UFT-8 or ASCII character xml parser. nuclear@0: IrrXMLReader* createIrrXMLReader(FILE* file) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(file)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UFT-8 or ASCII character xml parser. nuclear@0: IrrXMLReader* createIrrXMLReader(IFileReadCallBack* callback) nuclear@0: { nuclear@0: return new CXMLReaderImpl(callback, false); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-16 xml parser. nuclear@0: IrrXMLReaderUTF16* createIrrXMLReaderUTF16(const char* filename) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(filename)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-16 xml parser. nuclear@0: IrrXMLReaderUTF16* createIrrXMLReaderUTF16(FILE* file) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(file)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-16 xml parser. nuclear@0: IrrXMLReaderUTF16* createIrrXMLReaderUTF16(IFileReadCallBack* callback) nuclear@0: { nuclear@0: return new CXMLReaderImpl(callback, false); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-32 xml parser. nuclear@0: IrrXMLReaderUTF32* createIrrXMLReaderUTF32(const char* filename) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(filename)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-32 xml parser. nuclear@0: IrrXMLReaderUTF32* createIrrXMLReaderUTF32(FILE* file) nuclear@0: { nuclear@0: return new CXMLReaderImpl(new CFileReadCallBack(file)); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: //! Creates an instance of an UTF-32 xml parser. nuclear@0: IrrXMLReaderUTF32* createIrrXMLReaderUTF32(IFileReadCallBack* callback) nuclear@0: { nuclear@0: return new CXMLReaderImpl(callback, false); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: } // end namespace io nuclear@0: } // end namespace irr