MemoryIOWrapper.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, 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. // Modified by Lasse Oorni for Urho3D
  34. /** @file MemoryIOWrapper.h
  35. * Handy IOStream/IOSystem implemetation to read directly from a memory buffer */
  36. #ifndef AI_MEMORYIOSTREAM_H_INC
  37. #define AI_MEMORYIOSTREAM_H_INC
  38. #include "../include/assimp/IOStream.hpp"
  39. #include "../include/assimp/IOSystem.hpp"
  40. #include "../include/assimp/ai_assert.h"
  41. // Urho3D: VS2008 compatibility
  42. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  43. #include <stdint.h>
  44. #else
  45. #include "../include/assimp/Compiler/pstdint.h"
  46. #endif
  47. namespace Assimp {
  48. #define AI_MEMORYIO_MAGIC_FILENAME "$$$___magic___$$$"
  49. #define AI_MEMORYIO_MAGIC_FILENAME_LENGTH 17
  50. // ----------------------------------------------------------------------------------
  51. /** Implementation of IOStream to read directly from a memory buffer */
  52. // ----------------------------------------------------------------------------------
  53. class MemoryIOStream : public IOStream
  54. {
  55. //friend class MemoryIOSystem;
  56. public:
  57. MemoryIOStream (const uint8_t* buff, size_t len, bool own = false)
  58. : buffer (buff)
  59. , length(len)
  60. , pos((size_t)0)
  61. , own(own)
  62. {
  63. }
  64. public:
  65. ~MemoryIOStream () {
  66. if(own) {
  67. delete[] buffer;
  68. }
  69. }
  70. // -------------------------------------------------------------------
  71. // Read from stream
  72. size_t Read(void* pvBuffer, size_t pSize, size_t pCount) {
  73. const size_t cnt = std::min(pCount,(length-pos)/pSize),ofs = pSize*cnt;
  74. memcpy(pvBuffer,buffer+pos,ofs);
  75. pos += ofs;
  76. return cnt;
  77. }
  78. // -------------------------------------------------------------------
  79. // Write to stream
  80. size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/,size_t /*pCount*/) {
  81. ai_assert(false); // won't be needed
  82. return 0;
  83. }
  84. // -------------------------------------------------------------------
  85. // Seek specific position
  86. aiReturn Seek(size_t pOffset, aiOrigin pOrigin) {
  87. if (aiOrigin_SET == pOrigin) {
  88. if (pOffset >= length) {
  89. return AI_FAILURE;
  90. }
  91. pos = pOffset;
  92. }
  93. else if (aiOrigin_END == pOrigin) {
  94. if (pOffset >= length) {
  95. return AI_FAILURE;
  96. }
  97. pos = length-pOffset;
  98. }
  99. else {
  100. if (pOffset+pos >= length) {
  101. return AI_FAILURE;
  102. }
  103. pos += pOffset;
  104. }
  105. return AI_SUCCESS;
  106. }
  107. // -------------------------------------------------------------------
  108. // Get current seek position
  109. size_t Tell() const {
  110. return pos;
  111. }
  112. // -------------------------------------------------------------------
  113. // Get size of file
  114. size_t FileSize() const {
  115. return length;
  116. }
  117. // -------------------------------------------------------------------
  118. // Flush file contents
  119. void Flush() {
  120. ai_assert(false); // won't be needed
  121. }
  122. private:
  123. const uint8_t* buffer;
  124. size_t length,pos;
  125. bool own;
  126. };
  127. // ---------------------------------------------------------------------------
  128. /** Dummy IO system to read from a memory buffer */
  129. class MemoryIOSystem : public IOSystem
  130. {
  131. public:
  132. /** Constructor. */
  133. MemoryIOSystem (const uint8_t* buff, size_t len)
  134. : buffer (buff), length(len) {
  135. }
  136. /** Destructor. */
  137. ~MemoryIOSystem() {
  138. }
  139. // -------------------------------------------------------------------
  140. /** Tests for the existence of a file at the given path. */
  141. bool Exists( const char* pFile) const {
  142. return !strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH);
  143. }
  144. // -------------------------------------------------------------------
  145. /** Returns the directory separator. */
  146. char getOsSeparator() const {
  147. return '/'; // why not? it doesn't care
  148. }
  149. // -------------------------------------------------------------------
  150. /** Open a new file with a given path. */
  151. IOStream* Open( const char* pFile, const char* /*pMode*/ = "rb") {
  152. if (strncmp(pFile,AI_MEMORYIO_MAGIC_FILENAME,AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
  153. return NULL;
  154. }
  155. return new MemoryIOStream(buffer,length);
  156. }
  157. // -------------------------------------------------------------------
  158. /** Closes the given file and releases all resources associated with it. */
  159. void Close( IOStream* /*pFile*/) {
  160. }
  161. // -------------------------------------------------------------------
  162. /** Compare two paths */
  163. bool ComparePaths (const char* /*one*/, const char* /*second*/) const {
  164. return false;
  165. }
  166. private:
  167. const uint8_t* buffer;
  168. size_t length;
  169. };
  170. } // end namespace Assimp
  171. #endif