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
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
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
00145 char * m_Buffer;
00146 };
00147
00148
00149
00150
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
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
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291