nuclear@0: /* nuclear@0: --------------------------------------------------------------------------- nuclear@0: Open Asset Import Library (assimp) nuclear@0: --------------------------------------------------------------------------- nuclear@0: nuclear@0: Copyright (c) 2006-2012, assimp team nuclear@0: nuclear@0: All rights reserved. nuclear@0: nuclear@0: Redistribution and use of this software in source and binary forms, nuclear@0: with or without modification, are permitted provided that the following nuclear@0: conditions are met: nuclear@0: nuclear@0: * Redistributions of source code must retain the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer. nuclear@0: nuclear@0: * Redistributions in binary form must reproduce the above nuclear@0: copyright notice, this list of conditions and the nuclear@0: following disclaimer in the documentation and/or other nuclear@0: materials provided with the distribution. nuclear@0: nuclear@0: * Neither the name of the assimp team, nor the names of its nuclear@0: contributors may be used to endorse or promote products nuclear@0: derived from this software without specific prior nuclear@0: written permission of the assimp team. nuclear@0: nuclear@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS nuclear@0: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT nuclear@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR nuclear@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT nuclear@0: OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, nuclear@0: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT nuclear@0: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, nuclear@0: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY nuclear@0: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT nuclear@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE nuclear@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. nuclear@0: --------------------------------------------------------------------------- nuclear@0: */ nuclear@0: nuclear@0: /** @file Provides cheat implementations for IOSystem and IOStream to nuclear@0: * redirect exporter output to a blob chain.*/ nuclear@0: nuclear@0: #ifndef AI_BLOBIOSYSTEM_H_INCLUDED nuclear@0: #define AI_BLOBIOSYSTEM_H_INCLUDED nuclear@0: nuclear@0: namespace Assimp { nuclear@0: class BlobIOSystem; nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: /** Redirect IOStream to a blob */ nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: class BlobIOStream : public IOStream nuclear@0: { nuclear@0: public: nuclear@0: nuclear@0: BlobIOStream(BlobIOSystem* creator, const std::string& file, size_t initial = 4096) nuclear@0: : buffer() nuclear@0: , cur_size() nuclear@0: , file_size() nuclear@0: , cursor() nuclear@0: , initial(initial) nuclear@0: , file(file) nuclear@0: , creator(creator) nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: nuclear@0: virtual ~BlobIOStream(); nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: aiExportDataBlob* GetBlob() nuclear@0: { nuclear@0: aiExportDataBlob* blob = new aiExportDataBlob(); nuclear@0: blob->size = file_size; nuclear@0: blob->data = buffer; nuclear@0: nuclear@0: buffer = NULL; nuclear@0: nuclear@0: return blob; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual size_t Read(void* pvBuffer, nuclear@0: size_t pSize, nuclear@0: size_t pCount) nuclear@0: { nuclear@0: return 0; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual size_t Write(const void* pvBuffer, nuclear@0: size_t pSize, nuclear@0: size_t pCount) nuclear@0: { nuclear@0: pSize *= pCount; nuclear@0: if (cursor + pSize > cur_size) { nuclear@0: Grow(cursor + pSize); nuclear@0: } nuclear@0: nuclear@0: memcpy(buffer+cursor, pvBuffer, pSize); nuclear@0: cursor += pSize; nuclear@0: nuclear@0: file_size = std::max(file_size,cursor); nuclear@0: return pCount; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual aiReturn Seek(size_t pOffset, nuclear@0: aiOrigin pOrigin) nuclear@0: { nuclear@0: switch(pOrigin) nuclear@0: { nuclear@0: case aiOrigin_CUR: nuclear@0: cursor += pOffset; nuclear@0: nuclear@0: case aiOrigin_END: nuclear@0: cursor = file_size - pOffset; nuclear@0: nuclear@0: case aiOrigin_SET: nuclear@0: cursor = pOffset; nuclear@0: break; nuclear@0: nuclear@0: default: nuclear@0: return AI_FAILURE; nuclear@0: } nuclear@0: nuclear@0: if (cursor > file_size) { nuclear@0: Grow(cursor); nuclear@0: } nuclear@0: nuclear@0: file_size = std::max(cursor,file_size); nuclear@0: return AI_SUCCESS; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual size_t Tell() const nuclear@0: { nuclear@0: return cursor; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual size_t FileSize() const nuclear@0: { nuclear@0: return file_size; nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual void Flush() nuclear@0: { nuclear@0: // ignore nuclear@0: } nuclear@0: nuclear@0: nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: void Grow(size_t need = 0) nuclear@0: { nuclear@0: // 1.5 and phi are very heap-friendly growth factors (the first nuclear@0: // allows for frequent re-use of heap blocks, the second nuclear@0: // forms a fibonacci sequence with similar characteristics - nuclear@0: // since this heavily depends on the heap implementation nuclear@0: // and other factors as well, i'll just go with 1.5 since nuclear@0: // it is quicker to compute). nuclear@0: size_t new_size = std::max(initial, std::max( need, cur_size+(cur_size>>1) )); nuclear@0: nuclear@0: const uint8_t* const old = buffer; nuclear@0: buffer = new uint8_t[new_size]; nuclear@0: nuclear@0: if (old) { nuclear@0: memcpy(buffer,old,cur_size); nuclear@0: delete[] old; nuclear@0: } nuclear@0: nuclear@0: cur_size = new_size; nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: uint8_t* buffer; nuclear@0: size_t cur_size,file_size, cursor, initial; nuclear@0: nuclear@0: const std::string file; nuclear@0: BlobIOSystem* const creator; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: #define AI_BLOBIO_MAGIC "$blobfile" nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: /** Redirect IOSystem to a blob */ nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: class BlobIOSystem : public IOSystem nuclear@0: { nuclear@0: nuclear@0: friend class BlobIOStream; nuclear@0: typedef std::pair BlobEntry; nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: BlobIOSystem() nuclear@0: { nuclear@0: } nuclear@0: nuclear@0: virtual ~BlobIOSystem() nuclear@0: { nuclear@0: BOOST_FOREACH(BlobEntry& blobby, blobs) { nuclear@0: delete blobby.second; nuclear@0: } nuclear@0: } nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: const char* GetMagicFileName() const nuclear@0: { nuclear@0: return AI_BLOBIO_MAGIC; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: aiExportDataBlob* GetBlobChain() nuclear@0: { nuclear@0: // one must be the master nuclear@0: aiExportDataBlob* master = NULL, *cur; nuclear@0: BOOST_FOREACH(const BlobEntry& blobby, blobs) { nuclear@0: if (blobby.first == AI_BLOBIO_MAGIC) { nuclear@0: master = blobby.second; nuclear@0: break; nuclear@0: } nuclear@0: } nuclear@0: if (!master) { nuclear@0: DefaultLogger::get()->error("BlobIOSystem: no data written or master file was not closed properly."); nuclear@0: return NULL; nuclear@0: } nuclear@0: nuclear@0: master->name.Set(""); nuclear@0: nuclear@0: cur = master; nuclear@0: BOOST_FOREACH(const BlobEntry& blobby, blobs) { nuclear@0: if (blobby.second == master) { nuclear@0: continue; nuclear@0: } nuclear@0: nuclear@0: cur->next = blobby.second; nuclear@0: cur = cur->next; nuclear@0: nuclear@0: // extract the file extension from the file written nuclear@0: const std::string::size_type s = blobby.first.find_first_of('.'); nuclear@0: cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s+1)); nuclear@0: } nuclear@0: nuclear@0: // give up blob ownership nuclear@0: blobs.clear(); nuclear@0: return master; nuclear@0: } nuclear@0: nuclear@0: public: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual bool Exists( const char* pFile) const { nuclear@0: return created.find(std::string(pFile)) != created.end(); nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual char getOsSeparator() const { nuclear@0: return '/'; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual IOStream* Open(const char* pFile, nuclear@0: const char* pMode) nuclear@0: { nuclear@0: if (pMode[0] != 'w') { nuclear@0: return NULL; nuclear@0: } nuclear@0: nuclear@0: created.insert(std::string(pFile)); nuclear@0: return new BlobIOStream(this,std::string(pFile)); nuclear@0: } nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: virtual void Close( IOStream* pFile) nuclear@0: { nuclear@0: delete pFile; nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: nuclear@0: // ------------------------------------------------------------------- nuclear@0: void OnDestruct(const std::string& filename, BlobIOStream* child) nuclear@0: { nuclear@0: // we don't know in which the files are closed, so we nuclear@0: // can't reliably say that the first must be the master nuclear@0: // file ... nuclear@0: blobs.push_back( BlobEntry(filename,child->GetBlob()) ); nuclear@0: } nuclear@0: nuclear@0: private: nuclear@0: std::set created; nuclear@0: std::vector< BlobEntry > blobs; nuclear@0: }; nuclear@0: nuclear@0: nuclear@0: // -------------------------------------------------------------------------------------------- nuclear@0: BlobIOStream :: ~BlobIOStream() nuclear@0: { nuclear@0: creator->OnDestruct(file,this); nuclear@0: delete[] buffer; nuclear@0: } nuclear@0: nuclear@0: nuclear@0: } // end Assimp nuclear@0: nuclear@0: #endif