- Added missing projects "VFS" & "export" to SVN

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@4447 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Wanne
2011-05-26 11:25:04 +00:00
parent 6e2bdd557c
commit be95a2a7b1
203 changed files with 44525 additions and 0 deletions
+264
View File
@@ -0,0 +1,264 @@
/*
* bfVFS : vfs/Core/File/vfs_buffer_file.cpp
* - Buffer in RAM, implements File interface to unify usage of file and memory
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/File/vfs_buffer_file.h>
#include <vfs/Core/vfs_file_raii.h>
#include <vector>
#define ERROR_FILE(msg) (_BS(L"[") << this->getPath() << L"] - " << (msg) << _BS::wget)
#ifdef _DEBUG
#define IS_FILE_VALID() VFS_THROW_IFF( m_buffer.good(), ERROR_FILE(L"invalid file object") );
#else
#define IS_FILE_VALID() VFS_THROW_IFF( m_buffer.good(), ERROR_FILE(L"invalid file object") );
#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;
}
VFS_THROW(_BS(L"Unknown seek direction [") << seekDir << L"]" << _BS::wget);
}
///////////////////////////////////////////////////
vfs::CBufferFile::CBufferFile()
: tBaseClass(vfs::Path()), m_isOpen_read(false), m_isOpen_write(false)
{
m_buffer.str("");
}
vfs::CBufferFile::CBufferFile(vfs::Path const& fileName)
: tBaseClass(fileName), m_isOpen_read(false), m_isOpen_write(false)
{
m_buffer.str("");
}
vfs::CBufferFile::~CBufferFile()
{
m_buffer.str("");
m_buffer.clear();
}
vfs::FileAttributes vfs::CBufferFile::getAttributes()
{
return vfs::FileAttributes(vfs::FileAttributes::ATTRIB_NORMAL, vfs::FileAttributes::LT_NONE);
}
void vfs::CBufferFile::close()
{
m_buffer.clear();
m_isOpen_read = false;
m_isOpen_write = false;
IS_FILE_VALID();
}
vfs::size_t vfs::CBufferFile::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::CBufferFile::isOpenRead()
{
return m_isOpen_read;
}
bool vfs::CBufferFile::openRead()
{
if(!m_buffer.good())
{
return false;
}
return m_isOpen_read = true;
}
vfs::size_t vfs::CBufferFile::read(vfs::Byte* data, vfs::size_t bytesToRead)
{
if(!m_buffer.eof())
{
IS_FILE_VALID();
VFS_THROW_IFF(m_isOpen_read || this->openRead(), ERROR_FILE(L"open error"));
m_buffer.read(static_cast<Byte*>(data), bytesToRead);
std::streamsize bytesRead = m_buffer.gcount();
VFS_THROW_IFF( m_buffer.good() || m_buffer.eof(), ERROR_FILE(L"read error") );
return (vfs::size_t)bytesRead;
}
return 0;
}
vfs::size_t vfs::CBufferFile::getReadPosition()
{
IS_FILE_VALID();
VFS_THROW_IFF(m_isOpen_read || this->openRead(), ERROR_FILE(L"open error"));
return (vfs::size_t)m_buffer.tellg();
}
void vfs::CBufferFile::setReadPosition(vfs::size_t positionInBytes)
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_read || this->openRead(), ERROR_FILE(L"open error") );
m_buffer.seekg((std::streamoff)positionInBytes);
IS_FILE_VALID();
}
void vfs::CBufferFile::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_read || this->openRead(), ERROR_FILE(L"open error") );
std::ios::seekdir ioSeekDir;
VFS_TRYCATCH_RETHROW(ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
m_buffer.seekg(offsetInBytes, ioSeekDir);
IS_FILE_VALID();
}
bool vfs::CBufferFile::isOpenWrite()
{
return m_isOpen_write;
}
bool vfs::CBufferFile::openWrite(bool bCreateWhenNotExist, bool bTruncate)
{
if( !m_buffer.good() )
return false;
if( m_isOpen_write )
return true;
if(bTruncate)
{
m_buffer.str("");
m_buffer.clear();
}
m_isOpen_write = m_buffer.good();
return m_isOpen_write;
}
vfs::size_t vfs::CBufferFile::write(const vfs::Byte* data, vfs::size_t bytesToWrite)
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
std::streampos start = 0;
if(!m_buffer.str().empty())
{
start = m_buffer.tellp();
}
VFS_THROW_IFF( 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::size_t vfs::CBufferFile::getWritePosition()
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
return (vfs::size_t)m_buffer.tellp();
}
void vfs::CBufferFile::setWritePosition(vfs::size_t positionInBytes)
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
m_buffer.seekp((std::streamoff)positionInBytes);
IS_FILE_VALID();
}
void vfs::CBufferFile::setWritePosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
IS_FILE_VALID();
VFS_THROW_IFF( m_isOpen_write || this->openWrite(), ERROR_FILE(L"open error") );
std::ios::seekdir ioSeekDir;
VFS_TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error") );
m_buffer.seekp(offsetInBytes, ioSeekDir);
IS_FILE_VALID();
}
void vfs::CBufferFile::copyToBuffer(vfs::tReadableFile& rFile)
{
try
{
bool needToClose = !rFile.isOpenRead();
vfs::COpenReadFile readfile(&rFile);
if(!needToClose)
{
readfile.release();
}
typedef std::vector<vfs::Byte> tByteVector;
vfs::size_t size = rFile.getSize();
if(size > 0)
{
tByteVector vBuffer(size);
rFile.read(&vBuffer[0], size);
this->write(&vBuffer[0], size);
}
}
catch(std::exception& ex)
{
VFS_RETHROW(ERROR_FILE(L""), ex);
}
}
bool vfs::CBufferFile::deleteFile()
{
m_buffer.clear();
m_buffer.str("");
return m_buffer.good();
}
+156
View File
@@ -0,0 +1,156 @@
/*
* bfVFS : vfs/Core/File/vfs_dir_file.cpp
* - read/read-write files for usage in vfs locations (directories)
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/File/vfs_dir_file.h>
#include <vfs/Core/Interface/vfs_directory_interface.h>
#include <vfs/Core/vfs_os_functions.h>
#include <vfs/Aspects/vfs_settings.h>
vfs::CReadOnlyDirFile::CReadOnlyDirFile(vfs::Path const& filename, tLocation *directory)
: vfs::CReadOnlyFile(filename), _location(directory)
{
}
vfs::CReadOnlyDirFile::~CReadOnlyDirFile()
{
}
vfs::Path vfs::CReadOnlyDirFile::getPath()
{
if(_location)
{
return _location->getPath() + m_filename;
}
else
{
return m_filename;
}
}
bool vfs::CReadOnlyDirFile::_getRealPath(vfs::Path& path)
{
if(_location)
{
path = _location->getRealPath() + m_filename;
return true;
}
return false;
}
vfs::FileAttributes vfs::CReadOnlyDirFile::getAttributes()
{
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(_getRealPath(fname))
{
return vfs::OS::deleteRealFile(fname);
}
return false;
}
bool vfs::CDirFile::_getRealPath(vfs::Path& path)
{
if(_location)
{
path = _location->getRealPath() + m_filename;
return true;
}
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))
{
return false;
}
return _internalOpenRead(filename);
}
bool vfs::CDirFile::openWrite(bool createWhenNotExist, bool truncate)
{
vfs::Path filename;
if(!_getRealPath(filename))
{
return false;
}
return _internalOpenWrite(filename, createWhenNotExist, truncate);
}
+619
View File
@@ -0,0 +1,619 @@
/*
* bfVFS : vfs/Core/File/vfs_file.cpp
* - File with read/read-write access
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/File/vfs_file.h>
#include <vfs/Core/vfs_debug.h>
#include <vfs/Core/vfs_os_functions.h>
#include <vfs/Aspects/vfs_settings.h>
#include <sys/stat.h>
#define ERROR_FILE(msg) (_BS(L"[") << this->getPath() << L"] - " << (msg) << _BS::wget)
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(vfs::OS::FileAttributes::ATTRIB_ARCHIVE == (vfs::OS::FileAttributes::ATTRIB_ARCHIVE & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_ARCHIVE;
}
if(vfs::OS::FileAttributes::ATTRIB_COMPRESSED == (vfs::OS::FileAttributes::ATTRIB_COMPRESSED & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_COMPRESSED;
}
if(vfs::OS::FileAttributes::ATTRIB_DIRECTORY == (vfs::OS::FileAttributes::ATTRIB_DIRECTORY & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_DIRECTORY;
}
if(vfs::OS::FileAttributes::ATTRIB_HIDDEN == (vfs::OS::FileAttributes::ATTRIB_HIDDEN & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_HIDDEN;
}
if(vfs::OS::FileAttributes::ATTRIB_NORMAL == (vfs::OS::FileAttributes::ATTRIB_NORMAL & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_NORMAL;
}
if(vfs::OS::FileAttributes::ATTRIB_OFFLINE == (vfs::OS::FileAttributes::ATTRIB_OFFLINE & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_OFFLINE;
}
if(vfs::OS::FileAttributes::ATTRIB_READONLY == (vfs::OS::FileAttributes::ATTRIB_READONLY & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_READONLY;
}
if(vfs::OS::FileAttributes::ATTRIB_SYSTEM == (vfs::OS::FileAttributes::ATTRIB_SYSTEM & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_SYSTEM;
}
if(vfs::OS::FileAttributes::ATTRIB_TEMPORARY == (vfs::OS::FileAttributes::ATTRIB_TEMPORARY & osFileAttributes))
{
vfsFileAttributes |= vfs::FileAttributes::ATTRIB_TEMPORARY;
}
}
#ifdef WIN32
static inline DWORD _seekDir(vfs::IBaseFile::ESeekDir seekDir)
{
if(seekDir == vfs::IBaseFile::SD_BEGIN)
{
return FILE_BEGIN;
}
else if(seekDir == vfs::IBaseFile::SD_CURRENT)
{
return FILE_CURRENT;
}
else if(seekDir == vfs::IBaseFile::SD_END)
{
return FILE_END;
}
VFS_THROW(_BS(L"Unknown seek direction [") << seekDir << L"]" << _BS::wget);
}
#else
static inline int _seekDir(vfs::IBaseFile::ESeekDir seekDir)
{
if(seekDir == vfs::IBaseFile::SD_BEGIN)
{
return SEEK_SET;
}
else if(seekDir == vfs::IBaseFile::SD_CURRENT)
{
return SEEK_CUR;
}
else if(seekDir == vfs::IBaseFile::SD_END)
{
return SEEK_END;
}
VFS_THROW(_BS(L"Unknown seek direction [") << seekDir << L"]" << _BS::wget);
}
#endif
template<typename WriteType>
vfs::TFile<WriteType>::TFile(vfs::Path const& filename)
: tBaseClass(filename), m_isOpen_read(false), m_file(0)
{
}
template<typename WriteType>
vfs::TFile<WriteType>::~TFile()
{
//VFS_LOCK(m_mutex);
#ifndef WIN32
if(m_file)clearerr(m_file);
#endif
if(m_isOpen_read)
{
this->close();
}
}
template<typename WriteType>
void vfs::TFile<WriteType>::close()
{
//VFS_LOCK(m_mutex);
if(m_file)
{
#ifdef WIN32
if(!CloseHandle(m_file))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"Could not close file : ") << err << _BS::wget) );
}
}
#else
//clearerr(m_file);
fflush(m_file);
int error = fclose(m_file);
if(error)
{
//const char* error_str = perror(error);
VFS_THROW( ERROR_FILE(_BS(L"Could not close file : ") << error << _BS::wget) );
}
#endif
m_file = NULL;
}
m_isOpen_read = false;
}
template<typename WriteType>
vfs::FileAttributes vfs::TFile<WriteType>::getAttributes()
{
//VFS_LOCK(m_mutex);
vfs::Path fullpath;
VFS_THROW_IFF(this->_getRealPath(fullpath), ERROR_FILE(L""));
vfs::UInt32 osFileAttributes = 0;
vfs::OS::FileAttributes fa;
VFS_THROW_IFF( 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()
{
//VFS_LOCK(m_mutex);
return m_isOpen_read;
}
template<typename WriteType>
bool vfs::TFile<WriteType>::_internalOpenRead(vfs::Path const& path)
{
//VFS_LOCK(m_mutex);
if( m_isOpen_read )
return true;
#ifdef WIN32
m_file = vfs::Settings::getUseUnicode() ?
CreateFileW(path.c_str(),GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL) :
CreateFileA(vfs::String::narrow(path.c_str(),path.length()).c_str(),GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD err = GetLastError();
if(err != NO_ERROR && err != ERROR_ALREADY_EXISTS)
{
VFS_LOG_ERROR( ERROR_FILE(_BS(L"Error when opening file : ") << err << _BS::wget) );
return m_isOpen_read = false;
}
return m_isOpen_read = true;
#else
m_file = fopen(path.to_string().c_str(), "r");
return m_isOpen_read = (m_file != NULL);
#endif
}
template<typename WriteType>
bool vfs::TFile<WriteType>::openRead()
{
//VFS_LOCK(m_mutex);
return _internalOpenRead(this->m_filename);
}
template<typename WriteType>
vfs::size_t vfs::TFile<WriteType>::read(vfs::Byte* pData, vfs::size_t bytesToRead)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
#ifdef WIN32
DWORD has_read = 0;
if(!ReadFile(m_file, pData, bytesToRead, &has_read, NULL))
{
DWORD err = GetLastError();
if(err != NO_ERROR && err != ERROR_HANDLE_EOF)
{
VFS_THROW( ERROR_FILE(_BS(L"read error : ") << err << _BS::wget) );
}
}
#else
size_t has_read = fread(pData,1,bytesToRead,m_file);
if(has_read != bytesToRead)
{
int error = ferror(m_file);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"read error : ") << error << _BS::wget) );
}
clearerr(m_file);
}
#endif
return has_read;
}
template<typename WriteType>
vfs::size_t vfs::TFile<WriteType>::getReadPosition()
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
#ifdef WIN32
LARGE_INTEGER current_position,zero;
zero.QuadPart = 0;
if(!SetFilePointerEx(m_file, zero, &current_position, FILE_CURRENT))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
return (vfs::size_t)current_position.QuadPart;
#else
long int pos = ftell(m_file);
if(pos == -1L)
{
int error = ferror(m_file);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
}
return (vfs::size_t)pos;
#endif
}
template<typename WriteType>
void vfs::TFile<WriteType>::setReadPosition(vfs::size_t positionInBytes)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
#ifdef WIN32
LARGE_INTEGER pos;
pos.QuadPart = positionInBytes;
if(!SetFilePointerEx(m_file, pos, NULL, FILE_BEGIN))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
#else
int error = fseek(m_file,positionInBytes,SEEK_SET);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
#endif
}
template<typename WriteType>
void vfs::TFile<WriteType>::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
#ifdef WIN32
DWORD ioSeekDir;
VFS_TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
LARGE_INTEGER offset;
offset.QuadPart = offsetInBytes;
if(!SetFilePointerEx(m_file, offset, NULL, ioSeekDir))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
#else
int ioSeekDir;
VFS_TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
int error = fseek(m_file, offsetInBytes, ioSeekDir);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
#endif
}
/********************************************************************************/
/********************************************************************************/
vfs::CFile::CFile(vfs::Path const& filename)
: tBaseClass(filename), m_isOpen_write(false)
{
}
vfs::CFile::~CFile()
{
//VFS_LOCK(m_mutex);
if(m_isOpen_read || m_isOpen_write)
{
this->close();
}
}
void vfs::CFile::close()
{
//VFS_LOCK(m_mutex);
tBaseClass::close();
m_isOpen_write = false;
}
bool vfs::CFile::deleteFile()
{
//VFS_LOCK(m_mutex);
this->close();
return vfs::OS::deleteRealFile(m_filename);
}
bool vfs::CFile::isOpenWrite()
{
//VFS_LOCK(m_mutex);
return m_isOpen_write;
}
bool vfs::CFile::_internalOpenWrite(vfs::Path const& path, bool createWhenNotExist, bool truncate)
{
//VFS_LOCK(m_mutex);
if( m_isOpen_write )
return true;
#ifdef WIN32
DWORD Mode = 0;
if(createWhenNotExist)
{
Mode |= OPEN_ALWAYS;
}
else
{
Mode |= OPEN_EXISTING;
}
if(truncate)
{
Mode |= TRUNCATE_EXISTING;
}
m_file = vfs::Settings::getUseUnicode() ?
CreateFileW(path.c_str(),GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,Mode,FILE_ATTRIBUTE_NORMAL,NULL) :
CreateFileA(vfs::String::narrow(path.c_str(),path.length()).c_str(),GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,Mode,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD err = GetLastError();
if(truncate && err == ERROR_FILE_NOT_FOUND)
{
Mode = CREATE_ALWAYS;
m_file = vfs::Settings::getUseUnicode() ?
CreateFileW(path.c_str(),GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,Mode,FILE_ATTRIBUTE_NORMAL,NULL) :
CreateFileA(vfs::String::narrow(path.c_str(),path.length()).c_str(),GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,Mode,FILE_ATTRIBUTE_NORMAL,NULL);
err = GetLastError();
}
if(err != NO_ERROR && err != ERROR_ALREADY_EXISTS)
{
VFS_LOG_ERROR(_BS(L"Error when opening file - ") << path << L" - [" << err << L"]" << _BS::wget);
return m_isOpen_write = false;
}
return m_isOpen_write = true;
#else
m_file = fopen(path.to_string().c_str(), "r+");
if( (!m_file && createWhenNotExist) || (m_file && truncate))
{
m_file = fopen(path.to_string().c_str(), "w");
}
return m_isOpen_write = (m_file != NULL);
#endif
}
bool vfs::CFile::openWrite(bool createWhenNotExist, bool truncate)
{
//VFS_LOCK(m_mutex);
return _internalOpenWrite(m_filename, createWhenNotExist, truncate);
}
vfs::size_t vfs::CFile::write(const vfs::Byte* data, vfs::size_t bytesToWrite)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_write, ERROR_FILE(L"file not opened") );
#ifdef WIN32
DWORD has_written = 0;
if(!WriteFile(m_file, data, bytesToWrite, &has_written, NULL))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW(_BS(L"write error : ") << err << _BS::wget);
}
}
#else
size_t has_written = fwrite(data, 1, bytesToWrite, m_file);
if(has_written != bytesToWrite)
{
int error = ferror(m_file);
if(error)
{
VFS_THROW(_BS(L"write error : ") << error << _BS::wget);
}
}
#endif
return (vfs::size_t)has_written;
}
vfs::size_t vfs::CFile::getWritePosition()
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_write, ERROR_FILE(L"file not opened") );
#ifdef WIN32
LARGE_INTEGER current_position, zero;
zero.QuadPart = 0;
if(!SetFilePointerEx(m_file, zero, &current_position, FILE_CURRENT))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
return (vfs::size_t)current_position.QuadPart;
#else
long int pos = ftell(m_file);
if(pos == -1L)
{
int error = ferror(m_file);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
}
return (vfs::size_t)pos;
#endif
}
void vfs::CFile::setWritePosition(vfs::size_t positionInBytes)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_write, ERROR_FILE(L"file not opened") );
#ifdef WIN32
LARGE_INTEGER pos;
pos.QuadPart = positionInBytes;
if(!SetFilePointerEx(m_file, pos, NULL, FILE_CURRENT))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
#else
int error = fseek(m_file,positionInBytes,SEEK_SET);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
#endif
}
void vfs::CFile::setWritePosition(vfs::offset_t offsetInBytes, vfs::IBaseFile::ESeekDir seekDir)
{
//VFS_LOCK(m_mutex);
VFS_THROW_IFF( m_isOpen_write, ERROR_FILE(L"file not opened") );
#ifdef WIN32
DWORD ioSeekDir;
VFS_TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
LARGE_INTEGER offset;
offset.QuadPart = offsetInBytes;
if(!SetFilePointerEx(m_file, offset, NULL, ioSeekDir))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << err << _BS::wget) );
}
}
#else
int ioSeekDir;
VFS_TRYCATCH_RETHROW( ioSeekDir = _seekDir(seekDir), ERROR_FILE(L"seek error"));
int error = fseek(m_file, offsetInBytes, ioSeekDir);
if(error)
{
VFS_THROW( ERROR_FILE(_BS(L"set position error : ") << error << _BS::wget) );
}
#endif
}
template<typename T>
vfs::size_t vfs::TFile<T>::getSize()
{
//VFS_LOCK(m_mutex);
#ifdef WIN32
bool was_open = false;
if(m_file)
{
was_open = true;
}
else
{
VFS_THROW_IFF(this->openRead(),ERROR_FILE(L"could not open file"));
}
vfs::size_t size;
# ifdef _MSC_VER
LARGE_INTEGER li_size;
if(!GetFileSizeEx(m_file, &li_size))
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"get size error : ") << err << _BS::wget) );
}
}
size = (vfs::size_t)li_size.QuadPart;
# else
DWORD low_part, high_part;
low_part = GetFileSize(m_file, &high_part);
if(low_part == INVALID_FILE_SIZE)
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_THROW( ERROR_FILE(_BS(L"get size error : ") << err << _BS::wget) );
}
}
size = low_part;
# endif
if(!was_open)
{
this->close();
}
return size;
#else
// if file was alredy opened, keep it open, otherwise close it
bool closeAtExit = !m_isOpen_read;
VFS_THROW_IFF( m_isOpen_read || this->openRead(), ERROR_FILE(L"could not open file") )
// save current position
long int current_position = ftell(m_file);
// move to end of the file
fseek(m_file, 0, SEEK_END);
long int file_size = ftell(m_file);
// move to old position
fseek(m_file, current_position, SEEK_SET);
VFS_THROW_IFF(current_position == ftell(m_file), ERROR_FILE(L"could not restore seek position"));
if(closeAtExit)
{
this->close();
}
return (vfs::size_t)file_size;
#endif
}
/******************************************************************/
/******************************************************************/
template class vfs::TFile<vfs::IWriteType>;
template class vfs::TFile<vfs::IWritable>;
+163
View File
@@ -0,0 +1,163 @@
/*
* bfVFS : vfs/Core/File/vfs_lib_file.cpp
* - read/read-write files for usage in vfs locations (libraries)
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/File/vfs_lib_file.h>
#include <vfs/Core/vfs.h>
#define ERROR_FILE(msg) (_BS(L"[") << this->getPath()() << L"] - " << msg << _BS::wget)
vfs::ObjBlockAllocator<vfs::CLibFile>* vfs::CLibFile::_lfile_pool = NULL;
vfs::CLibFile* vfs::CLibFile::create(vfs::Path const& filename,
tLocation *location,
ILibrary *library,
vfs::ObjBlockAllocator<vfs::CLibFile>* allocator)
{
#if 0
vfs::CLibFile* pFile = new vfs::CLibFile();
#else
vfs::CLibFile* pFile;
if(allocator)
{
pFile = allocator->New();
}
else
{
if(!_lfile_pool)
{
_lfile_pool = new vfs::ObjBlockAllocator<vfs::CLibFile>();
vfs::ObjectAllocator::registerAllocator(_lfile_pool);
}
pFile = _lfile_pool->New();
}
#endif
pFile->m_filename = filename;
pFile->m_location = location;
pFile->m_library = library;
return pFile;
}
vfs::CLibFile::CLibFile()
: tBaseClass(L""),
m_isOpen_read(false),
m_library(NULL),
m_location(NULL)
{
};
vfs::CLibFile::~CLibFile()
{
}
void vfs::CLibFile::close()
{
if(m_isOpen_read)
{
m_library->close(this);
m_isOpen_read = false;
}
}
vfs::FileAttributes vfs::CLibFile::getAttributes()
{
return vfs::FileAttributes(vfs::FileAttributes::ATTRIB_NORMAL | vfs::FileAttributes::ATTRIB_READONLY,
vfs::FileAttributes::LT_LIBRARY);
}
vfs::Path vfs::CLibFile::getPath()
{
if(m_location)
{
return m_location->getPath() + m_filename;
}
else
{
return m_filename;
}
}
bool vfs::CLibFile::isOpenRead()
{
return m_isOpen_read;
}
bool vfs::CLibFile::openRead()
{
if(!m_isOpen_read)
{
VFS_TRYCATCH_RETHROW(m_isOpen_read = m_library->openRead(this), ERROR_FILE(L"read open error"));
}
return m_isOpen_read;
}
vfs::size_t vfs::CLibFile::read(vfs::Byte* data, vfs::size_t bytesToRead)
{
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
try
{
return m_library->read(this, data, bytesToRead);
}
catch(std::exception& ex)
{
VFS_RETHROW(ERROR_FILE(L"read error"), ex);
}
}
vfs::size_t vfs::CLibFile::getReadPosition()
{
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
try
{
return m_library->getReadPosition(this);
}
catch(std::exception& ex)
{
VFS_RETHROW(ERROR_FILE(L"library error"), ex);
}
}
void vfs::CLibFile::setReadPosition(vfs::size_t uiPositionInBytes)
{
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
VFS_TRYCATCH_RETHROW(m_library->setReadPosition(this,uiPositionInBytes), ERROR_FILE(L"library error") );
}
void vfs::CLibFile::setReadPosition(vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
VFS_THROW_IFF( m_isOpen_read, ERROR_FILE(L"file not opened") );
VFS_TRYCATCH_RETHROW(m_library->setReadPosition(this, offsetInBytes, seekDir), ERROR_FILE(L"library error"));
}
vfs::size_t vfs::CLibFile::getSize()
{
VFS_THROW_IFF( m_isOpen_read || this->openRead(), ERROR_FILE(L"could not open file") );
try
{
return m_library->getSize(this);
}
catch(std::exception& ex)
{
VFS_RETHROW(ERROR_FILE(L"library error"), ex);
}
}
@@ -0,0 +1,116 @@
/*
* bfVFS : vfs/Core/Interface/vfs_interface_members.cpp
* - non-generic code from the interface header files
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/Interface/vfs_file_interface.h>
#include <vfs/Core/Interface/vfs_library_interface.h>
/**********************************************************************
* vfs::FileAttributes
*/
vfs::FileAttributes::FileAttributes()
: _attribs(ATTRIB_NORMAL), _location(LT_NONE)
{
};
vfs::FileAttributes::FileAttributes(vfs::UInt32 attribs, LocationType location)
: _attribs(attribs), _location(location)
{
};
vfs::UInt32 vfs::FileAttributes::getAttrib() const
{
return _attribs;
};
vfs::UInt32 vfs::FileAttributes::getLocation() const
{
return _location;
};
bool vfs::FileAttributes::isAttribSet(vfs::UInt32 attribs) const
{
return attribs == (attribs & _attribs);
};
bool vfs::FileAttributes::isAttribNotSet(vfs::UInt32 attribs) const
{
return 0 == (attribs & _attribs);
};
bool vfs::FileAttributes::isLocation(vfs::UInt32 location) const
{
return location == (location & _location);
};
/**********************************************************************
* vfs::IBaseFile
*/
vfs::IBaseFile::IBaseFile(vfs::Path const& filename)
: m_filename(filename)
{};
vfs::IBaseFile::~IBaseFile()
{};
vfs::Path const& vfs::IBaseFile::getName()
{
return m_filename;
};
vfs::Path vfs::IBaseFile::getPath()
{
return this->getName();
};
bool vfs::IBaseFile::_getRealPath(vfs::Path& path)
{
return false;
}
/**********************************************************************
* vfs::ILibrary
*/
vfs::ILibrary::ILibrary(vfs::tReadableFile *libraryFile, vfs::Path const& mountPoint, bool ownFile)
: tBaseClass(mountPoint), m_ownLibFile(ownFile), m_libraryFile(libraryFile)
{
}
vfs::ILibrary::~ILibrary()
{
if(m_libraryFile && m_ownLibFile)
{
m_libraryFile->close();
delete m_libraryFile;
m_libraryFile = NULL;
}
}
vfs::Path const& vfs::ILibrary::getName()
{
return m_libraryFile->getName();
}
/**********************************************************************/
@@ -0,0 +1,628 @@
/*
* bfVFS : vfs/Core/Location/vfs_directory_tree.cpp
* - class for directories in a File System, implements Directory interface
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/Location/vfs_directory_tree.h>
#include <vfs/Core/File/vfs_dir_file.h>
#include <vfs/Core/vfs_os_functions.h>
#include <queue>
#include <list>
#include <set>
#define ERROR_FILE(msg) (_BS(msg) << L" : " << pFile->getPath() << _BS::wget)
namespace vfs
{
template<typename WriteType>
class TSubDir : public vfs::TDirectory<typename vfs::TDirectoryTree<WriteType>::tWriteType>
{
typedef vfs::TDirectory<typename vfs::TDirectoryTree<WriteType>::tWriteType> tBaseClass;
typedef typename tBaseClass::tFileType tFileType;
typedef std::map<vfs::Path, tFileType*, vfs::Path::Less> tFileCatalogue;
public:
typedef vfs::IBaseLocation::Iterator Iterator;
/////////////////////////////////////////////////////////////////////
class IterImpl : public vfs::IBaseLocation::Iterator::IImplementation
{
friend class TSubDir<WriteType>;
typedef vfs::IBaseLocation::Iterator::IImplementation tBaseClass;
IterImpl(TSubDir<WriteType>* dir): _dir(dir)
{
VFS_THROW_IFF(_dir, L"");
_iter = _dir->m_mapFiles.begin();
}
public:
IterImpl() : tBaseClass(), _dir(NULL)
{};
virtual ~IterImpl()
{};
virtual tFileType* value()
{
if(_iter != _dir->m_mapFiles.end())
{
return _iter->second;
}
return NULL;
}
virtual void next()
{
if(_iter != _dir->m_mapFiles.end())
{
_iter++;
}
}
protected:
tBaseClass* clone()
{
IterImpl* iter = new IterImpl();
iter->_dir = _dir;
iter->_iter = _iter;
return iter;
}
private:
TSubDir<WriteType>* _dir;
typename tFileCatalogue::iterator _iter;
};
/////////////////////////////////////////////////////////////////////
public:
TSubDir(vfs::Path const& sMountPoint, vfs::Path const& sRealPath)
: tBaseClass(sMountPoint,sRealPath)
{};
virtual ~TSubDir();
virtual bool fileExists(vfs::Path const& rFileName);
virtual vfs::IBaseFile* getFile(vfs::Path const& rFileName);
virtual tFileType* getFileTyped(vfs::Path const& rFileName);
virtual tFileType* addFile(vfs::Path const& sFilename, bool bDeleteOldFile=false);
virtual bool addFile(tFileType* pFile, bool bDeleteOldFile=false);
virtual bool createSubDirectory(vfs::Path const& sSubDirPath);
virtual bool deleteDirectory(vfs::Path const& sDirPath);
virtual bool deleteFileFromDirectory(vfs::Path const& sFileName);
virtual void getSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual Iterator begin();
private:
tFileCatalogue m_mapFiles;
};
template<>
vfs::TSubDir<vfs::IWriteType>::tFileType* vfs::TSubDir<vfs::IWriteType>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile);
template<>
vfs::TSubDir<vfs::IWritable>::tFileType* vfs::TSubDir<vfs::IWritable>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile);
template<>
bool vfs::TSubDir<vfs::IWriteType>::deleteDirectory(vfs::Path const& sDirPath);
template<>
bool vfs::TSubDir<vfs::IWritable>::deleteDirectory(vfs::Path const& sDirPath);
template<>
bool vfs::TSubDir<vfs::IWriteType>::deleteFileFromDirectory(vfs::Path const& rFileName);
template<>
bool vfs::TSubDir<vfs::IWritable>::deleteFileFromDirectory(vfs::Path const& rFileName);
}
/********************************************************/
template<typename WriteType>
vfs::TSubDir<WriteType>::~TSubDir()
{
typename tFileCatalogue::iterator it = m_mapFiles.begin();
for(; it != m_mapFiles.end(); ++it)
{
if(it->second)
{
it->second->close();
delete it->second;
}
}
m_mapFiles.clear();
}
template<typename WriteType>
bool vfs::TSubDir<WriteType>::fileExists(vfs::Path const& sFileName)
{
typename tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
bool success = (it != m_mapFiles.end()) && (it->second != NULL);
return success;
}
template<typename WriteType>
vfs::IBaseFile* vfs::TSubDir<WriteType>::getFile(vfs::Path const& sFileName)
{
return getFileTyped(sFileName);
}
template<typename WriteType>
typename vfs::TSubDir<WriteType>::tFileType* vfs::TSubDir<WriteType>::getFileTyped(vfs::Path const& sFileName)
{
typename tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
if(it != m_mapFiles.end())
{
return it->second;
}
return NULL;
}
template<>
vfs::TSubDir<vfs::IWriteType>::tFileType* vfs::TSubDir<vfs::IWriteType>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile)
{
tFileType* pFile = m_mapFiles[sFilename];
if(pFile)
{
if(!bDeleteOldFile)
{
// not allowed to replace old file
return NULL;
}
delete pFile;
}
pFile = new vfs::CReadOnlyDirFile(sFilename,this);
m_mapFiles[sFilename] = pFile;
return pFile;
}
template<>
vfs::TSubDir<vfs::IWritable>::tFileType* vfs::TSubDir<vfs::IWritable>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile)
{
tFileType* pFile = m_mapFiles[sFilename];
if(pFile)
{
if(!bDeleteOldFile)
{
// not allowed to replace old file
return NULL;
}
delete pFile;
}
pFile = new vfs::CDirFile(sFilename,this);
m_mapFiles[sFilename] = pFile;
return pFile;
}
template<typename WriteType>
bool vfs::TSubDir<WriteType>::addFile(tFileType* pFile, bool bDeleteOldFile)
{
if(!pFile)
{
return false;
}
tFileType* pOldFile = m_mapFiles[pFile->getName()];
if( pOldFile && (pOldFile != pFile) )
{
if(bDeleteOldFile)
{
pOldFile->close();
delete pOldFile;
}
}
m_mapFiles[pFile->getName()] = pFile;
return true;
}
template<>
bool vfs::TSubDir<vfs::IWritable>::deleteDirectory(vfs::Path const& sDirPath)
{
if( !(this->m_mountPoint == sDirPath) )
{
return false;
}
tFileCatalogue::iterator it = m_mapFiles.begin();
for(; it != m_mapFiles.end(); ++it)
{
tFileType* pFile = it->second;
if(pFile)
{
pFile->close();
if(!pFile->deleteFile())
{
VFS_THROW( ERROR_FILE(L"Could not delete file") );
}
delete pFile;
}
}
m_mapFiles.clear();
return true;
}
template<>
bool vfs::TSubDir<vfs::IWriteType>::deleteDirectory(vfs::Path const& sDirPath)
{
return false;
}
template<>
bool vfs::TSubDir<vfs::IWritable>::deleteFileFromDirectory(vfs::Path const& rFileName)
{
tFileCatalogue::iterator it = m_mapFiles.find(rFileName);
if( it != m_mapFiles.end() )
{
tFileType* pFile = it->second;
if(pFile)
{
pFile->close();
VFS_THROW_IFF(pFile->deleteFile(), ERROR_FILE(L"Could not delete file"));
delete pFile;
}
m_mapFiles.erase(it);
return true;
}
return false;
}
template<>
bool vfs::TSubDir<vfs::IWriteType>::deleteFileFromDirectory(vfs::Path const& rFileName)
{
return false;
}
template<typename WriteType>
bool vfs::TSubDir<WriteType>::createSubDirectory(vfs::Path const& sSubDirPath)
{
return false;
}
template<typename WriteType>
void vfs::TSubDir<WriteType>::getSubDirList(std::list<vfs::Path>& rlSubDirs)
{
}
template<typename WriteType>
typename vfs::TSubDir<WriteType>::Iterator vfs::TSubDir<WriteType>::begin()
{
return Iterator(new IterImpl(this));
}
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
template<typename WriteType>
class vfs::TDirectoryTree<WriteType>::IterImpl : public vfs::IBaseLocation::Iterator::IImplementation
{
typedef vfs::IBaseLocation::Iterator::IImplementation tBaseClass;
typedef vfs::TDirectoryTree<WriteType> tDirTree;
public:
IterImpl(tDirTree& tree);
virtual ~IterImpl();
virtual typename tDirTree::tFileType* value();
virtual void next();
protected:
virtual tBaseClass* clone()
{
IterImpl* iter = new IterImpl(_tree);
iter->_subdir_iter = _subdir_iter;
iter->_file_iter = _file_iter;
return iter;
}
private:
void operator=(typename vfs::TDirectoryTree<WriteType>::IterImpl const& tree);
tDirTree& _tree;
typename tDirTree::tDirCatalogue::iterator _subdir_iter;
typename tDirTree::tLocationType::Iterator _file_iter;
};
template<typename WriteType>
vfs::TDirectoryTree<WriteType>::IterImpl::IterImpl(vfs::TDirectoryTree<WriteType>& tree)
: tBaseClass(), _tree(tree)
{
_subdir_iter = _tree.m_catDirs.begin();
if(_subdir_iter != _tree.m_catDirs.end())
{
TSubDir<WriteType> *sdir = dynamic_cast<TSubDir<WriteType>*>(_subdir_iter->second);
typename TSubDir<WriteType>::Iterator it = sdir->begin();
_file_iter = it;
if(_file_iter.end())
{
next();
}
}
}
template<typename WriteType>
vfs::TDirectoryTree<WriteType>::IterImpl::~IterImpl()
{
}
template<typename WriteType>
typename vfs::TDirectoryTree<WriteType>::tFileType* vfs::TDirectoryTree<WriteType>::IterImpl::value()
{
if(!_file_iter.end())
{
return static_cast<typename vfs::TDirectoryTree<WriteType>::tFileType*>(_file_iter.value());
}
return NULL;
}
template<typename WriteType>
void vfs::TDirectoryTree<WriteType>::IterImpl::next()
{
if(!_file_iter.end())
{
_file_iter.next();
}
// need to loop for the case when one or many sub directories are empty
while(_file_iter.end())
{
if(_subdir_iter == _tree.m_catDirs.end())
{
break;
}
else
{
_subdir_iter++;
if(_subdir_iter != _tree.m_catDirs.end())
{
_file_iter = _subdir_iter->second->begin();
}
}
}
return;
}
/*****************************************************************************/
/*****************************************************************************/
template<typename WriteType>
vfs::TDirectoryTree<WriteType>::TDirectoryTree(vfs::Path const& sMountPoint, vfs::Path const& sRealPath)
: tBaseClass(sMountPoint,sRealPath)
{};
template<typename WriteType>
vfs::TDirectoryTree<WriteType>::~TDirectoryTree()
{
typename tDirCatalogue::iterator it = m_catDirs.begin();
for(;it != m_catDirs.end(); ++it)
{
delete it->second;
it->second = NULL;
}
m_catDirs.clear();
}
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::init()
{
// contains local path
typedef vfs::TSubDir<WriteType> tSubDir;
typedef std::pair<vfs::Path,tSubDir*> tDirs;
std::queue<tDirs> qSubDirs;
qSubDirs.push(tDirs(vfs::Path(vfs::Const::EMPTY()),new tSubDir(this->m_mountPoint, this->m_realPath)));
m_catDirs[this->m_mountPoint] = qSubDirs.front().second;
vfs::String sFilename;
tSubDir *pCurrentDir;
vfs::Path oCurDir;
while(!qSubDirs.empty())
{
pCurrentDir = qSubDirs.front().second;
oCurDir = this->m_realPath;
if( !qSubDirs.front().first.empty())
{
oCurDir += qSubDirs.front().first;
}
try
{
vfs::OS::CIterateDirectory::EFileAttribute eFA;
vfs::OS::CIterateDirectory iterFS(oCurDir, vfs::Const::STAR());
while ( iterFS.nextFile(sFilename, eFA) )
{
if (StrCmp::Equal(vfs::Const::DOT(),sFilename) || StrCmp::Equal(vfs::Const::DOTDOT(),sFilename) || StrCmp::Equal(vfs::Const::DOTSVN(),sFilename) )
{
continue;
}
if (eFA == vfs::OS::CIterateDirectory::FA_DIRECTORY)
{
vfs::Path sLocal = qSubDirs.front().first + sFilename;
vfs::Path temp = this->m_mountPoint+sLocal;
tSubDir *pNewDir = new tSubDir(sLocal, this->m_realPath+sLocal);
qSubDirs.push(tDirs(sLocal,pNewDir));
m_catDirs[temp] = pNewDir;
}
else
{
pCurrentDir->addFile(vfs::Path(sFilename));
}
}
}
catch(std::exception &ex)
{
// probably directory doesn't exist. abort or continue???
// -> abort AND continue
VFS_LOG_WARNING(ex.what());
return false;
}
qSubDirs.pop();
}
return true;
}
template<typename WriteType>
typename vfs::TDirectoryTree<WriteType>::tFileType* vfs::TDirectoryTree<WriteType>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile)
{
vfs::Path sDir,sFile;
sFilename.splitLast(sDir,sFile);
typename tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
vfs::Path sTemp,sCreateDir,sLeft,sRight = sDir;
while(!sRight.empty())
{
sRight.splitFirst(sLeft,sTemp);
sRight = sTemp;
sCreateDir += sLeft;
if(!this->createSubDirectory(sCreateDir))
{
VFS_THROW(_BS(L"could not create directory : ") << sCreateDir << _BS::wget);
}
}
it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
return NULL;
}
}
if(it->second)
{
return it->second->addFile(sFile,bDeleteOldFile);
}
return NULL;
}
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::addFile(tFileType* pFile, bool bDeleteOldFile)
{
// no files from outside
// these files are not connected with the correct directory object
return false;
}
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::deleteDirectory(vfs::Path const& sDirPath)
{
typename tDirCatalogue::iterator it = m_catDirs.find(sDirPath);
if(it == m_catDirs.end())
{
// no such directory
return false;
}
if(it->second)
{
return it->second->deleteDirectory(sDirPath);
}
return false;
}
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::deleteFileFromDirectory(vfs::Path const& sFileName)
{
vfs::Path sDir,sFile;
sFileName.splitLast(sDir,sFile);
typename tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
// no such directory
return false;
}
if(it->second)
{
return it->second->deleteFileFromDirectory(sFile);
}
return false;
}
/**
* IVFSLocation interface
*/
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::fileExists(vfs::Path const& sFileName)
{
vfs::Path sDir, sFile;
sFileName.splitLast(sDir, sFile);
typename tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
// no such directory
return false;
}
if(it->second)
{
return it->second->fileExists(sFile);
}
return false;
}
template<typename WriteType>
vfs::IBaseFile* vfs::TDirectoryTree<WriteType>::getFile(vfs::Path const& sFileName)
{
return getFileTyped(sFileName);
}
template<typename WriteType>
typename vfs::TDirectoryTree<WriteType>::tFileType* vfs::TDirectoryTree<WriteType>::getFileTyped(vfs::Path const& sFileName)
{
vfs::Path sDir, sFile;
sFileName.splitLast(sDir, sFile);
typename tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
// no such directory
return NULL;
}
if(it->second)
{
return it->second->getFileTyped(sFile);
}
return NULL;
}
template<typename WriteType>
bool vfs::TDirectoryTree<WriteType>::createSubDirectory(vfs::Path const& sSubDirPath)
{
if(vfs::OS::createRealDirectory( this->m_realPath + sSubDirPath ))
{
if( m_catDirs[sSubDirPath] == NULL)
{
m_catDirs[sSubDirPath] = new vfs::TSubDir<WriteType>(sSubDirPath, this->m_realPath + sSubDirPath);
}
return true;
}
return false;
}
template<typename WriteType>
void vfs::TDirectoryTree<WriteType>::getSubDirList(std::list<vfs::Path>& rlSubDirs)
{
typename tDirCatalogue::iterator it = m_catDirs.begin();
for(;it != m_catDirs.end(); ++it)
{
rlSubDirs.push_back(it->first);
}
}
template<typename WriteType>
typename vfs::TDirectoryTree<WriteType>::Iterator vfs::TDirectoryTree<WriteType>::begin()
{
return Iterator(new IterImpl(*this));
}
/*****************************************************************************/
/*****************************************************************************/
template class vfs::TDirectoryTree<vfs::IWritable>; // explicit template class instantiation
template class vfs::TDirectoryTree<vfs::IWriteType>; // explicit template class instantiation
+199
View File
@@ -0,0 +1,199 @@
/*
* bfVFS : vfs/Core/Location/vfs_lib_dir.cpp
* - class for readonly (sub)directories in archives/libraries
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/Location/vfs_lib_dir.h>
#include <vfs/Tools/vfs_log.h>
class vfs::CLibDirectory::IterImpl : public vfs::IBaseLocation::Iterator::IImplementation
{
typedef vfs::IBaseLocation::Iterator::IImplementation tBaseClass;
public:
IterImpl(CLibDirectory& lib);
virtual ~IterImpl();
virtual vfs::CLibDirectory::tFileType* value();
virtual void next();
protected:
virtual tBaseClass* clone();
private:
void operator=(vfs::CLibDirectory::IterImpl const& iter);
vfs::CLibDirectory& _lib;
vfs::CLibDirectory::tFileCatalogue::iterator _iter;
};
vfs::CLibDirectory::IterImpl::IterImpl(CLibDirectory& lib)
: tBaseClass(), _lib(lib)
{
_iter = _lib.m_files.begin();
}
vfs::CLibDirectory::IterImpl::~IterImpl()
{
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::IterImpl::value()
{
if(_iter != _lib.m_files.end())
{
return _iter->second;
}
return NULL;
}
void vfs::CLibDirectory::IterImpl::next()
{
if(_iter != _lib.m_files.end())
{
_iter++;
}
}
vfs::CLibDirectory::IterImpl::tBaseClass* vfs::CLibDirectory::IterImpl::clone()
{
IterImpl* iter = new IterImpl(_lib);
iter->_iter = _iter;
return iter;
}
/***************************************************************************/
/***************************************************************************/
vfs::CLibDirectory::CLibDirectory(vfs::Path const& sLocalPath, vfs::Path const& sRealPath)
: tBaseClass(sLocalPath,sRealPath)
{
}
vfs::CLibDirectory::~CLibDirectory()
{
tFileCatalogue::iterator it = m_files.begin();
for(; it != m_files.end(); ++it)
{
// don't delete objects here
//delete it->second;
}
m_files.clear();
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::addFile(vfs::Path const& filename, bool deleteOldFile)
{
return NULL;
}
bool vfs::CLibDirectory::addFile(tFileType* file, bool deleteOldFile)
{
if(!file)
{
return false;
}
vfs::Path const& name = file->getName();
tFileType* oldFile = m_files[name];
if(oldFile && (oldFile != file) )
{
if(deleteOldFile)
{
delete oldFile;
m_files[name] = file;
}
else
{
return false;
}
}
m_files[name] = file;
return true;
}
bool vfs::CLibDirectory::deleteDirectory(vfs::Path const& dirPath)
{
//if( !(m_mountPoint == dirPath) )
//{
// return false;
//}
//if(implementsWritable())
//{
// tFileCatalogue::iterator it = m_files.begin();
// for(; it != m_files.end(); ++it)
// {
// //delete it->second;
// }
// m_files.clear();
// return true;
//}
VFS_LOG_ERROR(L"called 'deleteDirectory', 'vfs::CLibDirectory' doesn't implement the IWritable interface");
return false;
}
bool vfs::CLibDirectory::deleteFileFromDirectory(vfs::Path const& filename)
{
//if(implementsWritable())
//{
// tFileCatalogue::iterator it = m_files.find(filename);
// if(it != m_files.end())
// {
// delete it->second;
// m_files.erase(it);
// return true;
// }
//}
VFS_LOG_ERROR(L"called 'deleteFileFromDirectory', 'vfs::CLibDirectory' doesn't implement the IWritable interface");
return false;
}
bool vfs::CLibDirectory::fileExists(vfs::Path const& filename)
{
return (m_files[filename] != NULL);
}
vfs::IBaseFile* vfs::CLibDirectory::getFile(vfs::Path const& filename)
{
return getFileTyped(filename);
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::getFileTyped(vfs::Path const& filename)
{
tFileCatalogue::iterator it = m_files.find(filename);
if(it != m_files.end())
{
return it->second;
}
return NULL;
}
bool vfs::CLibDirectory::createSubDirectory(vfs::Path const& subDirPath)
{
// libraries are readonly
return false;
}
void vfs::CLibDirectory::getSubDirList(std::list<vfs::Path>& rlSubDirs)
{
// nothing
}
vfs::CLibDirectory::Iterator vfs::CLibDirectory::begin()
{
return Iterator(new IterImpl(*this));
}
/***************************************************************************/
/***************************************************************************/
@@ -0,0 +1,320 @@
/*
* bfVFS : vfs/Core/Location/vfs_uncompressed_lib_base.cpp
* - partially implements library interface for uncompressed archive files
* - initialization is done in format-specific sub-classes
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/Location/vfs_uncompressed_lib_base.h>
/********************************************************************************************/
vfs::CUncompressedLibraryBase::SFileData& vfs::CUncompressedLibraryBase::_fileDataFromHandle(tFileType* handle)
{
tFileData::iterator it = m_fileData.find(handle);
if(it != m_fileData.end())
{
return it->second;
}
VFS_THROW(L"Invalid file handle");
}
/********************************************************************************************/
/********************************************************************************************/
class vfs::CUncompressedLibraryBase::IterImpl : public vfs::IBaseLocation::Iterator::IImplementation
{
typedef vfs::IBaseLocation::Iterator::IImplementation tBaseClass;
public:
IterImpl(CUncompressedLibraryBase& lib);
virtual ~IterImpl();
virtual tFileType* value();
virtual void next();
protected:
virtual tBaseClass* clone()
{
IterImpl* iter = new IterImpl(*_lib);
iter->_iter = _iter;
return iter;
}
private:
vfs::CUncompressedLibraryBase* _lib;
vfs::CUncompressedLibraryBase::tFileData::iterator _iter;
};
vfs::CUncompressedLibraryBase::IterImpl::IterImpl(vfs::CUncompressedLibraryBase &lib)
: tBaseClass(), _lib(&lib)
{
_iter = _lib->m_fileData.begin();
}
vfs::CUncompressedLibraryBase::IterImpl::~IterImpl()
{
}
vfs::CUncompressedLibraryBase::tFileType* vfs::CUncompressedLibraryBase::IterImpl::value()
{
if(_iter != _lib->m_fileData.end())
{
return _iter->first;
}
return NULL;
}
void vfs::CUncompressedLibraryBase::IterImpl::next()
{
if(_iter != _lib->m_fileData.end())
{
_iter++;
}
}
/************************************************************************/
/************************************************************************/
vfs::CUncompressedLibraryBase::CUncompressedLibraryBase(vfs::tReadableFile *libraryFile, vfs::Path const& mountPoint, bool ownFile)
: vfs::ILibrary(libraryFile,mountPoint,ownFile), m_numberOfOpenedFiles(0)
{
}
vfs::CUncompressedLibraryBase::~CUncompressedLibraryBase()
{
this->closeLibrary();
// delete sub dirs from catalogue
tDirCatalogue::iterator it = m_dirs.begin();
for(; it != m_dirs.end(); ++it)
{
delete it->second;
}
// LibData is invalid
// just clear it, since the file handles were deleted before
m_fileData.clear();
m_dirs.clear();
}
void vfs::CUncompressedLibraryBase::closeLibrary()
{
tFileData::iterator it = m_fileData.begin();
for(; it != m_fileData .end(); ++it)
{
// what if closing of (at least) one file fails?? continue or not??
// in the end, these are not real files!
VFS_IGNOREEXCEPTION(it->first->close(), true);
}
}
bool vfs::CUncompressedLibraryBase::fileExists(vfs::Path const& filename)
{
vfs::Path sDir,sFile;
filename.splitLast(sDir,sFile);
tDirCatalogue::iterator it = m_dirs.find(sDir);
if(it != m_dirs.end())
{
return it->second->fileExists(sFile);
}
return false;
}
vfs::IBaseFile* vfs::CUncompressedLibraryBase::getFile(vfs::Path const& filename)
{
return getFileTyped(filename);
}
vfs::CUncompressedLibraryBase::tFileType* vfs::CUncompressedLibraryBase::getFileTyped(vfs::Path const& filename)
{
vfs::Path sDir,sFile;
filename.splitLast(sDir,sFile);
tDirCatalogue::iterator it = m_dirs.find(sDir);
if(it != m_dirs.end())
{
return it->second->getFileTyped(sFile);
}
return NULL;
}
void vfs::CUncompressedLibraryBase::close(tFileType *fileHandle)
{
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
// reset read position
// can't do this when opening file,
// because you could try to open a file when it is already open and would so reset the read position
file._currentReadPosition = 0;
if(m_numberOfOpenedFiles > 0)
{
m_numberOfOpenedFiles--;
if(m_numberOfOpenedFiles == 0)
{
m_libraryFile->close();
}
}
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
bool vfs::CUncompressedLibraryBase::openRead(tFileType *fileHandle)
{
try
{
_fileDataFromHandle(fileHandle);
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
m_numberOfOpenedFiles++;
if(m_numberOfOpenedFiles == 1)
{
if(!m_libraryFile->isOpenRead() && !m_libraryFile->openRead())
{
return false;
}
}
// already open
return true;
}
vfs::size_t vfs::CUncompressedLibraryBase::read(tFileType *fileHandle, vfs::Byte* data, vfs::size_t bytesToRead)
{
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
if( (file._currentReadPosition + bytesToRead) > file._fileSize )
{
bytesToRead = file._fileSize - file._currentReadPosition;
}
if(bytesToRead == 0)
{
// eof
return 0;
}
// set lib-file's read-location to match location of lib-file
m_libraryFile->setReadPosition(file._fileOffset + file._currentReadPosition, IBaseFile::SD_BEGIN);
vfs::size_t bytesRead = m_libraryFile->read(data, bytesToRead);
VFS_THROW_IFF( bytesToRead == bytesRead, L"Number of bytes doesn't match" );
file._currentReadPosition += bytesRead;
return bytesRead;
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
vfs::size_t vfs::CUncompressedLibraryBase::getReadPosition(tFileType *fileHandle)
{
try
{
return _fileDataFromHandle(fileHandle)._currentReadPosition;
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
void vfs::CUncompressedLibraryBase::setReadPosition(tFileType *fileHandle, vfs::size_t positionInBytes)
{
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
if( positionInBytes > file._fileSize )
{
positionInBytes = file._fileSize;
}
// positionInBytes is offset to file-offset
file._currentReadPosition = positionInBytes;
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
static inline vfs::offset_t clampReadPosition(vfs::offset_t const& off, vfs::size_t const& size)
{
return ( off < 0 ) ? ( 0 ) : ( (vfs::size_t)off > size ? size : off );
}
void vfs::CUncompressedLibraryBase::setReadPosition(tFileType *fileHandle, vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
if(seekDir == IBaseFile::SD_BEGIN)
{
file._currentReadPosition = clampReadPosition(offsetInBytes, file._fileSize);
}
else if(seekDir == IBaseFile::SD_CURRENT)
{
vfs::offset_t pos = file._currentReadPosition + offsetInBytes;
file._currentReadPosition = clampReadPosition(pos, file._fileSize);
}
else if(seekDir == IBaseFile::SD_END)
{
vfs::offset_t pos = file._currentReadPosition + offsetInBytes;
file._currentReadPosition = clampReadPosition(pos, file._fileSize);
}
else
{
VFS_THROW(L"Unknown seek direction");
}
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
vfs::size_t vfs::CUncompressedLibraryBase::getSize(tFileType *fileHandle)
{
try
{
return _fileDataFromHandle(fileHandle)._fileSize;
}
catch(std::exception& ex)
{
VFS_RETHROW(L"", ex);
}
}
void vfs::CUncompressedLibraryBase::getSubDirList(std::list<vfs::Path>& rlSubDirs)
{
tDirCatalogue::iterator it = m_dirs.begin();
for(;it != m_dirs.end(); ++it)
{
rlSubDirs.push_back(it->first);
}
}
vfs::CUncompressedLibraryBase::Iterator vfs::CUncompressedLibraryBase::begin()
{
return Iterator(new IterImpl(*this));
}
+80
View File
@@ -0,0 +1,80 @@
## Core
##
set(INCLUDE_Core_root
${MOD_INCLUDE}/vfs.h
${MOD_INCLUDE}/vfs_debug.h
${MOD_INCLUDE}/vfs_file_raii.h
${MOD_INCLUDE}/vfs_init.h
${MOD_INCLUDE}/vfs_os_functions.h
${MOD_INCLUDE}/vfs_path.h
${MOD_INCLUDE}/vfs_profile.h
${MOD_INCLUDE}/vfs_string.h
${MOD_INCLUDE}/vfs_types.h
${MOD_INCLUDE}/vfs_vfile.h
${MOD_INCLUDE}/vfs_vloc.h
)
set(SOURCE_Core_root
${MOD_SOURCE}/vfs.cpp
${MOD_SOURCE}/vfs_debug.cpp
${MOD_SOURCE}/vfs_file_raii.cpp
${MOD_SOURCE}/vfs_init.cpp
${MOD_SOURCE}/vfs_os_functions.cpp
${MOD_SOURCE}/vfs_path.cpp
${MOD_SOURCE}/vfs_profile.cpp
${MOD_SOURCE}/vfs_string.cpp
${MOD_SOURCE}/vfs_types.cpp
${MOD_SOURCE}/vfs_vfile.cpp
${MOD_SOURCE}/vfs_vloc.cpp
)
source_group("Core" FILES ${INCLUDE_Core_root} ${SOURCE_Core_root})
##
set(INCLUDE_Core_File
${MOD_INCLUDE}/File/vfs_buffer_file.h
${MOD_INCLUDE}/File/vfs_dir_file.h
${MOD_INCLUDE}/File/vfs_file.h
${MOD_INCLUDE}/File/vfs_lib_file.h
)
set(SOURCE_Core_File
${MOD_SOURCE}/File/vfs_buffer_file.cpp
${MOD_SOURCE}/File/vfs_dir_file.cpp
${MOD_SOURCE}/File/vfs_file.cpp
${MOD_SOURCE}/File/vfs_lib_file.cpp
)
source_group("Core\\File" FILES ${INCLUDE_Core_File} ${SOURCE_Core_File})
##
set(INCLUDE_Core_Interface
${MOD_INCLUDE}/Interface/vfs_directory_interface.h
${MOD_INCLUDE}/Interface/vfs_file_interface.h
${MOD_INCLUDE}/Interface/vfs_iterator_interface.h
${MOD_INCLUDE}/Interface/vfs_library_interface.h
${MOD_INCLUDE}/Interface/vfs_location_interface.h
)
set(SOURCE_Core_Interface
${MOD_SOURCE}/Interface/vfs_interface_members.cpp
)
source_group("Core\\Interface" FILES ${INCLUDE_Core_Interface} ${SOURCE_Core_Interface})
##
set(INCLUDE_Core_Location
${MOD_INCLUDE}/Location/vfs_directory_tree.h
${MOD_INCLUDE}/Location/vfs_lib_dir.h
${MOD_INCLUDE}/Location/vfs_uncompressed_lib_base.h
)
set(SOURCE_Core_Location
${MOD_SOURCE}/Location/vfs_directory_tree.cpp
${MOD_SOURCE}/Location/vfs_lib_dir.cpp
${MOD_SOURCE}/Location/vfs_uncompressed_lib_base.cpp
)
source_group("Core\\Location" FILES ${INCLUDE_Core_Location} ${SOURCE_Core_Location})
set(${mod}_files
${INCLUDE_Core_root} ${SOURCE_Core_root}
${INCLUDE_Core_File} ${SOURCE_Core_File}
${INCLUDE_Core_Interface} ${SOURCE_Core_Interface}
${INCLUDE_Core_Location} ${SOURCE_Core_Location}
CACHE INTERNAL ""
)
+617
View File
@@ -0,0 +1,617 @@
/*
* bfVFS : vfs/Core/vfs.cpp
* - primary interface for the using program, get files from the VFS internal storage
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_types.h>
#include <vfs/Core/vfs.h>
#include <vfs/Core/Interface/vfs_directory_interface.h>
#include <vfs/Core/Interface/vfs_file_interface.h>
#include <vfs/Core/File/vfs_file.h>
#include <vfs/Core/File/vfs_dir_file.h>
#include <vfs/Core/File/vfs_lib_file.h>
#include <vfs/Core/vfs_file_raii.h>
#include <vfs/Core/vfs_vfile.h>
#include <vfs/Tools/vfs_property_container.h>
#include <vfs/Tools/vfs_parser_tools.h>
#include <stack>
template class vfs::TIterator<vfs::tReadableFile>; // explicit instantiation
/********************************************************************/
/********************************************************************/
class vfs::CVirtualFileSystem::CRegularIterator : public vfs::CVirtualFileSystem::Iterator::IImplementation
{
friend class vfs::CVirtualFileSystem;
typedef vfs::CVirtualFileSystem::Iterator::IImplementation tBaseClass;
CRegularIterator(vfs::CVirtualFileSystem* pVFS);
public:
CRegularIterator() : tBaseClass(), m_VFS(NULL)
{};
virtual ~CRegularIterator()
{};
virtual vfs::tReadableFile* value();
virtual void next();
protected:
virtual tBaseClass* clone()
{
CRegularIterator* iter = new CRegularIterator();
iter->m_VFS = m_VFS;
iter->_vloc_iter = _vloc_iter;
iter->_vfile_iter = _vfile_iter;
return iter;
}
private:
vfs::CVirtualFileSystem* m_VFS;
vfs::CVirtualFileSystem::tVFS::iterator _vloc_iter;
vfs::CVirtualLocation::Iterator _vfile_iter;
};
vfs::CVirtualFileSystem::CRegularIterator::CRegularIterator(vfs::CVirtualFileSystem* pVFS)
: tBaseClass(), m_VFS(pVFS)
{
_vloc_iter = m_VFS->m_mapFS.begin();
if(_vloc_iter != m_VFS->m_mapFS.end())
{
_vfile_iter = _vloc_iter->second->iterate();
}
}
vfs::tReadableFile* vfs::CVirtualFileSystem::CRegularIterator::value()
{
bool bExclusiveVLoc = false;
if(_vloc_iter != m_VFS->m_mapFS.end())
{
bExclusiveVLoc = _vloc_iter->second->getIsExclusive();
}
if(!_vfile_iter.end())
{
vfs::CVirtualFile* pVFile = _vfile_iter.value();
if(pVFile)
{
if(bExclusiveVLoc)
{
return vfs::tReadableFile::cast(pVFile->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE));
}
else
{
return vfs::tReadableFile::cast(pVFile->file(vfs::CVirtualFile::SF_TOP));
}
}
}
return NULL;
}
void vfs::CVirtualFileSystem::CRegularIterator::next()
{
if(!_vfile_iter.end())
{
_vfile_iter.next();
}
while(_vfile_iter.end())
{
if(_vloc_iter != m_VFS->m_mapFS.end())
{
_vloc_iter++;
if(_vloc_iter != m_VFS->m_mapFS.end())
{
_vfile_iter = _vloc_iter->second->iterate();
}
}
else
{
return;
}
}
}
/********************************************************************/
/********************************************************************/
class vfs::CVirtualFileSystem::CMatchingIterator : public vfs::CVirtualFileSystem::Iterator::IImplementation
{
friend class vfs::CVirtualFileSystem;
typedef vfs::CVirtualFileSystem::Iterator::IImplementation tBaseClass;
CMatchingIterator(vfs::Path const& sPattern, vfs::CVirtualFileSystem* pVFS);
public:
CMatchingIterator() : tBaseClass(), m_VFS(NULL)
{};
virtual ~CMatchingIterator()
{};
virtual vfs::tReadableFile* value();
virtual void next();
protected:
virtual tBaseClass* clone()
{
CMatchingIterator* iter = new CMatchingIterator();
iter->m_sLocPattern = m_sLocPattern;
iter->m_sFilePattern = m_sFilePattern;
iter->m_VFS = m_VFS;
iter->_vloc_iter = _vloc_iter;
iter->_vfile_iter = _vfile_iter;
return iter;
}
private:
bool nextLocationMatch();
bool nextFileMatch();
private:
vfs::Path m_sLocPattern, m_sFilePattern;
vfs::CVirtualFileSystem* m_VFS;
vfs::CVirtualFileSystem::tVFS::iterator _vloc_iter;
vfs::CVirtualLocation::Iterator _vfile_iter;
};
vfs::CVirtualFileSystem::CMatchingIterator::CMatchingIterator(vfs::Path const& sPattern, vfs::CVirtualFileSystem* pVFS)
: tBaseClass(), m_VFS(pVFS)
{
if(sPattern() == vfs::Const::STAR())
{
m_sLocPattern = vfs::Path(vfs::Const::STAR());
m_sFilePattern = vfs::Path(vfs::Const::STAR());
}
else
{
sPattern.splitLast(m_sLocPattern,m_sFilePattern);
}
_vloc_iter = m_VFS->m_mapFS.begin();
while(_vloc_iter != m_VFS->m_mapFS.end())
{
if( matchPattern(m_sLocPattern(),_vloc_iter->second->cPath()) )
{
bool bExclusiveVLoc = _vloc_iter->second->getIsExclusive();
_vfile_iter = _vloc_iter->second->iterate();
while(!_vfile_iter.end())
{
vfs::IBaseFile* pFile = NULL;
if(bExclusiveVLoc)
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
else
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_TOP);
}
if(pFile)
{
vfs::Path const& filename = pFile->getName();
if( matchPattern(m_sFilePattern(),filename()) )
{
return;
}
}
_vfile_iter.next();
}
}
_vloc_iter++;
}
}
vfs::tReadableFile* vfs::CVirtualFileSystem::CMatchingIterator::value()
{
bool bExclusiveVLoc = false;
if( _vloc_iter != m_VFS->m_mapFS.end() )
{
bExclusiveVLoc = _vloc_iter->second->getIsExclusive();
}
if(!_vfile_iter.end())
{
vfs::CVirtualFile* pVFile = _vfile_iter.value();
if(pVFile)
{
vfs::IBaseFile* pFile = NULL;
if(bExclusiveVLoc)
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
else
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_TOP);
}
return vfs::tReadableFile::cast(pFile);
}
}
return NULL;
}
bool vfs::CVirtualFileSystem::CMatchingIterator::nextLocationMatch()
{
while(_vloc_iter != m_VFS->m_mapFS.end())
{
_vloc_iter++;
if(_vloc_iter != m_VFS->m_mapFS.end())
{
if(matchPattern(m_sLocPattern(),_vloc_iter->second->cPath()))
{
return true;
}
}
}
return false;
}
bool vfs::CVirtualFileSystem::CMatchingIterator::nextFileMatch()
{
bool bExclusiveVLoc = false;
if( _vloc_iter != m_VFS->m_mapFS.end() )
{
bExclusiveVLoc = _vloc_iter->second->getIsExclusive();
}
while(!_vfile_iter.end())
{
_vfile_iter.next();
if(!_vfile_iter.end())
{
vfs::IBaseFile* pFile = NULL;
if(bExclusiveVLoc)
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
else
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_TOP);
}
if(pFile)
{
vfs::Path const& filename = pFile->getName();
if(matchPattern(m_sFilePattern(),filename()))
{
return true;
}
}
}
}
return false;
}
void vfs::CVirtualFileSystem::CMatchingIterator::next()
{
if(nextFileMatch())
{
return;
}
while(nextLocationMatch())
{
_vfile_iter = _vloc_iter->second->iterate();
if(!_vfile_iter.end())
{
bool bExclusiveVLoc = _vloc_iter->second->getIsExclusive();
vfs::IBaseFile* pFile = NULL;
if(bExclusiveVLoc)
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
else
{
pFile = _vfile_iter.value()->file(vfs::CVirtualFile::SF_TOP);
}
if(pFile && matchPattern(m_sFilePattern(),pFile->getName()()))
{
return;
}
else if(nextFileMatch())
{
return;
}
}
}
}
/********************************************************************/
/********************************************************************/
bool vfs::canWrite()
{
vfs::CVirtualProfile *prof = getVFS()->getProfileStack()->topProfile();
return prof && prof->cWritable;
}
vfs::CVirtualFileSystem* getVFS()
{
return vfs::CVirtualFileSystem::getVFS();
}
vfs::CVirtualFileSystem* vfs::CVirtualFileSystem::m_pSingleton = NULL;
vfs::CVirtualFileSystem* vfs::CVirtualFileSystem::getVFS()
{
if(!m_pSingleton)
{
m_pSingleton = new CVirtualFileSystem();
}
return m_pSingleton;
}
void vfs::CVirtualFileSystem::shutdownVFS()
{
if(m_pSingleton)
{
delete m_pSingleton;
m_pSingleton = NULL;
}
}
vfs::CVirtualFileSystem::CVirtualFileSystem()
{
}
vfs::CVirtualFileSystem::~CVirtualFileSystem()
{
tVFS::iterator it = m_mapFS.begin();
for(; it != m_mapFS.end(); ++it)
{
delete it->second;
}
m_mapFS.clear();
}
vfs::CProfileStack* vfs::CVirtualFileSystem::getProfileStack()
{
return &m_oProfileStack;
}
vfs::CVirtualFileSystem::Iterator vfs::CVirtualFileSystem::begin()
{
return Iterator(new vfs::CVirtualFileSystem::CRegularIterator(this));
}
vfs::CVirtualFileSystem::Iterator vfs::CVirtualFileSystem::begin(vfs::Path const& sPattern)
{
return Iterator(new vfs::CVirtualFileSystem::CMatchingIterator(sPattern,this));
}
bool vfs::CVirtualFileSystem::addLocation(vfs::IBaseLocation* pLocation, vfs::CVirtualProfile *pProfile)
{
VFS_THROW_IFF(pLocation != NULL, L"Invalid location object");
VFS_THROW_IFF(pProfile!= NULL, L"Invalid location object");
std::list<vfs::Path> lSubDirs;
pLocation->getSubDirList(lSubDirs);
std::list<vfs::Path>::const_iterator sd_cit = lSubDirs.begin();
for(;sd_cit != lSubDirs.end(); ++sd_cit)
{
this->getVirtualLocation(*sd_cit, true);
}
vfs::IBaseLocation::Iterator it = pLocation->begin();
for(; !it.end(); it.next())
{
vfs::IBaseFile *pFile = it.value();
vfs::Path const& sPath = pFile->getPath();
vfs::Path dir,file;
sPath.splitLast(dir,file);
CVirtualLocation* pLoc = this->getVirtualLocation(dir,true);
pLoc->addFile(pFile, pProfile->cName);
}
return true;
}
vfs::tReadableFile* vfs::CVirtualFileSystem::getReadFile(vfs::Path const& rLocalFilePath, vfs::CVirtualFile::ESearchFile eSF)
{
return vfs::tReadableFile::cast(this->getFile(rLocalFilePath,eSF));
}
vfs::tWritableFile* vfs::CVirtualFileSystem::getWriteFile(vfs::Path const& rLocalFilePath, vfs::CVirtualFile::ESearchFile eSF)
{
return vfs::tWritableFile::cast(this->getFile(rLocalFilePath,eSF));
}
vfs::IBaseFile* vfs::CVirtualFileSystem::getFile(vfs::Path const& rLocalFilePath, vfs::CVirtualFile::ESearchFile eSF)
{
VFS_LOG_DEBUG( (L"Get file : " + rLocalFilePath()).c_str() );
vfs::Path sDir,sFile;
rLocalFilePath.splitLast(sDir,sFile);
vfs::CVirtualLocation* pVLoc = this->getVirtualLocation(sDir);
if(pVLoc)
{
vfs::CVirtualFile *pVFile = pVLoc->getVirtualFile(sFile);
if(pVFile)
{
if(pVLoc->getIsExclusive())
{
return pVFile->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
return pVFile->file(eSF);
}
}
VFS_LOG_DEBUG( _BS(L"Could not find file : ") << rLocalFilePath << _BS::wget );
return NULL;
}
vfs::tReadableFile* vfs::CVirtualFileSystem::getReadFile(vfs::Path const& rLocalFilePath, vfs::String const& sProfileName)
{
return vfs::tReadableFile::cast(this->getFile(rLocalFilePath, sProfileName));
}
vfs::tWritableFile* vfs::CVirtualFileSystem::getWriteFile(vfs::Path const& rLocalFilePath, vfs::String const& sProfileName)
{
return vfs::tWritableFile::cast(this->getFile(rLocalFilePath, sProfileName));
}
vfs::IBaseFile* vfs::CVirtualFileSystem::getFile(vfs::Path const& rLocalFilePath, vfs::String const& sProfileName)
{
vfs::Path sDir,sFile;
rLocalFilePath.splitLast(sDir,sFile);
tVFS::iterator it_loc = m_mapFS.find(sDir);
if(it_loc == m_mapFS.end())
{
return NULL;
}
vfs::CVirtualLocation* pVLoc = it_loc->second;
if(pVLoc)
{
return pVLoc->getFile(sFile,sProfileName);
}
return NULL;
}
bool vfs::CVirtualFileSystem::fileExists(vfs::Path const& rLocalFilePath, std::string const& sProfileName)
{
return this->getFile(rLocalFilePath, sProfileName) != NULL;
}
bool vfs::CVirtualFileSystem::fileExists(vfs::Path const& rLocalFilePath, vfs::CVirtualFile::ESearchFile eSF)
{
return this->getFile(rLocalFilePath, eSF) != NULL;
}
vfs::CVirtualLocation* vfs::CVirtualFileSystem::getVirtualLocation(vfs::Path const& sPath, bool bCreate)
{
tVFS::iterator it = m_mapFS.find(sPath);
if(it == m_mapFS.end())
{
if(bCreate)
{
vfs::CVirtualLocation* pVLoc = new vfs::CVirtualLocation(sPath);
m_mapFS.insert(std::make_pair(sPath,pVLoc));
return pVLoc;
}
return NULL;
}
return it->second;
}
bool vfs::CVirtualFileSystem::removeDirectoryFromFS(vfs::Path const& sDir)
{
vfs::Path pattern = sDir + "*";
std::list<vfs::Path> files;
Iterator it = this->begin(pattern);
for(; !it.end(); it.next())
{
if(it.value()->implementsWritable())
{
files.push_back(it.value()->getPath());
}
}
bool success = true;
std::list<vfs::Path>::iterator fit = files.begin();
for(; fit != files.end(); ++fit)
{
success &= this->removeFileFromFS(*fit);
}
return success;
}
bool vfs::CVirtualFileSystem::removeFileFromFS(vfs::Path const& sFilePath)
{
vfs::Path sPath,sFile;
sFilePath.splitLast(sPath,sFile);
vfs::CVirtualProfile *pProf = m_oProfileStack.getWriteProfile();
if(pProf)
{
vfs::IBaseLocation *pBL = pProf->getLocation(sPath);
if(pBL && pBL->implementsWritable())
{
vfs::TDirectory<vfs::IWritable> *pDir = dynamic_cast<vfs::TDirectory<vfs::IWritable>*>(pBL);
if(pDir)
{
bool bSuccess = false;
// remove file from virtual structures first
vfs::IBaseFile* file = pDir->getFile(sFilePath);
if(file)
{
vfs::Path sDir,sFile;
sFilePath.splitLast(sDir,sFile);
vfs::CVirtualLocation *pVLoc = this->getVirtualLocation(sDir);
if(pVLoc)
{
bSuccess = pVLoc->removeFile(file);
}
}
// delete actual file
return bSuccess && pDir->deleteFileFromDirectory(sFilePath);
}
}
}
return false;
}
bool vfs::CVirtualFileSystem::createNewFile(vfs::Path const& sFilename)
{
vfs::Path sPath,sFile;
sFilename.splitLast(sPath,sFile);
vfs::CVirtualProfile *pProf = m_oProfileStack.getWriteProfile();
if(pProf)
{
bool bIsExclusive = false;
bool bNewLocation = false;
vfs::IBaseLocation *pProfLoc = pProf->getLocation(sPath);
if(!pProfLoc)
{
// try to find closest match
vfs::Path sTemp, sCreateDir, sRight, sLeft = sPath;
while(!pProfLoc && !sLeft.empty())
{
sLeft.splitLast(sTemp,sRight);
sLeft = sTemp;
sCreateDir = sRight + sCreateDir;
pProfLoc = pProf->getLocation(sLeft);
}
// see if the closest match is exclusive
// if yes, then the the new path is a subdirectory and has to be exclusive too
vfs::CVirtualLocation *pVLoc = this->getVirtualLocation(sLeft,true);
if(pVLoc)
{
bIsExclusive = pVLoc->getIsExclusive();
}
//else
//{
// VFS_THROW(L"location (closest match) should exist");
//}
bNewLocation = true;
}
if(pProfLoc && pProfLoc->implementsWritable())
{
// create file and add to location
vfs::TDirectory<vfs::IWritable> *pDir = dynamic_cast<vfs::TDirectory<vfs::IWritable>*>(pProfLoc);
vfs::IBaseFile* pFile = pDir->addFile(sFilename);
if(bNewLocation)
{
pProf->addLocation(pProfLoc);
}
if(pFile)
{
CVirtualLocation* pLoc = this->getVirtualLocation(sPath,true);
if(bIsExclusive)
{
pLoc->setIsExclusive(bIsExclusive);
}
pLoc->addFile(pFile,pProf->cName);
return true;
}
}
}
// throw ?
return false;
}
/************************************************************************************************/
+249
View File
@@ -0,0 +1,249 @@
/*
* bfVFS : vfs/Core/vfs_dfebug.cpp
* - Exception class and throw macros, used to notify the using program of unexpected situations
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_debug.h>
#include <vfs/Core/vfs_string.h>
#include <vfs/Core/vfs_file_raii.h>
#include <vfs/Core/File/vfs_file.h>
#include <vfs/Core/vfs_os_functions.h>
#include <vfs/Tools/vfs_log.h>
#include <sstream>
#include <ctime>
vfs::Exception::Exception(vfs::String const& text, vfs::String const& function, int line, const char* file)
: std::exception() //(text.utf8().c_str())
{
VFS_LOG_ERROR( text.c_str() );
time_t rawtime;
time ( &rawtime );
std::string datetime(ctime(&rawtime));
SEntry en;
en.message = text;
en.line = line;
en.file = file;
en.function = function;
VFS_IGNOREEXCEPTION(en.time = vfs::String(datetime.substr(0,datetime.length()-1)), false);
m_CallStack.push_back(en);
};
vfs::Exception::Exception(vfs::String const& text, vfs::String const& function, int line, const char* file, std::exception& ex)
: std::exception() //(text.utf8().c_str())
{
VFS_LOG_ERROR( text.c_str() );
if(dynamic_cast<vfs::Exception*>(&ex))
{
vfs::Exception& vfs_ex = *static_cast<vfs::Exception*>(&ex);
m_CallStack.insert(m_CallStack.end(), vfs_ex.m_CallStack.begin(), vfs_ex.m_CallStack.end());
}
else
{
SEntry en;
en.line = -1;
en.file = "";
en.function = "";
en.time = "";
VFS_IGNOREEXCEPTION( en.message = vfs::String(ex.what()), false );
m_CallStack.push_back(en);
}
time_t rawtime;
time ( &rawtime );
std::string datetime(ctime(&rawtime));
SEntry en;
en.message = text;
en.line = line;
en.file = file;
en.function = function;
VFS_IGNOREEXCEPTION(en.time = vfs::String(datetime.substr(0,datetime.length()-1)), false);
m_CallStack.push_back(en);
};
vfs::Exception::~Exception() throw()
{
}
vfs::String vfs::Exception::getLastEntryString() const
{
if(!m_CallStack.empty())
{
CALLSTACK::const_reverse_iterator rit = m_CallStack.rbegin();
std::wstringstream wss;
wss << rit->file.c_wcs()
<< L" (l. "
<< rit->line
<< ") : ["
<< rit->function.c_wcs()
<< L"] - "
<< rit->message.c_wcs();
return wss.str();
}
return "";
}
const char* vfs::Exception::what() const throw()
{
static std::string msg;
msg = "";
if(!m_CallStack.empty())
{
//msg = m_CallStack.front().message.utf8();
msg = this->getExceptionString().utf8();
}
return msg.c_str();
}
vfs::String vfs::Exception::getExceptionString() const
{
if(!m_CallStack.empty())
{
std::wstringstream wss;
CALLSTACK::const_reverse_iterator rit = m_CallStack.rbegin();
for(; rit != m_CallStack.rend(); ++rit)
{
wss << L"========== " << rit->time << L" ==========\r\n";
wss << L"File : " << rit->file << L"\r\n";
wss << L"Line : " << rit->line << L"\r\n";
wss << L"Location : " << rit->function << L"\r\n\r\n";
wss << L" " << rit->message << L"\r\n";
}
return wss.str();
}
return L"";
}
//
//void vfs::Exception::writeFile(vfs::Path const& sPath)
//{
// try
// {
// vfs::COpenWriteFile oFile(sPath,true,true);
// vfs::String s = this->getExceptionString();
// oFile->write(s.utf8().c_str(), s.length());
// oFile->close();
// }
// catch(vfs::Exception &ex)
// {
// vfs::Exception ex2(L"Could not write exception file into VFS",
// _FUNCTION_FORMAT_,__LINE__,__FILE__, ex);
// vfs::Exception out("Writing exception to disc failed : is there no writable profile?",
// _FUNCTION_FORMAT_,__LINE__,__FILE__, *this);
//
// out.m_CallStack.insert(out.m_CallStack.begin(), ex.m_CallStack.begin(), ex.m_CallStack.end());
//
// vfs::Path sDir,sFile;
// sPath.splitLast(sDir,sFile);
// vfs::CFile oFile(sFile);
//
// try
// {
// // can also fail, but there is only so much we can do
// vfs::COpenWriteFile file( vfs::tWritableFile::cast(&oFile) );
// vfs::String s = out.getExceptionString();
// file->write(s.utf8().c_str(), s.length());
// }
// catch(std::exception &fex)
// {
// VFS_RETHROW(L"Could write exception file at all",fex);
// }
// }
//}
//
//
//void logException(vfs::Exception const& ex)
//{
// static bool is_logging = false;
// if(is_logging)
// {
// // drop message
// return;
// }
// //////////////////
// is_logging = true;
// //////////////////
//
// static vfs::Log& exlog = *vfs::Log::create(L"game_exceptions.log", false, vfs::Log::FLUSH_IMMEDIATELY);
// try
// {
// exlog << ">>>>>>>>>>>>>>>>>>>>>" << vfs::Log::endl;
// exlog << ex.getExceptionString();
// exlog << "<<<<<<<<<<<<<<<<<<<<<" << vfs::Log::endl << vfs::Log::endl;
// }
// catch(...)
// {
// // don't throw at all
// }
//
// //////////////////
// is_logging = false;
// //////////////////
//}
//
//void logException(std::exception const& ex)
//{
// logException(ex.what());
//}
//
//void logException(const wchar_t* ex)
//{
// logException(vfs::String::as_utf8(ex).c_str());
//}
//
//void logException(const char* ex)
//{
// static bool is_logging = false;
// if(is_logging)
// {
// // drop message
// return;
// }
// //////////////////
// is_logging = true;
// //////////////////
//
// static vfs::Log& exlog = *vfs::Log::create(L"game_exceptions.log", false, vfs::Log::FLUSH_IMMEDIATELY);
// try
// {
// exlog << ">>>>>>>>>>>>>>>>>>>>>" << vfs::Log::endl;
// exlog << ex;
// exlog << "<<<<<<<<<<<<<<<<<<<<<" << vfs::Log::endl << vfs::Log::endl;
// }
// catch(...)
// {
// // don't throw at all
// }
//
// //////////////////
// is_logging = false;
// //////////////////}
//}
+144
View File
@@ -0,0 +1,144 @@
/*
* bfVFS : vfs/Core/vfs_file_raii.cpp
* - RAII classes to open files for reading/writing
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_file_raii.h>
#include <vfs/Core/vfs.h>
#include <sstream>
/********************************************************************************************/
/********************************************************************************************/
vfs::COpenReadFile::COpenReadFile(vfs::Path const& sPath, vfs::CVirtualFile::ESearchFile eSF)
{
vfs::IBaseFile *pFile = getVFS()->getFile(sPath,eSF);
VFS_THROW_IFF(pFile, _BS(L"file \"") << sPath << L"\" does not exist" << _BS::wget);
m_pFile = vfs::tReadableFile::cast(pFile);
VFS_THROW_IFF(m_pFile, _BS(L"File \"") << sPath << L"\" is not readable" << _BS::wget);
VFS_THROW_IFF(m_pFile->openRead(), _BS(L"Could not open file : ") << m_pFile->getPath() << _BS::wget);
}
vfs::COpenReadFile::COpenReadFile(vfs::tReadableFile *pFile)
{
try
{
m_pFile = pFile;
VFS_THROW_IFF(m_pFile, L"Invalid file object");
VFS_THROW_IFF(m_pFile->openRead(), _BS(L"Could not open file : ") << pFile->getPath() << _BS::wget);
}
catch(std::exception &ex)
{
VFS_RETHROW(L"",ex);
}
}
vfs::COpenReadFile::~COpenReadFile()
{
if(m_pFile)
{
m_pFile->close();
m_pFile = NULL;
}
}
vfs::tReadableFile* vfs::COpenReadFile::operator->()
{
return m_pFile;
}
vfs::tReadableFile& vfs::COpenReadFile::file()
{
return *m_pFile;
}
void vfs::COpenReadFile::release()
{
m_pFile = NULL;
}
/**************************************************************************/
vfs::COpenWriteFile::COpenWriteFile(vfs::Path const& sPath,
bool bCreate,
bool bTruncate,
vfs::CVirtualFile::ESearchFile eSF)
{
vfs::IBaseFile *pFile = getVFS()->getFile(sPath,eSF);
if(!pFile && bCreate)
{
if(getVFS()->createNewFile(sPath))
{
pFile = getVFS()->getFile(sPath,eSF);
}
else
{
VFS_THROW(_BS(L"Could not create VFS file \"") << sPath << L"\"" << _BS::wget);
}
}
VFS_THROW_IFF(pFile, _BS(L"File \"") << sPath << L"\" not found" << _BS::wget);
m_pFile = vfs::tWritableFile::cast(pFile);
VFS_THROW_IFF(m_pFile, _BS(L"File \"") << sPath << L"\" exists, but is not writable" << _BS::wget);
VFS_THROW_IFF(m_pFile->openWrite(bCreate,bTruncate), _BS(L"File \"") << sPath << L"\" could not be opened for writing" << _BS::wget);
}
vfs::COpenWriteFile::COpenWriteFile(vfs::tWritableFile *pFile)
{
try
{
m_pFile = pFile;
VFS_THROW_IFF(m_pFile, L"Invalid file object");
VFS_THROW_IFF(m_pFile->openWrite(true,false), _BS(L"Could not open file : ") << m_pFile->getPath() << _BS::wget);
}
catch(std::exception& ex)
{
VFS_RETHROW(L"",ex);
};
}
vfs::COpenWriteFile::~COpenWriteFile()
{
if(m_pFile)
{
m_pFile->close();
m_pFile = NULL;
}
}
vfs::tWritableFile* vfs::COpenWriteFile::operator->()
{
return m_pFile;
}
vfs::tWritableFile& vfs::COpenWriteFile::file()
{
return *m_pFile;
}
void vfs::COpenWriteFile::release()
{
m_pFile = NULL;
}
+351
View File
@@ -0,0 +1,351 @@
/*
* bfVFS : vfs/Core/vfs_init.cpp
* - initialization functions/classes
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs.h>
#include <vfs/Core/vfs_init.h>
#include <vfs/Core/File/vfs_file.h>
#include <vfs/Core/File/vfs_buffer_file.h>
#include <vfs/Core/Location/vfs_directory_tree.h>
#include <vfs/Core/Interface/vfs_library_interface.h>
#ifdef VFS_WITH_SLF
# include <vfs/Ext/slf/vfs_slf_library.h>
#endif
#ifdef VFS_WITH_7ZIP
# include <vfs/Ext/7z/vfs_7z_library.h>
# include <vfs/Ext/7z/vfs_create_7z_library.h>
#endif
#include <vfs/Tools/vfs_property_container.h>
#include <vfs/Tools/vfs_log.h>
#include <vfs/Aspects/vfs_logging.h>
#include <vfs/Aspects/vfs_settings.h>
/////////////////////////////////
vfs_init::Location::Location()
: m_optional(false)
{
};
/////////////////////////////////
vfs_init::Profile::Profile()
: m_writable(false)
{
};
vfs_init::Profile::~Profile()
{
t_locations::iterator it = locations.begin();
for(;it != locations.end(); ++it)
{
if(it->first) delete it->second;
}
locations.clear();
}
void vfs_init::Profile::addLocation(Location* loc, bool own)
{
locations.push_back(std::make_pair(own,loc));
}
/////////////////////////////////
vfs_init::VfsConfig::~VfsConfig()
{
t_profiles::iterator it = profiles.begin();
for(;it != profiles.end(); ++it)
{
if(it->first) delete it->second;
}
}
void vfs_init::VfsConfig::addProfile(Profile* prof, bool own)
{
profiles.push_back(std::make_pair(own,prof));
}
void vfs_init::VfsConfig::appendConfig(VfsConfig& conf)
{
VfsConfig::t_profiles::iterator it = conf.profiles.begin();
for(; it != conf.profiles.end(); ++it)
{
// even if the other object owns its profiles, this one does not
profiles.push_back(std::make_pair(false,it->second));
}
}
/********************************************************************/
/********************************************************************/
bool vfs_init::initVirtualFileSystem(vfs::Path const& vfs_ini)
{
std::list<vfs::Path> li;
li.push_back(vfs_ini);
return initVirtualFileSystem(li);
}
bool vfs_init::initVirtualFileSystem(std::list<vfs::Path> const& vfs_ini_list)
{
vfs::PropertyContainer oVFSProps;
std::list<vfs::Path>::const_iterator clit = vfs_ini_list.begin();
for(; clit != vfs_ini_list.end(); ++clit)
{
oVFSProps.initFromIniFile(*clit);
}
return initVirtualFileSystem(oVFSProps);
}
bool vfs_init::initVirtualFileSystem(vfs::PropertyContainer& oVFSProps)
{
VFS_LOG_INFO(L"Processing VFS configuration");
vfs_init::VfsConfig conf;
std::list<vfs::String> lProfiles, lLocSections;
oVFSProps.getStringListProperty(L"vfs_config",L"PROFILES",lProfiles,L"");
if(lProfiles.empty())
{
VFS_LOG_ERROR(L"no profiles specified");
return false;
}
std::list<vfs::String>::const_iterator prof_cit = lProfiles.begin();
for(; prof_cit != lProfiles.end(); ++prof_cit)
{
vfs::String sProfSection = vfs::String("PROFILE_") + vfs::String(*prof_cit);
vfs_init::Profile *prof = new vfs_init::Profile();
prof->m_name = oVFSProps.getStringProperty(sProfSection,L"NAME",L"");
prof->m_root = oVFSProps.getStringProperty(sProfSection,L"PROFILE_ROOT",L"");
prof->m_writable = oVFSProps.getBoolProperty(sProfSection,L"WRITE",false);
lLocSections.clear();
oVFSProps.getStringListProperty(sProfSection,L"LOCATIONS",lLocSections,L"");
std::list<vfs::String>::iterator loc_it = lLocSections.begin();
for(; loc_it != lLocSections.end(); ++loc_it)
{
vfs::String sLocSection = vfs::String("LOC_") + vfs::String(*loc_it);
vfs_init::Location *loc = new vfs_init::Location();
loc->m_path = oVFSProps.getStringProperty(sLocSection,L"PATH",L"");
loc->m_vfs_path = oVFSProps.getStringProperty(sLocSection,L"VFS_PATH",L"");
loc->m_mount_point = oVFSProps.getStringProperty(sLocSection,L"MOUNT_POINT",L"");
loc->m_type = oVFSProps.getStringProperty(sLocSection,L"TYPE",L"NOT_FOUND");
loc->m_optional = oVFSProps.getBoolProperty(sLocSection,L"OPTIONAL",false);
prof->addLocation(loc,true);
}
conf.addProfile(prof,true);
}
return initVirtualFileSystem(conf);
}
bool vfs_init::initWriteProfile(vfs::CVirtualProfile &rProf)
{
typedef vfs::TDirectory<vfs::IWritable> tWDir;
tWDir *pDir = NULL;
vfs::IBaseLocation *pLoc = rProf.getLocation(vfs::Path(vfs::Const::EMPTY()));
if(pLoc)
{
pDir = dynamic_cast<tWDir*>(pLoc);
}
else
{
VFS_LOG_WARNING(_BS(L"Could not find location (\"\") for profile '") << rProf.cName << L"'" << _BS::wget );
VFS_LOG_WARNING(_BS(L"Trying to initialize profile root : ") << rProf.cRoot << _BS::wget );
vfs::CDirectoryTree *pDirTree = NULL;
pDirTree = new vfs::CDirectoryTree(vfs::Path(vfs::Const::EMPTY()),rProf.cRoot);
if(!pDirTree->init())
{
return false;
}
VFS_TRYCATCH_RETHROW( rProf.addLocation(pDirTree), L"" );
getVFS()->addLocation(pDirTree, &rProf);
pDir = pDirTree;
}
return pDir != NULL;
}
bool vfs_init::initVirtualFileSystem(vfs_init::VfsConfig const& conf)
{
VFS_LOG_INFO(L"Initializing Virtual File System");
vfs::CVirtualFileSystem *pVFS = getVFS();
if(conf.profiles.empty())
{
return false;
}
vfs_init::VfsConfig::t_profiles::const_iterator prof_it = conf.profiles.begin();
for(; prof_it != conf.profiles.end(); ++prof_it)
{
vfs_init::Profile* prof = prof_it->second;
VFS_LOG_INFO(_BS(L" Reading profile : ") << prof->m_name << _BS::wget);
vfs::Path profileRoot = prof->m_root;
bool bIsWritable = prof->m_writable;
vfs::CProfileStack *pPS = pVFS->getProfileStack();
vfs::CVirtualProfile *pProf = pPS->getProfile(prof->m_name);
if(!pProf)
{
pProf = new vfs::CVirtualProfile(prof->m_name, profileRoot, bIsWritable);
pPS->pushProfile(pProf);
}
else
{
VFS_THROW_IFF(pProf->cWritable == bIsWritable, L"profile already exists, but their write properties differ");
VFS_THROW_IFF(pProf->cRoot == profileRoot, L"profile already exists, but their root directories differ");
continue;
}
vfs_init::Profile::t_locations::iterator loc_it = prof->locations.begin();
loc_it = prof->locations.begin();
for(; loc_it != prof->locations.end(); ++loc_it)
{
vfs_init::Location *loc = loc_it->second;
bool bOptional = loc->m_optional;
if(vfs::StrCmp::Equal(loc->m_type,L"LIBRARY"))
{
vfs::tReadableFile *pLibFile = NULL;
bool bOwnFile = false;
vfs::Path fullpath = profileRoot + loc->m_path;
VFS_LOG_INFO( _BS(L" library : \"") << fullpath << L"\"" << _BS::wget );
if(!loc->m_path.empty())
{
// try regular file
pLibFile = vfs::tReadableFile::cast( new vfs::CFile(fullpath) );
bOwnFile = true;
}
if(!pLibFile && !loc->m_vfs_path.empty())
{
// if regular file doesn't exist, try to find it in the (partially initialized) VFS
pLibFile = pVFS->getReadFile(profileRoot + loc->m_vfs_path);
}
if(pLibFile)
{
vfs::String full_str = pLibFile->getName()();
vfs::String ext = full_str.c_wcs().substr(full_str.length()-3,3);
vfs::ILibrary *pLib = NULL;
if(vfs::StrCmp::Equal(ext,L"slf"))
{
#ifdef VFS_WITH_SLF
pLib = new vfs::CSLFLibrary( pLibFile, loc->m_mount_point );
#else
VFS_LOG_ERROR(L"Trying to init slf library : SLF support disabled");
continue;
#endif
}
else if(vfs::StrCmp::Equal(ext,L".7z"))
{
#ifdef VFS_WITH_7ZIP
pLib = new vfs::CUncompressed7zLibrary( pLibFile, loc->m_mount_point );
#else
VFS_LOG_ERROR(L"Trying to init 7z library : 7zip support disabled");
continue;
#endif
}
else
{
VFS_THROW(_BS(L"File [") << loc->m_path << L"] in not an SLF or 7z library" << _BS::wget);
}
if(!pLib->init())
{
if(!bOptional)
{
VFS_THROW(_BS(L"Could not initialize library [ ") << loc->m_path << L" ]" <<
L" in : profile [ " << prof->m_name << L" ]," <<
L" path [ " << fullpath << L" ]" << _BS::wget);
}
}
else
{
pProf->addLocation(pLib);
pVFS->addLocation(vfs::tReadLocation::cast(pLib), pProf);
}
}
else
{
VFS_THROW(_BS(L"File not found : ") << loc->m_path << _BS::wget);
}
}
else if(vfs::StrCmp::Equal(loc->m_type,L"DIRECTORY"))
{
vfs::Path fullpath = profileRoot + loc->m_path;
VFS_LOG_INFO( _BS(L" directory : \"") << fullpath << L"\"" << _BS::wget );
vfs::IBaseLocation *pDirLocation = NULL;
bool init_success = false;
if(bIsWritable)
{
vfs::CDirectoryTree *pDirTree = new vfs::CDirectoryTree(loc->m_mount_point, fullpath);
init_success = pDirTree->init();
pDirLocation = pDirTree;
}
else
{
vfs::CReadOnlyDirectoryTree *pDirTree = new vfs::CReadOnlyDirectoryTree(loc->m_mount_point, fullpath);
init_success = pDirTree->init();
pDirLocation = pDirTree;
}
if(!init_success)
{
VFS_THROW(_BS(L"Could not initialize directory [\"") << loc->m_path << L"\"]" <<
L" in : profile [\"" << prof->m_name << L"\"]," <<
L" path [\"" << fullpath << L"\"]" << _BS::wget);
}
else
{
pProf->addLocation(pDirLocation);
pVFS->addLocation(pDirLocation, pProf);
}
}
}
if(bIsWritable)
{
vfs::CProfileStack *pPS = pVFS->getProfileStack();
vfs::CVirtualProfile *pProf = pPS->getProfile(prof->m_name);
if(!pProf)
{
pProf = new vfs::CVirtualProfile(prof->m_name,profileRoot,true);
pPS->pushProfile(pProf);
}
else if(!pProf->cWritable)
{
VFS_THROW(_BS(L"Profile [") << prof->m_name << L"] is supposed to be writable!" << _BS::wget);
}
initWriteProfile(*pProf);
}
}
return true;
}
+402
View File
@@ -0,0 +1,402 @@
/*
* bfVFS : vfs/Core/os_functions.cpp
* - abstractions for OS dependant code
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_os_functions.h>
#include <vfs/Core/vfs_debug.h>
#include <vfs/Core/vfs_string.h>
#include <vfs/Tools/vfs_log.h>
#include <vfs/Aspects/vfs_settings.h>
#include <cstdlib>
#include <cstring>
#include <sstream>
#ifndef WIN32
# include "errno.h"
# include "sys/stat.h"
#endif
vfs::OS::CIterateDirectory::CIterateDirectory(vfs::Path const& sPath, vfs::String const& searchPattern)
{
#ifdef WIN32
if(!vfs::Settings::getUseUnicode())
{
std::string s;
vfs::String::narrow((sPath+searchPattern).c_wcs(), s);
fSearchHandle = FindFirstFileA(s.c_str(), &fFileInfoA);
}
else
{
fSearchHandle = FindFirstFileW((sPath+searchPattern).c_wcs().c_str(), &fFileInfoW);
}
if (fSearchHandle == INVALID_HANDLE_VALUE)
{
DWORD error = GetLastError();
VFS_THROW(_BS(L"Error accessing path [") << (sPath+searchPattern) << L"], error code : " << error << _BS::wget);
}
#else
count = scandir(vfs::String::as_utf8(sPath()).c_str(),&files,NULL,NULL);
if(count == -1)
{
vfs::String err = strerror(errno);
VFS_THROW(err);
}
current_pos = 0;
#endif
fFirstRequest = true;
}
vfs::OS::CIterateDirectory::~CIterateDirectory()
{
#ifdef WIN32
FindClose(fSearchHandle);
#else
#endif
}
bool vfs::OS::CIterateDirectory::nextFile(vfs::String &fileName, CIterateDirectory::EFileAttribute &attrib)
{
#ifdef WIN32
VFS_THROW_IFF(fSearchHandle != INVALID_HANDLE_VALUE, L"Invalid Handle Value");
if (fFirstRequest)
{
fFirstRequest = false;
}
//else
{
if(!vfs::Settings::getUseUnicode())
{
if( !FindNextFileA(fSearchHandle, &fFileInfoA) )
{
return false;
}
fileName.r_wcs().assign( vfs::String::widen( fFileInfoA.cFileName, strlen(fFileInfoA.cFileName) ) );
attrib = (fFileInfoA.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE;
}
else
{
if ( !FindNextFileW(fSearchHandle, &fFileInfoW) )
{
return false;
}
fileName.r_wcs().assign(fFileInfoW.cFileName);
attrib = (fFileInfoW.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE;
}
}
return true;
#else
if(current_pos < count)
{
struct dirent* entry = files[current_pos];
fileName = vfs::String(entry->d_name);
attrib = (entry->d_type == DT_DIR) ? CIterateDirectory::FA_DIRECTORY : CIterateDirectory::FA_FILE;
current_pos++;
return true;
}
return false;
#endif
}
bool vfs::OS::checkRealDirectory(vfs::Path const& sDir)
{
#ifdef WIN32
bool bDirExists = false;
if(!vfs::Settings::getUseUnicode())
{
WIN32_FIND_DATAA fd;
memset(&fd,0,sizeof(WIN32_FIND_DATAA));
std::string s;
vfs::String::narrow(sDir.c_wcs(), s);
HANDLE hFile = FindFirstFileA( s.c_str(), &fd);
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD error = GetLastError();
VFS_LOG_ERROR(_BS(L"Directory [") << sDir << L"] does not exist : " << error << _BS::wget);
}
bDirExists = (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
FindClose(hFile);
}
else
{
WIN32_FIND_DATAW fd;
memset(&fd,0,sizeof(WIN32_FIND_DATAW));
HANDLE hFile = FindFirstFileW( sDir.c_str(), &fd);
if(hFile == INVALID_HANDLE_VALUE)
{
DWORD error = GetLastError();
VFS_LOG_ERROR(_BS(L"Directory [") << sDir << L"] does not exist : " << error << _BS::wget);
}
bDirExists = (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
FindClose(hFile);
}
return bDirExists;
#else
int result = access(sDir.to_string().c_str(), F_OK /*0400*/);
if(result == -1)
{
vfs::String err = strerror(errno);
VFS_LOG_ERROR(err.c_str());
return false;
}
return true;
#endif
}
bool vfs::OS::createRealDirectory(vfs::Path const& sDir)
{
#ifdef WIN32
BOOL success;
vfs::String::str_t const& str = sDir.c_wcs();
success = vfs::Settings::getUseUnicode() ?
CreateDirectoryW(sDir.c_str(),NULL) :
CreateDirectoryA( vfs::String::narrow( str.c_str(), str.length() ).c_str(), NULL );
if(success == 0)
{
DWORD error = GetLastError();
if(error == ERROR_ALREADY_EXISTS)
{
return true;
}
VFS_LOG_ERROR((_BS("Could not create directory [") << sDir << L"] : " << error).get());
return false;
}
return true;
#else
int result = mkdir(sDir.to_string().c_str(), S_IRWXU | S_IRGRP /*0777*/);
if(result == -1 && errno != EEXIST)
{
vfs::String err = strerror(errno);
VFS_THROW(err);
}
return true;
#endif
}
bool vfs::OS::FileAttributes::getFileAttributes(vfs::Path const& sDir, vfs::UInt32& uiAttribs)
{
#ifdef WIN32
DWORD attribs = vfs::Settings::getUseUnicode() ?
GetFileAttributesW(sDir.c_str()) :
GetFileAttributesA(vfs::String::narrow(sDir.c_str(), sDir.length()).c_str());
if(attribs == INVALID_FILE_ATTRIBUTES)
{
DWORD error = GetLastError();
VFS_LOG_ERROR(_BS(L"Invalid File Attributes : ") << error << _BS::wget);
return false;
}
for(vfs::UInt32 attribMask = 0x80000000; attribMask > 0; attribMask >>= 1)
{
switch(attribs & attribMask)
{
case FILE_ATTRIBUTE_ARCHIVE:
uiAttribs |= ATTRIB_ARCHIVE;
break;
case FILE_ATTRIBUTE_DIRECTORY:
uiAttribs |= ATTRIB_DIRECTORY;
break;
case FILE_ATTRIBUTE_HIDDEN:
uiAttribs |= ATTRIB_HIDDEN;
break;
case FILE_ATTRIBUTE_NORMAL:
uiAttribs |= ATTRIB_NORMAL;
break;
case FILE_ATTRIBUTE_READONLY:
uiAttribs |= ATTRIB_READONLY;
break;
case FILE_ATTRIBUTE_SYSTEM:
uiAttribs |= ATTRIB_SYSTEM;
break;
case FILE_ATTRIBUTE_TEMPORARY:
uiAttribs |= ATTRIB_TEMPORARY;
break;
case FILE_ATTRIBUTE_COMPRESSED:
uiAttribs |= ATTRIB_COMPRESSED;
break;
case FILE_ATTRIBUTE_OFFLINE:
uiAttribs |= ATTRIB_OFFLINE;
break;
}
}
#else
#endif
return true;
}
bool vfs::OS::deleteRealFile(vfs::Path const& sDir)
{
#ifdef WIN32
BOOL del = vfs::Settings::getUseUnicode() ?
DeleteFileW( sDir.c_str() ) :
DeleteFileA( vfs::String::narrow(sDir.c_str(), sDir.length()).c_str() );
if(!del)
{
DWORD err = GetLastError();
if(err != NO_ERROR)
{
VFS_LOG_ERROR(L"Could not delete file");
}
}
return (del != FALSE);
#else
return (remove( vfs::String::as_utf8(sDir()).c_str() ) == 0);
#endif
}
void vfs::OS::getExecutablePath(vfs::Path& sDir, vfs::Path& sFile)
{
#ifdef WIN32
DWORD error;
if(!vfs::Settings::getUseUnicode())
{
char path[256];
if( 0 != (error = ::GetModuleFileNameA(NULL, path, 256)) )
{
vfs::Path(vfs::String::widen(path,256)).splitLast(sDir, sFile);
}
}
else
{
wchar_t path[256];
if( 0 != (error = ::GetModuleFileNameW(NULL, path, 256)) )
{
vfs::Path(path).splitLast(sDir, sFile);
}
}
if(error == 0)
{
DWORD code = GetLastError();
VFS_THROW(_BS(L"Could not get current directory [") <<
(!vfs::Settings::getUseUnicode() ? L"no unicode" : L"unicode") <<
L"], error code : " << code << _BS::wget);
}
#else
char buf[256];
ssize_t size = readlink("/proc/self/exe", buf, 256);
if(size == -1)
{
vfs::String err = strerror(errno);
VFS_THROW(err);
}
buf[size] = 0;
vfs::Path exedir(buf);
exedir.splitLast(sDir, sFile);
#endif
}
void vfs::OS::getCurrentDirectory(vfs::Path& sPath)
{
#ifdef WIN32
DWORD error;
vfs::Path path;
if( !vfs::Settings::getUseUnicode() )
{
char path[256];
if( 0 != (error = ::GetCurrentDirectoryA(256, path)) )
{
sPath = vfs::Path(vfs::String::widen(path,256));
}
}
else
{
wchar_t path[256];
if( 0 != (error = ::GetCurrentDirectoryW(256, path)) )
{
sPath = vfs::Path(path);
}
}
if(error == 0)
{
DWORD code = GetLastError();
VFS_THROW(_BS(L"Could not determine current directory ") <<
(!vfs::Settings::getUseUnicode() ? L"[no unicode]" : L"[unicode]") <<
L", error code : " << _BS::wget);
}
#else
char* cwd = getcwd(NULL,0);
if(!cwd)
{
vfs::String err = strerror(errno);
VFS_THROW(err);
}
sPath = cwd;
free(cwd);
#endif
}
void vfs::OS::setCurrectDirectory(vfs::Path const& sPath)
{
#ifdef WIN32
if(!vfs::Settings::getUseUnicode())
{
std::string str;
vfs::String::narrow( sPath.c_wcs(), str );
VFS_THROW_IFF( ::SetCurrentDirectoryA( str.c_str() ) == TRUE,
_BS(L"Could not set current directory [no unicode] : ") << sPath << _BS::wget );
}
else
{
VFS_THROW_IFF( ::SetCurrentDirectoryW( sPath.c_str() ) == TRUE,
_BS(L"Could not set current directory [unicode] : ") << sPath <<_BS::wget );
}
#else
if(chdir(sPath.to_string().c_str()) != 0)
{
vfs::String err = strerror(errno);
VFS_THROW(err);
}
#endif
}
bool vfs::OS::getEnv(vfs::String const& key, vfs::String& value)
{
#ifdef _MSC_VER
wchar_t *val_buf = NULL;
::size_t buf_len;
errno_t err = _wdupenv_s(&val_buf,&buf_len, key.c_str());
if(err == 0 && val_buf)
{
// success
value = val_buf;
free(val_buf);
}
return err == 0;
#else
char* val_buf = getenv(key.utf8().c_str());
if(val_buf)
{
value = val_buf;
return true;
}
return false;
#endif
}
+599
View File
@@ -0,0 +1,599 @@
/*
* bfVFS : vfs/Core/vfs_path.cpp
* - Path class, stores and validates a file/directory path string, offers meaningful path operations
* - path comparison functions (operator overloading)
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_path.h>
#include <vfs/Core/vfs_debug.h>
#include <vfs/Core/vfs_os_functions.h>
#include <vfs/Aspects/vfs_settings.h>
#include <stack>
#include <vector>
typedef struct{ vfs::String::size_t start, end; } t_env;
bool vfs::Path::expandEnv()
{
vfs::String::ptr_t raw_ptr = &_path[0];
std::stack<t_env> pos;
vfs::Int32 pos_start = -1, pos_end = -1, pos_current = 0;
while(*raw_ptr != 0)
{
if(*raw_ptr == L'$')
{
pos_start = pos_current;
}
else if(pos_start >= 0)
{
if(*raw_ptr == '(' && pos_current == (pos_start+1))
{
// ensure the syntax $(VARNAME) is followed
pos_end = pos_start;
}
//else
//{
// pos_start = pos_end = -1;
//}
else if(pos_start == pos_end && *raw_ptr == ')')
{
pos_end = pos_current;
}
//
if(pos_end >= 0 && pos_start != pos_end)
{
t_env e;
e.start = pos_start;
e.end = pos_end;
pos.push(e);
pos_start = pos_end = -1;
}
}
pos_current++;
raw_ptr++;
}
if(!pos.empty())
{
while(!pos.empty())
{
t_env v = pos.top();
vfs::String var_name = _path.substr(v.start+2, v.end-v.start-2);
vfs::String var_value;
if(!vfs::OS::getEnv(var_name, var_value))
{
VFS_LOG_WARNING(_BS(L"Could not expand environment variable : ") << var_name << _BS::wget);
return false;
}
_path.replace(v.start,v.end-v.start+1,var_value.c_wcs());
pos.pop();
}
doCheck();
}
return true;
}
static void unifySeparators(vfs::String::str_t &sPath)
{
vfs::String::char_t &raw = sPath[0];
vfs::String::ptr_t raw_ptr = &raw;
while(*raw_ptr != 0)
{
if(*raw_ptr == '\\' || *raw_ptr == '/')
{
*raw_ptr = vfs::Const::SEPARATOR_CHAR();
}
raw_ptr++;
}
}
/**
* call after unifying separators
*/
static vfs::UInt32 removeSeparators(vfs::String::str_t &str)
{
vfs::UInt32 sepcount = 0;
vfs::Int32 numsep = 0;
::size_t put_pos = 0;
::size_t len = str.length();
vfs::String::char_t& raw = str[0];
vfs::String::ptr_t old_ptr = &raw;
vfs::String::ptr_t new_ptr = &raw;
vfs::String::ptr_t last_ptr = &raw;
vfs::String::char_t sep = vfs::Const::SEPARATOR_CHAR();
while(*old_ptr != 0)
{
if(*old_ptr == sep)
{
numsep++;
if(numsep == 1)
{
sepcount++;
}
}
else
{
// we have normal text (again)
numsep = 0;
}
if(numsep <= 1)
{
*new_ptr = *old_ptr;
last_ptr = new_ptr++;
put_pos++;
}
old_ptr++;
}
if(*last_ptr == sep)
{
put_pos--;
if(sepcount>0)
{
sepcount--;
}
}
if(put_pos < len)
{
str.erase(put_pos);
}
return sepcount;
}
static void removeLastSeparator(vfs::String::str_t &str)
{
if( *str.rbegin() == vfs::Const::SEPARATOR_CHAR() )
{
str.erase( str.length()-1);
}
}
static void removeDots(vfs::String::str_t &str, vfs::UInt32 number_of_separators)
{
vfs::String::char_t& raw = str[0];
vfs::String::ptr_t old_ptr = &raw;
vfs::String::ptr_t new_ptr = &raw;
vfs::String::size_t LENGTH = str.length();
bool dirty = false;
// see if we start with ./ (not . as this might be a hidden files on unix systems)
if( (LENGTH > 1) && (*old_ptr == vfs::Const::DOT_CHAR()) && (*(old_ptr+1) == vfs::Const::SEPARATOR_CHAR()) )
{
old_ptr += 2;
dirty = true;
}
std::vector<vfs::String::ptr_t> sub_strings;
sub_strings.resize(number_of_separators);
int pos=0;
if(*old_ptr != 0 && *old_ptr == vfs::Const::SEPARATOR_CHAR())
{
if(!dirty)
{
// in-place : just move along
new_ptr++;
old_ptr++;
}
else
{
*new_ptr++ = *old_ptr++;
}
}
else
{
}
sub_strings[pos++] = new_ptr;
while(*old_ptr != 0)
{
if(*old_ptr != vfs::Const::SEPARATOR_CHAR())
{
if(!dirty)
{
new_ptr++;
old_ptr++;
}
else
{
*new_ptr++ = *old_ptr++;
}
continue;
}
*new_ptr++ = *old_ptr++;
sub_strings[pos++] = new_ptr;
if((&raw+3) < old_ptr)
{
if( (*(old_ptr-4) == vfs::Const::SEPARATOR_CHAR()) &&
(*(old_ptr-3) == vfs::Const::DOT_CHAR()) &&
(*(old_ptr-2) == vfs::Const::DOT_CHAR()) &&
(*(old_ptr-1) == vfs::Const::SEPARATOR_CHAR()) )
{
if(pos > 1)
{
new_ptr = sub_strings[pos-3];
pos -= 2;
dirty = true;
}
}
else if((*(old_ptr-3) == vfs::Const::SEPARATOR_CHAR()) &&
(*(old_ptr-2) == vfs::Const::DOT_CHAR()) &&
(*(old_ptr-1) == vfs::Const::SEPARATOR_CHAR()) )
{
if(pos > 0)
{
new_ptr = sub_strings[pos-2];
pos -= 1;
dirty = true;
}
}
}
else if((&raw+2) < old_ptr)
{
if( (*(old_ptr-3) == vfs::Const::SEPARATOR_CHAR()) &&
(*(old_ptr-2) == vfs::Const::DOT_CHAR()) &&
(*(old_ptr-1) == vfs::Const::SEPARATOR_CHAR()) )
{
if(pos > 0)
{
new_ptr = sub_strings[pos-2];
pos -= 1;
dirty = true;
}
}
}
}
::size_t ttt = (new_ptr - &raw);
if(ttt < LENGTH)
{
str.erase(ttt);
}
}
static void getFirstLastSeparator(const vfs::String::char_t* sPath, vfs::size_t &iFirst, vfs::size_t &iLast)
{
iFirst = vfs::npos;
iLast = vfs::npos;
const vfs::String::char_t* ptr = sPath;
vfs::size_t pos = 0;
while(*ptr != 0)
{
if(*ptr == vfs::Const::SEPARATOR_CHAR())
{
if(iFirst == vfs::npos)
{
iFirst = pos;
}
iLast = pos;
}
pos++;
ptr++;
}
}
//////////////////////////////////////////////////////////////////////
bool vfs::Path::Less::operator ()(vfs::Path const& s1, vfs::Path const& s2) const
{
return vfs::String::less(s1._path.c_str(), s2._path.c_str());
}
bool vfs::Path::Equal::operator ()(vfs::Path const& s1, vfs::Path const& s2) const
{
return vfs::String::equal(s1._path.c_str(), s2._path.c_str());
}
//////////////////////////////////////////////////////////////////////
vfs::Path::Path()
{
}
vfs::Path::Path(vfs::String const& sPath)
: _path(sPath.c_wcs())
{
doCheck();
}
vfs::Path::Path(const char* sPath)
{
if(vfs::Settings::getUseUnicode())
{
vfs::String::as_utf16(sPath,_path);
}
else
{
vfs::String::widen(std::string(sPath),_path);
}
doCheck();
}
vfs::Path::Path(std::string const& sPath)
{
if(vfs::Settings::getUseUnicode())
{
vfs::String::as_utf16(sPath,_path);
}
else
{
vfs::String::widen(sPath,_path);
}
doCheck();
}
vfs::Path::Path(const wchar_t* sPath)
: _path(sPath)
{
doCheck();
}
const vfs::String::char_t* vfs::Path::c_str() const
{
return _path.c_str();
}
const vfs::String::str_t& vfs::Path::c_wcs() const
{
return _path;
}
const vfs::String::str_t& vfs::Path::operator()() const
{
return _path;
}
std::string vfs::Path::to_string() const
{
if(vfs::Settings::getUseUnicode())
{
return vfs::String::as_utf8(_path);
}
else
{
std::string s;
vfs::String::narrow(_path,s);
return s;
}
}
bool vfs::Path::empty() const
{
// is there a case where a non-empty string can become empty after a check?
// if it is so then i don't care
return _path.empty();
}
vfs::String::size_t vfs::Path::length() const
{
return _path.length();
}
void vfs::Path::doCheck()
{
if(!_path.empty())
{
unifySeparators(_path);
vfs::UInt32 number_of_separators = removeSeparators(_path);
if(number_of_separators>0)
{
removeDots(_path,number_of_separators+1);
getFirstLastSeparator(_path.c_str(),_sep.first,_sep.last);
}
}
}
static void pathSplitLast(vfs::String::str_t const& path,
vfs::String::str_t &head,
vfs::String::str_t &last,
vfs::size_t const& sep_first,
vfs::size_t const& sep_last)
{
if(path.empty())
{
return;
}
if(&head == &path || &last == &path)
{
VFS_THROW(L"cannot use output parameters that are equal to 'this'");
}
#if 1
// use results from "GetFirstLastSeparator(..)"
if(sep_last != vfs::npos)
{
head.assign(path.substr(0,sep_last));
last.assign(path.substr(sep_last+1, path.length()-sep_last-1));
return;
}
#else
vfs::String::size_t position = path.length();
while(--position != vfs::npos)
{
vfs::String::char_t const& c = path.at(position);
if(c == '\\' || c == '/')
{
break;
}
}
if(position != vfs::npos)
{
head.assign(path.substr(0,position++));
last.assign(path.substr(position,path.length()-position));
return;
}
#endif
head.assign(vfs::Const::EMPTY());
last.assign(path);
}
void vfs::Path::splitLast(vfs::Path &rsHead, vfs::Path &rsLast) const
{
pathSplitLast(_path, rsHead._path, rsLast._path, _sep.first, _sep.last);
getFirstLastSeparator(rsHead.c_str(), rsHead._sep.first, rsHead._sep.last);
getFirstLastSeparator(rsLast.c_str(), rsLast._sep.first, rsLast._sep.last);
// no need to check, as the original path is already checked
//rPath.DoCheck();
//rFile.DoCheck();
}
static void pathSplitFirst(vfs::String::str_t const& path,
vfs::String::str_t &first,
vfs::String::str_t &tail,
vfs::size_t const& sep_first,
vfs::size_t const& sep_last)
{
if(path.empty())
{
return;
}
if(&first == &path || &tail == &path)
{
VFS_THROW(L"cannot use output parameters that are equal to 'this'");
}
#if 1
// use results from "GetFirstLastSeparator(..)"
if(sep_first != vfs::npos)
{
first.assign(path.substr(0,sep_first));
tail.assign(path.substr(sep_first+1, path.length()-sep_first-1));
return;
}
#else
vfs::String::size_t position = 0;
while(position < path.length())
{
vfs::String::char_t const& c = path.at(position);
if(c == '\\' || c == '/')
{
break;
}
position++;
}
if(position < path.length())
{
first.assign(path.substr(0,position++));
tail.assign(path.substr(position,path.length()-position));
return;
}
#endif
first.assign(path);
tail.assign(vfs::Const::EMPTY());
}
void vfs::Path::splitFirst(vfs::Path &rsFirst, vfs::Path &rsTail) const
{
pathSplitFirst(_path, rsFirst._path, rsTail._path, _sep.first, _sep.last);
getFirstLastSeparator(rsFirst.c_str(), rsFirst._sep.first, rsFirst._sep.last);
getFirstLastSeparator(rsTail.c_str(), rsTail._sep.first, rsTail._sep.last);
// no need to check, as the original path is already checked
//rPath.doCheck();
//rFile.doCheck();
}
bool vfs::Path::extension(vfs::String &sExt) const
{
if(_path.empty())
{
return false;
}
vfs::String::size_t SIZE = _path.length();
if(_path.at(SIZE-1) == L'.')
{
// not an extension
return false;
}
for(vfs::String::size_t i=SIZE-2; i > 0; i--)
{
if(_path.at(i) == L'.')
{
sExt.r_wcs().assign(&_path.at(i+1),SIZE-i-1);
return true;
}
}
return false;
}
vfs::Path& vfs::Path::operator+=(vfs::String const& p)
{
return *this += vfs::Path(p);
}
vfs::Path& vfs::Path::operator+=(vfs::Path const& p)
{
if(_path.empty())
{
_path = p._path;
}
else if(!p.empty())
{
_path += vfs::Const::SEPARATOR();
_path += p._path;
getFirstLastSeparator(_path.c_str(),_sep.first,_sep.last);
}
return *this;
}
vfs::Path operator+(vfs::Path const& p1, vfs::Path const& p2)
{
vfs::Path newpath = p1;
newpath += p2;
return newpath;
}
bool vfs::Path::operator==(vfs::Path const& p2)
{
return vfs::String::equal(_path.c_str(), p2._path.c_str());
}
bool operator==(vfs::Path const& p1, vfs::Path const& p2)
{
return vfs::StrCmp::Equal(p1.c_str(), p2.c_str());
}
bool operator==(vfs::Path const& p1, vfs::String const& p2)
{
return vfs::StrCmp::Equal(p1.c_str(), p2);
}
bool operator==(vfs::Path const& p1, vfs::String::str_t const& p2)
{
return vfs::StrCmp::Equal(p1.c_str(), p2);
}
bool operator==(vfs::Path const& p1, const vfs::String::char_t* p2)
{
return vfs::StrCmp::Equal(p1.c_str(), p2);
}
template<>
BuildString& BuildString::add<vfs::Path>(vfs::Path const& value)
{
this->add(value.c_str());
return *this;
}
template<>
BuildString& BuildString::operator<< <vfs::Path>(vfs::Path const& value)
{
this->add(value.c_str());
return *this;
}
+432
View File
@@ -0,0 +1,432 @@
/*
* bfVFS : vfs/Core/vfs_profile.cpp
* - Virtual Profile, container for real file system locations or archives
* - Profile Stack, orders profiles in a linear fashion (top-bottom)
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_profile.h>
#include <vfs/Core/vfs.h>
#include <vfs/Core/Location/vfs_lib_dir.h>
#include <vfs/Core/Location/vfs_directory_tree.h>
#include <vfs/Tools/vfs_log.h>
#include <vfs/Tools/vfs_parser_tools.h>
#include <sstream>
class vfs::CVirtualProfile::IterImpl : public vfs::CVirtualProfile::Iterator::IImplementation
{
friend class vfs::CVirtualProfile;
typedef vfs::CVirtualProfile::Iterator::IImplementation tBaseClass;
IterImpl(CVirtualProfile* profile) : tBaseClass(), m_profile(profile)
{
VFS_THROW_IFF(profile, L"");
// only unique locations
_loc_iter = m_profile->m_setLocations.begin();
}
public:
IterImpl() : tBaseClass(), m_profile(NULL)
{};
virtual ~IterImpl()
{};
//////
virtual vfs::IBaseLocation* value()
{
if(_loc_iter != m_profile->m_setLocations.end())
{
return *_loc_iter;
}
return NULL;
}
virtual void next()
{
if(_loc_iter != m_profile->m_setLocations.end())
{
_loc_iter++;
}
}
protected:
virtual tBaseClass* clone()
{
IterImpl* iter = new IterImpl();
iter->m_profile = m_profile;
iter->_loc_iter = _loc_iter;
return iter;
}
private:
vfs::CVirtualProfile* m_profile;
vfs::CVirtualProfile::tUniqueLoc::iterator _loc_iter;
};
/***************************************************************************/
/***************************************************************************/
class vfs::CVirtualProfile::FileIterImpl : public vfs::CVirtualProfile::FileIterator::IImplementation
{
friend class vfs::CVirtualProfile;
typedef vfs::CVirtualProfile::FileIterator::IImplementation tBaseClass;
FileIterImpl(vfs::Path const& sPattern, CVirtualProfile* profile);
public:
FileIterImpl() : tBaseClass(), m_profile(NULL)
{};
virtual ~FileIterImpl()
{};
/////
virtual vfs::IBaseFile* value()
{
return file;
}
virtual void next();
protected:
virtual tBaseClass* clone()
{
FileIterImpl* iter2 = new FileIterImpl();
iter2->m_pattern = m_pattern;
iter2->m_profile = m_profile;
iter2->iter = iter;
iter2->fiter = fiter;
iter2->file = file;
return iter2;
}
private:
vfs::Path m_pattern;
vfs::CVirtualProfile* m_profile;
vfs::CVirtualProfile::Iterator iter;
vfs::IBaseLocation::Iterator fiter;
vfs::IBaseFile* file;
};
vfs::CVirtualProfile::FileIterImpl::FileIterImpl(vfs::Path const& sPattern, CVirtualProfile* profile)
: tBaseClass(), m_pattern(sPattern), m_profile(profile)
{
VFS_THROW_IFF(profile, L"");
iter = m_profile->begin();
while(!iter.end())
{
fiter = iter.value()->begin();
while(!fiter.end())
{
file = fiter.value();
if( matchPattern(m_pattern(), file->getPath()()) )
{
return;
}
fiter.next();
}
iter.next();
}
file = NULL;
}
void vfs::CVirtualProfile::FileIterImpl::next()
{
if(!fiter.end())
{
fiter.next();
}
while(!iter.end())
{
while(!fiter.end())
{
file = fiter.value();
if( matchPattern(m_pattern(), file->getPath()()) )
{
return;
}
fiter.next();
}
iter.next();
if(!iter.end())
{
fiter = iter.value()->begin();
}
}
file = NULL;
}
/***************************************************************************/
/***************************************************************************/
vfs::CVirtualProfile::CVirtualProfile(vfs::String const& profile_name, vfs::Path profile_root, bool writable)
: cName(profile_name), cRoot(profile_root), cWritable(writable)
{
}
vfs::CVirtualProfile::~CVirtualProfile()
{
tUniqueLoc::iterator it = m_setLocations.begin();
for(; it != m_setLocations.end(); ++it)
{
delete (*it);
//(*it) = NULL;
}
m_setLocations.clear();
m_mapLocations.clear();
};
vfs::CVirtualProfile::Iterator vfs::CVirtualProfile::begin()
{
return Iterator(new IterImpl(this));
}
vfs::CVirtualProfile::FileIterator vfs::CVirtualProfile::files(vfs::Path const& sPattern)
{
return FileIterator( new FileIterImpl(sPattern, this));
}
void vfs::CVirtualProfile::addLocation(vfs::IBaseLocation* pLoc)
{
if(pLoc)
{
m_setLocations.insert(pLoc);
std::list<vfs::Path> lDirs;
pLoc->getSubDirList(lDirs);
std::list<vfs::Path>::const_iterator cit = lDirs.begin();
for(;cit != lDirs.end(); ++cit)
{
vfs::IBaseLocation *pOldLoc = m_mapLocations[*cit];
if(!pOldLoc)
{
m_mapLocations[*cit] = pLoc;
}
else if(pOldLoc == pLoc)
{
// seems to be an update. do nothing
}
else
{
VFS_LOG_WARNING((L"Another location is already mapped to '" + ((*cit)()) + L"' [keeping old location]").c_str());
//VFS_THROW(L"Location already taken");
}
}
}
}
vfs::IBaseLocation* vfs::CVirtualProfile::getLocation(vfs::Path const& sPath) const
{
tLocations::const_iterator it = m_mapLocations.find(sPath);
if(it != m_mapLocations.end())
{
return it->second;
}
return NULL;
}
vfs::IBaseFile* vfs::CVirtualProfile::getFile(vfs::Path const& sPath) const
{
vfs::Path sDir,sFile;
sPath.splitLast(sDir,sFile);
tLocations::const_iterator it = m_mapLocations.find(sDir);
if(it != m_mapLocations.end())
{
return it->second->getFile(sPath);
}
return NULL;
}
/***************************************************************************/
/***************************************************************************/
class vfs::CProfileStack::IterImpl : public vfs::CProfileStack::Iterator::IImplementation
{
friend class CProfileStack;
typedef CProfileStack::Iterator::IImplementation tBaseClass;
IterImpl(CProfileStack* pPStack) : tBaseClass(), m_pPStack(pPStack)
{
VFS_THROW_IFF(m_pPStack, L"");
_prof_iter = m_pPStack->m_profiles.begin();
}
public:
IterImpl() : tBaseClass(), m_pPStack(NULL)
{};
~IterImpl()
{};
//////
virtual vfs::CVirtualProfile* value()
{
if(_prof_iter != m_pPStack->m_profiles.end())
{
return *_prof_iter;
}
return NULL;
}
virtual void next()
{
if(_prof_iter != m_pPStack->m_profiles.end())
{
_prof_iter++;
}
}
protected:
virtual tBaseClass* clone()
{
IterImpl* iter = new IterImpl();
iter->m_pPStack = m_pPStack;
iter->_prof_iter = _prof_iter;
return iter;
}
private:
vfs::CProfileStack* m_pPStack;
std::list<vfs::CVirtualProfile*>::iterator _prof_iter;
};
/***************************************************************************/
/***************************************************************************/
vfs::CProfileStack::CProfileStack()
{
}
vfs::CProfileStack::~CProfileStack()
{
t_profiles::iterator it = m_profiles.begin();
for(; it != m_profiles.end(); ++it)
{
delete (*it);
(*it) = NULL;
}
m_profiles.clear();
}
vfs::CVirtualProfile* vfs::CProfileStack::getProfile(vfs::String const& sName) const
{
t_profiles::const_iterator it = m_profiles.begin();
for(;it != m_profiles.end(); ++it)
{
if( StrCmp::EqualCase((*it)->cName, sName) )
{
return *it;
}
}
return NULL;
}
vfs::CVirtualProfile* vfs::CProfileStack::getWriteProfile()
{
t_profiles::const_iterator it = m_profiles.begin();
for(;it != m_profiles.end(); ++it)
{
if((*it)->cWritable)
{
return *it;
}
}
return NULL;
}
vfs::CVirtualProfile* vfs::CProfileStack::topProfile() const
{
if(!m_profiles.empty())
{
return m_profiles.front();
}
return NULL;
}
bool vfs::CProfileStack::popProfile()
{
// there might be some files in this profile that are referenced in a Log object
// we need to it to release the file
vfs::Log::flushReleaseAll();
// an observer pattern would probably be the better solution,
// but for now lets do it this way
vfs::CVirtualProfile* prof = this->topProfile();
if(prof)
{
vfs::CVirtualProfile::Iterator loc_it = prof->begin();
for(; !loc_it.end(); loc_it.next())
{
vfs::IBaseLocation* loc = loc_it.value();
vfs::IBaseLocation::Iterator f_it = loc->begin();
for(; !f_it.end(); f_it.next())
{
vfs::IBaseFile* file = f_it.value();
vfs::Path sDir, sFile;
if(file)
{
file->getPath().splitLast(sDir,sFile);
vfs::CVirtualLocation* vloc = getVFS()->getVirtualLocation(sDir);
if(vloc)
{
if( !vloc->removeFile(file) )
{
VFS_THROW(_BS(L"Could not remove file [") << file->getPath()
<< L"] in Profile [" << prof->cName << L"]" << _BS::wget);
}
}
else
{
VFS_THROW(_BS(L"Virtual location [") << sDir
<< L"] doesn't exist. Maybe the VFS was not properly setup." << _BS::wget);
}
}
else
{
VFS_THROW(_BS(L"File is NULL during iteration over files in location [")
<< loc->getPath() << L"]" << _BS::wget);
}
}
}
// delete only when nothing went wrong
this->m_profiles.pop_front();
delete prof;
}
return true;
}
void vfs::CProfileStack::pushProfile(CVirtualProfile* pProfile)
{
if(!getProfile(pProfile->cName))
{
if(pProfile->cWritable)
{
m_profiles.push_front(pProfile);
}
else
{
t_profiles::iterator pit = m_profiles.begin();
while(pit != m_profiles.end() && (*pit)->cWritable)
{
pit++;
}
//if(pit != m_profiles.end())
{
m_profiles.insert(pit,pProfile);
}
//else
//{
// m_profiles.push_front(pProfile);
//}
}
return;
}
VFS_THROW(L"A profile with this name already exists");
}
vfs::CProfileStack::Iterator vfs::CProfileStack::begin()
{
return Iterator(new IterImpl(this));
}
+596
View File
@@ -0,0 +1,596 @@
/*
* bfVFS : vfs/Core/vfs_string.cpp
* - string class that allows conversions to/from Unicode representation (uses wchar_t internally)
* - comparison, concatenation, stream output class/functions
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_string.h>
#include <utf8.h>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <vfs/Core/vfs_debug.h>
////////////////////////////////////////////////////////////////////
namespace _StrCmp
{
////////////////////////////////////////////////////////////////
static inline void Advance( const char*& s1, const char*& s2 )
{
while (*s1 && *s2 && toupper(*s1) == toupper(*s2))
{
++s1;
++s2;
}
}
static inline void Advance( const wchar_t*& s1, const wchar_t*& s2 )
{
// should be 'towupper'
while (*s1 && *s2 && toupper(*s1) == toupper(*s2))
{
++s1;
++s2;
}
}
template<typename CharType>
static inline void AdvanceCase( const CharType*& s1, const CharType*& s2 )
{
while (*s1 && *s2 && (*s1 == *s2))
{
++s1;
++s2;
}
}
////////////////////////////////////////////////////////////////
template<typename CharType>
static inline bool Equal( const CharType* s1, const CharType* s2 )
{
return !(*s1 || *s2);
}
////////////////////////////////////////////////////////////////
static inline bool Less( const wchar_t* s1, const wchar_t* s2 )
{
if (!*s1) return *s2 != 0;
if (!*s2) return false;
// should be 'towupper'
return toupper(*s1) < toupper(*s2);
}
static inline bool Less( const char* s1, const char* s2 )
{
if (!*s1) return *s2 != 0;
if (!*s2) return false;
return toupper(*s1) < toupper(*s2);
}
template<typename CharType>
static inline bool LessCase( const CharType* s1, const CharType* s2 )
{
if (!*s1) return *s2 != 0;
if (!*s2) return false;
return (*s1) < (*s2);
}
}
////////////////////////////////////////////////////////////////////
bool vfs::String::lessCase(const vfs::String::char_t* s1, const vfs::String::char_t* s2)
{
_StrCmp::AdvanceCase(s1,s2);
return _StrCmp::LessCase(s1,s2);
}
bool vfs::String::less(const vfs::String::char_t* s1, const vfs::String::char_t* s2)
{
_StrCmp::Advance(s1,s2);
return _StrCmp::Less(s1,s2);
}
bool vfs::String::equalCase(const vfs::String::char_t* s1, const vfs::String::char_t* s2)
{
_StrCmp::AdvanceCase(s1,s2);
return _StrCmp::Equal(s1,s2);
}
bool vfs::String::equal(const vfs::String::char_t* s1, const vfs::String::char_t* s2)
{
_StrCmp::Advance(s1,s2);
return _StrCmp::Equal(s1,s2);
}
////////////////////////////////////////////////////////////////////
/// class constructors
////////////////////////////////////////////////////////////////////
vfs::String::String()
{
}
vfs::String::String(const char* str)
{
vfs::String::as_utf16(str,_str);
}
vfs::String::String(std::string const& str)
{
vfs::String::as_utf16(str,_str);
}
vfs::String::String(const wchar_t* str)
{
_str.assign(str);
}
vfs::String::String(std::wstring const& str)
{
_str.assign(str);
}
////////////////////////////////////////////////////////////////////
/// static class methods
////////////////////////////////////////////////////////////////////
vfs::String::str_t vfs::String::as_utf16(std::string const& str)
{
vfs::String::str_t s;
vfs::String::as_utf16(str, s);
return s;
}
void vfs::String::as_utf16(std::string const& str, vfs::String::str_t &str16)
{
try
{
::size_t d = utf8::distance(str.begin(), str.end());
if(d > 0)
{
str16.resize(d);
utf8::utf8to16(str.begin(), str.end(), &str16[0]);
}
}
catch(utf8::invalid_utf8& ex)
{
utf8::uint8_t c = ex.utf8_octet();
VFS_THROW( _BS(L"Invalid UTF8 character '") << (wchar_t)c << L"'=" << (unsigned char)c <<_BS::wget );
}
catch(utf8::not_enough_room &ex)
{
std::wstring err;
VFS_IGNOREEXCEPTION(vfs::String::as_utf16(ex.what(),err), true);
VFS_THROW( _BS(L"Incomplete UTF8 string [") << err << L"]" << _BS::wget );
}
catch(...)
{
VFS_THROW(L"Unicode error");
}
}
vfs::String::str_t vfs::String::as_utf16(const char* str)
{
vfs::String::str_t s;
vfs::String::as_utf16(str, s);
return s;
}
void vfs::String::as_utf16(const char* str, vfs::String::str_t &str16)
{
if(str == NULL)
{
return;
}
try
{
::size_t len = strlen(str);
::size_t d = utf8::distance(str,str+len);
if(d > 0)
{
str16.resize(d);
utf8::utf8to16(str, str+len, &str16[0]);
}
}
catch(utf8::invalid_utf8& ex)
{
utf8::uint8_t c = ex.utf8_octet();
VFS_THROW( _BS(L"Invalid UTF8 character '") << (wchar_t)c << L"'=" << (unsigned char)c << _BS::wget );
}
catch(utf8::not_enough_room &ex)
{
std::wstring err;
VFS_IGNOREEXCEPTION(vfs::String::as_utf16(ex.what(), err), true);
VFS_THROW( _BS(L"Incomplete UTF8 string [") << err << L"]" << _BS::wget );
}
}
std::string vfs::String::as_utf8(vfs::String const& str)
{
return str.utf8();
}
std::string vfs::String::as_utf8(std::wstring const& str)
{
if(str.empty())
{
return "";
}
#if 0
std::string s;
utf8::utf16to8(str.begin(), str.end(), std::back_inserter(s));
#else
const int UTF8_MAX_CHARS = 4;
std::vector<char> buffer(str.length()+UTF8_MAX_CHARS);
::size_t pos_in=0, pos_out=0;
while(pos_in < str.length())
{
if( (pos_out+UTF8_MAX_CHARS) >= buffer.size() )
{
buffer.resize(buffer.size()+str.length());
}
pos_out = utf8::append(str.at(pos_in++), &buffer[pos_out]) - &buffer[0];
}
std::string s(&buffer[0],pos_out);
#endif
return s;
}
// if 'strlength' is 0, length is determined automatically
std::string vfs::String::as_utf8(const wchar_t* str, vfs::String::size_t strlength)
{
std::string s;
if(str != NULL)
{
::size_t len = strlength;
if(len == 0)
{
len = wcslen(str);
}
if(len != 0)
{
utf8::utf16to8(str,str+len,std::back_inserter(s));
}
}
return s;
}
// convenience method, for the case it should be used in generic code (overloading)
std::string vfs::String::as_utf8(std::string const& str)
{
return str;
}
vfs::String::size_t vfs::String::narrow(std::wstring const& src, std::string& dst)
{
vfs::String::size_t len = vfs::String::narrow(src.c_str(),src.length(),NULL,0);
if(len > 0)
{
dst.resize(len);
return vfs::String::narrow(src.c_str(),src.length(),&dst[0],len);
}
return 0;
}
std::string vfs::String::narrow(wchar_t const* str, vfs::String::size_t length)
{
std::string s;
vfs::String::size_t len = vfs::String::narrow(str,length,NULL,0);
if(len > 0)
{
s.resize(len);
vfs::String::narrow(str,length,&s[0],len);
}
return s;
}
vfs::String::size_t vfs::String::narrow(wchar_t const* src_str, vfs::String::size_t src_len, char* dst_str, vfs::String::size_t dst_len)
{
if(src_str && src_len>0)
{
if(!dst_str || dst_len==0)
{
return wcstombs(NULL, src_str, src_len);
}
return wcstombs(dst_str, src_str, std::min<vfs::String::size_t>(src_len,dst_len));
}
return 0;
}
//
std::wstring vfs::String::widen(char const* str, vfs::String::size_t length)
{
std::wstring ws;
vfs::String::size_t len = vfs::String::widen(str,length,NULL,0);
if(len > 0)
{
ws.resize(len);
vfs::String::widen(str,length,&ws[0],len);
}
return ws;
}
vfs::String::size_t vfs::String::widen(std::string const& src, std::wstring& dst)
{
vfs::String::size_t len = vfs::String::widen(src.c_str(),src.length(),NULL,0);
if(len > 0)
{
dst.resize(len);
return vfs::String::widen(src.c_str(),src.length(),&dst[0],len);
}
return 0;
}
vfs::String::size_t vfs::String::widen(char const* src_str, size_t src_len, wchar_t* dst_str, size_t dst_len)
{
if(src_str && src_len>0)
{
if(!dst_str || dst_len==0)
{
return mbstowcs(NULL, src_str, src_len);
}
return mbstowcs(dst_str, src_str, std::min<size_t>(src_len,dst_len));
}
return 0;
}
////////////////////////////////////////////////////////////////////
bool vfs::String::empty() const
{
return _str.empty();
}
vfs::String::size_t vfs::String::length() const
{
return _str.length();
}
vfs::String vfs::String::operator+(vfs::String const& str)
{
return vfs::String(_str + str._str);
}
vfs::String vfs::String::operator+=(vfs::String const& str)
{
_str += str._str;
return vfs::String(_str);
}
////////////////////////////////////////////////////////////////
bool vfs::operator<(vfs::String const& s1, vfs::String const& s2)
{
return s1._str < s2._str;
}
std::wostream& operator<<(std::wostream& out, vfs::String const& str)
{
out.write(str.c_str(), (std::streamsize)str.length());
return out;
}
std::wostream& operator<<(std::wostream& out, vfs::String::str_t const& str)
{
out.write(str.c_str(), (std::streamsize)str.length());
return out;
}
std::wostream& operator<<(std::wostream& out, const vfs::String::char_t* str)
{
out.write(str, (std::streamsize)wcslen(str));
return out;
}
/*****************************************************************************************/
/*****************************************************************************************/
// case IN-sensitive
bool vfs::StrCmp::Equal(const char* s1, const char* s2)
{
_StrCmp::Advance(s1,s2);
return _StrCmp::Equal(s1,s2);
}
bool vfs::StrCmp::Equal(std::string const& s1, std::string const& s2)
{
const char* p1 = s1.c_str();
const char* p2 = s2.c_str();
_StrCmp::Advance(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::Equal(std::string const& s1, const char* s2)
{
const char* p1 = s1.c_str();
_StrCmp::Advance(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::Equal(const char* s1, std::string const& s2)
{
const char* p2 = s2.c_str();
_StrCmp::Advance(s1,p2);
return _StrCmp::Equal(s1,p2);
}
//
bool vfs::StrCmp::Equal(const wchar_t* s1, const wchar_t* s2)
{
_StrCmp::Advance(s1,s2);
return _StrCmp::Equal(s1,s2);
}
bool vfs::StrCmp::Equal(std::wstring const& s1, std::wstring const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::Equal(std::wstring const& s1, const wchar_t* s2)
{
const wchar_t* p1 = s1.c_str();
_StrCmp::Advance(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::Equal(const wchar_t* s1, std::wstring const& s2)
{
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(s1,p2);
return _StrCmp::Equal(s1,p2);
}
//
bool vfs::StrCmp::Equal(vfs::String const& s1, vfs::String const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::Equal(vfs::String const& s1, std::wstring const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::Equal(std::wstring const& s1, vfs::String const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::Equal(vfs::String const& s1, const wchar_t* s2)
{
const wchar_t* p1 = s1.c_str();
_StrCmp::Advance(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::Equal(const wchar_t* s1, vfs::String const& s2)
{
const wchar_t* p2 = s2.c_str();
_StrCmp::Advance(s1,p2);
return _StrCmp::Equal(s1,p2);
}
// case Sensitive
bool vfs::StrCmp::EqualCase(const char* s1, const char* s2)
{
_StrCmp::AdvanceCase(s1,s2);
return _StrCmp::Equal(s1,s2);
}
bool vfs::StrCmp::EqualCase(std::string const& s1, std::string const& s2)
{
const char* p1 = s1.c_str();
const char* p2 = s2.c_str();
_StrCmp::AdvanceCase(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::EqualCase(std::string const& s1, const char* s2)
{
const char* p1 = s1.c_str();
_StrCmp::AdvanceCase(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::EqualCase(const char* s1, std::string const& s2)
{
const char* p2 = s2.c_str();
_StrCmp::AdvanceCase(s1,p2);
return _StrCmp::Equal(s1,p2);
}
//
bool vfs::StrCmp::EqualCase(const wchar_t* s1, const wchar_t* s2)
{
_StrCmp::AdvanceCase(s1,s2);
return _StrCmp::Equal(s1,s2);
}
bool vfs::StrCmp::EqualCase(std::wstring const& s1, std::wstring const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::EqualCase(std::wstring const& s1, const wchar_t* s2)
{
const wchar_t* p1 = s1.c_str();
_StrCmp::AdvanceCase(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::EqualCase(const wchar_t* s1, std::wstring const& s2)
{
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(s1,p2);
return _StrCmp::Equal(s1,p2);
}
//
bool vfs::StrCmp::EqualCase(vfs::String const& s1, vfs::String const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::EqualCase(vfs::String const& s1, std::wstring const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::EqualCase(std::wstring const& s1, vfs::String const& s2)
{
const wchar_t* p1 = s1.c_str();
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(p1,p2);
return _StrCmp::Equal(p1,p2);
}
bool vfs::StrCmp::EqualCase(vfs::String const& s1, const wchar_t* s2)
{
const wchar_t* p1 = s1.c_str();
_StrCmp::AdvanceCase(p1,s2);
return _StrCmp::Equal(p1,s2);
}
bool vfs::StrCmp::EqualCase(const wchar_t* s1, vfs::String const& s2)
{
const wchar_t* p2 = s2.c_str();
_StrCmp::AdvanceCase(s1,p2);
return _StrCmp::Equal(s1,p2);
}
/*****************************************************************************************/
/*****************************************************************************************/
template<>
BuildString& BuildString::add<vfs::String>(vfs::String const& value)
{
this->add(value.c_str());
return *this;
}
template<>
BuildString& BuildString::add<std::string>(std::string const& value)
{
this->add(vfs::String::as_utf16(value));
return *this;
}
template<>
BuildString& BuildString::add<const char*>(const char* const& value)
{
this->add(vfs::String::as_utf16(value));
return *this;
}
template<>
BuildString& BuildString::operator<< <vfs::String>(vfs::String const& value)
{
this->add(value.c_str());
return *this;
}
template<>
BuildString& BuildString::operator<< <std::string>(std::string const& value)
{
this->add(vfs::String::as_utf16(value));
return *this;
}
template<>
BuildString& BuildString::operator<< <const char*>(const char* const& value)
{
this->add(vfs::String::as_utf16(value));
return *this;
}
+34
View File
@@ -0,0 +1,34 @@
/*
* bfVFS : vfs/Core/vfs_types.cpp
* - basic integer types, "incomplete" list of useful constant strings
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_types.h>
//////////////////////////////////////////////////////////////////////
namespace vfs
{
const vfs::size_t npos = vfs::size_t(-1);
}
//////////////////////////////////////////////////////////////////////
+220
View File
@@ -0,0 +1,220 @@
/*
* bfVFS : vfs/Core/vfs_vfile.cpp
* - Virtual File, handles access to files from different VFS profiles
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_vfile.h>
#include <vfs/Core/vfs_vloc.h>
#include <vfs/Core/vfs.h>
// static member
vfs::ObjBlockAllocator<vfs::CVirtualFile>* vfs::CVirtualFile::_vfile_pool = NULL;
vfs::CVirtualFile* vfs::CVirtualFile::create(vfs::Path const& sFilePath, vfs::CProfileStack& rPStack)
{
unsigned int ID=0;
#ifdef VFILE_BLOCK_CREATE
if(!_vfile_pool)
{
_vfile_pool = new ObjBlockAllocator<vfs::CVirtualFile>();
vfs::ObjectAllocator::registerAllocator(_vfile_pool);
}
CVirtualFile* file = _vfile_pool->New(&ID);
#else
CVirtualFile* file = new CVirtualFile();
#endif
file->_path = sFilePath;
file->_pstack = &rPStack;
file->_myID = ID;
return file;
}
void vfs::CVirtualFile::destroy()
{
#ifndef VFILE_BLOCK_CREATE
delete this;
#endif
}
vfs::CVirtualFile::CVirtualFile()
: _path(L""), _top_pname(L"_INVALID_"), _top_file(NULL), _pstack(NULL), _myID(vfs::UInt32(-1))
{
};
vfs::CVirtualFile::~CVirtualFile()
{
}
vfs::Path const& vfs::CVirtualFile::path()
{
return _path;
}
void vfs::CVirtualFile::add(vfs::IBaseFile *pFile, vfs::String sProfileName, bool bReplace)
{
if(pFile)
{
// if there is no file then just set it
// if bReplace is set to true then override all other settings and just set the file
if(!_top_file || bReplace)
{
_top_file = pFile;
_top_pname = sProfileName;
return;
}
// file already set, but new file is? the same object
if(pFile == _top_file)
{
VFS_THROW_IFF( StrCmp::Equal(sProfileName,_top_pname), L"same file, different profile name");
}
// OK, not the same file, but these two different files are supposed to have the same filename
VFS_THROW_IFF( _top_file->getName() == pFile->getName(), L"different filenames");
// set new file only when its profile is on top of the current file's profile
bool bFoundOld = false, bFoundNew = false;
vfs::CProfileStack::Iterator it = _pstack->begin();
for(; !it.end(); it.next())
{
if(_top_pname == it.value()->cName)
{
bFoundOld = true;
break;
}
else if(sProfileName == it.value()->cName)
{
bFoundNew = true;
break;
}
}
if(bFoundNew && !bFoundOld)
{
_top_file = pFile;
_top_pname = sProfileName;
}
}
}
/**
* @returns : returns true if pFile is not top file or top file could be replaced with another file
* returns false if there is no more files with given name. in this case object should be destroyed
*/
bool vfs::CVirtualFile::remove(vfs::IBaseFile *pFile)
{
if(_top_file == pFile)
{
if(_path == pFile->getPath())
{
// need to replace '_top_file'
vfs::CProfileStack::Iterator prof_it = _pstack->begin();
for(; !prof_it.end(); prof_it.next())
{
CVirtualProfile *pProf = prof_it.value();
if(pProf)
{
vfs::IBaseFile *file = pProf->getFile(_path);
if(file && (file != pFile))
{
_top_file = file;
_top_pname = pProf->cName;
return true;
}
}
}
// no more files
_top_file = NULL;
_top_pname = "";
return false;
}
else
{
VFS_THROW(L"Same file object but different file paths? WTH?");
}
}
return true;
}
vfs::IBaseFile* vfs::CVirtualFile::file(ESearchFile eSearch)
{
if(eSearch == SF_TOP)
{
return _top_file;
}
else if(eSearch == SF_FIRST_WRITABLE)
{
CVirtualProfile *pVProf = _pstack->getWriteProfile();
if(pVProf)
{
return pVProf->getFile(_path);
}
}
else if(eSearch == SF_STOP_ON_WRITABLE_PROFILE)
{
vfs::CProfileStack::Iterator prof_it = _pstack->begin();
for(; !prof_it.end(); prof_it.next())
{
CVirtualProfile *pProf = prof_it.value();
if(pProf)
{
if(pProf->cWritable)
{
return pProf->getFile(_path);
}
else
{
vfs::IBaseFile *pFile = pProf->getFile(_path);
if(pFile)
{
return pFile;
}
}
}
}
}
return NULL;
}
vfs::IBaseFile* vfs::CVirtualFile::file(vfs::String const& sProfileName)
{
if(sProfileName == _top_pname)
{
return vfs::tReadableFile::cast(_top_file);
}
else
{
CVirtualProfile* pProf = _pstack->getProfile(sProfileName);
if(pProf)
{
CVirtualProfile::Iterator loc_it = pProf->begin();
for(; !loc_it.end(); loc_it.next())
{
if(loc_it.value() && loc_it.value()->fileExists(_path))
{
if(loc_it.value())
{
return loc_it.value()->getFile(_path);
}
}
}
}
}
return NULL;
}
+172
View File
@@ -0,0 +1,172 @@
/*
* bfVFS : vfs/Core/vfs_vloc.cpp
* - Virtual Location, stores Virtual Files
*
* Copyright (C) 2008 - 2010 (BF) john.bf.smith@googlemail.com
*
* This file is part of the bfVFS library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <vfs/Core/vfs_vloc.h>
#include <vfs/Core/vfs_vfile.h>
#include <vfs/Core/vfs_profile.h>
#include <vfs/Core/vfs.h>
/************************************************************************/
class vfs::CVirtualLocation::VFileIterator : public vfs::CVirtualLocation::Iterator::IImplementation
{
friend class vfs::CVirtualLocation;
typedef vfs::CVirtualLocation::Iterator::IImplementation tBaseClass;
VFileIterator(vfs::CVirtualLocation* pLoc): tBaseClass(), m_pLoc(pLoc)
{
VFS_THROW_IFF(pLoc, L"");
_vfile_iter = m_pLoc->m_VFiles.begin();
}
public:
VFileIterator() : tBaseClass(), m_pLoc(NULL)
{};
virtual ~VFileIterator()
{};
virtual vfs::CVirtualFile* value()
{
if(m_pLoc && _vfile_iter != m_pLoc->m_VFiles.end())
{
return _vfile_iter->second;
}
return NULL;
}
virtual void next()
{
if(m_pLoc && _vfile_iter != m_pLoc->m_VFiles.end())
{
_vfile_iter++;
}
}
protected:
virtual tBaseClass* clone()
{
VFileIterator* iter = new VFileIterator(m_pLoc);
iter->_vfile_iter = _vfile_iter;
return iter;
}
private:
vfs::CVirtualLocation* m_pLoc;
vfs::CVirtualLocation::tVFiles::iterator _vfile_iter;
};
/************************************************************************/
vfs::CVirtualLocation::CVirtualLocation(vfs::Path const& path)
: cPath(path), m_exclusive(false)
{};
vfs::CVirtualLocation::~CVirtualLocation()
{
tVFiles::iterator it = m_VFiles.begin();
for(; it != m_VFiles.end(); ++it)
{
it->second->destroy();
}
m_VFiles.clear();
}
void vfs::CVirtualLocation::setIsExclusive(bool exclusive)
{
m_exclusive = exclusive;
}
bool vfs::CVirtualLocation::getIsExclusive()
{
return m_exclusive;
}
void vfs::CVirtualLocation::addFile(vfs::IBaseFile* file, vfs::String const& profileName)
{
vfs::CVirtualFile *pVFile = NULL;
tVFiles::iterator it = m_VFiles.find(file->getName());
if(it == m_VFiles.end())
{
vfs::Path fp = file->getPath();
vfs::CProfileStack& stack = *(getVFS()->getProfileStack());
pVFile = vfs::CVirtualFile::create(fp,stack);
it = m_VFiles.insert(m_VFiles.end(), std::pair<vfs::Path,vfs::CVirtualFile*>(file->getName(),pVFile));
}
it->second->add(file,profileName,true);
}
vfs::IBaseFile* vfs::CVirtualLocation::getFile(vfs::Path const& filename, vfs::String const& profileName) const
{
tVFiles::const_iterator cit = m_VFiles.find(filename);
if(cit != m_VFiles.end() && cit->second)
{
if(profileName.empty())
{
if(m_exclusive)
{
return cit->second->file(vfs::CVirtualFile::SF_STOP_ON_WRITABLE_PROFILE);
}
else
{
return cit->second->file(vfs::CVirtualFile::SF_TOP);
}
}
else
{
// you know what you are doing
return cit->second->file(profileName);
}
}
return NULL;
}
vfs::CVirtualFile* vfs::CVirtualLocation::getVirtualFile(vfs::Path const& filename)
{
tVFiles::const_iterator cit = m_VFiles.find(filename);
if(cit != m_VFiles.end())
{
return cit->second;
}
return NULL;
}
bool vfs::CVirtualLocation::removeFile(vfs::IBaseFile* file)
{
if(file)
{
vfs::Path sDir,sFile;
file->getPath().splitLast(sDir,sFile);
tVFiles::iterator it = m_VFiles.find(sFile);
if(it != m_VFiles.end())
{
if(!it->second->remove(file))
{
//CVirtualFile* vfile = it->second;
//delete vfile;
m_VFiles.erase(it);
}
return true;
}
}
return false;
}
vfs::CVirtualLocation::Iterator vfs::CVirtualLocation::iterate()
{
return Iterator(new VFileIterator(this));
}