BlobIOSystem.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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. namespace Assimp {
  39. class BlobIOSystem;
  40. // --------------------------------------------------------------------------------------------
  41. /** Redirect IOStream to a blob */
  42. // --------------------------------------------------------------------------------------------
  43. class BlobIOStream : public IOStream
  44. {
  45. public:
  46. BlobIOStream(BlobIOSystem* creator, const std::string& file, size_t initial = 4096)
  47. : buffer()
  48. , cur_size()
  49. , file_size()
  50. , cursor()
  51. , initial(initial)
  52. , file(file)
  53. , creator(creator)
  54. {
  55. }
  56. virtual ~BlobIOStream();
  57. public:
  58. // -------------------------------------------------------------------
  59. aiExportDataBlob* GetBlob()
  60. {
  61. aiExportDataBlob* blob = new aiExportDataBlob();
  62. blob->size = file_size;
  63. blob->data = buffer;
  64. buffer = NULL;
  65. return blob;
  66. }
  67. public:
  68. // -------------------------------------------------------------------
  69. virtual size_t Read(void* pvBuffer,
  70. size_t pSize,
  71. size_t pCount)
  72. {
  73. return 0;
  74. }
  75. // -------------------------------------------------------------------
  76. virtual size_t Write(const void* pvBuffer,
  77. size_t pSize,
  78. size_t pCount)
  79. {
  80. pSize *= pCount;
  81. if (cursor + pSize > cur_size) {
  82. Grow(cursor + pSize);
  83. }
  84. memcpy(buffer+cursor, pvBuffer, pSize);
  85. cursor += pSize;
  86. file_size = std::max(file_size,cursor);
  87. return pCount;
  88. }
  89. // -------------------------------------------------------------------
  90. virtual aiReturn Seek(size_t pOffset,
  91. aiOrigin pOrigin)
  92. {
  93. switch(pOrigin)
  94. {
  95. case aiOrigin_CUR:
  96. cursor += pOffset;
  97. case aiOrigin_END:
  98. cursor = file_size - pOffset;
  99. case aiOrigin_SET:
  100. cursor = pOffset;
  101. break;
  102. default:
  103. return AI_FAILURE;
  104. }
  105. if (cursor > file_size) {
  106. Grow(cursor);
  107. }
  108. file_size = std::max(cursor,file_size);
  109. return AI_SUCCESS;
  110. }
  111. // -------------------------------------------------------------------
  112. virtual size_t Tell() const
  113. {
  114. return cursor;
  115. }
  116. // -------------------------------------------------------------------
  117. virtual size_t FileSize() const
  118. {
  119. return file_size;
  120. }
  121. // -------------------------------------------------------------------
  122. virtual void Flush()
  123. {
  124. // ignore
  125. }
  126. private:
  127. // -------------------------------------------------------------------
  128. void Grow(size_t need = 0)
  129. {
  130. // 1.5 and phi are very heap-friendly growth factors (the first
  131. // allows for frequent re-use of heap blocks, the second
  132. // forms a fibonacci sequence with similar characteristics -
  133. // since this heavily depends on the heap implementation
  134. // and other factors as well, i'll just go with 1.5 since
  135. // it is quicker to compute).
  136. size_t new_size = std::max(initial, std::max( need, cur_size+(cur_size>>1) ));
  137. const uint8_t* const old = buffer;
  138. buffer = new uint8_t[new_size];
  139. if (old) {
  140. memcpy(buffer,old,cur_size);
  141. delete[] old;
  142. }
  143. cur_size = new_size;
  144. }
  145. private:
  146. uint8_t* buffer;
  147. size_t cur_size,file_size, cursor, initial;
  148. const std::string file;
  149. BlobIOSystem* const creator;
  150. };
  151. #define AI_BLOBIO_MAGIC "$blobfile"
  152. // --------------------------------------------------------------------------------------------
  153. /** Redirect IOSystem to a blob */
  154. // --------------------------------------------------------------------------------------------
  155. class BlobIOSystem : public IOSystem
  156. {
  157. friend class BlobIOStream;
  158. typedef std::pair<std::string, aiExportDataBlob*> BlobEntry;
  159. public:
  160. BlobIOSystem()
  161. {
  162. }
  163. virtual ~BlobIOSystem()
  164. {
  165. BOOST_FOREACH(BlobEntry& blobby, blobs) {
  166. delete blobby.second;
  167. }
  168. }
  169. public:
  170. // -------------------------------------------------------------------
  171. const char* GetMagicFileName() const
  172. {
  173. return AI_BLOBIO_MAGIC;
  174. }
  175. // -------------------------------------------------------------------
  176. aiExportDataBlob* GetBlobChain()
  177. {
  178. // one must be the master
  179. aiExportDataBlob* master = NULL, *cur;
  180. BOOST_FOREACH(const BlobEntry& blobby, blobs) {
  181. if (blobby.first == AI_BLOBIO_MAGIC) {
  182. master = blobby.second;
  183. break;
  184. }
  185. }
  186. if (!master) {
  187. DefaultLogger::get()->error("BlobIOSystem: no data written or master file was not closed properly.");
  188. return NULL;
  189. }
  190. master->name.Set("");
  191. cur = master;
  192. BOOST_FOREACH(const BlobEntry& blobby, blobs) {
  193. if (blobby.second == master) {
  194. continue;
  195. }
  196. cur->next = blobby.second;
  197. cur = cur->next;
  198. // extract the file extension from the file written
  199. const std::string::size_type s = blobby.first.find_first_of('.');
  200. cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s+1));
  201. }
  202. // give up blob ownership
  203. blobs.clear();
  204. return master;
  205. }
  206. public:
  207. // -------------------------------------------------------------------
  208. virtual bool Exists( const char* pFile) const {
  209. return created.find(std::string(pFile)) != created.end();
  210. }
  211. // -------------------------------------------------------------------
  212. virtual char getOsSeparator() const {
  213. return '/';
  214. }
  215. // -------------------------------------------------------------------
  216. virtual IOStream* Open(const char* pFile,
  217. const char* pMode)
  218. {
  219. if (pMode[0] != 'w') {
  220. return NULL;
  221. }
  222. created.insert(std::string(pFile));
  223. return new BlobIOStream(this,std::string(pFile));
  224. }
  225. // -------------------------------------------------------------------
  226. virtual void Close( IOStream* pFile)
  227. {
  228. delete pFile;
  229. }
  230. private:
  231. // -------------------------------------------------------------------
  232. void OnDestruct(const std::string& filename, BlobIOStream* child)
  233. {
  234. // we don't know in which the files are closed, so we
  235. // can't reliably say that the first must be the master
  236. // file ...
  237. blobs.push_back( BlobEntry(filename,child->GetBlob()) );
  238. }
  239. private:
  240. std::set<std::string> created;
  241. std::vector< BlobEntry > blobs;
  242. };
  243. // --------------------------------------------------------------------------------------------
  244. BlobIOStream :: ~BlobIOStream()
  245. {
  246. creator->OnDestruct(file,this);
  247. delete[] buffer;
  248. }
  249. } // end Assimp
  250. #endif