vrshoot

view libs/assimp/irrXMLWrapper.h @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 #ifndef INCLUDED_AI_IRRXML_WRAPPER
42 #define INCLUDED_AI_IRRXML_WRAPPER
44 // some long includes ....
45 #include "irrXML/irrXML.h"
46 #include "assimp/IOStream.hpp"
47 namespace Assimp {
49 // ---------------------------------------------------------------------------------
50 /** @brief Utility class to make IrrXML work together with our custom IO system
51 * See the IrrXML docs for more details.
52 *
53 * Construct IrrXML-Reader in BaseImporter::InternReadFile():
54 * @code
55 * // open the file
56 * boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
57 * if( file.get() == NULL) {
58 * throw DeadlyImportError( "Failed to open file " + pFile + ".");
59 * }
60 *
61 * // generate a XML reader for it
62 * boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
63 * mReader = irr::io::createIrrXMLReader( mIOWrapper.get());
64 * if( !mReader) {
65 * ThrowException( "xxxx: Unable to open file.");
66 * }
67 * @endcode
68 **/
69 class CIrrXML_IOStreamReader
70 : public irr::io::IFileReadCallBack
71 {
72 public:
74 // ----------------------------------------------------------------------------------
75 //! Construction from an existing IOStream
76 CIrrXML_IOStreamReader(IOStream* _stream)
77 : stream (_stream)
78 , t (0)
79 {
81 // Map the buffer into memory and convert it to UTF8. IrrXML provides its
82 // own conversion, which is merely a cast from uintNN_t to uint8_t. Thus,
83 // it is not suitable for our purposes and we have to do it BEFORE IrrXML
84 // gets the buffer. Sadly, this forces as to map the whole file into
85 // memory.
87 data.resize(stream->FileSize());
88 stream->Read(&data[0],data.size(),1);
90 BaseImporter::ConvertToUTF8(data);
91 }
93 // ----------------------------------------------------------------------------------
94 //! Virtual destructor
95 virtual ~CIrrXML_IOStreamReader() {};
97 // ----------------------------------------------------------------------------------
98 //! Reads an amount of bytes from the file.
99 /** @param buffer: Pointer to output buffer.
100 * @param sizeToRead: Amount of bytes to read
101 * @return Returns how much bytes were read. */
102 virtual int read(void* buffer, int sizeToRead) {
103 if(sizeToRead<0) {
104 return 0;
105 }
106 if(t+sizeToRead>data.size()) {
107 sizeToRead = data.size()-t;
108 }
110 memcpy(buffer,&data.front()+t,sizeToRead);
112 t += sizeToRead;
113 return sizeToRead;
114 }
116 // ----------------------------------------------------------------------------------
117 //! Returns size of file in bytes
118 virtual int getSize() {
119 return (int)data.size();
120 }
122 private:
123 IOStream* stream;
124 std::vector<char> data;
125 size_t t;
127 }; // ! class CIrrXML_IOStreamReader
129 } // ! Assimp
131 #endif // !! INCLUDED_AI_IRRXML_WRAPPER