2
0

MemoryIOWrapper.h 7.8 KB

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