Q3BSPZipArchive.cpp 6.0 KB

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