MemoryIOWrapper.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, 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. /** @file MemoryIOWrapper.h
  34. * Handy IOStream/IOSystem implemetation to read directly from a memory buffer */
  35. #pragma once
  36. #ifndef AI_MEMORYIOSTREAM_H_INC
  37. #define AI_MEMORYIOSTREAM_H_INC
  38. #ifdef __GNUC__
  39. # pragma GCC system_header
  40. #endif
  41. #include <assimp/IOStream.hpp>
  42. #include <assimp/IOSystem.hpp>
  43. #include <assimp/ai_assert.h>
  44. #include <stdint.h>
  45. namespace Assimp {
  46. #define AI_MEMORYIO_MAGIC_FILENAME "$$$___magic___$$$"
  47. #define AI_MEMORYIO_MAGIC_FILENAME_LENGTH 17
  48. // ----------------------------------------------------------------------------------
  49. /** Implementation of IOStream to read directly from a memory buffer */
  50. // ----------------------------------------------------------------------------------
  51. class MemoryIOStream : public IOStream {
  52. public:
  53. MemoryIOStream (const uint8_t* buff, size_t len, bool own = false)
  54. : buffer (buff)
  55. , length(len)
  56. , pos((size_t)0)
  57. , own(own) {
  58. // empty
  59. }
  60. ~MemoryIOStream () {
  61. if(own) {
  62. delete[] buffer;
  63. }
  64. }
  65. // -------------------------------------------------------------------
  66. // Read from stream
  67. size_t Read(void* pvBuffer, size_t pSize, size_t pCount) {
  68. ai_assert(nullptr != pvBuffer);
  69. ai_assert(0 != pSize);
  70. const size_t cnt = std::min( pCount, (length-pos) / pSize);
  71. const size_t ofs = pSize * cnt;
  72. ::memcpy(pvBuffer,buffer+pos,ofs);
  73. pos += ofs;
  74. return cnt;
  75. }
  76. // -------------------------------------------------------------------
  77. // Write to stream
  78. size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/,size_t /*pCount*/) {
  79. ai_assert(false); // won't be needed
  80. return 0;
  81. }
  82. // -------------------------------------------------------------------
  83. // Seek specific position
  84. aiReturn Seek(size_t pOffset, aiOrigin pOrigin) {
  85. if (aiOrigin_SET == pOrigin) {
  86. if (pOffset > length) {
  87. return AI_FAILURE;
  88. }
  89. pos = pOffset;
  90. } else if (aiOrigin_END == pOrigin) {
  91. if (pOffset > length) {
  92. return AI_FAILURE;
  93. }
  94. pos = length-pOffset;
  95. } else {
  96. if (pOffset+pos > length) {
  97. return AI_FAILURE;
  98. }
  99. pos += pOffset;
  100. }
  101. return AI_SUCCESS;
  102. }
  103. // -------------------------------------------------------------------
  104. // Get current seek position
  105. size_t Tell() const {
  106. return pos;
  107. }
  108. // -------------------------------------------------------------------
  109. // Get size of file
  110. size_t FileSize() const {
  111. return length;
  112. }
  113. // -------------------------------------------------------------------
  114. // Flush file contents
  115. void Flush() {
  116. ai_assert(false); // won't be needed
  117. }
  118. private:
  119. const uint8_t* buffer;
  120. size_t length,pos;
  121. bool own;
  122. };
  123. // ---------------------------------------------------------------------------
  124. /** Dummy IO system to read from a memory buffer */
  125. class MemoryIOSystem : public IOSystem {
  126. public:
  127. /** Constructor. */
  128. MemoryIOSystem(const uint8_t* buff, size_t len, IOSystem* io)
  129. : buffer(buff)
  130. , length(len)
  131. , existing_io(io)
  132. , created_streams() {
  133. // empty
  134. }
  135. /** Destructor. */
  136. ~MemoryIOSystem() {
  137. }
  138. // -------------------------------------------------------------------
  139. /** Tests for the existence of a file at the given path. */
  140. bool Exists(const char* pFile) const override {
  141. if (0 == strncmp( pFile, AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH ) ) {
  142. return true;
  143. }
  144. return existing_io ? existing_io->Exists(pFile) : false;
  145. }
  146. // -------------------------------------------------------------------
  147. /** Returns the directory separator. */
  148. char getOsSeparator() const override {
  149. return existing_io ? existing_io->getOsSeparator()
  150. : '/'; // why not? it doesn't care
  151. }
  152. // -------------------------------------------------------------------
  153. /** Open a new file with a given path. */
  154. IOStream* Open(const char* pFile, const char* pMode = "rb") override {
  155. if ( 0 == strncmp( pFile, AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH ) ) {
  156. created_streams.emplace_back(new MemoryIOStream(buffer, length));
  157. return created_streams.back();
  158. }
  159. return existing_io ? existing_io->Open(pFile, pMode) : NULL;
  160. }
  161. // -------------------------------------------------------------------
  162. /** Closes the given file and releases all resources associated with it. */
  163. void Close( IOStream* pFile) override {
  164. auto it = std::find(created_streams.begin(), created_streams.end(), pFile);
  165. if (it != created_streams.end()) {
  166. delete pFile;
  167. created_streams.erase(it);
  168. } else if (existing_io) {
  169. existing_io->Close(pFile);
  170. }
  171. }
  172. // -------------------------------------------------------------------
  173. /** Compare two paths */
  174. bool ComparePaths(const char* one, const char* second) const override {
  175. return existing_io ? existing_io->ComparePaths(one, second) : false;
  176. }
  177. bool PushDirectory( const std::string &path ) override {
  178. return existing_io ? existing_io->PushDirectory(path) : false;
  179. }
  180. const std::string &CurrentDirectory() const override {
  181. static std::string empty;
  182. return existing_io ? existing_io->CurrentDirectory() : empty;
  183. }
  184. size_t StackSize() const override {
  185. return existing_io ? existing_io->StackSize() : 0;
  186. }
  187. bool PopDirectory() override {
  188. return existing_io ? existing_io->PopDirectory() : false;
  189. }
  190. bool CreateDirectory( const std::string &path ) override {
  191. return existing_io ? existing_io->CreateDirectory(path) : false;
  192. }
  193. bool ChangeDirectory( const std::string &path ) override {
  194. return existing_io ? existing_io->ChangeDirectory(path) : false;
  195. }
  196. bool DeleteFile( const std::string &file ) override {
  197. return existing_io ? existing_io->DeleteFile(file) : false;
  198. }
  199. private:
  200. const uint8_t* buffer;
  201. size_t length;
  202. IOSystem* existing_io;
  203. std::vector<IOStream*> created_streams;
  204. };
  205. } // end namespace Assimp
  206. #endif