MercuryFiles.h

Go to the documentation of this file.
00001 #ifndef _MERCURY_FILES_H
00002 #define _MERCURY_FILES_H
00003 
00004 #include "global.h"
00005 #include "MercuryVector.h"
00006 #include <map>
00007 
00008 /****************************************************************/
00009 //File Objects:
00010 
00012 enum FilePermission
00013 {
00014     MFP_READ_ONLY,
00015     MFP_WRITE_ONLY,
00016     MFP_READ_AND_WRITE,
00017     MFP_APPEND,
00018 };
00019 
00021 class MercuryFile
00022 {
00023 public:
00024     MercuryFile();
00025     virtual ~MercuryFile();
00027     virtual bool Init( const MString & sPath, FilePermission p );
00029     virtual void Close() = 0;
00031     virtual bool Seek( unsigned long position ) = 0;
00033     virtual unsigned long Tell() = 0;
00035     virtual unsigned long Length() = 0;
00037     virtual bool Write( void * data, unsigned long length ) = 0;
00039     virtual unsigned long Read( void * data, unsigned long length ) = 0;
00041     virtual bool ReadLine( MString & data );
00043     virtual bool Check() = 0;
00045     virtual bool Eof() = 0;
00047     virtual unsigned long GetModTime() { return 0; }
00048 
00049     MString GetName() { return m_sPath; }
00050 protected:
00051     MString m_sPath;
00052     FilePermission m_p;
00053 };
00054 
00056 class MercuryFileObjectDirect: public MercuryFile
00057 {
00058 public:
00059     MercuryFileObjectDirect();
00060     virtual ~MercuryFileObjectDirect();
00061 
00062     virtual bool Init( const MString & fName, FilePermission p );
00063     virtual bool Seek( unsigned long position );
00064     virtual void Close();
00065     virtual unsigned long Tell();
00066     virtual unsigned long Length();
00067     virtual bool Write( void * data, unsigned long length );
00068     virtual unsigned long Read( void * data, unsigned long length );
00069     virtual bool Check();
00070     virtual bool Eof();
00071     virtual unsigned long GetModTime();
00072 private:
00073     FILE * m_fF;
00074     unsigned long length;
00075 };
00076 
00078 class MercuryFileObjectPacked: public MercuryFile
00079 {
00080 public:
00081     virtual ~MercuryFileObjectPacked();
00082     virtual bool Init( const MString & fName, FilePermission p, const MString & base, unsigned offset, unsigned size );
00083     virtual bool Seek( unsigned long position );
00084     virtual void Close();
00085     virtual unsigned long Tell();
00086     virtual unsigned long Length();
00087     virtual bool Write( void * data, unsigned long length );
00088     virtual unsigned long Read( void * data, unsigned long length );
00089     virtual bool Check();
00090     virtual bool Eof();
00091 private:
00092     MercuryFile * m_base;
00093     unsigned long m_offset;
00094     unsigned long m_size;
00095     unsigned long m_location;
00096 };
00097 
00099 class MercuryFileObjectZipped: public MercuryFile
00100 {
00101 public:
00102     virtual ~MercuryFileObjectZipped();
00103     virtual bool Init( const MString & fName, FilePermission p, const MString & base, unsigned offset, unsigned size, unsigned packedsize, unsigned decompID );
00104     virtual bool MemFileInit( const MString & sName, const char * pData, unsigned long iSize );
00105     virtual bool Seek( unsigned long position );
00106     virtual void Close();
00107     virtual unsigned long Tell();
00108     virtual unsigned long Length();
00109     virtual bool Write( void * data, unsigned long length );
00110     virtual unsigned long Read( void * data, unsigned long length );
00111     virtual bool Check();
00112     virtual bool Eof();
00113 private:
00114     unsigned long m_size;
00115     unsigned long m_location;
00116 
00117     bool m_bIsDummy;
00118 
00119     //Used for the file dump buffer as well as the file itself when done
00120     char * m_Buffer;
00121 };
00122 
00123 
00124 class MercuryFileObjectNet: public MercuryFile
00125 {
00126 public:
00127     MercuryFileObjectNet() : m_pSocket(0), m_Buffer(0) { }
00128     virtual ~MercuryFileObjectNet();
00129     virtual bool Init( const MString & fName, FilePermission p );
00130     virtual bool Seek( unsigned long position );
00131     virtual void Close();
00132     virtual unsigned long Tell();
00133     virtual unsigned long Length();
00134     virtual bool Write( void * data, unsigned long length ) { return false; }
00135     virtual unsigned long Read( void * data, unsigned long length );
00136     virtual bool Check();
00137     virtual bool Eof();
00138 private:
00139     unsigned long m_size;
00140     unsigned long m_location;
00141 
00142     void * m_pSocket;
00143 
00144     //Used for the file dump buffer as well as the file itself when done
00145     char * m_Buffer;
00146 };
00147 
00148 
00149 /****************************************************************/
00150 //File Drivers:
00151 
00153 class MercuryFileDriver
00154 {
00155 public:
00156     virtual ~MercuryFileDriver() {};
00157 
00158     virtual void Init() { }
00159     virtual MercuryFile * GetFileHandle( const MString & sPath, FilePermission p  ) = 0;
00160     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly ) = 0;
00161 };
00162 
00164 class MercuryFileDriverNet : public MercuryFileDriver
00165 {
00166 public:
00167     virtual ~MercuryFileDriverNet() {};
00168 
00169     virtual void Init() { }
00170     virtual MercuryFile * GetFileHandle( const MString & sPath, FilePermission p  );
00171     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly );
00172 };
00173 
00175 class MercuryFileDirverDirect : public MercuryFileDriver
00176 {
00177 public:
00178     virtual ~MercuryFileDirverDirect() {};
00179 
00180     virtual MercuryFile * GetFileHandle( const MString & sPath, FilePermission p  );
00181     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly );
00182 };
00183 
00185 class MercuryFileDriverPacked : public MercuryFileDriver
00186 {
00187 public: 
00188     virtual void Init();
00189     virtual ~MercuryFileDriverPacked() { }
00190     virtual MercuryFile * GetFileHandle( const MString & sPath, FilePermission p  );
00191     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly );
00192 private:
00194     struct PckFileEntry
00195     {
00196         bool m_bFolder;
00197         MString m_sPackageName;
00198         unsigned long m_iOffset;
00199         unsigned long m_iSize;
00200     };
00201     std::map< MString, std::map< MString, PckFileEntry > > m_mFileFolders;
00202 };
00203 
00205 class MercuryFileDriverZipped : public MercuryFileDriver
00206 {
00207 public: 
00208     virtual void Init();
00209     virtual ~MercuryFileDriverZipped() {}
00210     virtual MercuryFile * GetFileHandle( const MString & sPath, FilePermission p  );
00211     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly );
00212 private:
00214     struct ZipFileEntry
00215     {
00216         bool m_bFolder;
00217         MString m_sPackageName;
00218         unsigned long m_iOffset;
00219         unsigned long m_iSize;
00220 
00221         unsigned long m_iPackedSize;
00222         unsigned long m_pkID;
00223         unsigned long m_pkID2;
00224     };
00225     std::map< MString, std::map< MString, ZipFileEntry > > m_mFileFolders;
00226 };
00227 
00228 class MercuryFileDriverMem : public MercuryFileDriver
00229 {
00230 public:
00231     virtual ~MercuryFileDriverMem() {};
00232 
00233     virtual void Init();
00234     virtual MercuryFile * GetFileHandle( const MString & sPath, enum FilePermission p );
00235     virtual void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly );
00236     int m_iFileSize;
00237 };
00238 
00239 /****************************************************************/
00240 //File Manager:
00241 
00243 class MercuryFileManager
00244 {
00245 public:
00247     void Init();
00249     inline void CheckInit() { if ( m_bInit ) return; Init(); }
00250 
00252     MercuryFile * Open( const MString & sPath, FilePermission p = MFP_READ_ONLY );
00254     void ListDirectory( const MString & sPath, MVector< MString > & output, bool bDirsOnly=false );
00255 private:
00257     bool m_bInit;
00258     MVector< MercuryFileDriver * > * m_Drivers;
00259 };
00260 
00261 extern MercuryFileManager FILEMAN;
00262 
00263 #endif
00264 
00265 /* 
00266  * Copyright (c) 2005-2006, Charles Lohr
00267  * All rights reserved.
00268  *
00269  * Redistribution and use in source and binary forms, with or
00270  * without modification, are permitted provided that the following
00271  * conditions are met:
00272  *  -   Redistributions of source code must retain the above
00273  *      copyright notice, this list of conditions and the following disclaimer.
00274  *  -   Redistributions in binary form must reproduce the above copyright
00275  *      notice, this list of conditions and the following disclaimer in
00276  *      the documentation and/or other materials provided with the distribution.
00277  *  -   Neither the name of the Mercury Engine nor the names of its
00278  *      contributors may be used to endorse or promote products derived from
00279  *      this software without specific prior written permission.
00280  *
00281  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00282  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00283  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00284  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00285  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00286  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00287  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00288  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00289  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00290  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00291  */

Hosted by SourceForge.net Logo