Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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