2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2016-01-01 20:07:24 +00:00
|
|
|
Copyright (c) 2006-2016, assimp team
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_AI_IRRXML_WRAPPER
|
|
|
|
#define INCLUDED_AI_IRRXML_WRAPPER
|
|
|
|
|
|
|
|
// some long includes ....
|
2017-04-27 15:19:51 +00:00
|
|
|
#include <irrXML.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
#include "./../include/assimp/IOStream.hpp"
|
|
|
|
#include "BaseImporter.h"
|
|
|
|
#include <vector>
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
namespace Assimp {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------
|
|
|
|
/** @brief Utility class to make IrrXML work together with our custom IO system
|
|
|
|
* See the IrrXML docs for more details.
|
|
|
|
*
|
|
|
|
* Construct IrrXML-Reader in BaseImporter::InternReadFile():
|
|
|
|
* @code
|
|
|
|
* // open the file
|
2016-04-05 21:23:53 +00:00
|
|
|
* std::unique_ptr<IOStream> file( pIOHandler->Open( pFile));
|
2015-05-19 03:48:29 +00:00
|
|
|
* if( file.get() == NULL) {
|
2015-05-19 03:57:13 +00:00
|
|
|
* throw DeadlyImportError( "Failed to open file " + pFile + ".");
|
2015-05-19 03:48:29 +00:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* // generate a XML reader for it
|
2016-04-05 21:23:53 +00:00
|
|
|
* std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
|
2015-05-19 03:48:29 +00:00
|
|
|
* mReader = irr::io::createIrrXMLReader( mIOWrapper.get());
|
|
|
|
* if( !mReader) {
|
|
|
|
* ThrowException( "xxxx: Unable to open file.");
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
**/
|
2015-05-19 03:52:10 +00:00
|
|
|
class CIrrXML_IOStreamReader
|
2015-05-19 03:57:13 +00:00
|
|
|
: public irr::io::IFileReadCallBack
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
//! Construction from an existing IOStream
|
2015-10-29 12:04:23 +00:00
|
|
|
explicit CIrrXML_IOStreamReader(IOStream* _stream)
|
2015-05-19 03:57:13 +00:00
|
|
|
: stream (_stream)
|
|
|
|
, 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
|
|
|
|
// gets the buffer. Sadly, this forces us to map the whole file into
|
|
|
|
// memory.
|
|
|
|
|
|
|
|
data.resize(stream->FileSize());
|
|
|
|
stream->Read(&data[0],data.size(),1);
|
|
|
|
|
|
|
|
// Remove null characters from the input sequence otherwise the parsing will utterly fail
|
|
|
|
unsigned int size = 0;
|
2016-11-19 14:52:04 +00:00
|
|
|
unsigned int size_max = static_cast<unsigned int>(data.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
for(unsigned int i = 0; i < size_max; i++) {
|
|
|
|
if(data[i] != '\0') {
|
|
|
|
data[size++] = data[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data.resize(size);
|
|
|
|
|
|
|
|
BaseImporter::ConvertToUTF8(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
//! Virtual destructor
|
|
|
|
virtual ~CIrrXML_IOStreamReader() {}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
//! Reads an amount of bytes from the file.
|
|
|
|
/** @param buffer: Pointer to output buffer.
|
|
|
|
* @param sizeToRead: Amount of bytes to read
|
|
|
|
* @return Returns how much bytes were read. */
|
|
|
|
virtual int read(void* buffer, int sizeToRead) {
|
|
|
|
if(sizeToRead<0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(t+sizeToRead>data.size()) {
|
2016-11-19 14:52:04 +00:00
|
|
|
sizeToRead = static_cast<int>(data.size()-t);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer,&data.front()+t,sizeToRead);
|
|
|
|
|
|
|
|
t += sizeToRead;
|
|
|
|
return sizeToRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------
|
|
|
|
//! Returns size of file in bytes
|
|
|
|
virtual int getSize() {
|
|
|
|
return (int)data.size();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
private:
|
2015-05-19 03:57:13 +00:00
|
|
|
IOStream* stream;
|
|
|
|
std::vector<char> data;
|
|
|
|
size_t t;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
}; // ! class CIrrXML_IOStreamReader
|
|
|
|
|
|
|
|
} // ! Assimp
|
|
|
|
|
|
|
|
#endif // !! INCLUDED_AI_IRRXML_WRAPPER
|