Fixed bugs in Q3BSPZipArchive & DefaultLogger

pull/261/head
Léo Terziman 2013-11-08 18:07:08 +01:00
parent 053df6907a
commit 51bf836db4
3 changed files with 221 additions and 192 deletions

View File

@ -253,7 +253,7 @@ void DefaultLogger::OnDebug( const char* message )
if ( m_Severity == Logger::NORMAL ) if ( m_Severity == Logger::NORMAL )
return; return;
char msg[MAX_LOG_MESSAGE_LENGTH*2]; char msg[MAX_LOG_MESSAGE_LENGTH + 16];
::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message ); ::sprintf(msg,"Debug, T%i: %s", GetThreadID(), message );
WriteToStreams( msg, Logger::Debugging ); WriteToStreams( msg, Logger::Debugging );
@ -263,7 +263,7 @@ void DefaultLogger::OnDebug( const char* message )
// Logs an info // Logs an info
void DefaultLogger::OnInfo( const char* message ) void DefaultLogger::OnInfo( const char* message )
{ {
char msg[MAX_LOG_MESSAGE_LENGTH*2]; char msg[MAX_LOG_MESSAGE_LENGTH + 16];
::sprintf(msg,"Info, T%i: %s", GetThreadID(), message ); ::sprintf(msg,"Info, T%i: %s", GetThreadID(), message );
WriteToStreams( msg , Logger::Info ); WriteToStreams( msg , Logger::Info );
@ -273,7 +273,7 @@ void DefaultLogger::OnInfo( const char* message )
// Logs a warning // Logs a warning
void DefaultLogger::OnWarn( const char* message ) void DefaultLogger::OnWarn( const char* message )
{ {
char msg[MAX_LOG_MESSAGE_LENGTH*2]; char msg[MAX_LOG_MESSAGE_LENGTH + 16];
::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message ); ::sprintf(msg,"Warn, T%i: %s", GetThreadID(), message );
WriteToStreams( msg, Logger::Warn ); WriteToStreams( msg, Logger::Warn );
@ -283,7 +283,7 @@ void DefaultLogger::OnWarn( const char* message )
// Logs an error // Logs an error
void DefaultLogger::OnError( const char* message ) void DefaultLogger::OnError( const char* message )
{ {
char msg[MAX_LOG_MESSAGE_LENGTH*2]; char msg[MAX_LOG_MESSAGE_LENGTH + 16];
::sprintf(msg,"Error, T%i: %s", GetThreadID(), message ); ::sprintf(msg,"Error, T%i: %s", GetThreadID(), message );
WriteToStreams( msg, Logger::Err ); WriteToStreams( msg, Logger::Err );

View File

@ -46,23 +46,100 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
namespace Assimp namespace Assimp {
{ namespace Q3BSP {
namespace Q3BSP
{ ZipFile::ZipFile(const std::string &rFileName, unzFile zipFile) : m_Name(rFileName), m_zipFile(zipFile) {
ai_assert(m_zipFile != NULL);
}
ZipFile::~ZipFile() {
m_zipFile = NULL;
}
size_t ZipFile::Read(void* pvBuffer, size_t pSize, size_t pCount ) {
size_t bytes_read = 0;
DefaultLogger::get()->warn("file: \"" + m_Name + "\".");
if(m_zipFile != NULL) {
DefaultLogger::get()->warn("file: zip exist.");
// search file and place file pointer there
if(unzLocateFile(m_zipFile, m_Name.c_str(), 0) == UNZ_OK) {
DefaultLogger::get()->warn("file: file located in the zip archive.");
// get file size, etc.
unz_file_info fileInfo;
if(unzGetCurrentFileInfo(m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0) == UNZ_OK) {
const size_t size = pSize * pCount;
assert(size <= fileInfo.uncompressed_size);
std::stringstream size_str;
size_str << fileInfo.uncompressed_size;
DefaultLogger::get()->warn("file: size = " + size_str.str() + ".");
// The file has EXACTLY the size of uncompressed_size. In C
// you need to mark the last character with '\0', so add
// another character
if(unzOpenCurrentFile(m_zipFile) == UNZ_OK) {
DefaultLogger::get()->warn("file: file opened.");
if(unzReadCurrentFile(m_zipFile, pvBuffer, fileInfo.uncompressed_size) == (long int) fileInfo.uncompressed_size) {
std::string file((char*) pvBuffer, ((char*) pvBuffer) + (fileInfo.uncompressed_size < 1000 ? fileInfo.uncompressed_size : 1000));
DefaultLogger::get()->warn("file: data = \"" + file + "\".");
if(unzCloseCurrentFile(m_zipFile) == UNZ_OK) {
DefaultLogger::get()->warn("file: file closed.");
bytes_read = fileInfo.uncompressed_size;
}
}
}
}
}
}
return bytes_read;
}
size_t ZipFile::Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) {
return 0;
}
size_t ZipFile::FileSize() const {
size_t size = 0;
if(m_zipFile != NULL) {
if(unzLocateFile(m_zipFile, m_Name.c_str(), 0) == UNZ_OK) {
unz_file_info fileInfo;
if(unzGetCurrentFileInfo(m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0) == UNZ_OK) {
size = fileInfo.uncompressed_size;
}
}
}
return size;
}
aiReturn ZipFile::Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) {
return aiReturn_FAILURE;
}
size_t ZipFile::Tell() const {
return 0;
}
void ZipFile::Flush() {
// empty
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Constructor. // Constructor.
Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) : Q3BSPZipArchive::Q3BSPZipArchive(const std::string& rFile) : m_ZipFileHandle(NULL), m_ArchiveMap(), m_FileList(), m_bDirty(true) {
m_ZipFileHandle( NULL ), if (! rFile.empty()) {
m_FileList(), m_ZipFileHandle = unzOpen(rFile.c_str());
m_bDirty( true ) if(m_ZipFileHandle != NULL) {
{
if ( !rFile.empty() )
{
m_ZipFileHandle = unzOpen( rFile.c_str() );
if ( NULL != m_ZipFileHandle )
{
mapArchive(); mapArchive();
} }
} }
@ -70,129 +147,126 @@ Q3BSPZipArchive::Q3BSPZipArchive( const std::string& rFile ) :
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Destructor. // Destructor.
Q3BSPZipArchive::~Q3BSPZipArchive() Q3BSPZipArchive::~Q3BSPZipArchive() {
{ for( std::map<IOStream*, std::string>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it ) {
for( std::map<std::string, IOStream*>::iterator it(m_ArchiveMap.begin()), end(m_ArchiveMap.end()); it != end; ++it ) ZipFile *pZipFile = reinterpret_cast<ZipFile*>(it->first);
{
ZipFile *pZipFile = reinterpret_cast<ZipFile*>(it->second);
delete pZipFile; delete pZipFile;
} }
m_ArchiveMap.clear(); m_ArchiveMap.clear();
m_FileList.clear(); m_FileList.clear();
if ( NULL != m_ZipFileHandle ) if(m_ZipFileHandle != NULL) {
{ unzClose(m_ZipFileHandle);
unzClose( m_ZipFileHandle ); m_ZipFileHandle = NULL;
} }
m_ZipFileHandle = NULL;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns true, if the archive is already open. // Returns true, if the archive is already open.
bool Q3BSPZipArchive::isOpen() const bool Q3BSPZipArchive::isOpen() const {
{ return (m_ZipFileHandle != NULL);
return ( NULL != m_ZipFileHandle );
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns true, if the filename is part of the archive. // Returns true, if the filename is part of the archive.
bool Q3BSPZipArchive::Exists( const char* pFile ) const bool Q3BSPZipArchive::Exists(const char* pFile) const {
{ ai_assert(pFile != NULL);
ai_assert( NULL != pFile );
if ( NULL == pFile ) bool exist = false;
{
return false; if (pFile != NULL) {
std::string rFile(pFile);
std::set<std::string>::const_iterator it = m_FileList.find(rFile);
if(it != m_FileList.end()) {
exist = true;
}
} }
std::string rFile( pFile ); return exist;
std::vector<std::string>::const_iterator it = std::find( m_FileList.begin(), m_FileList.end(), rFile );
if ( m_FileList.end() == it )
{
return false;
}
return true;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns the separator delimiter. // Returns the separator delimiter.
char Q3BSPZipArchive::getOsSeparator() const char Q3BSPZipArchive::getOsSeparator() const {
{ #ifndef _WIN32
return '/'; return '/';
#else
return '\\';
#endif
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Opens a file, which is part of the archive. // Opens a file, which is part of the archive.
IOStream *Q3BSPZipArchive::Open( const char* pFile, const char* /*pMode*/ ) IOStream *Q3BSPZipArchive::Open(const char* pFile, const char* /*pMode*/) {
{ ai_assert(pFile != NULL);
ai_assert( NULL != pFile );
std::string rItem( pFile ); IOStream* result = NULL;
std::vector<std::string>::iterator it = std::find( m_FileList.begin(), m_FileList.end(), rItem );
if ( m_FileList.end() == it )
return NULL;
ZipFile *pZipFile = new ZipFile( *it, m_ZipFileHandle ); std::string rItem(pFile);
m_ArchiveMap[ rItem ] = pZipFile; std::set<std::string>::iterator it = m_FileList.find(rItem);
return pZipFile; if(it != m_FileList.end()) {
ZipFile *pZipFile = new ZipFile(*it, m_ZipFileHandle);
m_ArchiveMap[pZipFile] = rItem;
result = pZipFile;
}
return result;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Close a filestream. // Close a filestream.
void Q3BSPZipArchive::Close( IOStream *pFile ) void Q3BSPZipArchive::Close(IOStream *pFile) {
{ ai_assert(pFile != NULL);
ai_assert( NULL != pFile );
std::map<std::string, IOStream*>::iterator it; std::map<IOStream*, std::string>::iterator it = m_ArchiveMap.find(pFile);
for ( it = m_ArchiveMap.begin(); it != m_ArchiveMap.end(); ++it )
{ if(it != m_ArchiveMap.end()) {
if ( (*it).second == pFile ) ZipFile *pZipFile = reinterpret_cast<ZipFile*>((*it).first);
{ delete pZipFile;
ZipFile *pZipFile = reinterpret_cast<ZipFile*>( (*it).second );
delete pZipFile; m_ArchiveMap.erase(it);
m_ArchiveMap.erase( it );
break;
}
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns the file-list of the archive. // Returns the file-list of the archive.
void Q3BSPZipArchive::getFileList( std::vector<std::string> &rFileList ) void Q3BSPZipArchive::getFileList(std::vector<std::string> &rFileList) {
{ rFileList.clear();
rFileList = m_FileList;
std::copy(m_FileList.begin(), m_FileList.end(), rFileList.begin());
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Maps the archive content. // Maps the archive content.
bool Q3BSPZipArchive::mapArchive() bool Q3BSPZipArchive::mapArchive() {
{ bool success = false;
if ( NULL == m_ZipFileHandle )
return false;
if ( !m_bDirty ) if(m_ZipFileHandle != NULL) {
return true; if(m_bDirty) {
m_FileList.clear();
if ( !m_FileList.empty() ) // At first ensure file is already open
m_FileList.resize( 0 ); if(unzGoToFirstFile(m_ZipFileHandle) == UNZ_OK) {
// Loop over all files
do {
char filename[FileNameSize];
// At first ensure file is already open if(unzGetCurrentFileInfo(m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0) == UNZ_OK) {
if ( UNZ_OK == unzGoToFirstFile( m_ZipFileHandle ) ) m_FileList.insert(filename);
{ }
// Loop over all files } while(unzGoToNextFile(m_ZipFileHandle) != UNZ_END_OF_LIST_OF_FILE);
do { }
char filename[ FileNameSize ];
unzGetCurrentFileInfo( m_ZipFileHandle, NULL, filename, FileNameSize, NULL, 0, NULL, 0 );
m_FileList.push_back( filename );
unzCloseCurrentFile( m_ZipFileHandle );
} while ( unzGoToNextFile( m_ZipFileHandle ) != UNZ_END_OF_LIST_OF_FILE );
}
std::sort( m_FileList.begin(), m_FileList.end() ); m_bDirty = false;
m_bDirty = false; }
return true; success = true;
}
return success;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -48,10 +48,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <map> #include <map>
#include <cassert> #include <cassert>
namespace Assimp namespace Assimp {
{ namespace Q3BSP {
namespace Q3BSP
{
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
/// \class ZipFile /// \class ZipFile
@ -59,88 +57,31 @@ namespace Q3BSP
/// ///
/// \brief /// \brief
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
class ZipFile : public IOStream class ZipFile : public IOStream {
{
public: public:
ZipFile( const std::string &rFileName, unzFile zipFile ) :
m_Name( rFileName ), ZipFile(const std::string &rFileName, unzFile zipFile);
m_zipFile( zipFile )
{
ai_assert( NULL != m_zipFile );
}
~ZipFile() ~ZipFile();
{
m_zipFile = NULL;
}
size_t Read(void* pvBuffer, size_t pSize, size_t pCount ) size_t Read(void* pvBuffer, size_t pSize, size_t pCount );
{
size_t bytes_read = 0;
if ( NULL == m_zipFile )
return bytes_read;
// search file and place file pointer there
if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
{
// get file size, etc.
unz_file_info fileInfo;
unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
const size_t size = pSize * pCount;
assert( size <= fileInfo.uncompressed_size );
// The file has EXACTLY the size of uncompressed_size. In C
// you need to mark the last character with '\0', so add
// another character
unzOpenCurrentFile( m_zipFile );
const int ret = unzReadCurrentFile( m_zipFile, pvBuffer, fileInfo.uncompressed_size);
size_t filesize = fileInfo.uncompressed_size;
if ( ret < 0 || size_t(ret) != filesize )
{
return 0;
}
bytes_read = ret;
unzCloseCurrentFile( m_zipFile );
}
return bytes_read;
}
size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) size_t Write(const void* /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/);
{
return 0;
}
size_t FileSize() const size_t FileSize() const;
{
if ( NULL == m_zipFile )
return 0;
if ( unzLocateFile( m_zipFile, m_Name.c_str(), 0 ) == UNZ_OK )
{
unz_file_info fileInfo;
unzGetCurrentFileInfo( m_zipFile, &fileInfo, 0, 0, 0, 0, 0, 0 );
return fileInfo.uncompressed_size;
}
return 0;
}
aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/);
{
return aiReturn_FAILURE;
}
size_t Tell() const size_t Tell() const;
{
return 0;
}
void Flush() void Flush();
{
// empty
}
private: private:
std::string m_Name;
unzFile m_zipFile; std::string m_Name;
unzFile m_zipFile;
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -150,29 +91,43 @@ private:
/// \brief IMplements a zip archive like the WinZip archives. Will be also used to import data /// \brief IMplements a zip archive like the WinZip archives. Will be also used to import data
/// from a P3K archive ( Quake level format ). /// from a P3K archive ( Quake level format ).
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
class Q3BSPZipArchive : public Assimp::IOSystem class Q3BSPZipArchive : public Assimp::IOSystem {
{
public:
static const unsigned int FileNameSize = 256;
public: public:
Q3BSPZipArchive( const std::string & rFile );
~Q3BSPZipArchive();
bool Exists( const char* pFile) const;
char getOsSeparator() const;
IOStream* Open(const char* pFile, const char* pMode = "rb");
void Close( IOStream* pFile);
bool isOpen() const;
void getFileList( std::vector<std::string> &rFileList );
private: static const unsigned int FileNameSize = 256;
bool mapArchive();
private: public:
unzFile m_ZipFileHandle;
std::map<std::string, IOStream*> m_ArchiveMap; Q3BSPZipArchive(const std::string & rFile);
std::vector<std::string> m_FileList;
bool m_bDirty; ~Q3BSPZipArchive();
bool Exists(const char* pFile) const;
char getOsSeparator() const;
IOStream* Open(const char* pFile, const char* pMode = "rb");
void Close(IOStream* pFile);
bool isOpen() const;
void getFileList(std::vector<std::string> &rFileList);
private:
bool mapArchive();
private:
unzFile m_ZipFileHandle;
std::map<IOStream*, std::string> m_ArchiveMap;
std::set<std::string> m_FileList;
bool m_bDirty;
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------