Q3BSPZipArchive.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. namespace Q3BSP {
  40. ZipFile::ZipFile(const std::string &rFileName, unzFile zipFile) : m_Name(rFileName), m_zipFile(zipFile) {
  41. ai_assert(m_zipFile != NULL);
  42. }
  43. ZipFile::~ZipFile() {
  44. m_zipFile = NULL;
  45. }
  46. size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount ) {
  47. size_t bytes_read = 0;
  48. if(m_zipFile != NULL) {
  49. // search file and place file pointer there
  50. if(unzLocateFile(m_zipFile, m_Name.c_str(), 0) == UNZ_OK) {
  51. // get file size, etc.
  52. unz_file_info fileInfo;
  53. if(unzGetCurrentFileInfo(m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0) == UNZ_OK) {
  54. const size_t size = pSize * pCount;
  55. assert(size <= fileInfo.uncompressed_size);
  56. // The file has EXACTLY the size of uncompressed_size. In C
  57. // you need to mark the last character with '\0', so add
  58. // another character
  59. if(unzOpenCurrentFile(m_zipFile) == UNZ_OK) {
  60. if(unzReadCurrentFile(m_zipFile, pvBuffer, fileInfo.uncompressed_size) == (long int) fileInfo.uncompressed_size) {
  61. if(unzCloseCurrentFile(m_zipFile) == UNZ_OK) {
  62. bytes_read = fileInfo.uncompressed_size;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. return bytes_read;
  70. }
  71. size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
  72. return 0;
  73. }
  74. size_t ZipFile::FileSize() const {
  75. size_t size = 0;
  76. if(m_zipFile != NULL) {
  77. if(unzLocateFile(m_zipFile, m_Name.c_str(), 0) == UNZ_OK) {
  78. unz_file_info fileInfo;
  79. if(unzGetCurrentFileInfo(m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0) == UNZ_OK) {
  80. size = fileInfo.uncompressed_size;
  81. }
  82. }
  83. }
  84. return size;
  85. }
  86. aiReturn ZipFile::Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) {
  87. return aiReturn_FAILURE;
  88. }
  89. size_t ZipFile::Tell() const {
  90. return 0;
  91. }
  92. void ZipFile::Flush() {
  93. // empty
  94. }
  95. // ------------------------------------------------------------------------------------------------
  96. // Constructor.
  97. Q3BSPZipArchive::Q3BSPZipArchive(const std::string& rFile) : m_ZipFileHandle(NULL), m_ArchiveMap(), m_FileList(), m_bDirty(true) {
  98. if (! rFile.empty()) {
  99. m_ZipFileHandle = unzOpen(rFile.c_str());
  100. if(m_ZipFileHandle != NULL) {
  101. mapArchive();
  102. }
  103. }
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. // Destructor.
  107. Q3BSPZipArchive::~Q3BSPZipArchive() {
  108. for( std::map<IOStream*, std::string>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it ) {
  109. ZipFile *pZipFile = reinterpret_cast<ZipFile*>(it->first);
  110. delete pZipFile;
  111. }
  112. m_ArchiveMap.clear();
  113. m_FileList.clear();
  114. if(m_ZipFileHandle != NULL) {
  115. unzClose(m_ZipFileHandle);
  116. m_ZipFileHandle = NULL;
  117. }
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. // Returns true, if the archive is already open.
  121. bool Q3BSPZipArchive::isOpen() const {
  122. return (m_ZipFileHandle != NULL);
  123. }
  124. // ------------------------------------------------------------------------------------------------
  125. // Returns true, if the filename is part of the archive.
  126. bool Q3BSPZipArchive::Exists(const char* pFile) const {
  127. ai_assert(pFile != NULL);
  128. bool exist = false;
  129. if (pFile != NULL) {
  130. std::string rFile(pFile);
  131. std::set<std::string>::const_iterator it = m_FileList.find(rFile);
  132. if(it != m_FileList.end()) {
  133. exist = true;
  134. }
  135. }
  136. return exist;
  137. }
  138. // ------------------------------------------------------------------------------------------------
  139. // Returns the separator delimiter.
  140. char Q3BSPZipArchive::getOsSeparator() const {
  141. #ifndef _WIN32
  142. return '/';
  143. #else
  144. return '\\';
  145. #endif
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. // Opens a file, which is part of the archive.
  149. IOStream *Q3BSPZipArchive::Open(const char* pFile, const char* /*pMode*/) {
  150. ai_assert(pFile != NULL);
  151. IOStream* result = NULL;
  152. std::string rItem(pFile);
  153. std::set<std::string>::iterator it = m_FileList.find(rItem);
  154. if(it != m_FileList.end()) {
  155. ZipFile *pZipFile = new ZipFile(*it, m_ZipFileHandle);
  156. m_ArchiveMap[pZipFile] = rItem;
  157. result = pZipFile;
  158. }
  159. return result;
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. // Close a filestream.
  163. void Q3BSPZipArchive::Close(IOStream *pFile) {
  164. ai_assert(pFile != NULL);
  165. std::map<IOStream*, std::string>::iterator it = m_ArchiveMap.find(pFile);
  166. if(it != m_ArchiveMap.end()) {
  167. ZipFile *pZipFile = reinterpret_cast<ZipFile*>((*it).first);
  168. delete pZipFile;
  169. m_ArchiveMap.erase(it);
  170. }
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Returns the file-list of the archive.
  174. void Q3BSPZipArchive::getFileList(std::vector<std::string> &rFileList) {
  175. rFileList.clear();
  176. std::copy(m_FileList.begin(), m_FileList.end(), rFileList.begin());
  177. }
  178. // ------------------------------------------------------------------------------------------------
  179. // Maps the archive content.
  180. bool Q3BSPZipArchive::mapArchive() {
  181. bool success = false;
  182. if(m_ZipFileHandle != NULL) {
  183. if(m_bDirty) {
  184. m_FileList.clear();
  185. // At first ensure file is already open
  186. if(unzGoToFirstFile(m_ZipFileHandle) == UNZ_OK) {
  187. // Loop over all files
  188. do {
  189. char filename[FileNameSize];
  190. if(unzGetCurrentFileInfo(m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0) == UNZ_OK) {
  191. m_FileList.insert(filename);
  192. }
  193. } while(unzGoToNextFile(m_ZipFileHandle) != UNZ_END_OF_LIST_OF_FILE);
  194. }
  195. m_bDirty = false;
  196. }
  197. success = true;
  198. }
  199. return success;
  200. }
  201. // ------------------------------------------------------------------------------------------------
  202. } // Namespace Q3BSP
  203. } // Namespace Assimp
  204. #endif // ASSIMP_BUILD_NO_Q3BSP_IMPORTER