vrshoot
diff libs/assimp/BlobIOSystem.h @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/BlobIOSystem.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,326 @@ 1.4 +/* 1.5 +--------------------------------------------------------------------------- 1.6 +Open Asset Import Library (assimp) 1.7 +--------------------------------------------------------------------------- 1.8 + 1.9 +Copyright (c) 2006-2012, assimp team 1.10 + 1.11 +All rights reserved. 1.12 + 1.13 +Redistribution and use of this software in source and binary forms, 1.14 +with or without modification, are permitted provided that the following 1.15 +conditions are met: 1.16 + 1.17 +* Redistributions of source code must retain the above 1.18 + copyright notice, this list of conditions and the 1.19 + following disclaimer. 1.20 + 1.21 +* Redistributions in binary form must reproduce the above 1.22 + copyright notice, this list of conditions and the 1.23 + following disclaimer in the documentation and/or other 1.24 + materials provided with the distribution. 1.25 + 1.26 +* Neither the name of the assimp team, nor the names of its 1.27 + contributors may be used to endorse or promote products 1.28 + derived from this software without specific prior 1.29 + written permission of the assimp team. 1.30 + 1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.42 +--------------------------------------------------------------------------- 1.43 +*/ 1.44 + 1.45 +/** @file Provides cheat implementations for IOSystem and IOStream to 1.46 + * redirect exporter output to a blob chain.*/ 1.47 + 1.48 +#ifndef AI_BLOBIOSYSTEM_H_INCLUDED 1.49 +#define AI_BLOBIOSYSTEM_H_INCLUDED 1.50 + 1.51 +namespace Assimp { 1.52 + class BlobIOSystem; 1.53 + 1.54 +// -------------------------------------------------------------------------------------------- 1.55 +/** Redirect IOStream to a blob */ 1.56 +// -------------------------------------------------------------------------------------------- 1.57 +class BlobIOStream : public IOStream 1.58 +{ 1.59 +public: 1.60 + 1.61 + BlobIOStream(BlobIOSystem* creator, const std::string& file, size_t initial = 4096) 1.62 + : buffer() 1.63 + , cur_size() 1.64 + , file_size() 1.65 + , cursor() 1.66 + , initial(initial) 1.67 + , file(file) 1.68 + , creator(creator) 1.69 + { 1.70 + } 1.71 + 1.72 + 1.73 + virtual ~BlobIOStream(); 1.74 + 1.75 +public: 1.76 + 1.77 + // ------------------------------------------------------------------- 1.78 + aiExportDataBlob* GetBlob() 1.79 + { 1.80 + aiExportDataBlob* blob = new aiExportDataBlob(); 1.81 + blob->size = file_size; 1.82 + blob->data = buffer; 1.83 + 1.84 + buffer = NULL; 1.85 + 1.86 + return blob; 1.87 + } 1.88 + 1.89 + 1.90 +public: 1.91 + 1.92 + 1.93 + // ------------------------------------------------------------------- 1.94 + virtual size_t Read(void* pvBuffer, 1.95 + size_t pSize, 1.96 + size_t pCount) 1.97 + { 1.98 + return 0; 1.99 + } 1.100 + 1.101 + // ------------------------------------------------------------------- 1.102 + virtual size_t Write(const void* pvBuffer, 1.103 + size_t pSize, 1.104 + size_t pCount) 1.105 + { 1.106 + pSize *= pCount; 1.107 + if (cursor + pSize > cur_size) { 1.108 + Grow(cursor + pSize); 1.109 + } 1.110 + 1.111 + memcpy(buffer+cursor, pvBuffer, pSize); 1.112 + cursor += pSize; 1.113 + 1.114 + file_size = std::max(file_size,cursor); 1.115 + return pCount; 1.116 + } 1.117 + 1.118 + // ------------------------------------------------------------------- 1.119 + virtual aiReturn Seek(size_t pOffset, 1.120 + aiOrigin pOrigin) 1.121 + { 1.122 + switch(pOrigin) 1.123 + { 1.124 + case aiOrigin_CUR: 1.125 + cursor += pOffset; 1.126 + 1.127 + case aiOrigin_END: 1.128 + cursor = file_size - pOffset; 1.129 + 1.130 + case aiOrigin_SET: 1.131 + cursor = pOffset; 1.132 + break; 1.133 + 1.134 + default: 1.135 + return AI_FAILURE; 1.136 + } 1.137 + 1.138 + if (cursor > file_size) { 1.139 + Grow(cursor); 1.140 + } 1.141 + 1.142 + file_size = std::max(cursor,file_size); 1.143 + return AI_SUCCESS; 1.144 + } 1.145 + 1.146 + // ------------------------------------------------------------------- 1.147 + virtual size_t Tell() const 1.148 + { 1.149 + return cursor; 1.150 + } 1.151 + 1.152 + // ------------------------------------------------------------------- 1.153 + virtual size_t FileSize() const 1.154 + { 1.155 + return file_size; 1.156 + } 1.157 + 1.158 + // ------------------------------------------------------------------- 1.159 + virtual void Flush() 1.160 + { 1.161 + // ignore 1.162 + } 1.163 + 1.164 + 1.165 + 1.166 +private: 1.167 + 1.168 + // ------------------------------------------------------------------- 1.169 + void Grow(size_t need = 0) 1.170 + { 1.171 + // 1.5 and phi are very heap-friendly growth factors (the first 1.172 + // allows for frequent re-use of heap blocks, the second 1.173 + // forms a fibonacci sequence with similar characteristics - 1.174 + // since this heavily depends on the heap implementation 1.175 + // and other factors as well, i'll just go with 1.5 since 1.176 + // it is quicker to compute). 1.177 + size_t new_size = std::max(initial, std::max( need, cur_size+(cur_size>>1) )); 1.178 + 1.179 + const uint8_t* const old = buffer; 1.180 + buffer = new uint8_t[new_size]; 1.181 + 1.182 + if (old) { 1.183 + memcpy(buffer,old,cur_size); 1.184 + delete[] old; 1.185 + } 1.186 + 1.187 + cur_size = new_size; 1.188 + } 1.189 + 1.190 +private: 1.191 + 1.192 + uint8_t* buffer; 1.193 + size_t cur_size,file_size, cursor, initial; 1.194 + 1.195 + const std::string file; 1.196 + BlobIOSystem* const creator; 1.197 +}; 1.198 + 1.199 + 1.200 +#define AI_BLOBIO_MAGIC "$blobfile" 1.201 + 1.202 +// -------------------------------------------------------------------------------------------- 1.203 +/** Redirect IOSystem to a blob */ 1.204 +// -------------------------------------------------------------------------------------------- 1.205 +class BlobIOSystem : public IOSystem 1.206 +{ 1.207 + 1.208 + friend class BlobIOStream; 1.209 + typedef std::pair<std::string, aiExportDataBlob*> BlobEntry; 1.210 + 1.211 +public: 1.212 + 1.213 + BlobIOSystem() 1.214 + { 1.215 + } 1.216 + 1.217 + virtual ~BlobIOSystem() 1.218 + { 1.219 + BOOST_FOREACH(BlobEntry& blobby, blobs) { 1.220 + delete blobby.second; 1.221 + } 1.222 + } 1.223 + 1.224 +public: 1.225 + 1.226 + // ------------------------------------------------------------------- 1.227 + const char* GetMagicFileName() const 1.228 + { 1.229 + return AI_BLOBIO_MAGIC; 1.230 + } 1.231 + 1.232 + 1.233 + // ------------------------------------------------------------------- 1.234 + aiExportDataBlob* GetBlobChain() 1.235 + { 1.236 + // one must be the master 1.237 + aiExportDataBlob* master = NULL, *cur; 1.238 + BOOST_FOREACH(const BlobEntry& blobby, blobs) { 1.239 + if (blobby.first == AI_BLOBIO_MAGIC) { 1.240 + master = blobby.second; 1.241 + break; 1.242 + } 1.243 + } 1.244 + if (!master) { 1.245 + DefaultLogger::get()->error("BlobIOSystem: no data written or master file was not closed properly."); 1.246 + return NULL; 1.247 + } 1.248 + 1.249 + master->name.Set(""); 1.250 + 1.251 + cur = master; 1.252 + BOOST_FOREACH(const BlobEntry& blobby, blobs) { 1.253 + if (blobby.second == master) { 1.254 + continue; 1.255 + } 1.256 + 1.257 + cur->next = blobby.second; 1.258 + cur = cur->next; 1.259 + 1.260 + // extract the file extension from the file written 1.261 + const std::string::size_type s = blobby.first.find_first_of('.'); 1.262 + cur->name.Set(s == std::string::npos ? blobby.first : blobby.first.substr(s+1)); 1.263 + } 1.264 + 1.265 + // give up blob ownership 1.266 + blobs.clear(); 1.267 + return master; 1.268 + } 1.269 + 1.270 +public: 1.271 + 1.272 + // ------------------------------------------------------------------- 1.273 + virtual bool Exists( const char* pFile) const { 1.274 + return created.find(std::string(pFile)) != created.end(); 1.275 + } 1.276 + 1.277 + 1.278 + // ------------------------------------------------------------------- 1.279 + virtual char getOsSeparator() const { 1.280 + return '/'; 1.281 + } 1.282 + 1.283 + 1.284 + // ------------------------------------------------------------------- 1.285 + virtual IOStream* Open(const char* pFile, 1.286 + const char* pMode) 1.287 + { 1.288 + if (pMode[0] != 'w') { 1.289 + return NULL; 1.290 + } 1.291 + 1.292 + created.insert(std::string(pFile)); 1.293 + return new BlobIOStream(this,std::string(pFile)); 1.294 + } 1.295 + 1.296 + // ------------------------------------------------------------------- 1.297 + virtual void Close( IOStream* pFile) 1.298 + { 1.299 + delete pFile; 1.300 + } 1.301 + 1.302 +private: 1.303 + 1.304 + // ------------------------------------------------------------------- 1.305 + void OnDestruct(const std::string& filename, BlobIOStream* child) 1.306 + { 1.307 + // we don't know in which the files are closed, so we 1.308 + // can't reliably say that the first must be the master 1.309 + // file ... 1.310 + blobs.push_back( BlobEntry(filename,child->GetBlob()) ); 1.311 + } 1.312 + 1.313 +private: 1.314 + std::set<std::string> created; 1.315 + std::vector< BlobEntry > blobs; 1.316 +}; 1.317 + 1.318 + 1.319 +// -------------------------------------------------------------------------------------------- 1.320 +BlobIOStream :: ~BlobIOStream() 1.321 +{ 1.322 + creator->OnDestruct(file,this); 1.323 + delete[] buffer; 1.324 +} 1.325 + 1.326 + 1.327 +} // end Assimp 1.328 + 1.329 +#endif