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