|
From: Charles L. <cn...@us...> - 2005-12-18 06:52:50
|
Update of /cvsroot/hgengine/Mercury/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1269 Modified Files: Mercury.dsp Added Files: MercuryFiles.cpp MercuryFiles.h Log Message: Mercury File System (commit now) --- NEW FILE: MercuryFiles.cpp --- #include "MercuryFiles.h" MercuryFileManager FILEMAN; //Core base only. MercuryFileObject::MercuryFileObject() { //No code } MercuryFileObject::~MercuryFileObject() { //No code } bool MercuryFileObject::Init( const CString &sPath, FilePermission p ) { m_sPath = sPath; m_p = p; return true; } MercuryFileObjectDirect::MercuryFileObjectDirect( ): MercuryFileObject( ) { m_fF = NULL; } MercuryFileObjectDirect::~MercuryFileObjectDirect() { if ( m_fF != NULL ) { fclose ( m_fF ); m_fF = NULL; } } void MercuryFileObjectDirect::Close() { if ( m_fF != NULL ) { fclose ( m_fF ); m_fF = NULL; } } bool MercuryFileObjectDirect::Init( const CString & fName, FilePermission p ) { MercuryFileObject::Init( fName, p ); if ( m_fF != NULL ) SAFE_DELETE( m_fF ); switch ( p ) { case FILE_READ_ONLY: m_fF = fopen( fName.c_str(), "r" ); break; case FILE_WRITE_ONLY: m_fF = fopen( fName.c_str(), "w" ); break; case FILE_READ_AND_WRITE: m_fF = fopen( fName.c_str(), "w+" ); break; case FILE_APPEND: m_fF = fopen( fName.c_str(), "a" ); break; default: m_fF = NULL; break; } return ( m_fF != NULL ); } bool MercuryFileObjectDirect::Seek( unsigned long position ) { if ( m_fF == NULL ) return false; return ( fseek( m_fF, position, SEEK_SET ) == 0 ); } unsigned long MercuryFileObjectDirect::Tell() { if ( m_fF == NULL ) return false; return ftell( m_fF ); } unsigned long MercuryFileObjectDirect::Length() { if ( m_fF == NULL ) return false; unsigned long prev = ftell( m_fF ); fseek( m_fF, 0, SEEK_END ); unsigned long ret = ftell( m_fF ); fseek( m_fF, 0, SEEK_SET ); return ret; } bool MercuryFileObjectDirect::Write( void * data, unsigned long length ) { if ( m_fF == NULL ) return false; return ( fwrite( data, length, 1, m_fF ) > 0 ); } unsigned long MercuryFileObjectDirect::Read( void * data, unsigned long length ) { if ( m_fF == NULL ) return false; return ( fread( data, 1, length, m_fF) ); } bool MercuryFileObjectDirect::Check() { if ( m_fF == NULL ) return false; return (ferror( m_fF )!=0); } bool MercuryFileObjectDirect::Eof() { if ( m_fF == NULL ) return false; return (feof( m_fF )!=0); } MercuryFileObject * MercuryFileDirverDirect::GetFileHandle( const CString & sPath, FilePermission p ) { MercuryFileObject * ret = new MercuryFileObjectDirect; if ( ret->Init( sPath, p ) ) return ret; SAFE_DELETE(ret); return NULL; } #if defined(WIN32) #include <windows.h> #endif void MercuryFileDirverDirect::ListDirectory( const CString & sPath, std::vector< CString > & output, bool bDirsOnly ) { #if defined(WIN32) WIN32_FIND_DATA fd; HANDLE hFind = ::FindFirstFile( sPath, &fd ); if( INVALID_HANDLE_VALUE == hFind ) // no files found return; do { if( bDirsOnly && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) continue; // skip if( (!bDirsOnly) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) continue; // skip CString sDirName( fd.cFileName ); if( sDirName == "." || sDirName == ".." ) continue; output.push_back( sDirName ); } while( ::FindNextFile( hFind, &fd ) ); ::FindClose( hFind ); #else DIR * pDe = opendir( "./" + sPath ); if ( pDe == NULL ) return; dirent * pEnt; while ( pEnt = readdir( de ) ) { if ( ( pEnt->d_type & DT_DIR ) && !bDirsOnly ) continue; if ( !( pEnt->d_type & DT_DIR ) && bDirsOnly ) continue; if ( strcmp( pEnt->d_name, "." ) == 0 ) continue; if ( strcmp( pEnt->d_name, ".." ) == 0 ) continue; output.push_back( pEnt->d_name ); } closedir( pDe ); #endif } void MercuryFileManager::Init() { m_bInit = true; m_Drivers = new std::vector< MercuryFileDriver * >; m_Drivers->push_back( new MercuryFileDirverDirect ); } MercuryFileObject * MercuryFileManager::Open( const CString & sPath, FilePermission p ) { CheckInit(); MercuryFileObject * ret; for ( unsigned int i = 0; i < m_Drivers->size(); i++ ) { ret = m_Drivers->at(i)->GetFileHandle( sPath, p ); if ( ret != NULL ) return ret; } return NULL; } void MercuryFileManager::ListDirectory( const CString & sPath, std::vector< CString > & output, bool bDirsOnly ) { CheckInit(); for ( unsigned int i = 0; i < m_Drivers->size(); i++ ) { m_Drivers->at(i)->ListDirectory( sPath, output, bDirsOnly ); } } --- NEW FILE: MercuryFiles.h --- #ifndef _MERCURY_FILES_H #define _MERCURY_FILES_H #include "global.h" #include "StdString.h" #include <vector> /****************************************************************/ //File Objects: enum FilePermission { FILE_READ_ONLY, FILE_WRITE_ONLY, FILE_READ_AND_WRITE, FILE_APPEND, }; class MercuryFileObject { public: MercuryFileObject(); ~MercuryFileObject(); virtual bool Init( const CString & sPath, FilePermission p ); virtual void Close() = 0; virtual bool Seek( unsigned long position ) = 0; virtual unsigned long Tell() = 0; virtual unsigned long Length() = 0; /** Writes length bytes, returns true if it wrote successfully */ virtual bool Write( void * data, unsigned long length ) = 0; /** Returns the number of bytes read */ virtual unsigned long Read( void * data, unsigned long length ) = 0; /** Returns non-zero value if there's an error */ virtual bool Check() = 0; /** Return true if end of file */ virtual bool Eof() = 0; CString GetName() { return m_sPath; } protected: CString m_sPath; FilePermission m_p; }; class MercuryFileObjectDirect: public MercuryFileObject { public: MercuryFileObjectDirect(); ~MercuryFileObjectDirect(); virtual bool Init( const CString & fName, FilePermission p ); virtual bool Seek( unsigned long position ); virtual void Close(); virtual unsigned long Tell(); virtual unsigned long Length(); virtual bool Write( void * data, unsigned long length ); virtual unsigned long Read( void * data, unsigned long length ); virtual bool Check(); virtual bool Eof(); private: FILE * m_fF; }; /****************************************************************/ //File Drivers: class MercuryFileDriver { public: virtual void Init() { } virtual MercuryFileObject * GetFileHandle( const CString & sPath, FilePermission p ) = 0; virtual void ListDirectory( const CString & sPath, std::vector< CString > & output, bool bDirsOnly ) = 0; }; class MercuryFileDirverDirect : public MercuryFileDriver { public: virtual MercuryFileObject * GetFileHandle( const CString & sPath, FilePermission p ); virtual void ListDirectory( const CString & sPath, std::vector< CString > & output, bool bDirsOnly ); }; /****************************************************************/ //File Manager: class MercuryFileManager { public: void Init(); inline void CheckInit() { if ( m_bInit ) return; Init(); } MercuryFileObject * Open( const CString & sPath, FilePermission p = FILE_READ_ONLY ); void ListDirectory( const CString & sPath, std::vector< CString > & output, bool bDirsOnly=false ); private: bool m_bInit; std::vector< MercuryFileDriver * > * m_Drivers; }; extern MercuryFileManager FILEMAN; #endif /* * (c) 2005 Charles Lohr * All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, and/or sell copies of the Software, and to permit persons to * whom the Software is furnished to do so, provided that the above * copyright notice(s) and this permission notice appear in all copies of * the Software and that both the above copyright notice(s) and this * permission notice appear in supporting documentation. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ Index: Mercury.dsp =================================================================== RCS file: /cvsroot/hgengine/Mercury/src/Mercury.dsp,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** Mercury.dsp 18 Dec 2005 02:46:52 -0000 1.28 --- Mercury.dsp 18 Dec 2005 06:52:43 -0000 1.29 *************** *** 160,163 **** --- 160,167 ---- # Begin Source File + SOURCE=.\MercuryFiles.cpp + # End Source File + # Begin Source File + SOURCE=.\MercuryINI.cpp # End Source File *************** *** 288,291 **** --- 292,299 ---- # Begin Source File + SOURCE=.\MercuryFiles.h + # End Source File + # Begin Source File + SOURCE=.\MercuryINI.h # End Source File |