vrshoot
diff libs/assimp/Q3BSPZipArchive.cpp @ 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/Q3BSPZipArchive.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,201 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2008, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +#include "AssimpPCH.h" 1.45 + 1.46 +#ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER 1.47 + 1.48 +#include "Q3BSPZipArchive.h" 1.49 +#include <algorithm> 1.50 +#include <cassert> 1.51 + 1.52 +namespace Assimp 1.53 +{ 1.54 +namespace Q3BSP 1.55 +{ 1.56 + 1.57 +// ------------------------------------------------------------------------------------------------ 1.58 +// Constructor. 1.59 +Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) : 1.60 + m_ZipFileHandle( NULL ), 1.61 + m_FileList(), 1.62 + m_bDirty( true ) 1.63 +{ 1.64 + if ( !rFile.empty() ) 1.65 + { 1.66 + m_ZipFileHandle = unzOpen( rFile.c_str() ); 1.67 + if ( NULL != m_ZipFileHandle ) 1.68 + { 1.69 + mapArchive(); 1.70 + } 1.71 + } 1.72 +} 1.73 + 1.74 +// ------------------------------------------------------------------------------------------------ 1.75 +// Destructor. 1.76 +Q3BSPZipArchive::~Q3BSPZipArchive() 1.77 +{ 1.78 + if ( NULL != m_ZipFileHandle ) 1.79 + { 1.80 + unzClose( m_ZipFileHandle ); 1.81 + } 1.82 + m_ZipFileHandle = NULL; 1.83 + m_FileList.clear(); 1.84 +} 1.85 + 1.86 +// ------------------------------------------------------------------------------------------------ 1.87 +// Returns true, if the archive is already open. 1.88 +bool Q3BSPZipArchive::isOpen() const 1.89 +{ 1.90 + return ( NULL != m_ZipFileHandle ); 1.91 +} 1.92 + 1.93 +// ------------------------------------------------------------------------------------------------ 1.94 +// Returns true, if the filename is part of the archive. 1.95 +bool Q3BSPZipArchive::Exists( const char* pFile ) const 1.96 +{ 1.97 + ai_assert( NULL != pFile ); 1.98 + if ( NULL == pFile ) 1.99 + { 1.100 + return false; 1.101 + } 1.102 + 1.103 + std::string rFile( pFile ); 1.104 + std::vector<std::string>::const_iterator it = std::find( m_FileList.begin(), m_FileList.end(), rFile ); 1.105 + if ( m_FileList.end() == it ) 1.106 + { 1.107 + return false; 1.108 + } 1.109 + 1.110 + return true; 1.111 +} 1.112 + 1.113 +// ------------------------------------------------------------------------------------------------ 1.114 +// Returns the separator delimiter. 1.115 +char Q3BSPZipArchive::getOsSeparator() const 1.116 +{ 1.117 + return '/'; 1.118 +} 1.119 + 1.120 +// ------------------------------------------------------------------------------------------------ 1.121 +// Opens a file, which is part of the archive. 1.122 +IOStream *Q3BSPZipArchive::Open( const char* pFile, const char* /*pMode*/ ) 1.123 +{ 1.124 + ai_assert( NULL != pFile ); 1.125 + 1.126 + std::string rItem( pFile ); 1.127 + std::vector<std::string>::iterator it = std::find( m_FileList.begin(), m_FileList.end(), rItem ); 1.128 + if ( m_FileList.end() == it ) 1.129 + return NULL; 1.130 + 1.131 + ZipFile *pZipFile = new ZipFile( *it, m_ZipFileHandle ); 1.132 + m_ArchiveMap[ rItem ] = pZipFile; 1.133 + 1.134 + return pZipFile; 1.135 +} 1.136 + 1.137 +// ------------------------------------------------------------------------------------------------ 1.138 +// Close a filestream. 1.139 +void Q3BSPZipArchive::Close( IOStream *pFile ) 1.140 +{ 1.141 + ai_assert( NULL != pFile ); 1.142 + 1.143 + std::map<std::string, IOStream*>::iterator it; 1.144 + for ( it = m_ArchiveMap.begin(); it != m_ArchiveMap.end(); ++it ) 1.145 + { 1.146 + if ( (*it).second == pFile ) 1.147 + { 1.148 + ZipFile *pZipFile = reinterpret_cast<ZipFile*>( (*it).second ); 1.149 + delete pZipFile; 1.150 + m_ArchiveMap.erase( it ); 1.151 + break; 1.152 + } 1.153 + } 1.154 +} 1.155 +// ------------------------------------------------------------------------------------------------ 1.156 +// Returns the file-list of the archive. 1.157 +void Q3BSPZipArchive::getFileList( std::vector<std::string> &rFileList ) 1.158 +{ 1.159 + rFileList = m_FileList; 1.160 +} 1.161 + 1.162 +// ------------------------------------------------------------------------------------------------ 1.163 +// Maps the archive content. 1.164 +bool Q3BSPZipArchive::mapArchive() 1.165 +{ 1.166 + if ( NULL == m_ZipFileHandle ) 1.167 + return false; 1.168 + 1.169 + if ( !m_bDirty ) 1.170 + return true; 1.171 + 1.172 + if ( !m_FileList.empty() ) 1.173 + m_FileList.resize( 0 ); 1.174 + 1.175 + // At first ensure file is already open 1.176 + if ( UNZ_OK == unzGoToFirstFile( m_ZipFileHandle ) ) 1.177 + { 1.178 + char filename[ FileNameSize ]; 1.179 + unzGetCurrentFileInfo( m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0 ); 1.180 + m_FileList.push_back( filename ); 1.181 + unzCloseCurrentFile( m_ZipFileHandle ); 1.182 + 1.183 + // Loop over all files 1.184 + while ( unzGoToNextFile( m_ZipFileHandle ) != UNZ_END_OF_LIST_OF_FILE ) 1.185 + { 1.186 + char filename[ FileNameSize ]; 1.187 + unzGetCurrentFileInfo( m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0 ); 1.188 + m_FileList.push_back( filename ); 1.189 + unzCloseCurrentFile( m_ZipFileHandle ); 1.190 + } 1.191 + } 1.192 + 1.193 + std::sort( m_FileList.begin(), m_FileList.end() ); 1.194 + m_bDirty = false; 1.195 + 1.196 + return true; 1.197 +} 1.198 + 1.199 +// ------------------------------------------------------------------------------------------------ 1.200 + 1.201 +} // Namespace Q3BSP 1.202 +} // Namespace Assimp 1.203 + 1.204 +#endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER