vrshoot

diff libs/assimp/Q3BSPZipArchive.h @ 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.h	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,183 @@
     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 +#ifndef AI_Q3BSP_ZIPARCHIVE_H_INC
    1.44 +#define AI_Q3BSP_ZIPARCHIVE_H_INC
    1.45 +
    1.46 +#include "../contrib/unzip/unzip.h"
    1.47 +#include "assimp/IOStream.hpp"
    1.48 +#include "assimp/IOSystem.hpp"
    1.49 +#include <string>
    1.50 +#include <vector>
    1.51 +#include <map>
    1.52 +#include <cassert>
    1.53 +
    1.54 +namespace Assimp
    1.55 +{
    1.56 +namespace Q3BSP
    1.57 +{
    1.58 +
    1.59 +// ------------------------------------------------------------------------------------------------
    1.60 +///	\class		ZipFile
    1.61 +///	\ingroup	Assimp::Q3BSP
    1.62 +///
    1.63 +///	\brief
    1.64 +// ------------------------------------------------------------------------------------------------
    1.65 +class ZipFile : public IOStream
    1.66 +{
    1.67 +public:
    1.68 +	ZipFile( const std::string &rFileName, unzFile zipFile ) :
    1.69 +		m_Name( rFileName ),
    1.70 +		m_zipFile( zipFile )
    1.71 +	{
    1.72 +		ai_assert( NULL != m_zipFile );
    1.73 +	}
    1.74 +	
    1.75 +	~ZipFile()
    1.76 +	{
    1.77 +		m_zipFile = NULL;
    1.78 +	}
    1.79 +
    1.80 +	size_t Read(void* pvBuffer, size_t pSize, size_t pCount )
    1.81 +	{
    1.82 +		size_t bytes_read = 0;
    1.83 +		if ( NULL == m_zipFile )
    1.84 +			return bytes_read;
    1.85 +		
    1.86 +		// search file and place file pointer there
    1.87 +		if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
    1.88 +		{
    1.89 +			// get file size, etc.
    1.90 +			unz_file_info fileInfo;
    1.91 +			unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
    1.92 +			const size_t size = pSize * pCount;
    1.93 +			assert( size <= fileInfo.uncompressed_size );
    1.94 +			
    1.95 +			// The file has EXACTLY the size of uncompressed_size. In C
    1.96 +			// you need to mark the last character with '\0', so add 
    1.97 +			// another character
    1.98 +			unzOpenCurrentFile( m_zipFile );
    1.99 +			const int ret = unzReadCurrentFile( m_zipFile, pvBuffer, fileInfo.uncompressed_size);
   1.100 +			size_t filesize = fileInfo.uncompressed_size;
   1.101 +			if ( ret < 0 || size_t(ret) != filesize )
   1.102 +			{
   1.103 +				return 0;
   1.104 +			}
   1.105 +			bytes_read = ret;
   1.106 +			unzCloseCurrentFile( m_zipFile );
   1.107 +		}
   1.108 +		return bytes_read;
   1.109 +	}
   1.110 +
   1.111 +	size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/)
   1.112 +	{
   1.113 +		return 0;
   1.114 +	}
   1.115 +
   1.116 +	size_t FileSize() const
   1.117 +	{
   1.118 +		if ( NULL == m_zipFile )
   1.119 +			return 0;
   1.120 +		if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK ) 
   1.121 +		{
   1.122 +			unz_file_info fileInfo;
   1.123 +			unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
   1.124 +			return fileInfo.uncompressed_size;
   1.125 +		}
   1.126 +		return 0;
   1.127 +	}
   1.128 +
   1.129 +	aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/)
   1.130 +	{
   1.131 +		return aiReturn_FAILURE;
   1.132 +	}
   1.133 +
   1.134 +    size_t Tell() const
   1.135 +	{
   1.136 +		return 0;
   1.137 +	}
   1.138 +
   1.139 +	void Flush()
   1.140 +	{
   1.141 +		// empty
   1.142 +	}
   1.143 +
   1.144 +private:
   1.145 +	std::string m_Name;
   1.146 +	unzFile m_zipFile;
   1.147 +};
   1.148 +
   1.149 +// ------------------------------------------------------------------------------------------------
   1.150 +///	\class		Q3BSPZipArchive
   1.151 +///	\ingroup	Assimp::Q3BSP
   1.152 +///	
   1.153 +///	\brief	IMplements a zip archive like the WinZip archives. Will be also used to import data 
   1.154 +///	from a P3K archive ( Quake level format ).
   1.155 +// ------------------------------------------------------------------------------------------------
   1.156 +class Q3BSPZipArchive : public Assimp::IOSystem
   1.157 +{
   1.158 +public:
   1.159 +	static const unsigned int FileNameSize = 256;
   1.160 +
   1.161 +public:
   1.162 +	Q3BSPZipArchive( const std::string & rFile );
   1.163 +	~Q3BSPZipArchive();
   1.164 +	bool Exists( const char* pFile) const;
   1.165 +	char getOsSeparator() const;
   1.166 +	IOStream* Open(const char* pFile, const char* pMode = "rb");
   1.167 +	void Close( IOStream* pFile);
   1.168 +	bool isOpen() const;
   1.169 +	void getFileList( std::vector<std::string> &rFileList );
   1.170 +
   1.171 +private:
   1.172 +	bool mapArchive();
   1.173 +
   1.174 +private:
   1.175 +	unzFile m_ZipFileHandle;
   1.176 +	std::map<std::string, IOStream*> m_ArchiveMap;
   1.177 +	std::vector<std::string> m_FileList;
   1.178 +	bool m_bDirty;
   1.179 +};
   1.180 +
   1.181 +// ------------------------------------------------------------------------------------------------
   1.182 +
   1.183 +} // Namespace Q3BSP
   1.184 +} // Namespace Assimp
   1.185 +
   1.186 +#endif // AI_Q3BSP_ZIPARCHIVE_H_INC