nuclear@0: /* nuclear@0: Open Asset Import Library (assimp) nuclear@0: ---------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the nuclear@0: following conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: nuclear@0: ---------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: #ifndef INCLUDED_AI_IRRXML_WRAPPER nuclear@0: #define INCLUDED_AI_IRRXML_WRAPPER nuclear@0: nuclear@0: // some long includes .... nuclear@0: #include "irrXML/irrXML.h" nuclear@0: #include "assimp/IOStream.hpp" nuclear@0: namespace Assimp { nuclear@0: nuclear@0: // --------------------------------------------------------------------------------- nuclear@0: /** @brief Utility class to make IrrXML work together with our custom IO system nuclear@0: * See the IrrXML docs for more details. nuclear@0: * nuclear@0: * Construct IrrXML-Reader in BaseImporter::InternReadFile(): nuclear@0: * @code nuclear@0: * // open the file nuclear@0: * boost::scoped_ptr file( pIOHandler->Open( pFile)); nuclear@0: * if( file.get() == NULL) { nuclear@0: * throw DeadlyImportError( "Failed to open file " + pFile + "."); nuclear@0: * } nuclear@0: * nuclear@0: * // generate a XML reader for it nuclear@0: * boost::scoped_ptr mIOWrapper( new CIrrXML_IOStreamReader( file.get())); nuclear@0: * mReader = irr::io::createIrrXMLReader( mIOWrapper.get()); nuclear@0: * if( !mReader) { nuclear@0: * ThrowException( "xxxx: Unable to open file."); nuclear@0: * } nuclear@0: * @endcode nuclear@0: **/ nuclear@0: class CIrrXML_IOStreamReader nuclear@0: : public irr::io::IFileReadCallBack nuclear@0: { nuclear@0: public: nuclear@0: nuclear@0: // ---------------------------------------------------------------------------------- nuclear@0: //! Construction from an existing IOStream nuclear@0: CIrrXML_IOStreamReader(IOStream* _stream) nuclear@0: : stream (_stream) nuclear@0: , t (0) nuclear@0: { nuclear@0: nuclear@0: // Map the buffer into memory and convert it to UTF8. IrrXML provides its nuclear@0: // own conversion, which is merely a cast from uintNN_t to uint8_t. Thus, nuclear@0: // it is not suitable for our purposes and we have to do it BEFORE IrrXML nuclear@0: // gets the buffer. Sadly, this forces as to map the whole file into nuclear@0: // memory. nuclear@0: nuclear@0: data.resize(stream->FileSize()); nuclear@0: stream->Read(&data[0],data.size(),1); nuclear@0: nuclear@0: BaseImporter::ConvertToUTF8(data); nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------------------- nuclear@0: //! Virtual destructor nuclear@0: virtual ~CIrrXML_IOStreamReader() {}; nuclear@0: nuclear@0: // ---------------------------------------------------------------------------------- nuclear@0: //! Reads an amount of bytes from the file. nuclear@0: /** @param buffer: Pointer to output buffer. nuclear@0: * @param sizeToRead: Amount of bytes to read nuclear@0: * @return Returns how much bytes were read. */ nuclear@0: virtual int read(void* buffer, int sizeToRead) { nuclear@0: if(sizeToRead<0) { nuclear@0: return 0; nuclear@0: } nuclear@0: if(t+sizeToRead>data.size()) { nuclear@0: sizeToRead = data.size()-t; nuclear@0: } nuclear@0: nuclear@0: memcpy(buffer,&data.front()+t,sizeToRead); nuclear@0: nuclear@0: t += sizeToRead; nuclear@0: return sizeToRead; nuclear@0: } nuclear@0: nuclear@0: // ---------------------------------------------------------------------------------- nuclear@0: //! Returns size of file in bytes nuclear@0: virtual int getSize() { nuclear@0: return (int)data.size(); nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: IOStream* stream; nuclear@0: std::vector data; nuclear@0: size_t t; nuclear@0: nuclear@0: }; // ! class CIrrXML_IOStreamReader nuclear@0: nuclear@0: } // ! Assimp nuclear@0: nuclear@0: #endif // !! INCLUDED_AI_IRRXML_WRAPPER