irrXMLWrapper.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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. #include "BaseImporter.h"
  39. #include <vector>
  40. namespace Assimp {
  41. // ---------------------------------------------------------------------------------
  42. /** @brief Utility class to make IrrXML work together with our custom IO system
  43. * See the IrrXML docs for more details.
  44. *
  45. * Construct IrrXML-Reader in BaseImporter::InternReadFile():
  46. * @code
  47. * // open the file
  48. * std::unique_ptr<IOStream> file( pIOHandler->Open( pFile));
  49. * if( file.get() == NULL) {
  50. * throw DeadlyImportError( "Failed to open file " + pFile + ".");
  51. * }
  52. *
  53. * // generate a XML reader for it
  54. * std::unique_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
  55. * mReader = irr::io::createIrrXMLReader( mIOWrapper.get());
  56. * if( !mReader) {
  57. * ThrowException( "xxxx: Unable to open file.");
  58. * }
  59. * @endcode
  60. **/
  61. class CIrrXML_IOStreamReader
  62. : public irr::io::IFileReadCallBack
  63. {
  64. public:
  65. // ----------------------------------------------------------------------------------
  66. //! Construction from an existing IOStream
  67. explicit CIrrXML_IOStreamReader(IOStream* _stream)
  68. : stream (_stream)
  69. , t (0)
  70. {
  71. // Map the buffer into memory and convert it to UTF8. IrrXML provides its
  72. // own conversion, which is merely a cast from uintNN_t to uint8_t. Thus,
  73. // it is not suitable for our purposes and we have to do it BEFORE IrrXML
  74. // gets the buffer. Sadly, this forces us to map the whole file into
  75. // memory.
  76. data.resize(stream->FileSize());
  77. stream->Read(&data[0],data.size(),1);
  78. // Remove null characters from the input sequence otherwise the parsing will utterly fail
  79. unsigned int size = 0;
  80. unsigned int size_max = static_cast<unsigned int>(data.size());
  81. for(unsigned int i = 0; i < size_max; i++) {
  82. if(data[i] != '\0') {
  83. data[size++] = data[i];
  84. }
  85. }
  86. data.resize(size);
  87. BaseImporter::ConvertToUTF8(data);
  88. }
  89. // ----------------------------------------------------------------------------------
  90. //! Virtual destructor
  91. virtual ~CIrrXML_IOStreamReader() {}
  92. // ----------------------------------------------------------------------------------
  93. //! Reads an amount of bytes from the file.
  94. /** @param buffer: Pointer to output buffer.
  95. * @param sizeToRead: Amount of bytes to read
  96. * @return Returns how much bytes were read. */
  97. virtual int read(void* buffer, int sizeToRead) {
  98. if(sizeToRead<0) {
  99. return 0;
  100. }
  101. if(t+sizeToRead>data.size()) {
  102. sizeToRead = static_cast<int>(data.size()-t);
  103. }
  104. memcpy(buffer,&data.front()+t,sizeToRead);
  105. t += sizeToRead;
  106. return sizeToRead;
  107. }
  108. // ----------------------------------------------------------------------------------
  109. //! Returns size of file in bytes
  110. virtual int getSize() {
  111. return (int)data.size();
  112. }
  113. private:
  114. IOStream* stream;
  115. std::vector<char> data;
  116. size_t t;
  117. }; // ! class CIrrXML_IOStreamReader
  118. } // ! Assimp
  119. #endif // !! INCLUDED_AI_IRRXML_WRAPPER