From 8f68a555a67a281cd7a74182cb5b135d96fb226e Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 21 May 2005 00:40:18 +0000 Subject: [PATCH] Load() methods, to allow error checking --- stepmania/src/RageFileDriverZip.cpp | 60 +++++++++++++++++++++-------- stepmania/src/RageFileDriverZip.h | 6 ++- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/stepmania/src/RageFileDriverZip.cpp b/stepmania/src/RageFileDriverZip.cpp index 05b92e0f0d..62a4722e3a 100644 --- a/stepmania/src/RageFileDriverZip.cpp +++ b/stepmania/src/RageFileDriverZip.cpp @@ -30,35 +30,64 @@ struct FileInfo int m_iCompressedSize, m_iUncompressedSize; }; +RageFileDriverZip::RageFileDriverZip(): + RageFileDriver( new NullFilenameDB ), + m_Mutex( "RageFileDriverZip" ) +{ + m_bFileOwned = false; + m_pZip = NULL; +} + RageFileDriverZip::RageFileDriverZip( CString sPath ): RageFileDriver( new NullFilenameDB ), - m_Mutex( ssprintf("RageFileDriverZip(%s)", sPath.c_str()) ) + m_Mutex( "RageFileDriverZip" ) { + m_bFileOwned = false; + m_pZip = NULL; + Load( sPath ); +} + +/* deprecated */ +RageFileDriverZip::RageFileDriverZip( RageFileBasic *pFile ): + RageFileDriver( new NullFilenameDB ), + m_Mutex( "RageFileDriverZip" ) +{ + m_bFileOwned = false; + m_pZip = NULL; + Load( pFile ); +} + +bool RageFileDriverZip::Load( const CString &sPath ) +{ + ASSERT( m_pZip == NULL ); /* don't load twice */ + m_bFileOwned = true; m_sPath = sPath; + m_Mutex.SetName( ssprintf("RageFileDriverZip(%s)", sPath.c_str()) ); RageFile *pFile = new RageFile; - m_pZip = pFile; if( !pFile->Open(sPath) ) { LOG->Warn( "Couldn't open %s: %s", sPath.c_str(), pFile->GetError().c_str() ); - return; + delete pFile; + return false; } - ParseZipfile(); + m_pZip = pFile; + + return ParseZipfile(); } -RageFileDriverZip::RageFileDriverZip( RageFileBasic *pFile ): - RageFileDriver( new NullFilenameDB ), - m_Mutex( ssprintf("RageFileDriverZip(%p)", pFile) ) +bool RageFileDriverZip::Load( RageFileBasic *pFile ) { + ASSERT( m_pZip == NULL ); /* don't load twice */ m_sPath = ssprintf("%p", pFile); + m_Mutex.SetName( ssprintf("RageFileDriverZip(%p)", pFile) ); - m_bFileOwned = false; m_pZip = pFile; - - ParseZipfile(); + + return ParseZipfile(); } @@ -118,18 +147,18 @@ bool RageFileDriverZip::SeekToEndCentralRecord() return false; } -void RageFileDriverZip::ParseZipfile() +bool RageFileDriverZip::ParseZipfile() { if( !SeekToEndCentralRecord() ) { LOG->Warn( "Couldn't open %s: couldn't find end of central directory record", m_sPath.c_str() ); - return; + return false; } /* Read the end of central directory record. */ int iTotalEntries, iCentralDirectoryOffset; if( !ReadEndCentralRecord(iTotalEntries, iCentralDirectoryOffset) ) - return; /* warned already */ + return false; /* warned already */ /* Seek to the start of the central file directory. */ m_pZip->Seek( iCentralDirectoryOffset ); @@ -152,6 +181,8 @@ void RageFileDriverZip::ParseZipfile() if( m_pFiles.size() == 0 ) LOG->Warn( "%s: no files found in central file header", m_sPath.c_str() ); + + return true; } int RageFileDriverZip::ProcessCdirFileHdr( FileInfo &info ) @@ -311,8 +342,7 @@ RageFileBasic *RageFileDriverZip::Open( const CString &sPath, int iMode, int &iE } default: /* unknown compression method */ - ASSERT( 0 ); - iErr = EINVAL; + iErr = ENOSYS; return NULL; } } diff --git a/stepmania/src/RageFileDriverZip.h b/stepmania/src/RageFileDriverZip.h index fc04e13910..dba526e342 100644 --- a/stepmania/src/RageFileDriverZip.h +++ b/stepmania/src/RageFileDriverZip.h @@ -11,8 +11,12 @@ struct end_central_dir_record; class RageFileDriverZip: public RageFileDriver { public: + RageFileDriverZip(); RageFileDriverZip( CString sPath ); RageFileDriverZip( RageFileBasic *pFile ); + bool Load( const CString &sPath ); + bool Load( RageFileBasic *pFile ); + virtual ~RageFileDriverZip(); RageFileBasic *Open( const CString &sPath, int iMode, int &iErr ); @@ -32,7 +36,7 @@ private: * around in it when reading files. */ RageMutex m_Mutex; - void ParseZipfile(); + bool ParseZipfile(); bool ReadEndCentralRecord( int &total_entries_central_dir, int &offset_start_central_directory ); int ProcessCdirFileHdr( FileInfo &info ); bool SeekToEndCentralRecord();