BlobIOSystem.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Provides cheat implementations for IOSystem and IOStream to
  35. * redirect exporter output to a blob chain.*/
  36. #ifndef AI_BLOBIOSYSTEM_H_INCLUDED
  37. #define AI_BLOBIOSYSTEM_H_INCLUDED
  38. #include "./../include/assimp/IOStream.hpp"
  39. #include "./../include/assimp/cexport.h"
  40. #include "./../include/assimp/IOSystem.hpp"
  41. #include "./../include/assimp/DefaultLogger.hpp"
  42. #include <stdint.h>
  43. #include <set>
  44. #include <vector>
  45. namespace Assimp {
  46. class BlobIOSystem;
  47. // --------------------------------------------------------------------------------------------
  48. /** Redirect IOStream to a blob */
  49. // --------------------------------------------------------------------------------------------
  50. class BlobIOStream : public IOStream
  51. {
  52. public:
  53. BlobIOStream(BlobIOSystem* creator, const std::string& file, size_t initial = 4096)
  54. : buffer()
  55. , cur_size()
  56. , file_size()
  57. , cursor()
  58. , initial(initial)
  59. , file(file)
  60. , creator(creator)
  61. {
  62. }
  63. virtual ~BlobIOStream();
  64. public:
  65. // -------------------------------------------------------------------
  66. aiExportDataBlob* GetBlob()
  67. {
  68. aiExportDataBlob* blob = new aiExportDataBlob();
  69. blob->size = file_size;
  70. blob->data = buffer;
  71. buffer = NULL;
  72. return blob;
  73. }
  74. public:
  75. // -------------------------------------------------------------------
  76. virtual size_t Read( void *,
  77. size_t,
  78. size_t )
  79. {
  80. return 0;
  81. }
  82. // -------------------------------------------------------------------
  83. virtual size_t Write(const void* pvBuffer,
  84. size_t pSize,
  85. size_t pCount)
  86. {
  87. pSize *= pCount;
  88. if (cursor + pSize > cur_size) {
  89. Grow(cursor + pSize);
  90. }
  91. memcpy(buffer+cursor, pvBuffer, pSize);
  92. cursor += pSize;
  93. file_size = std::max(file_size,cursor);
  94. return pCount;
  95. }
  96. // -------------------------------------------------------------------
  97. virtual aiReturn Seek(size_t pOffset,
  98. aiOrigin pOrigin)
  99. {
  100. switch(pOrigin)
  101. {
  102. case aiOrigin_CUR:
  103. cursor += pOffset;
  104. break;
  105. case aiOrigin_END:
  106. cursor = file_size - pOffset;
  107. break;
  108. case aiOrigin_SET:
  109. cursor = pOffset;
  110. break;
  111. default:
  112. return AI_FAILURE;
  113. }
  114. if (cursor > file_size) {
  115. Grow(cursor);
  116. }
  117. file_size = std::max(cursor,file_size);
  118. return AI_SUCCESS;
  119. }
  120. // -------------------------------------------------------------------
  121. virtual size_t Tell() const
  122. {
  123. return cursor;
  124. }
  125. // -------------------------------------------------------------------
  126. virtual size_t FileSize() const
  127. {
  128. return file_size;
  129. }
  130. // -------------------------------------------------------------------
  131. virtual void Flush()
  132. {
  133. // ignore
  134. }
  135. private:
  136. // -------------------------------------------------------------------
  137. void Grow(size_t need = 0)
  138. {
  139. // 1.5 and phi are very heap-friendly growth factors (the first
  140. // allows for frequent re-use of heap blocks, the second
  141. // forms a fibonacci sequence with similar characteristics -
  142. // since this heavily depends on the heap implementation
  143. // and other factors as well, i'll just go with 1.5 since
  144. // it is quicker to compute).
  145. size_t new_size = std::max(initial, std::max( need, cur_size+(cur_size>>1) ));
  146. const uint8_t* const old = buffer;
  147. buffer = new uint8_t[new_size];
  148. if (old) {
  149. memcpy(buffer,old,cur_size);
  150. delete[] old;
  151. }
  152. cur_size = new_size;
  153. }
  154. private:
  155. uint8_t* buffer;
  156. size_t cur_size,file_size, cursor, initial;
  157. const std::string file;
  158. BlobIOSystem* const creator;
  159. };
  160. #define AI_BLOBIO_MAGIC "$blobfile"
  161. // --------------------------------------------------------------------------------------------
  162. /** Redirect IOSystem to a blob */
  163. // --------------------------------------------------------------------------------------------
  164. class BlobIOSystem : public IOSystem
  165. {
  166. friend class BlobIOStream;
  167. typedef std::pair<std::string, aiExportDataBlob*> BlobEntry;
  168. public:
  169. BlobIOSystem()
  170. {
  171. }
  172. virtual ~BlobIOSystem()
  173. {
  174. for(BlobEntry& blobby : blobs) {
  175. delete blobby.second;
  176. }
  177. }
  178. public:
  179. // -------------------------------------------------------------------
  180. const char* GetMagicFileName() const
  181. {
  182. return AI_BLOBIO_MAGIC;
  183. }
  184. // -------------------------------------------------------------------
  185. aiExportDataBlob* GetBlobChain()
  186. {
  187. // one must be the master
  188. aiExportDataBlob* master = NULL, *cur;
  189. for(const BlobEntry& blobby : blobs) {
  190. if (blobby.first == AI_BLOBIO_MAGIC) {
  191. master = blobby.second;
  192. break;
  193. }
  194. }
  195. if (!master) {
  196. DefaultLogger::get()->error("BlobIOSystem: no data written or master file was not closed properly.");
  197. return NULL;
  198. }
  199. master->name.Set("");
  200. cur = master;
  201. for(const BlobEntry& blobby : blobs) {
  202. if (blobby.second == master) {
  203. continue;
  204. }
  205. cur->next = blobby.second;
  206. cur = cur->next;
  207. // extract the file extension from the file written
  208. const std::string::size_type s = blobby.first.find_first_of('.');
  209. cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s+1));
  210. }
  211. // give up blob ownership
  212. blobs.clear();
  213. return master;
  214. }
  215. public:
  216. // -------------------------------------------------------------------
  217. virtual bool Exists( const char* pFile) const {
  218. return created.find(std::string(pFile)) != created.end();
  219. }
  220. // -------------------------------------------------------------------
  221. virtual char getOsSeparator() const {
  222. return '/';
  223. }
  224. // -------------------------------------------------------------------
  225. virtual IOStream* Open(const char* pFile,
  226. const char* pMode)
  227. {
  228. if (pMode[0] != 'w') {
  229. return NULL;
  230. }
  231. created.insert(std::string(pFile));
  232. return new BlobIOStream(this,std::string(pFile));
  233. }
  234. // -------------------------------------------------------------------
  235. virtual void Close( IOStream* pFile)
  236. {
  237. delete pFile;
  238. }
  239. private:
  240. // -------------------------------------------------------------------
  241. void OnDestruct(const std::string& filename, BlobIOStream* child)
  242. {
  243. // we don't know in which the files are closed, so we
  244. // can't reliably say that the first must be the master
  245. // file ...
  246. blobs.push_back( BlobEntry(filename,child->GetBlob()) );
  247. }
  248. private:
  249. std::set<std::string> created;
  250. std::vector< BlobEntry > blobs;
  251. };
  252. // --------------------------------------------------------------------------------------------
  253. BlobIOStream :: ~BlobIOStream()
  254. {
  255. creator->OnDestruct(file,this);
  256. delete[] buffer;
  257. }
  258. } // end Assimp
  259. #endif