You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

142 lines
4.6 KiB

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