vrshoot

view 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 source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2008, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 #include "AssimpPCH.h"
43 #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
45 #include "Q3BSPZipArchive.h"
46 #include <algorithm>
47 #include <cassert>
49 namespace Assimp
50 {
51 namespace Q3BSP
52 {
54 // ------------------------------------------------------------------------------------------------
55 // Constructor.
56 Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) :
57 m_ZipFileHandle( NULL ),
58 m_FileList(),
59 m_bDirty( true )
60 {
61 if ( !rFile.empty() )
62 {
63 m_ZipFileHandle = unzOpen( rFile.c_str() );
64 if ( NULL != m_ZipFileHandle )
65 {
66 mapArchive();
67 }
68 }
69 }
71 // ------------------------------------------------------------------------------------------------
72 // Destructor.
73 Q3BSPZipArchive::~Q3BSPZipArchive()
74 {
75 if ( NULL != m_ZipFileHandle )
76 {
77 unzClose( m_ZipFileHandle );
78 }
79 m_ZipFileHandle = NULL;
80 m_FileList.clear();
81 }
83 // ------------------------------------------------------------------------------------------------
84 // Returns true, if the archive is already open.
85 bool Q3BSPZipArchive::isOpen() const
86 {
87 return ( NULL != m_ZipFileHandle );
88 }
90 // ------------------------------------------------------------------------------------------------
91 // Returns true, if the filename is part of the archive.
92 bool Q3BSPZipArchive::Exists( const char* pFile ) const
93 {
94 ai_assert( NULL != pFile );
95 if ( NULL == pFile )
96 {
97 return false;
98 }
100 std::string rFile( pFile );
101 std::vector<std::string>::const_iterator it = std::find( m_FileList.begin(), m_FileList.end(), rFile );
102 if ( m_FileList.end() == it )
103 {
104 return false;
105 }
107 return true;
108 }
110 // ------------------------------------------------------------------------------------------------
111 // Returns the separator delimiter.
112 char Q3BSPZipArchive::getOsSeparator() const
113 {
114 return '/';
115 }
117 // ------------------------------------------------------------------------------------------------
118 // Opens a file, which is part of the archive.
119 IOStream *Q3BSPZipArchive::Open( const char* pFile, const char* /*pMode*/ )
120 {
121 ai_assert( NULL != pFile );
123 std::string rItem( pFile );
124 std::vector<std::string>::iterator it = std::find( m_FileList.begin(), m_FileList.end(), rItem );
125 if ( m_FileList.end() == it )
126 return NULL;
128 ZipFile *pZipFile = new ZipFile( *it, m_ZipFileHandle );
129 m_ArchiveMap[ rItem ] = pZipFile;
131 return pZipFile;
132 }
134 // ------------------------------------------------------------------------------------------------
135 // Close a filestream.
136 void Q3BSPZipArchive::Close( IOStream *pFile )
137 {
138 ai_assert( NULL != pFile );
140 std::map<std::string, IOStream*>::iterator it;
141 for ( it = m_ArchiveMap.begin(); it != m_ArchiveMap.end(); ++it )
142 {
143 if ( (*it).second == pFile )
144 {
145 ZipFile *pZipFile = reinterpret_cast<ZipFile*>( (*it).second );
146 delete pZipFile;
147 m_ArchiveMap.erase( it );
148 break;
149 }
150 }
151 }
152 // ------------------------------------------------------------------------------------------------
153 // Returns the file-list of the archive.
154 void Q3BSPZipArchive::getFileList( std::vector<std::string> &rFileList )
155 {
156 rFileList = m_FileList;
157 }
159 // ------------------------------------------------------------------------------------------------
160 // Maps the archive content.
161 bool Q3BSPZipArchive::mapArchive()
162 {
163 if ( NULL == m_ZipFileHandle )
164 return false;
166 if ( !m_bDirty )
167 return true;
169 if ( !m_FileList.empty() )
170 m_FileList.resize( 0 );
172 // At first ensure file is already open
173 if ( UNZ_OK == unzGoToFirstFile( m_ZipFileHandle ) )
174 {
175 char filename[ FileNameSize ];
176 unzGetCurrentFileInfo( m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0 );
177 m_FileList.push_back( filename );
178 unzCloseCurrentFile( m_ZipFileHandle );
180 // Loop over all files
181 while ( unzGoToNextFile( m_ZipFileHandle ) != UNZ_END_OF_LIST_OF_FILE )
182 {
183 char filename[ FileNameSize ];
184 unzGetCurrentFileInfo( m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0 );
185 m_FileList.push_back( filename );
186 unzCloseCurrentFile( m_ZipFileHandle );
187 }
188 }
190 std::sort( m_FileList.begin(), m_FileList.end() );
191 m_bDirty = false;
193 return true;
194 }
196 // ------------------------------------------------------------------------------------------------
198 } // Namespace Q3BSP
199 } // Namespace Assimp
201 #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER