mirror of
https://github.com/1dot13/source.git
synced 2026-09-09 14:46:05 +02:00
**********************************************************
** Big Maps Projects code (incl. Multiplayer v1.5 ** ********************************************************** - Merged Big Maps Project code from BMP+MP trunk (Revision: 3340) o Complete SVN Revision history: https://81.169.133.124/source/ja2/branches/Wanne/JA2%201.13%20MP - Before THIS merge, I made a branch of the existing 1.13 source o SVN Branch: https://81.169.133.124/source/ja2/branches/JA2_rev.3336/src - Removed old VS 6.0 and VS 2003 project and solutions files, because compilation is broken long time ago - I will add VS 2010 projects and solution file in the next few days git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@3341 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
+98
-179
@@ -1,213 +1,132 @@
|
||||
#include "vfs_dir_file.h"
|
||||
#include "../Interface/vfs_directory_interface.h"
|
||||
#include "../iteratedir.h"
|
||||
#include "../vfs_settings.h"
|
||||
#include "../os_functions.h"
|
||||
|
||||
extern bool g_VFS_NO_UNICODE;
|
||||
|
||||
vfs::Path vfs::CVFSFile::GetFullPath()
|
||||
vfs::CReadOnlyDirFile::CReadOnlyDirFile(vfs::Path const& filename, tLocation *directory)
|
||||
: vfs::CReadOnlyFile(filename), _location(directory)
|
||||
{
|
||||
if(_pLoc_)
|
||||
}
|
||||
|
||||
vfs::CReadOnlyDirFile::~CReadOnlyDirFile()
|
||||
{
|
||||
}
|
||||
|
||||
vfs::Path vfs::CReadOnlyDirFile::getPath()
|
||||
{
|
||||
if(_location)
|
||||
{
|
||||
return _pLoc_->GetFullPath() + m_sFileName;
|
||||
return _location->getPath() + m_filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_sFileName;
|
||||
return m_filename;
|
||||
}
|
||||
}
|
||||
|
||||
bool vfs::CVFSFile::_getRealPath(vfs::Path& rPath)
|
||||
bool vfs::CReadOnlyDirFile::_getRealPath(vfs::Path& path)
|
||||
{
|
||||
if(_pLoc_)
|
||||
if(_location)
|
||||
{
|
||||
rPath = static_cast<IDirectory<CVFSFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
path = _location->getRealPath() + m_filename;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool vfs::CVFSFile::Delete()
|
||||
vfs::FileAttributes vfs::CReadOnlyDirFile::getAttributes()
|
||||
{
|
||||
Close();
|
||||
vfs::FileAttributes _attribs = vfs::CReadOnlyFile::getAttributes();
|
||||
|
||||
vfs::UInt32 attr = _attribs.getAttrib();
|
||||
attr |= vfs::FileAttributes::ATTRIB_READONLY;
|
||||
|
||||
return vfs::FileAttributes(attr, vfs::FileAttributes::LT_READONLY_DIRECTORY);
|
||||
}
|
||||
|
||||
bool vfs::CReadOnlyDirFile::openRead()
|
||||
{
|
||||
vfs::Path filename;
|
||||
if(!_getRealPath(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _internalOpenRead(filename);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
vfs::CDirFile::CDirFile(vfs::Path const& filename, tLocation *directory)
|
||||
: CFile(filename), _location(directory)
|
||||
{
|
||||
}
|
||||
|
||||
vfs::CDirFile::~CDirFile()
|
||||
{
|
||||
}
|
||||
|
||||
vfs::Path vfs::CDirFile::getPath()
|
||||
{
|
||||
if(_location)
|
||||
{
|
||||
return _location->getPath() + m_filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_filename;
|
||||
}
|
||||
}
|
||||
|
||||
bool vfs::CDirFile::deleteFile()
|
||||
{
|
||||
this->close();
|
||||
vfs::Path fname;
|
||||
if(_pLoc_)
|
||||
if(_getRealPath(fname))
|
||||
{
|
||||
fname = static_cast<IDirectory<CVFSFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
return os::deleteRealFile(fname);
|
||||
}
|
||||
else
|
||||
{
|
||||
fname = m_sFileName;
|
||||
}
|
||||
return os::DeleteRealFile(fname);
|
||||
}
|
||||
|
||||
bool vfs::CVFSFile::OpenRead()
|
||||
{
|
||||
// std::cout << "[CVFSFile::OpenRead] open file <" << m_sFileName() << ">\n" ;
|
||||
if( !m_oFile.good() )
|
||||
{
|
||||
std::cout << "ERROR" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if(m_bIsOpen_read)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
vfs::Path sFileName;
|
||||
if(_pLoc_)
|
||||
{
|
||||
sFileName = static_cast<IDirectory<CFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
sFileName = m_sFileName;
|
||||
}
|
||||
// try to open
|
||||
Mode = std::ios::in|std::ios::binary;
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_read = m_oFile.is_open()) )
|
||||
{
|
||||
m_oFile.clear();
|
||||
return false;
|
||||
}
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
bool vfs::CVFSFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
{
|
||||
// std::cout << "[CVFSFile::OpenWrite] opening file <" << m_sFileName << "> .. ";
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
// std::cout << "ERROR" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if(m_bIsOpen_write)
|
||||
{
|
||||
// std::cout << "already open" << std::endl;
|
||||
return m_oFile.good();
|
||||
}
|
||||
vfs::Path sFileName;
|
||||
if(_pLoc_)
|
||||
{
|
||||
sFileName = static_cast<IDirectory<CFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
sFileName = m_sFileName;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::in|std::ios::out|std::ios::binary;
|
||||
if(bTruncate)
|
||||
{
|
||||
Mode |= std::ios::trunc;
|
||||
}
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
{
|
||||
m_oFile.clear();
|
||||
if(bCreateWhenNotExist)
|
||||
{
|
||||
// create file
|
||||
Mode = std::ios::out|std::ios::binary;
|
||||
if(bTruncate)
|
||||
{
|
||||
Mode |= std::ios::trunc;
|
||||
}
|
||||
#ifdef WIN32
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_oFile.close();
|
||||
Mode |= std::ios::in;
|
||||
#ifdef WIN32
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_oFile.good();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
vfs::Path vfs::CVFSTextFile::GetFullPath()
|
||||
bool vfs::CDirFile::_getRealPath(vfs::Path& path)
|
||||
{
|
||||
if(_pLoc_)
|
||||
{
|
||||
return static_cast<IDirectory<CFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_sFileName;
|
||||
}
|
||||
}
|
||||
|
||||
bool vfs::CVFSTextFile::OpenRead()
|
||||
{
|
||||
if( !m_oFile.good() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(m_bIsOpen_read)
|
||||
if(_location)
|
||||
{
|
||||
path = _location->getRealPath() + m_filename;
|
||||
return true;
|
||||
}
|
||||
vfs::Path sFileName;
|
||||
if(_pLoc_)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
vfs::FileAttributes vfs::CDirFile::getAttributes()
|
||||
{
|
||||
vfs::FileAttributes _attribs = vfs::CFile::getAttributes();
|
||||
|
||||
return vfs::FileAttributes(_attribs.getAttrib(), vfs::FileAttributes::LT_DIRECTORY);
|
||||
}
|
||||
|
||||
bool vfs::CDirFile::openRead()
|
||||
{
|
||||
vfs::Path filename;
|
||||
if(!_getRealPath(filename))
|
||||
{
|
||||
sFileName = static_cast<IDirectory<CFile::write_type>*>(_pLoc_)->GetRealPath() + m_sFileName;
|
||||
}
|
||||
else
|
||||
{
|
||||
sFileName = m_sFileName;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::in;
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_read = m_oFile.is_open()) )
|
||||
{
|
||||
m_oFile.clear();
|
||||
return false;
|
||||
}
|
||||
return m_oFile.good();
|
||||
return _internalOpenRead(filename);
|
||||
}
|
||||
|
||||
bool vfs::CDirFile::openWrite(bool createWhenNotExist, bool truncate)
|
||||
{
|
||||
vfs::Path filename;
|
||||
if(!_getRealPath(filename))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return _internalOpenWrite(filename, createWhenNotExist, truncate);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+28
-21
@@ -6,41 +6,48 @@
|
||||
#include "../Interface/vfs_file_interface.h"
|
||||
#include "../Interface/vfs_location_aware_file_interface.h"
|
||||
#include "../Interface/vfs_location_interface.h"
|
||||
#include "../Interface/vfs_directory_interface.h"
|
||||
|
||||
namespace vfs
|
||||
{
|
||||
|
||||
class CVFSFile : public vfs::CFile, public vfs::ILocationAware<vfs::IReadable, vfs::IWriteable>
|
||||
class VFS_API CReadOnlyDirFile : public vfs::CReadOnlyFile
|
||||
{
|
||||
protected:
|
||||
typedef vfs::TDirectory<vfs::CReadOnlyDirFile::write_type> tLocation;
|
||||
public:
|
||||
CVFSFile(vfs::Path const& sFileName, vfs::IDirectory<vfs::CFile::write_type> *pDirectory)
|
||||
: vfs::CFile(sFileName), vfs::ILocationAware<vfs::IReadable, vfs::IWriteable>(pDirectory)
|
||||
{};
|
||||
virtual ~CVFSFile()
|
||||
{};
|
||||
CReadOnlyDirFile(vfs::Path const& filename, tLocation *directory);
|
||||
virtual ~CReadOnlyDirFile();
|
||||
|
||||
virtual vfs::Path GetFullPath();
|
||||
virtual bool Delete();
|
||||
virtual vfs::FileAttributes getAttributes();
|
||||
|
||||
virtual bool OpenRead();
|
||||
virtual bool OpenWrite(bool bCreateWhenNotExist = false, bool bTruncate = false);
|
||||
virtual vfs::Path getPath();
|
||||
|
||||
virtual bool _getRealPath(vfs::Path& rPath);
|
||||
virtual bool openRead();
|
||||
|
||||
virtual bool _getRealPath(vfs::Path& path);
|
||||
private:
|
||||
tLocation* _location;
|
||||
};
|
||||
|
||||
class CVFSTextFile : public vfs::CTextFile, public vfs::ILocationAware<vfs::CFile::read_type, vfs::CFile::write_type>
|
||||
class VFS_API CDirFile : public vfs::CFile
|
||||
{
|
||||
typedef vfs::TDirectory<vfs::CFile::write_type> tLocation;
|
||||
public:
|
||||
CVFSTextFile(vfs::Path const& sFileName, vfs::IDirectory<vfs::CFile::write_type> *pDirectory)
|
||||
: vfs::CTextFile(sFileName), vfs::ILocationAware<vfs::CFile::read_type, vfs::CFile::write_type>(pDirectory)
|
||||
{};
|
||||
virtual ~CVFSTextFile()
|
||||
{};
|
||||
CDirFile(vfs::Path const& filename, tLocation *directory);
|
||||
virtual ~CDirFile();
|
||||
|
||||
virtual vfs::Path GetFullPath();
|
||||
virtual bool OpenRead();
|
||||
virtual vfs::FileAttributes getAttributes();
|
||||
|
||||
virtual vfs::Path getPath();
|
||||
virtual bool deleteFile();
|
||||
|
||||
virtual bool openRead();
|
||||
virtual bool openWrite(bool createWhenNotExist = false, bool truncate = false);
|
||||
|
||||
virtual bool _getRealPath(vfs::Path& path);
|
||||
private:
|
||||
tLocation* _location;
|
||||
};
|
||||
|
||||
} // end namespace
|
||||
|
||||
#endif // _VFS_DIR_FILE_H_
|
||||
|
||||
+313
-367
@@ -1,194 +1,289 @@
|
||||
#include "vfs_file.h"
|
||||
#include <cassert>
|
||||
#include "../vfs_debug.h"
|
||||
#include "../vfs_settings.h"
|
||||
#include "../os_functions.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
extern bool g_VFS_NO_UNICODE;
|
||||
#define ERROR_FILE(msg) BuildString().add(L"[").add(this->getPath()).add(L"] - ").add(msg).get()
|
||||
|
||||
vfs::CFile::CFile(vfs::Path const& sFileName)
|
||||
: vfs::IFileTemplate<vfs::IReadable,vfs::IWriteable>(sFileName), m_bIsOpen_read(false), m_bIsOpen_write(false)
|
||||
#ifdef _DEBUG
|
||||
#define IS_FILE_VALID() THROWIFFALSE( m_file.good(), ERROR_FILE(BuildString().add(L"invalid file object").add(m_file.eof() ? L" [EOF]" : L"").get()) );
|
||||
#else
|
||||
#define IS_FILE_VALID() THROWIFFALSE( m_file.good(), ERROR_FILE(BuildString().add(L"invalid file object").add(m_file.eof() ? L" [EOF]" : L"").get()) );
|
||||
#endif
|
||||
|
||||
static inline bool hasAttrib(vfs::UInt32 const& attrib, vfs::UInt32 Attribs)
|
||||
{
|
||||
return attrib == (attrib & Attribs);
|
||||
}
|
||||
|
||||
static inline void copyAttributes(vfs::UInt32 osFileAttributes, vfs::UInt32& vfsFileAttributes)
|
||||
{
|
||||
if(os::FileAttributes::ATTRIB_ARCHIVE == (os::FileAttributes::ATTRIB_ARCHIVE & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_ARCHIVE;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_COMPRESSED == (os::FileAttributes::ATTRIB_COMPRESSED & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_COMPRESSED;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_DIRECTORY == (os::FileAttributes::ATTRIB_DIRECTORY & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_DIRECTORY;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_HIDDEN == (os::FileAttributes::ATTRIB_HIDDEN & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_HIDDEN;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_NORMAL == (os::FileAttributes::ATTRIB_NORMAL & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_NORMAL;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_OFFLINE == (os::FileAttributes::ATTRIB_OFFLINE & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_OFFLINE;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_READONLY == (os::FileAttributes::ATTRIB_READONLY & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_READONLY;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_SYSTEM == (os::FileAttributes::ATTRIB_SYSTEM & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_SYSTEM;
|
||||
}
|
||||
if(os::FileAttributes::ATTRIB_TEMPORARY == (os::FileAttributes::ATTRIB_TEMPORARY & osFileAttributes))
|
||||
{
|
||||
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_TEMPORARY;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void _openFile(std::fstream& file, vfs::Path const& filename, std::ios::openmode mode)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
utf8string::str_t const& fname = filename.c_wcs();
|
||||
vfs::Settings::getUseUnicode() ?
|
||||
file.open( fname.c_str(), mode ) :
|
||||
file.open( utf8string::narrow(fname.c_str(),fname.length()).c_str(), mode );
|
||||
#else
|
||||
file.open(utf8string::as_utf8(filename()).c_str(), mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline std::ios::seekdir _seekDir(vfs::IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
if(seekDir == vfs::IBaseFile::SD_BEGIN)
|
||||
{
|
||||
return std::ios::beg;
|
||||
}
|
||||
else if(seekDir == vfs::IBaseFile::SD_CURRENT)
|
||||
{
|
||||
return std::ios::cur;
|
||||
}
|
||||
else if(seekDir == vfs::IBaseFile::SD_END)
|
||||
{
|
||||
return std::ios::end;
|
||||
}
|
||||
THROWEXCEPTION(BuildString().add(L"Unknown seek direction [").add(seekDir).add(L"]").get());
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
vfs::TFile<WriteType>::TFile(vfs::Path const& filename)
|
||||
: tBaseClass(filename), m_isOpen_read(false)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
vfs::TFile<WriteType>::~TFile()
|
||||
{
|
||||
m_file.clear();
|
||||
if(m_isOpen_read)
|
||||
{
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
void vfs::TFile<WriteType>::close()
|
||||
{
|
||||
m_file.clear();
|
||||
if( m_file.is_open() )
|
||||
{
|
||||
m_file.close();
|
||||
}
|
||||
m_isOpen_read = false;
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
vfs::FileAttributes vfs::TFile<WriteType>::getAttributes()
|
||||
{
|
||||
vfs::Path fullpath;
|
||||
THROWIFFALSE(this->_getRealPath(fullpath), ERROR_FILE(L""));
|
||||
|
||||
vfs::UInt32 osFileAttributes = 0;
|
||||
os::FileAttributes fa;
|
||||
THROWIFFALSE( fa.getFileAttributes(fullpath, osFileAttributes), ERROR_FILE(L"Could not read file Attributes") );
|
||||
|
||||
vfs::UInt32 _attribs = vfs::FileAttributes::ATTRIB_INVALID;
|
||||
copyAttributes(osFileAttributes, _attribs);
|
||||
|
||||
if(!this->implementsWritable())
|
||||
{
|
||||
_attribs &= ~vfs::FileAttributes::ATTRIB_NORMAL;
|
||||
_attribs |= vfs::FileAttributes::ATTRIB_READONLY;
|
||||
}
|
||||
|
||||
return vfs::FileAttributes(_attribs, vfs::FileAttributes::LT_NONE);
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
bool vfs::TFile<WriteType>::isOpenRead()
|
||||
{
|
||||
return m_isOpen_read;
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
bool vfs::TFile<WriteType>::_internalOpenRead(vfs::Path const& path)
|
||||
{
|
||||
if( m_isOpen_read )
|
||||
return true;
|
||||
if( !m_file.good() )
|
||||
return false;
|
||||
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::in|std::ios::binary;
|
||||
// try to open
|
||||
_openFile(m_file, path, Mode);
|
||||
m_isOpen_read = m_file.is_open() && m_file.good();
|
||||
return m_isOpen_read;
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
bool vfs::TFile<WriteType>::openRead()
|
||||
{
|
||||
return _internalOpenRead(this->m_filename);
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
vfs::size_t vfs::TFile<WriteType>::read(vfs::Byte* pData, vfs::size_t bytesToRead)
|
||||
{
|
||||
if(!m_file.eof())
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
|
||||
m_file.read(static_cast<char*>(pData), bytesToRead);
|
||||
std::streamsize bytesRead = m_file.gcount();
|
||||
|
||||
// don't treat EOF as error
|
||||
THROWIFFALSE( m_file.good() || m_file.eof(), ERROR_FILE(L"read error") );
|
||||
return (vfs::size_t)bytesRead;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
vfs::size_t vfs::TFile<WriteType>::getReadPosition()
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
return (vfs::size_t)m_file.tellg();
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
void vfs::TFile<WriteType>::setReadPosition(vfs::size_t positionInBytes)
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
|
||||
m_file.seekg( (std::streamoff)positionInBytes );
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
template<typename WriteType>
|
||||
void vfs::TFile<WriteType>::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
|
||||
std::ios::seekdir ioSeekDir;
|
||||
TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
|
||||
m_file.seekg( offsetInBytes, ioSeekDir );
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
/********************************************************************************/
|
||||
/********************************************************************************/
|
||||
|
||||
|
||||
vfs::CFile::CFile(vfs::Path const& filename)
|
||||
: tBaseClass(filename), m_isOpen_write(false)
|
||||
{
|
||||
}
|
||||
|
||||
vfs::CFile::~CFile()
|
||||
{
|
||||
m_oFile.clear();
|
||||
if(m_bIsOpen_read || m_bIsOpen_write)
|
||||
m_file.clear();
|
||||
if(m_isOpen_read || m_isOpen_write)
|
||||
{
|
||||
this->Close();
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
bool vfs::CFile::Delete()
|
||||
void vfs::CFile::close()
|
||||
{
|
||||
Close();
|
||||
if(remove(m_sFileName().utf8().c_str()) == 0)
|
||||
{
|
||||
tBaseClass::close();
|
||||
m_isOpen_write = false;
|
||||
}
|
||||
|
||||
bool vfs::CFile::deleteFile()
|
||||
{
|
||||
this->close();
|
||||
return os::deleteRealFile(m_filename);
|
||||
}
|
||||
|
||||
bool vfs::CFile::isOpenWrite()
|
||||
{
|
||||
return m_isOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CFile::_internalOpenWrite(vfs::Path const& path, bool createWhenNotExist, bool truncate)
|
||||
{
|
||||
if( m_isOpen_write )
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool vfs::CFile::Close()
|
||||
{
|
||||
m_oFile.clear();
|
||||
if( (m_bIsOpen_read || m_bIsOpen_write) && m_oFile.good() )
|
||||
{
|
||||
m_oFile.close();
|
||||
m_bIsOpen_read = false;
|
||||
m_bIsOpen_write = false;
|
||||
}
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
bool vfs::CFile::IsOpenRead()
|
||||
{
|
||||
return m_bIsOpen_read;
|
||||
}
|
||||
|
||||
bool vfs::CFile::OpenRead()
|
||||
{
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
if( !m_file.good() )
|
||||
return false;
|
||||
}
|
||||
if(m_bIsOpen_read)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
// try to open
|
||||
Mode = std::ios::in|std::ios::binary;
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = m_sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open( utf8string::narrow(fname.c_str(),fname.length()).c_str(), Mode ) :
|
||||
m_oFile.open( fname.c_str(), Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(), Mode);
|
||||
#endif
|
||||
m_bIsOpen_read = m_oFile.is_open() && m_oFile.good();
|
||||
return m_bIsOpen_read;
|
||||
}
|
||||
|
||||
bool vfs::CFile::Read(vfs::Byte* pData, vfs::UInt32 uiBytesToRead, vfs::UInt32& uiBytesRead)
|
||||
{
|
||||
|
||||
uiBytesRead = 0;
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_oFile.read(static_cast<char*>(pData),uiBytesToRead);
|
||||
uiBytesRead = m_oFile.gcount();
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
vfs::UInt32 vfs::CFile::GetReadLocation()
|
||||
{
|
||||
return m_oFile.tellg();
|
||||
}
|
||||
bool vfs::CFile::SetReadLocation(vfs::UInt32 uiPositionInBytes)
|
||||
{
|
||||
if(m_oFile.good())
|
||||
{
|
||||
if(m_bIsOpen_read || this->OpenRead())
|
||||
{
|
||||
m_oFile.seekg(uiPositionInBytes);
|
||||
}
|
||||
}
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
bool vfs::CFile::SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir)
|
||||
{
|
||||
if(!m_bIsOpen_read && !OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::ios::seekdir ioSeekDir;
|
||||
if(eSeekDir == IBaseFile::SD_BEGIN)
|
||||
{
|
||||
ioSeekDir = std::ios::beg;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_CURRENT)
|
||||
{
|
||||
ioSeekDir = std::ios::cur;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_END)
|
||||
{
|
||||
ioSeekDir = std::ios::end;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_oFile.good();
|
||||
}
|
||||
m_oFile.seekg(uiOffsetInBytes,ioSeekDir);
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
bool vfs::CFile::IsOpenWrite()
|
||||
{
|
||||
return m_bIsOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
{
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(m_bIsOpen_write)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::in|std::ios::out|std::ios::binary;
|
||||
if(bTruncate)
|
||||
if(truncate)
|
||||
{
|
||||
Mode |= std::ios::trunc;
|
||||
}
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = m_sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
_openFile(m_file, path, Mode);
|
||||
if( !(m_isOpen_write = m_file.is_open() && m_file.good()) )
|
||||
{
|
||||
m_oFile.clear();
|
||||
if(bCreateWhenNotExist)
|
||||
m_file.clear();
|
||||
if(createWhenNotExist)
|
||||
{
|
||||
// create file
|
||||
Mode = std::ios::out|std::ios::binary;
|
||||
if(bTruncate)
|
||||
if(truncate)
|
||||
{
|
||||
Mode |= std::ios::trunc;
|
||||
}
|
||||
//m_oFile.open(VfsString(sFileName).AsString().c_str(),Mode);
|
||||
#ifdef WIN32
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
_openFile(m_file, path, Mode);
|
||||
if( !(m_isOpen_write = m_file.is_open() && m_file.good()) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_oFile.close();
|
||||
m_file.close();
|
||||
Mode |= std::ios::in;
|
||||
#ifdef WIN32
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if( !(m_bIsOpen_write = m_oFile.is_open()) )
|
||||
_openFile(m_file, path, Mode);
|
||||
if( !(m_isOpen_write = m_file.is_open() && m_file.good()) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -198,237 +293,88 @@ bool vfs::CFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return m_oFile.good();
|
||||
return m_isOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CFile::Write(const vfs::Byte* pData, vfs::UInt32 uiBytesToWrite, vfs::UInt32& uiBytesWritten)
|
||||
bool vfs::CFile::openWrite(bool createWhenNotExist, bool truncate)
|
||||
{
|
||||
uiBytesWritten = 0;
|
||||
// expect file to be opened (if not, should i open it? with what parameters?)
|
||||
if(!m_bIsOpen_write && !this->OpenWrite(true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
vfs::UInt64 start = m_oFile.tellp();
|
||||
if(!m_oFile.write(pData,uiBytesToWrite))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
vfs::UInt64 end = m_oFile.tellp();
|
||||
uiBytesWritten = end-start;
|
||||
if(uiBytesWritten != uiBytesToWrite)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_oFile.good();
|
||||
return _internalOpenWrite(m_filename, createWhenNotExist, truncate);
|
||||
}
|
||||
|
||||
vfs::size_t vfs::CFile::write(const vfs::Byte* data, vfs::size_t bytesToWrite)
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write, ERROR_FILE(L"file not opened") );
|
||||
|
||||
std::streampos start = m_file.tellp();
|
||||
THROWIFFALSE( m_file.write(data, bytesToWrite), ERROR_FILE(L"write error") );
|
||||
std::streampos bytesWritten = m_file.tellp() - start;
|
||||
|
||||
IS_FILE_VALID();
|
||||
|
||||
return (vfs::size_t)bytesWritten;
|
||||
}
|
||||
|
||||
vfs::UInt32 vfs::CFile::GetWriteLocation()
|
||||
vfs::size_t vfs::CFile::getWritePosition()
|
||||
{
|
||||
return m_oFile.tellp();
|
||||
}
|
||||
bool vfs::CFile::SetWriteLocation(vfs::Int32 uiPositionInBytes)
|
||||
{
|
||||
if(m_oFile.good())
|
||||
{
|
||||
if(!m_bIsOpen_write && !this->OpenWrite(true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_oFile.seekp(uiPositionInBytes);
|
||||
}
|
||||
return m_oFile.good();
|
||||
IS_FILE_VALID();
|
||||
return (vfs::size_t)m_file.tellp();
|
||||
}
|
||||
|
||||
bool vfs::CFile::SetWriteLocation(Int32 uiOffsetInBytes, vfs::IBaseFile::ESeekDir eSeekDir)
|
||||
void vfs::CFile::setWritePosition(vfs::size_t positionInBytes)
|
||||
{
|
||||
if(m_oFile.good())
|
||||
{
|
||||
if(!m_bIsOpen_write && !this->OpenWrite(true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::ios::seekdir ioSeekDir;
|
||||
if(eSeekDir == IBaseFile::SD_BEGIN)
|
||||
{
|
||||
ioSeekDir = std::ios::beg;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_CURRENT)
|
||||
{
|
||||
ioSeekDir = std::ios::cur;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_END)
|
||||
{
|
||||
ioSeekDir = std::ios::end;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_oFile.seekp(uiOffsetInBytes,ioSeekDir);
|
||||
}
|
||||
return m_oFile.good();
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write, ERROR_FILE(L"file not opened") );
|
||||
|
||||
m_file.seekp( (std::streamoff)positionInBytes );
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
bool vfs::CFile::GetFileSize(UInt32& uiFileSize)
|
||||
void vfs::CFile::setWritePosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
uiFileSize = 0;
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool closeAtExit = !m_bIsOpen_read;
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write, ERROR_FILE(L"file not opened") );
|
||||
|
||||
std::ios::seekdir ioSeekDir;
|
||||
TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error") );
|
||||
m_file.seekp(offsetInBytes,ioSeekDir);
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
//vfs::size_t vfs::CFile::getSize()
|
||||
template<typename T>
|
||||
vfs::size_t vfs::TFile<T>::getSize()
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
|
||||
// if file was alredy opened, keep it open, otherwise close it
|
||||
bool closeAtExit = !m_isOpen_read;
|
||||
|
||||
THROWIFFALSE( m_isOpen_read || this->openRead(), ERROR_FILE(L"could not open file") )
|
||||
|
||||
// save the current position
|
||||
std::ios::pos_type current_position = m_oFile.tellg();
|
||||
std::streampos current_position = m_file.tellg();
|
||||
|
||||
// move to end of the file
|
||||
m_oFile.seekg(0,std::ios::end);
|
||||
std::ios::pos_type file_size = m_oFile.tellg();
|
||||
m_file.seekg(0,std::ios::end);
|
||||
std::streampos file_size = m_file.tellg();
|
||||
|
||||
// move to old position
|
||||
m_oFile.seekg(current_position,std::ios::beg);
|
||||
assert(current_position == m_oFile.tellg());
|
||||
uiFileSize = file_size;
|
||||
m_file.seekg(current_position,std::ios::beg);
|
||||
THROWIFFALSE(current_position == m_file.tellg(), ERROR_FILE(L"could not restore seek position"));
|
||||
|
||||
if(closeAtExit)
|
||||
{
|
||||
this->Close();
|
||||
this->close();
|
||||
}
|
||||
return m_oFile.good();
|
||||
IS_FILE_VALID();
|
||||
return (vfs::size_t)file_size;
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
bool vfs::CTextFile::OpenRead()
|
||||
{
|
||||
if(m_bIsOpen_read)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::in;
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = m_sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if(!(m_bIsOpen_read = m_oFile.is_open()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_bIsOpen_read && m_oFile.good();
|
||||
}
|
||||
|
||||
|
||||
bool vfs::CTextFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
{
|
||||
if(m_oFile.good())
|
||||
{
|
||||
if(m_bIsOpen_write)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
std::ios::openmode Mode;
|
||||
Mode = std::ios::out|std::ios::app;
|
||||
#ifdef WIN32
|
||||
utf8string::str_t const& fname = m_sFileName().c_wcs();
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if(!m_oFile.is_open())
|
||||
{
|
||||
m_oFile.clear();
|
||||
if(bCreateWhenNotExist)
|
||||
{
|
||||
// create file
|
||||
Mode = std::ios::out;
|
||||
Mode |= bTruncate ? std::ios::trunc : std::ios::app;
|
||||
#ifdef WIN32
|
||||
g_VFS_NO_UNICODE ?
|
||||
m_oFile.open(utf8string::narrow(fname.c_str(),fname.length()).c_str(),Mode) :
|
||||
m_oFile.open(fname.c_str(),Mode);
|
||||
#else
|
||||
m_oFile.open(m_sFileName().utf8().c_str(),Mode);
|
||||
#endif
|
||||
if(!m_oFile.is_open())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_bIsOpen_write = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
m_bIsOpen_write = true;
|
||||
}
|
||||
return m_oFile.good();
|
||||
}
|
||||
|
||||
bool vfs::CTextFile::ReadLine(std::string &sLine, UInt32 uiMaxNumChars)
|
||||
{
|
||||
if(!m_bIsOpen_read && !OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(!m_oFile.eof())
|
||||
{
|
||||
char *text = new char[uiMaxNumChars+1];
|
||||
m_oFile.getline(text,uiMaxNumChars);
|
||||
sLine.assign(text);
|
||||
delete[] text;
|
||||
return m_oFile.good();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool vfs::CTextFile::Read(Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead)
|
||||
{
|
||||
if(!m_bIsOpen_read)
|
||||
{
|
||||
OpenRead();
|
||||
}
|
||||
if(!m_oFile.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if(!m_oFile.eof())
|
||||
{
|
||||
m_oFile.getline((char*)pData,uiBytesToRead);
|
||||
uiBytesRead = m_oFile.gcount();
|
||||
return m_oFile.good();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool vfs::CTextFile::Write(const Byte* pData, UInt32 uiBytesToWrite, UInt32& uiBytesWritten)
|
||||
{
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool vfs::CTextFile::WriteLine(std::string const& sLine)
|
||||
{
|
||||
UInt32 uiWritten, uiToWrite=sLine.size();
|
||||
return CFile::Write(sLine.c_str(), uiToWrite ,uiWritten) && (uiWritten == uiToWrite);
|
||||
}
|
||||
|
||||
|
||||
template class vfs::TFile<vfs::IWriteType>;
|
||||
|
||||
|
||||
+47
-39
@@ -13,58 +13,66 @@ namespace vfs
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
class CFile : public vfs::IFileTemplate<IReadable,IWriteable>
|
||||
template<typename WriteType=vfs::IWriteType>
|
||||
class VFS_API TFile : public vfs::TFileTemplate<vfs::IReadable,WriteType>
|
||||
{
|
||||
typedef vfs::TFileTemplate<vfs::IReadable,WriteType> tBaseClass;
|
||||
public :
|
||||
CFile(vfs::Path const& sFileName);
|
||||
TFile(vfs::Path const& filename);
|
||||
virtual ~TFile();
|
||||
|
||||
virtual vfs::FileAttributes getAttributes();
|
||||
|
||||
virtual void close();
|
||||
virtual vfs::size_t getSize();
|
||||
|
||||
virtual bool isOpenRead();
|
||||
virtual bool openRead();
|
||||
virtual vfs::size_t read(vfs::Byte* data, vfs::size_t bytesToRead);
|
||||
|
||||
virtual vfs::size_t getReadPosition();
|
||||
virtual void setReadPosition(vfs::size_t positionInBytes);
|
||||
virtual void setReadPosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir);
|
||||
|
||||
protected:
|
||||
bool _internalOpenRead(vfs::Path const& path);
|
||||
protected:
|
||||
bool m_isOpen_read;
|
||||
std::fstream m_file;
|
||||
};
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
// implements the IWritable interface for TFile
|
||||
class VFS_API CFile : public vfs::TFile<vfs::IWritable>
|
||||
{
|
||||
typedef vfs::TFile<vfs::IWritable> tBaseClass;
|
||||
public :
|
||||
CFile(vfs::Path const& filename);
|
||||
virtual ~CFile();
|
||||
|
||||
virtual bool Close();
|
||||
virtual bool GetFileSize(UInt32& uiFileSize);
|
||||
virtual void close();
|
||||
|
||||
virtual bool IsOpenRead();
|
||||
virtual bool OpenRead();
|
||||
virtual bool Read(vfs::Byte* pData, vfs::UInt32 uiBytesToRead, vfs::UInt32& uiBytesRead);
|
||||
virtual bool isOpenWrite();
|
||||
virtual bool openWrite(bool createWhenNotExist = false, bool truncate = false);
|
||||
virtual vfs::size_t write(const vfs::Byte* data, vfs::size_t bytesToWrite);
|
||||
|
||||
virtual vfs::UInt32 GetReadLocation();
|
||||
virtual bool SetReadLocation(UInt32 uiPositionInBytes);
|
||||
virtual bool SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
|
||||
virtual vfs::size_t getWritePosition();
|
||||
virtual void setWritePosition(vfs::size_t positionInBytes);
|
||||
virtual void setWritePosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir);
|
||||
|
||||
virtual bool IsOpenWrite();
|
||||
virtual bool OpenWrite(bool bCreateWhenNotExist = false, bool bTruncate = false);
|
||||
virtual bool Write(const vfs::Byte* pData, vfs::UInt32 uiBytesToWrite, vfs::UInt32& uiBytesWritten);
|
||||
|
||||
virtual vfs::UInt32 GetWriteLocation();
|
||||
virtual bool SetWriteLocation(Int32 uiPositionInBytes);
|
||||
virtual bool SetWriteLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
|
||||
|
||||
virtual bool Delete();
|
||||
virtual bool deleteFile();
|
||||
protected:
|
||||
std::fstream m_oFile;
|
||||
bool m_bIsOpen_read, m_bIsOpen_write;
|
||||
bool _internalOpenWrite(vfs::Path const& path, bool createWhenNotExist = false, bool truncate = false);
|
||||
protected:
|
||||
bool m_isOpen_write;
|
||||
};
|
||||
|
||||
/******************************************************************/
|
||||
/******************************************************************/
|
||||
|
||||
class CTextFile : public CFile
|
||||
{
|
||||
public:
|
||||
CTextFile(vfs::Path const& sFileName)
|
||||
: CFile(sFileName)
|
||||
{};
|
||||
virtual ~CTextFile()
|
||||
{};
|
||||
|
||||
virtual bool OpenRead();
|
||||
virtual bool OpenWrite(bool bCreateWhenNotExist = false, bool bTruncate = false);
|
||||
|
||||
virtual bool Read(Byte* pData, vfs::UInt32 uiBytesToRead, vfs::UInt32& uiBytesRead);
|
||||
virtual bool ReadLine(std::string &sLine, vfs::UInt32 uiMaxNumChars);
|
||||
|
||||
virtual bool Write(const vfs::Byte* pData, vfs::UInt32 uiBytesToWrite, vfs::UInt32& uiBytesWritten);
|
||||
virtual bool WriteLine(std::string const& sLine);
|
||||
};
|
||||
typedef vfs::TFile<vfs::IWriteType> CReadOnlyFile; // needs explicit template instantiation
|
||||
|
||||
} // end namespace
|
||||
|
||||
|
||||
+64
-55
@@ -1,11 +1,14 @@
|
||||
#include "vfs_lib_file.h"
|
||||
#include "../vfs.h"
|
||||
|
||||
|
||||
#define ERROR_FILE(msg) BuildString().add(L"[").add(this->getPath()).add(L"] - ").add(msg).get()
|
||||
|
||||
ObjBlockAllocator<vfs::CLibFile>* vfs::CLibFile::_lfile_pool = NULL;
|
||||
|
||||
vfs::CLibFile* vfs::CLibFile::Create(vfs::Path const& sFileName,
|
||||
vfs::IVFSLocation<vfs::IReadable,vfs::IWriteType> *pLocation,
|
||||
ILibrary *pLibrary,
|
||||
vfs::CLibFile* vfs::CLibFile::create(vfs::Path const& filename,
|
||||
tLocation *location,
|
||||
ILibrary *library,
|
||||
ObjBlockAllocator<vfs::CLibFile>* allocator)
|
||||
{
|
||||
#if 0
|
||||
@@ -21,22 +24,22 @@ vfs::CLibFile* vfs::CLibFile::Create(vfs::Path const& sFileName,
|
||||
if(!_lfile_pool)
|
||||
{
|
||||
_lfile_pool = new ObjBlockAllocator<vfs::CLibFile>();
|
||||
CFileAllocator::RegisterAllocator(_lfile_pool);
|
||||
CFileAllocator::registerAllocator(_lfile_pool);
|
||||
}
|
||||
pFile = _lfile_pool->New();
|
||||
}
|
||||
#endif
|
||||
pFile->m_sFileName = sFileName;
|
||||
pFile->_pLoc_ = pLocation;
|
||||
pFile->m_pLibrary = pLibrary;
|
||||
pFile->m_filename = filename;
|
||||
pFile->m_location = location;
|
||||
pFile->m_library = library;
|
||||
return pFile;
|
||||
}
|
||||
|
||||
vfs::CLibFile::CLibFile()
|
||||
: vfs::IFileTemplate<IReadable,IWriteType>(L""),
|
||||
vfs::ILocationAware<vfs::IReadable,vfs::IWriteType>(NULL),
|
||||
m_bIsOpen_read(false),
|
||||
m_pLibrary(NULL)
|
||||
: tBaseClass(L""),
|
||||
m_isOpen_read(false),
|
||||
m_library(NULL),
|
||||
m_location(NULL)
|
||||
{
|
||||
};
|
||||
|
||||
@@ -44,88 +47,94 @@ vfs::CLibFile::~CLibFile()
|
||||
{
|
||||
}
|
||||
|
||||
bool vfs::CLibFile::Close()
|
||||
void vfs::CLibFile::close()
|
||||
{
|
||||
if(m_bIsOpen_read)
|
||||
if(m_isOpen_read)
|
||||
{
|
||||
m_bIsOpen_read = !m_pLibrary->Close(this);
|
||||
return m_bIsOpen_read;
|
||||
m_library->close(this);
|
||||
m_isOpen_read = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
vfs::Path vfs::CLibFile::GetFullPath()
|
||||
vfs::FileAttributes vfs::CLibFile::getAttributes()
|
||||
{
|
||||
if(_pLoc_)
|
||||
return vfs::FileAttributes(vfs::FileAttributes::ATTRIB_NORMAL | vfs::FileAttributes::ATTRIB_READONLY,
|
||||
vfs::FileAttributes::LT_LIBRARY);
|
||||
}
|
||||
|
||||
vfs::Path vfs::CLibFile::getPath()
|
||||
{
|
||||
if(m_location)
|
||||
{
|
||||
return _pLoc_->GetFullPath() + m_sFileName;
|
||||
return m_location->getPath() + m_filename;
|
||||
}
|
||||
else
|
||||
{
|
||||
return m_sFileName;
|
||||
return m_filename;
|
||||
}
|
||||
}
|
||||
bool vfs::CLibFile::IsOpenRead()
|
||||
bool vfs::CLibFile::isOpenRead()
|
||||
{
|
||||
return m_bIsOpen_read;
|
||||
return m_isOpen_read;
|
||||
}
|
||||
|
||||
bool vfs::CLibFile::OpenRead()
|
||||
bool vfs::CLibFile::openRead()
|
||||
{
|
||||
if(!m_bIsOpen_read)
|
||||
if(!m_isOpen_read)
|
||||
{
|
||||
if(!(m_bIsOpen_read = m_pLibrary->OpenRead(this)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
TRYCATCH_RETHROW(m_isOpen_read = m_library->openRead(this), ERROR_FILE(L"read open error"));
|
||||
}
|
||||
return true;
|
||||
return m_isOpen_read;
|
||||
}
|
||||
|
||||
bool vfs::CLibFile::Read(Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead)
|
||||
vfs::size_t vfs::CLibFile::read(vfs::Byte* data, vfs::size_t bytesToRead)
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
try
|
||||
{
|
||||
return false;
|
||||
return m_library->read(this, data, bytesToRead);
|
||||
}
|
||||
catch(CBasicException& ex)
|
||||
{
|
||||
RETHROWEXCEPTION(ERROR_FILE(L"read error"), &ex);
|
||||
}
|
||||
bool success = m_pLibrary->Read(this,pData,uiBytesToRead,uiBytesRead);
|
||||
return success;
|
||||
}
|
||||
|
||||
vfs::UInt32 vfs::CLibFile::GetReadLocation()
|
||||
vfs::size_t vfs::CLibFile::getReadPosition()
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
try
|
||||
{
|
||||
return -1;
|
||||
return m_library->getReadPosition(this);
|
||||
}
|
||||
catch(CBasicException& ex)
|
||||
{
|
||||
RETHROWEXCEPTION(ERROR_FILE(L"library error"), &ex);
|
||||
}
|
||||
return m_pLibrary->GetReadLocation(this);
|
||||
}
|
||||
|
||||
|
||||
bool vfs::CLibFile::SetReadLocation(vfs::UInt32 uiPositionInBytes)
|
||||
void vfs::CLibFile::setReadPosition(vfs::size_t uiPositionInBytes)
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool success = m_pLibrary->SetReadLocation(this,uiPositionInBytes);
|
||||
return success;
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
TRYCATCH_RETHROW(m_library->setReadPosition(this,uiPositionInBytes), ERROR_FILE(L"library error") );
|
||||
}
|
||||
|
||||
bool vfs::CLibFile::SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir)
|
||||
void vfs::CLibFile::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_pLibrary->SetReadLocation(this,uiOffsetInBytes,eSeekDir);
|
||||
THROWIFFALSE( m_isOpen_read, ERROR_FILE(L"file not opened") );
|
||||
TRYCATCH_RETHROW(m_library->setReadPosition(this, offsetInBytes, seekDir), ERROR_FILE(L"library error"));
|
||||
}
|
||||
|
||||
bool vfs::CLibFile::GetFileSize(vfs::UInt32& uiFileSize)
|
||||
vfs::size_t vfs::CLibFile::getSize()
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
THROWIFFALSE( m_isOpen_read || this->openRead(), ERROR_FILE(L"could not open file") );
|
||||
try
|
||||
{
|
||||
return false;
|
||||
return m_library->getSize(this);
|
||||
}
|
||||
catch(CBasicException& ex)
|
||||
{
|
||||
RETHROWEXCEPTION(ERROR_FILE(L"library error"), &ex);
|
||||
}
|
||||
return m_pLibrary->GetFileSize(this,uiFileSize);
|
||||
}
|
||||
|
||||
+22
-18
@@ -11,37 +11,41 @@ namespace vfs
|
||||
|
||||
class ILibrary;
|
||||
|
||||
class CLibFile : public vfs::IFileTemplate<vfs::IReadable,vfs::IWriteType>, public vfs::ILocationAware<vfs::IReadable,vfs::IWriteType>
|
||||
class CLibFile : public vfs::TFileTemplate<vfs::IReadable,vfs::IWriteType>
|
||||
{
|
||||
typedef vfs::IFileTemplate<vfs::IReadable,vfs::IWriteType> tBaseType;
|
||||
typedef vfs::TFileTemplate<vfs::IReadable,vfs::IWriteType> tBaseClass;
|
||||
typedef vfs::TLocationTemplate<vfs::IReadable,vfs::IWriteType> tLocation;
|
||||
public:
|
||||
static CLibFile* Create(vfs::Path const& sFileName,
|
||||
vfs::IVFSLocation<vfs::IReadable,vfs::IWriteType> *pLocation,
|
||||
ILibrary *pLibrary,
|
||||
CLibFile();
|
||||
static CLibFile* create(vfs::Path const& filename,
|
||||
tLocation *location,
|
||||
vfs::ILibrary *library,
|
||||
ObjBlockAllocator<CLibFile>* allocator = NULL);
|
||||
|
||||
// don't delete objects that YOU have not created with 'new'
|
||||
// dtor has to remain public to be usable at all
|
||||
virtual ~CLibFile();
|
||||
|
||||
virtual bool Close();
|
||||
virtual vfs::FileAttributes getAttributes();
|
||||
|
||||
virtual vfs::Path GetFullPath();
|
||||
virtual void close();
|
||||
|
||||
virtual bool IsOpenRead();
|
||||
virtual bool OpenRead();
|
||||
virtual bool Read(Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead);
|
||||
virtual vfs::Path getPath();
|
||||
|
||||
virtual UInt32 GetReadLocation();
|
||||
virtual bool SetReadLocation(UInt32 uiPositionInBytes);
|
||||
virtual bool SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
|
||||
virtual bool isOpenRead();
|
||||
virtual bool openRead();
|
||||
virtual vfs::size_t read(vfs::Byte* pData, vfs::size_t bytesToRead);
|
||||
|
||||
virtual bool GetFileSize(UInt32& uiFileSize);
|
||||
virtual vfs::size_t getReadPosition();
|
||||
virtual void setReadPosition(vfs::size_t positionInBytes);
|
||||
virtual void setReadPosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir);
|
||||
|
||||
virtual vfs::size_t getSize();
|
||||
protected:
|
||||
bool m_bIsOpen_read;
|
||||
ILibrary* m_pLibrary;
|
||||
bool m_isOpen_read;
|
||||
ILibrary* m_library;
|
||||
tLocation* m_location;
|
||||
private:
|
||||
friend class std::vector<CLibFile>;
|
||||
CLibFile();
|
||||
static ObjBlockAllocator<CLibFile>* _lfile_pool;
|
||||
};
|
||||
|
||||
|
||||
+172
-156
@@ -3,220 +3,236 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
vfs::CMemoryFile::CMemoryFile()
|
||||
: vfs::IFileTemplate<vfs::IReadable,vfs::IWriteable>(vfs::Path()), m_bIsOpen_read(false), m_bIsOpen_write(false)
|
||||
#define ERROR_FILE(msg) BuildString().add(L"[").add(this->getPath()).add(L"] - ").add(msg).get()
|
||||
|
||||
#ifdef _DEBUG
|
||||
#define IS_FILE_VALID() THROWIFFALSE( m_buffer.good(), ERROR_FILE(L"invalid file object") );
|
||||
#else
|
||||
#define IS_FILE_VALID() THROWIFFALSE( m_buffer.good(), ERROR_FILE(L"invalid file object") );
|
||||
#endif
|
||||
|
||||
static inline std::ios::seekdir _seekDir(vfs::IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
m_ssBuffer.str("");
|
||||
if(seekDir == vfs::IBaseFile::SD_BEGIN)
|
||||
{
|
||||
return std::ios::beg;
|
||||
}
|
||||
else if(seekDir == vfs::IBaseFile::SD_CURRENT)
|
||||
{
|
||||
return std::ios::cur;
|
||||
}
|
||||
else if(seekDir == vfs::IBaseFile::SD_END)
|
||||
{
|
||||
return std::ios::end;
|
||||
}
|
||||
THROWEXCEPTION(BuildString().add(L"Unknown seek direction [").add(seekDir).add(L"]").get());
|
||||
}
|
||||
|
||||
vfs::CMemoryFile::CMemoryFile(vfs::Path const& sFileName)
|
||||
: vfs::IFileTemplate<vfs::IReadable,vfs::IWriteable>(sFileName), m_bIsOpen_read(false), m_bIsOpen_write(false)
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
vfs::CMemoryFile::CMemoryFile()
|
||||
: tBaseClass(vfs::Path()), m_isOpen_read(false), m_isOpen_write(false)
|
||||
{
|
||||
m_ssBuffer.str("");
|
||||
m_buffer.str("");
|
||||
}
|
||||
|
||||
vfs::CMemoryFile::CMemoryFile(vfs::Path const& fileName)
|
||||
: tBaseClass(fileName), m_isOpen_read(false), m_isOpen_write(false)
|
||||
{
|
||||
m_buffer.str("");
|
||||
}
|
||||
|
||||
vfs::CMemoryFile::~CMemoryFile()
|
||||
{
|
||||
m_ssBuffer.str("");
|
||||
m_ssBuffer.clear();
|
||||
m_buffer.str("");
|
||||
m_buffer.clear();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::Close()
|
||||
vfs::FileAttributes vfs::CMemoryFile::getAttributes()
|
||||
{
|
||||
m_ssBuffer.clear();
|
||||
m_bIsOpen_read = false;
|
||||
m_bIsOpen_write = false;
|
||||
return m_ssBuffer.good();
|
||||
return vfs::FileAttributes(vfs::FileAttributes::ATTRIB_NORMAL, vfs::FileAttributes::LT_NONE);
|
||||
}
|
||||
bool vfs::CMemoryFile::GetFileSize(UInt32& uiFileSize)
|
||||
|
||||
void vfs::CMemoryFile::close()
|
||||
{
|
||||
uiFileSize = 0;
|
||||
if(!m_ssBuffer.good())
|
||||
m_buffer.clear();
|
||||
m_isOpen_read = false;
|
||||
m_isOpen_write = false;
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
vfs::size_t vfs::CMemoryFile::getSize()
|
||||
{
|
||||
IS_FILE_VALID();
|
||||
|
||||
std::streampos size = 0;
|
||||
if(!m_buffer.str().empty())
|
||||
{
|
||||
std::streampos current_position = m_buffer.tellg();
|
||||
m_buffer.seekg(0,std::ios::end);
|
||||
size = m_buffer.tellg();
|
||||
m_buffer.seekg(current_position,std::ios::beg);
|
||||
}
|
||||
|
||||
IS_FILE_VALID();
|
||||
return (vfs::size_t)size;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::isOpenRead()
|
||||
{
|
||||
return m_isOpen_read;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::openRead()
|
||||
{
|
||||
if(!m_buffer.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
std::ios::pos_type current_position = 0;
|
||||
if(!m_ssBuffer.str().empty())
|
||||
return m_isOpen_read = true;
|
||||
}
|
||||
vfs::size_t vfs::CMemoryFile::read(vfs::Byte* data, vfs::size_t bytesToRead)
|
||||
{
|
||||
if(!m_buffer.eof())
|
||||
{
|
||||
current_position = m_ssBuffer.tellg();
|
||||
m_ssBuffer.seekg(0,std::ios::end);
|
||||
uiFileSize = m_ssBuffer.tellg();
|
||||
m_ssBuffer.seekg(current_position,std::ios::beg);
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE(m_isOpen_read || this->openRead(), ERROR_FILE(L"open error"));
|
||||
|
||||
m_buffer.read(static_cast<Byte*>(data), bytesToRead);
|
||||
std::streamsize bytesRead = m_buffer.gcount();
|
||||
|
||||
THROWIFFALSE( m_buffer.good() || m_buffer.eof(), ERROR_FILE(L"read error") );
|
||||
return (vfs::size_t)bytesRead;
|
||||
}
|
||||
return m_ssBuffer.good();
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::IsOpenRead()
|
||||
vfs::size_t vfs::CMemoryFile::getReadPosition()
|
||||
{
|
||||
return m_bIsOpen_read;
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE(m_isOpen_read || this->openRead(), ERROR_FILE(L"open error"));
|
||||
|
||||
return (vfs::size_t)m_buffer.tellg();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::OpenRead()
|
||||
void vfs::CMemoryFile::setReadPosition(vfs::size_t positionInBytes)
|
||||
{
|
||||
m_bIsOpen_read = m_ssBuffer.good();
|
||||
return m_bIsOpen_read;
|
||||
}
|
||||
bool vfs::CMemoryFile::Read(Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead)
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_ssBuffer.read(static_cast<Byte*>(pData),uiBytesToRead);
|
||||
uiBytesRead = m_ssBuffer.gcount();
|
||||
return m_ssBuffer.good();
|
||||
}
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read || this->openRead(), ERROR_FILE(L"open error") );
|
||||
|
||||
vfs::UInt32 vfs::CMemoryFile::GetReadLocation()
|
||||
{
|
||||
if(!m_bIsOpen_read && this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_ssBuffer.tellg();
|
||||
}
|
||||
m_buffer.seekg((std::streamoff)positionInBytes);
|
||||
|
||||
bool vfs::CMemoryFile::SetReadLocation(UInt32 uiPositionInBytes)
|
||||
{
|
||||
if(!m_ssBuffer.good())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_ssBuffer.seekg(uiPositionInBytes);
|
||||
return m_ssBuffer.good();
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
bool vfs::CMemoryFile::SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir)
|
||||
void vfs::CMemoryFile::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
if(!m_bIsOpen_read && !this->OpenRead())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_read || this->openRead(), ERROR_FILE(L"open error") );
|
||||
|
||||
std::ios::seekdir ioSeekDir;
|
||||
if(eSeekDir == IBaseFile::SD_BEGIN)
|
||||
{
|
||||
ioSeekDir = std::ios::beg;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_CURRENT)
|
||||
{
|
||||
ioSeekDir = std::ios::cur;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_END)
|
||||
{
|
||||
ioSeekDir = std::ios::end;
|
||||
}
|
||||
else
|
||||
{
|
||||
TRYCATCH_RETHROW(ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
|
||||
m_buffer.seekg(offsetInBytes, ioSeekDir);
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::isOpenWrite()
|
||||
{
|
||||
return m_isOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::openWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
{
|
||||
if( !m_buffer.good() )
|
||||
return false;
|
||||
}
|
||||
m_ssBuffer.seekg(uiOffsetInBytes,ioSeekDir);
|
||||
return m_ssBuffer.good();
|
||||
}
|
||||
if( m_isOpen_write )
|
||||
return true;
|
||||
|
||||
bool vfs::CMemoryFile::IsOpenWrite()
|
||||
{
|
||||
return m_bIsOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::OpenWrite(bool bCreateWhenNotExist, bool bTruncate)
|
||||
{
|
||||
if(bTruncate)
|
||||
{
|
||||
m_ssBuffer.str("");
|
||||
m_ssBuffer.clear();
|
||||
m_buffer.str("");
|
||||
m_buffer.clear();
|
||||
}
|
||||
m_bIsOpen_write = m_ssBuffer.good();
|
||||
return m_bIsOpen_write;
|
||||
m_isOpen_write = m_buffer.good();
|
||||
return m_isOpen_write;
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::Write(const Byte* pData, UInt32 uiBytesToWrite, UInt32& uiBytesWritten)
|
||||
vfs::size_t vfs::CMemoryFile::write(const vfs::Byte* data, vfs::size_t bytesToWrite)
|
||||
{
|
||||
if(!m_bIsOpen_write && !this->OpenWrite())
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
|
||||
|
||||
std::streampos start = 0;
|
||||
if(!m_buffer.str().empty())
|
||||
{
|
||||
return false;
|
||||
start = m_buffer.tellp();
|
||||
}
|
||||
vfs::UInt64 start = 0;
|
||||
if(!m_ssBuffer.str().empty())
|
||||
{
|
||||
start = m_ssBuffer.tellp();
|
||||
}
|
||||
m_ssBuffer.write(pData,uiBytesToWrite);
|
||||
vfs::UInt64 end = m_ssBuffer.tellp();
|
||||
uiBytesWritten = end-start;
|
||||
if(uiBytesWritten != uiBytesToWrite)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_ssBuffer.good();
|
||||
THROWIFFALSE( m_buffer.write(data, bytesToWrite), ERROR_FILE(L"write error") );
|
||||
std::streampos bytesWritten = m_buffer.tellp() - start;
|
||||
|
||||
IS_FILE_VALID();
|
||||
|
||||
return (vfs::size_t)bytesWritten;
|
||||
}
|
||||
|
||||
vfs::UInt32 vfs::CMemoryFile::GetWriteLocation()
|
||||
vfs::size_t vfs::CMemoryFile::getWritePosition()
|
||||
{
|
||||
return m_ssBuffer.tellp();
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
|
||||
|
||||
return (vfs::size_t)m_buffer.tellp();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::SetWriteLocation(Int32 uiPositionInBytes)
|
||||
void vfs::CMemoryFile::setWritePosition(vfs::size_t positionInBytes)
|
||||
{
|
||||
if(!m_bIsOpen_write && !this->OpenWrite())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_ssBuffer.seekp(uiPositionInBytes);
|
||||
return m_ssBuffer.good();
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
|
||||
|
||||
m_buffer.seekp((std::streamoff)positionInBytes);
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::SetWriteLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir)
|
||||
void vfs::CMemoryFile::setWritePosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
|
||||
{
|
||||
if(!m_bIsOpen_write && !this->OpenWrite())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IS_FILE_VALID();
|
||||
THROWIFFALSE( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
|
||||
|
||||
std::ios::seekdir ioSeekDir;
|
||||
if(eSeekDir == IBaseFile::SD_BEGIN)
|
||||
{
|
||||
ioSeekDir = std::ios::beg;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_CURRENT)
|
||||
{
|
||||
ioSeekDir = std::ios::cur;
|
||||
}
|
||||
else if(eSeekDir == IBaseFile::SD_END)
|
||||
{
|
||||
ioSeekDir = std::ios::end;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_ssBuffer.seekp(uiOffsetInBytes,ioSeekDir);
|
||||
return m_ssBuffer.good();
|
||||
TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error") );
|
||||
m_buffer.seekp(offsetInBytes, ioSeekDir);
|
||||
|
||||
IS_FILE_VALID();
|
||||
}
|
||||
|
||||
bool vfs::CMemoryFile::CopyToBuffer(vfs::tReadableFile& rFile)
|
||||
void vfs::CMemoryFile::copyToBuffer(vfs::tReadableFile& rFile)
|
||||
{
|
||||
bool needToClose = !rFile.IsOpenRead();
|
||||
vfs::COpenReadFile readfile(&rFile);
|
||||
if(!needToClose)
|
||||
try
|
||||
{
|
||||
readfile.release();
|
||||
}
|
||||
vfs::UInt32 uiSize,uiIO;
|
||||
|
||||
uiSize = rFile.GetFileSize();
|
||||
std::vector<Byte> vBuffer(uiSize);
|
||||
if( rFile.Read(&vBuffer[0],uiSize,uiIO) && (uiSize == uiIO) )
|
||||
{
|
||||
if( this->Write(&vBuffer[0],uiSize,uiIO) && (uiSize == uiIO) )
|
||||
bool needToClose = !rFile.isOpenRead();
|
||||
vfs::COpenReadFile readfile(&rFile);
|
||||
if(!needToClose)
|
||||
{
|
||||
return true;
|
||||
readfile.release();
|
||||
}
|
||||
|
||||
typedef std::vector<vfs::Byte> tByteVector;
|
||||
|
||||
vfs::size_t size = rFile.getSize();
|
||||
tByteVector vBuffer(size);
|
||||
|
||||
rFile.read(&vBuffer[0], size);
|
||||
this->write(&vBuffer[0], size);
|
||||
}
|
||||
catch(CBasicException& ex)
|
||||
{
|
||||
RETHROWEXCEPTION(ERROR_FILE(L""), &ex);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool vfs::CMemoryFile::Delete()
|
||||
bool vfs::CMemoryFile::deleteFile()
|
||||
{
|
||||
m_ssBuffer.clear();
|
||||
m_ssBuffer.str("");
|
||||
return m_ssBuffer.good();
|
||||
m_buffer.clear();
|
||||
m_buffer.str("");
|
||||
return m_buffer.good();
|
||||
}
|
||||
|
||||
+23
-20
@@ -6,39 +6,42 @@
|
||||
|
||||
namespace vfs
|
||||
{
|
||||
class CMemoryFile : public vfs::IFileTemplate<vfs::IReadable,vfs::IWriteable>
|
||||
class VFS_API CMemoryFile : public vfs::TFileTemplate<vfs::IReadable,vfs::IWritable>
|
||||
{
|
||||
typedef vfs::TFileTemplate<vfs::IReadable,vfs::IWritable> tBaseClass;
|
||||
public :
|
||||
CMemoryFile();
|
||||
CMemoryFile(vfs::Path const& sFileName);
|
||||
CMemoryFile(vfs::Path const& filename);
|
||||
virtual ~CMemoryFile();
|
||||
|
||||
virtual bool Close();
|
||||
virtual bool GetFileSize(UInt32& uiFileSize);
|
||||
virtual vfs::FileAttributes getAttributes();
|
||||
|
||||
virtual bool IsOpenRead();
|
||||
virtual bool OpenRead();
|
||||
virtual bool Read(Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead);
|
||||
virtual void close();
|
||||
virtual vfs::size_t getSize();
|
||||
|
||||
virtual UInt32 GetReadLocation();
|
||||
virtual bool SetReadLocation(UInt32 uiPositionInBytes);
|
||||
virtual bool SetReadLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
|
||||
virtual bool isOpenRead();
|
||||
virtual bool openRead();
|
||||
virtual vfs::size_t read(vfs::Byte* data, vfs::size_t bytesToRead);
|
||||
|
||||
virtual bool IsOpenWrite();
|
||||
virtual bool OpenWrite(bool bCreateWhenNotExist = false, bool bTruncate = false);
|
||||
virtual bool Write(const Byte* pData, UInt32 uiBytesToWrite, UInt32& uiBytesWritten);
|
||||
virtual vfs::size_t getReadPosition();
|
||||
virtual void setReadPosition(vfs::size_t positionInBytes);
|
||||
virtual void setReadPosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir);
|
||||
|
||||
virtual UInt32 GetWriteLocation();
|
||||
virtual bool SetWriteLocation(Int32 uiPositionInBytes);
|
||||
virtual bool SetWriteLocation(Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
|
||||
virtual bool isOpenWrite();
|
||||
virtual bool openWrite(bool bCreateWhenNotExist = false, bool bTruncate = false);
|
||||
virtual vfs::size_t write(const vfs::Byte* data, vfs::size_t bytesToWrite);
|
||||
|
||||
virtual bool Delete();
|
||||
virtual vfs::size_t getWritePosition();
|
||||
virtual void setWritePosition(vfs::size_t positionInBytes);
|
||||
virtual void setWritePosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir);
|
||||
|
||||
virtual bool deleteFile();
|
||||
|
||||
// convenience method
|
||||
bool CopyToBuffer(vfs::tReadableFile& rFile);
|
||||
void copyToBuffer(vfs::tReadableFile& rFile);
|
||||
protected:
|
||||
std::stringstream m_ssBuffer;
|
||||
bool m_bIsOpen_read, m_bIsOpen_write;
|
||||
std::stringstream m_buffer;
|
||||
bool m_isOpen_read, m_isOpen_write;
|
||||
};
|
||||
|
||||
} // end namespace
|
||||
|
||||
Reference in New Issue
Block a user