vrshoot

view libs/assimp/Q3BSPZipArchive.h @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +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 */
40 #ifndef AI_Q3BSP_ZIPARCHIVE_H_INC
41 #define AI_Q3BSP_ZIPARCHIVE_H_INC
43 #include "../contrib/unzip/unzip.h"
44 #include "assimp/IOStream.hpp"
45 #include "assimp/IOSystem.hpp"
46 #include <string>
47 #include <vector>
48 #include <map>
49 #include <cassert>
51 namespace Assimp
52 {
53 namespace Q3BSP
54 {
56 // ------------------------------------------------------------------------------------------------
57 /// \class ZipFile
58 /// \ingroup Assimp::Q3BSP
59 ///
60 /// \brief
61 // ------------------------------------------------------------------------------------------------
62 class ZipFile : public IOStream
63 {
64 public:
65 ZipFile( const std::string &rFileName, unzFile zipFile ) :
66 m_Name( rFileName ),
67 m_zipFile( zipFile )
68 {
69 ai_assert( NULL != m_zipFile );
70 }
72 ~ZipFile()
73 {
74 m_zipFile = NULL;
75 }
77 size_t Read(void* pvBuffer, size_t pSize, size_t pCount )
78 {
79 size_t bytes_read = 0;
80 if ( NULL == m_zipFile )
81 return bytes_read;
83 // search file and place file pointer there
84 if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
85 {
86 // get file size, etc.
87 unz_file_info fileInfo;
88 unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
89 const size_t size = pSize * pCount;
90 assert( size <= fileInfo.uncompressed_size );
92 // The file has EXACTLY the size of uncompressed_size. In C
93 // you need to mark the last character with '\0', so add
94 // another character
95 unzOpenCurrentFile( m_zipFile );
96 const int ret = unzReadCurrentFile( m_zipFile, pvBuffer, fileInfo.uncompressed_size);
97 size_t filesize = fileInfo.uncompressed_size;
98 if ( ret < 0 || size_t(ret) != filesize )
99 {
100 return 0;
101 }
102 bytes_read = ret;
103 unzCloseCurrentFile( m_zipFile );
104 }
105 return bytes_read;
106 }
108 size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/)
109 {
110 return 0;
111 }
113 size_t FileSize() const
114 {
115 if ( NULL == m_zipFile )
116 return 0;
117 if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
118 {
119 unz_file_info fileInfo;
120 unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
121 return fileInfo.uncompressed_size;
122 }
123 return 0;
124 }
126 aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/)
127 {
128 return aiReturn_FAILURE;
129 }
131 size_t Tell() const
132 {
133 return 0;
134 }
136 void Flush()
137 {
138 // empty
139 }
141 private:
142 std::string m_Name;
143 unzFile m_zipFile;
144 };
146 // ------------------------------------------------------------------------------------------------
147 /// \class Q3BSPZipArchive
148 /// \ingroup Assimp::Q3BSP
149 ///
150 /// \brief IMplements a zip archive like the WinZip archives. Will be also used to import data
151 /// from a P3K archive ( Quake level format ).
152 // ------------------------------------------------------------------------------------------------
153 class Q3BSPZipArchive : public Assimp::IOSystem
154 {
155 public:
156 static const unsigned int FileNameSize = 256;
158 public:
159 Q3BSPZipArchive( const std::string & rFile );
160 ~Q3BSPZipArchive();
161 bool Exists( const char* pFile) const;
162 char getOsSeparator() const;
163 IOStream* Open(const char* pFile, const char* pMode = "rb");
164 void Close( IOStream* pFile);
165 bool isOpen() const;
166 void getFileList( std::vector<std::string> &rFileList );
168 private:
169 bool mapArchive();
171 private:
172 unzFile m_ZipFileHandle;
173 std::map<std::string, IOStream*> m_ArchiveMap;
174 std::vector<std::string> m_FileList;
175 bool m_bDirty;
176 };
178 // ------------------------------------------------------------------------------------------------
180 } // Namespace Q3BSP
181 } // Namespace Assimp
183 #endif // AI_Q3BSP_ZIPARCHIVE_H_INC