**********************************************************

** Big Maps Projects code (incl. Multiplayer v1.5 **
**********************************************************
- Merged Big Maps Project code from BMP+MP trunk (Revision: 3340)
o Complete SVN Revision history: https://81.169.133.124/source/ja2/branches/Wanne/JA2%201.13%20MP
- Before THIS merge, I made a branch of the existing 1.13 source
o SVN Branch: https://81.169.133.124/source/ja2/branches/JA2_rev.3336/src
- Removed old VS 6.0 and VS 2003 project and solutions files, because compilation is broken long time ago
- I will add VS 2010 projects and solution file in the next few days

git-svn-id: https://ja2svn.mooo.com/source/ja2/trunk/GameSource/ja2_v1.13/Build@3341 3b4a5df2-a311-0410-b5c6-a8a6f20db521
This commit is contained in:
Wanne
2010-02-28 18:38:52 +00:00
parent a98c44ac78
commit 14750c6903
461 changed files with 23615 additions and 120157 deletions
+124 -108
View File
@@ -32,21 +32,21 @@ namespace szExt
} CVfsFileInStream;
static sz::SRes VfsFileInStream_Read(void *pp, void *buf, size_t *size)
static sz::SRes VfsFileInStream_Read(void *pp, void *buf, ::size_t *size)
{
CVfsFileInStream *p = (CVfsFileInStream *)pp;
vfs::UInt32 to_read = *size;
vfs::UInt32 has_read = 0;
::size_t to_read = *size;
sz::SRes res;
if(p->file.file->Read((vfs::Byte*)buf,to_read, has_read))
try
{
*size = (::size_t)p->file.file->read((vfs::Byte*)buf,to_read);
res = SZ_OK;
}
else
catch(CBasicException &ex)
{
logException(ex);
res = SZ_ERROR_READ;
}
*size = has_read;
return res;
}
@@ -67,17 +67,19 @@ namespace szExt
eSD = vfs::IBaseFile::SD_END;
break;
default:
return ERROR_INVALID_PARAMETER;
return SZ_ERROR_PARAM;
}
vfs::Int32 _pos = (vfs::Int32)(*pos); // only 32 bit
vfs::offset_t _pos = (vfs::offset_t)(*pos);
sz::SRes res;
if(p->file.file->SetReadLocation(_pos,eSD))
try
{
*pos = p->file.file->GetReadLocation();
p->file.file->setReadPosition(_pos,eSD);
*pos = p->file.file->getReadPosition();
res = SZ_OK;
}
else
catch(CBasicException& ex)
{
logException(ex);
res = SZ_ERROR_READ;
}
return res;
@@ -94,6 +96,20 @@ namespace szExt
/********************************************************************************************/
/********************************************************************************************/
vfs::CUncompressed7zLibrary::CUncompressed7zLibrary(
tReadableFile *libraryFile,
vfs::Path const& mountPoint,
bool ownFile,
ObjBlockAllocator<vfs::CLibFile>* allocator)
: vfs::CUncompressedLibraryBase(libraryFile, mountPoint, ownFile), _allocator(allocator)
{
}
vfs::CUncompressed7zLibrary::~CUncompressed7zLibrary()
{
}
#define k_Copy 0
sz::UInt64 GetSum(const sz::UInt64 *values, sz::UInt32 index)
@@ -107,109 +123,109 @@ sz::UInt64 GetSum(const sz::UInt64 *values, sz::UInt32 index)
return sum;
}
bool vfs::CUncompressed7zLibrary::Init()
bool vfs::CUncompressed7zLibrary::init()
{
if(!m_pLibraryFile)
if(!m_libraryFile)
{
return false;
}
szExt::CVfsFileInStream archiveStream;
sz::CLookToRead lookStream;
sz::CSzArEx db;
sz::SRes res;
sz::ISzAlloc allocImp;
sz::ISzAlloc allocTempImp;
if(!m_pLibraryFile->OpenRead())
try
{
szExt::CVfsFileInStream archiveStream;
sz::CLookToRead lookStream;
sz::CSzArEx db;
sz::SRes res;
sz::ISzAlloc allocImp;
sz::ISzAlloc allocTempImp;
vfs::COpenReadFile rfile(m_libraryFile);
archiveStream.file.file = m_libraryFile;
szExt::VfsFileInStream_CreateVTable(&archiveStream);
sz::LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
sz::LookToRead_Init(&lookStream);
allocImp.Alloc = sz::SzAlloc;
allocImp.Free = sz::SzFree;
allocTempImp.Alloc = sz::SzAllocTemp;
allocTempImp.Free = sz::SzFreeTemp;
sz::CrcGenerateTable();
sz::SzArEx_Init(&db);
if( SZ_OK != (res = sz::SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp)) )
{
THROWEXCEPTION(BuildString().add(L"Could not open 7z archive [").add(m_libraryFile->getPath()).add(L"]").get());
}
vfs::TDirectory<ILibrary::tWriteType>* pLD = NULL;
vfs::Path oDir, oFile;
vfs::Path oDirPath;
for(vfs::UInt32 i = 0; i < db.db.NumFiles; i++)
{
sz::CSzFileItem *f = db.db.Files + i;
if (f->IsDir)
{
continue;
}
vfs::Path sPath(f->Name);
sPath.splitLast(oDir,oFile);
oDirPath = m_mountPoint;
if(!oDir.empty())
{
oDirPath += oDir;
}
// determine offset and size
sz::UInt32 folderIndex = db.FileIndexToFolderIndexMap[i];
sz::CSzFolder *folder = db.db.Folders + folderIndex;
sz::UInt64 unpackSizeSpec = sz::SzFolder_GetUnpackSize(folder);
size_t unpackSize = (size_t)unpackSizeSpec;
sz::UInt64 startOffset = sz::SzArEx_GetFolderStreamPos(&db, folderIndex, 0);
//const sz::UInt64 *packSizes = db.db.PackSizes + db.FolderStartPackStreamIndex[folderIndex];
//CSzCoderInfo *coder = &folder->Coders[0];
//if (coder->MethodID == k_Copy)
//{
// UInt32 si = 0;
// UInt64 offset;
// UInt64 inSize;
// offset = GetSum(packSizes, si);
// inSize = packSizes[si];
//}
// get or create according directory object
tDirCatalogue::iterator it = m_dirs.find(oDirPath);
if(it != m_dirs.end())
{
pLD = it->second;
}
else
{
pLD = new vfs::CLibDirectory(oDir,oDirPath);
m_dirs.insert(std::make_pair(oDirPath,pLD));
}
// create file
vfs::CLibFile *pFile = vfs::CLibFile::create(oFile,pLD,this,_allocator);
// add file to directory
THROWIFFALSE( pLD->addFile(pFile), L"" );
// link file data struct to file object
m_fileData.insert(std::make_pair(pFile,SFileData(unpackSize, (vfs::size_t)startOffset)));
}
return true;
}
catch(CBasicException& ex)
{
logException(ex);
return false;
}
archiveStream.file.file = m_pLibraryFile;
szExt::VfsFileInStream_CreateVTable(&archiveStream);
sz::LookToRead_CreateVTable(&lookStream, False);
lookStream.realStream = &archiveStream.s;
sz::LookToRead_Init(&lookStream);
allocImp.Alloc = sz::SzAlloc;
allocImp.Free = sz::SzFree;
allocTempImp.Alloc = sz::SzAllocTemp;
allocTempImp.Free = sz::SzFreeTemp;
sz::CrcGenerateTable();
sz::SzArEx_Init(&db);
if( SZ_OK != (res = sz::SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp)) )
{
std::stringstream wss;
wss << "Could not open 7z archive [" << m_pLibraryFile->GetFullPath()().utf8() << "]";
THROWEXCEPTION(wss.str().c_str());
}
vfs::IDirectory<ILibrary::tWriteType>* pLD = NULL;
vfs::Path oDir, oFile;
vfs::Path oDirPath;
for(vfs::UInt32 i = 0; i < db.db.NumFiles; i++)
{
sz::CSzFileItem *f = db.db.Files + i;
if (f->IsDir)
{
continue;
}
vfs::Path sPath(f->Name);
sPath.SplitLast(oDir,oFile);
oDirPath = m_sMountPoint;
if(!oDir.empty())
{
oDirPath += oDir;
}
// determine offset and size
sz::UInt32 folderIndex = db.FileIndexToFolderIndexMap[i];
sz::CSzFolder *folder = db.db.Folders + folderIndex;
sz::UInt64 unpackSizeSpec = sz::SzFolder_GetUnpackSize(folder);
size_t unpackSize = (size_t)unpackSizeSpec;
sz::UInt64 startOffset = sz::SzArEx_GetFolderStreamPos(&db, folderIndex, 0);
const sz::UInt64 *packSizes = db.db.PackSizes + db.FolderStartPackStreamIndex[folderIndex];
//CSzCoderInfo *coder = &folder->Coders[0];
//if (coder->MethodID == k_Copy)
//{
// UInt32 si = 0;
// UInt64 offset;
// UInt64 inSize;
// offset = GetSum(packSizes, si);
// inSize = packSizes[si];
//}
// get or create according directory object
tDirCatalogue::iterator it = m_catDirs.find(oDirPath);
if(it != m_catDirs.end())
{
pLD = it->second;
}
else
{
pLD = new CLibDirectory(oDir,oDirPath);
m_catDirs.insert(std::make_pair(oDirPath,pLD));
}
// create file
vfs::CLibFile *pFile = vfs::CLibFile::Create(oFile,pLD,this,_allocator);
// add file to directory
if(!pLD->AddFile(pFile))
{
// something is wrong
m_pLibraryFile->Close();
return false;
}
// link file data struct to file object
m_mapLibData.insert(std::pair<tFileType*,sFileData>(pFile,sFileData(unpackSize, (UInt32)startOffset)));
}
return true;
}
+7 -12
View File
@@ -9,18 +9,13 @@ namespace vfs
class CUncompressed7zLibrary : public vfs::CUncompressedLibraryBase
{
public:
CUncompressed7zLibrary(tReadableFile *pLibraryFile,
vfs::Path const& sMountPoint,
bool bOwnFile = false,
ObjBlockAllocator<vfs::CLibFile>* allocator=NULL)
: vfs::CUncompressedLibraryBase(pLibraryFile,sMountPoint,bOwnFile), _allocator(allocator)
{};
virtual ~CUncompressed7zLibrary()
{};
/**
* ILibrary interface
*/
virtual bool Init();
CUncompressed7zLibrary(tReadableFile *libraryFile,
vfs::Path const& mountPoint,
bool ownFile = false,
ObjBlockAllocator<vfs::CLibFile>* allocator=NULL);
virtual ~CUncompressed7zLibrary();
virtual bool init();
private:
ObjBlockAllocator<vfs::CLibFile>* _allocator;
};
+60 -61
View File
@@ -1,4 +1,5 @@
//#define VFS_BASIC_TYPES
#include <cstring>
#include "../vfs_types.h"
#include "vfs_create_7z_library.h"
@@ -22,21 +23,21 @@ extern "C"
/******************************************************************************************/
namespace szExt
{
inline vfs::UInt32 WRITEBYTE(std::ostream& out, sz::Byte const& value)
inline ::size_t WRITEBYTE(std::ostream& out, sz::Byte const& value)
{
out.write((char*)&value,sizeof(sz::Byte));
return 1;
}
template<typename T>
inline vfs::UInt32 WRITEALL(std::ostream& out, T const& value)
inline ::size_t WRITEALL(std::ostream& out, T const& value)
{
out.write((char*)&value,sizeof(T));
return sizeof(T);
}
template<typename T>
inline vfs::UInt32 WRITEBUFFER(std::ostream& out, T* value, size_t num_elements)
inline ::size_t WRITEBUFFER(std::ostream& out, T* value, ::size_t num_elements)
{
out.write((char*)value,sizeof(T) * num_elements);
out.write((char*)value, num_elements*sizeof(T));
return num_elements*sizeof(T);
}
@@ -46,16 +47,16 @@ namespace szExt
* - if the number is smaller than 128, use the extra byte to store the value
*/
template<typename T>
inline vfs::UInt32 WRITE(std::ostream& out, T const& value)
inline ::size_t WRITE(std::ostream& out, T const& value)
{
vfs::UInt32 count = 0;
::size_t count = 0;
sz::Byte data[8];
sz::Byte firstByte = 0;
sz::Byte* b = (sz::Byte*)&value;
size_t SIZE = sizeof(T);
::size_t SIZE = sizeof(T);
b+= SIZE-1;
vfs::Int32 i;
for(i=SIZE-1; i>=0; --i)
for(i = (vfs::Int32)(SIZE-1); i>=0; --i)
{
if( (*b & 0xFF) != 0)
{
@@ -107,7 +108,7 @@ vfs::CCreateUncompressed7zLibrary::~CCreateUncompressed7zLibrary()
m_mapDirInfo.clear();
}
bool vfs::CCreateUncompressed7zLibrary::AddFile(vfs::tReadableFile* pFile)
bool vfs::CCreateUncompressed7zLibrary::addFile(vfs::tReadableFile* pFile)
{
if(!pFile)
{
@@ -121,13 +122,13 @@ bool vfs::CCreateUncompressed7zLibrary::AddFile(vfs::tReadableFile* pFile)
catch(CBasicException &ex)
{
std::wstringstream wss;
wss << L"Could not open File \"" << pFile->GetFullPath()() << L"\"";
wss << L"Could not open File \"" << pFile->getPath()() << L"\"";
RETHROWEXCEPTION(wss.str().c_str(),&ex);
}
SFileInfo fi;
vfs::Path filename = pFile->GetFullPath();
fi.name = filename().c_wcs();
fi.size = pFile->GetFileSize();
vfs::Path filename = pFile->getPath();
fi.name = filename.c_wcs();
fi.size = pFile->getSize();
if(m_lFileInfo.empty())
{
fi.offset = 0;
@@ -138,23 +139,23 @@ bool vfs::CCreateUncompressed7zLibrary::AddFile(vfs::tReadableFile* pFile)
fi.offset = fic.offset + fic.size;
}
std::vector<vfs::Byte> data(fi.size);
UInt32 ui_read;
pFile->Read(&data[0], fi.size, ui_read);
fi.CRC = sz::CrcCalc(&data[0],fi.size);
m_ssFileStream.write(&data[0],fi.size);
typedef std::vector<vfs::Byte> tByteVector;
tByteVector data( (tByteVector::size_type)fi.size );
THROWIFFALSE(fi.size == pFile->read(&data[0], (vfs::size_t)fi.size), L"");
fi.CRC = sz::CrcCalc(&data[0],(::size_t)fi.size);
m_ssFileStream.write(&data[0],(std::streamsize)fi.size);
m_lFileInfo.push_back(fi);
vfs::Path path,dummy;
filename.SplitLast(path,dummy);
filename.splitLast(path,dummy);
if(!path.empty())
{
tDirInfo::iterator it_find = m_mapDirInfo.find(path().c_wcs());
tDirInfo::iterator it_find = m_mapDirInfo.find(path.c_wcs());
if(it_find == m_mapDirInfo.end())
{
SFileInfo dir;
dir.name = path().c_wcs();
dir.name = path.c_wcs();
dir.offset = 0;
dir.size = 0;
dir.time_creation = 0;
@@ -166,12 +167,12 @@ bool vfs::CCreateUncompressed7zLibrary::AddFile(vfs::tReadableFile* pFile)
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteLibrary(vfs::Path const& sLibName)
bool vfs::CCreateUncompressed7zLibrary::writeLibrary(vfs::Path const& sLibName)
{
vfs::COpenWriteFile outfile(sLibName,true);
return WriteLibrary(&outfile.file());
return writeLibrary(&outfile.file());
}
bool vfs::CCreateUncompressed7zLibrary::WriteLibrary(vfs::tWriteableFile* pFile)
bool vfs::CCreateUncompressed7zLibrary::writeLibrary(vfs::tWritableFile* pFile)
{
if(!pFile)
{
@@ -182,32 +183,30 @@ bool vfs::CCreateUncompressed7zLibrary::WriteLibrary(vfs::tWriteableFile* pFile)
return false;
}
m_pLibFile = pFile;
if(!m_pLibFile->IsOpenWrite() && !m_pLibFile->OpenWrite(true,true))
if(!m_pLibFile->isOpenWrite() && !m_pLibFile->openWrite(true,true))
{
return false;
}
//
WriteNextHeader(m_ssInfoStream);
writeNextHeader(m_ssInfoStream);
//
std::stringstream ssSigHeader;
WriteSignatureHeader(ssSigHeader);
writeSignatureHeader(ssSigHeader);
//
UInt32 written;
m_pLibFile->write(ssSigHeader.str().c_str() , (vfs::size_t)ssSigHeader.str().length());
//
m_pLibFile->Write(ssSigHeader.str().c_str(),ssSigHeader.str().length(), written);
m_pLibFile->write(m_ssFileStream.str().c_str() , (vfs::size_t)m_ssFileStream.str().length());
//
m_pLibFile->Write(m_ssFileStream.str().c_str(),m_ssFileStream.str().length(), written);
//
m_pLibFile->Write(m_ssInfoStream.str().c_str(),m_ssInfoStream.str().length(), written);
m_pLibFile->write(m_ssInfoStream.str().c_str() , (vfs::size_t)m_ssInfoStream.str().length());
return true;
}
/**************************************************************************************/
bool vfs::CCreateUncompressed7zLibrary::WriteSignatureHeader(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeSignatureHeader(std::ostream& out)
{
vfs::UInt32 count=0;
::size_t count=0;
// #define k7zSignatureSize -> not in namespace sz
count += szExt::WRITEBUFFER(out, sz::k7zSignature, k7zSignatureSize);
@@ -221,7 +220,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteSignatureHeader(std::ostream& out)
SFileInfo const& fi = m_lFileInfo.back();
sz::UInt64 NextHeaderOffset = fi.offset + fi.size;
sz::UInt64 NextHeaderSize = m_ssInfoStream.str().length()*sizeof(char);
NextHeaderCRC = sz::CrcCalc(m_ssInfoStream.str().c_str(),(size_t)NextHeaderSize);
NextHeaderCRC = sz::CrcCalc(m_ssInfoStream.str().c_str(),(::size_t)NextHeaderSize);
std::stringstream sstemp;
count += szExt::WRITEALL(sstemp, (sz::UInt64)NextHeaderOffset );
@@ -234,7 +233,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteSignatureHeader(std::ostream& out)
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteNextHeader(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeNextHeader(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdHeader);
@@ -243,25 +242,25 @@ bool vfs::CCreateUncompressed7zLibrary::WriteNextHeader(std::ostream& out)
// this->WriteAdditionalStreamsInfo(out)
//
this->WriteMainStreamsInfo(out);
this->writeMainStreamsInfo(out);
//
this->WriteFilesInfo(out);
this->writeFilesInfo(out);
szExt::WRITE(out, (sz::Byte)sz::k7zIdEnd );
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteMainStreamsInfo(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeMainStreamsInfo(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdMainStreamsInfo );
this->WritePackInfo(out);
this->writePackInfo(out);
this->WriteUnPackInfo(out);
this->writeUnPackInfo(out);
this->WriteSubStreamsInfo(out);
this->writeSubStreamsInfo(out);
szExt::WRITE(out, (sz::Byte)sz::k7zIdEnd );
@@ -269,7 +268,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteMainStreamsInfo(std::ostream& out)
}
bool vfs::CCreateUncompressed7zLibrary::WritePackInfo(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writePackInfo(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdPackInfo );
szExt::WRITE(out, (sz::UInt64)0 ); // data offset
@@ -285,7 +284,7 @@ bool vfs::CCreateUncompressed7zLibrary::WritePackInfo(std::ostream& out)
szExt::WRITE(out, (sz::Byte)sz::k7zIdEnd );
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteUnPackInfo(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeUnPackInfo(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdUnpackInfo );
@@ -296,7 +295,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteUnPackInfo(std::ostream& out)
std::list<SFileInfo>::iterator fit = m_lFileInfo.begin();
for(;fit != m_lFileInfo.end(); ++fit)
{
this->WriteFolder(out);
this->writeFolder(out);
}
szExt::WRITE(out, (sz::Byte)sz::k7zIdCodersUnpackSize );
@@ -310,7 +309,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteUnPackInfo(std::ostream& out)
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteSubStreamsInfo(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeSubStreamsInfo(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdSubStreamsInfo );
szExt::WRITE(out, (sz::Byte)sz::k7zIdCRC );
@@ -326,7 +325,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteSubStreamsInfo(std::ostream& out)
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteFolder(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeFolder(std::ostream& out)
{
szExt::WRITE(out, (sz::UInt32)1 ); // NumCoders
@@ -336,7 +335,7 @@ bool vfs::CCreateUncompressed7zLibrary::WriteFolder(std::ostream& out)
return true;
}
bool vfs::CCreateUncompressed7zLibrary::WriteFilesInfo(std::ostream& out)
bool vfs::CCreateUncompressed7zLibrary::writeFilesInfo(std::ostream& out)
{
szExt::WRITE(out, (sz::Byte)sz::k7zIdFilesInfo );
@@ -346,34 +345,34 @@ bool vfs::CCreateUncompressed7zLibrary::WriteFilesInfo(std::ostream& out)
// empty stream -> pack info in bit-vector
szExt::WRITE(out, (sz::Byte)sz::k7zIdEmptyStream );
sz::UInt64 num_empty64 = num_files/8 + (num_files%8 == 0 ? 0 : 1);
size_t num_empty = (size_t)num_empty64;
::size_t num_empty = (::size_t)num_empty64;
THROWIFFALSE(num_empty == num_empty64, L"WTF");
sz::Byte *empty_vector = new sz::Byte[num_empty];
memset(empty_vector,0,num_empty);
for(vfs::UInt32 e=m_lFileInfo.size(); e < num_files; ++e)
memset(empty_vector,0,(::size_t)num_empty);
for(::size_t e=m_lFileInfo.size(); e < num_files; ++e)
{
size_t index = e / 8;
::size_t index = e / 8;
empty_vector[index] |= 1 << (7 - e%8);
}
szExt::WRITE(out, (sz::UInt64)num_empty ); // size
szExt::WRITEBUFFER(out, empty_vector, num_empty);
szExt::WRITEBUFFER(out, empty_vector, (::size_t)num_empty);
delete[] empty_vector;
// names
szExt::WRITE(out, (sz::Byte)sz::k7zIdName );
sz::UInt32 count = 0;
::size_t count = 0;
std::stringstream name_stream;
count += szExt::WRITE(name_stream, (sz::Byte)0 ); // switch
std::list<SFileInfo>::iterator fit = m_lFileInfo.begin();
for(;fit != m_lFileInfo.end(); ++fit)
{
count += this->WriteFileName(name_stream, fit->name);
count += (::size_t)this->writeFileName(name_stream, fit->name);
}
std::map<utf8string::str_t,SFileInfo>::iterator dit = m_mapDirInfo.begin();
for(;dit != m_mapDirInfo.end(); ++dit)
{
count += this->WriteFileName(name_stream, dit->second.name);
count += (::size_t)this->writeFileName(name_stream, dit->second.name);
}
szExt::WRITE(out, (sz::UInt64)count ); // size
szExt::WRITEBUFFER(out, name_stream.str().c_str(), name_stream.str().length() );
@@ -390,13 +389,13 @@ bool vfs::CCreateUncompressed7zLibrary::WriteFilesInfo(std::ostream& out)
return true;
}
vfs::UInt32 vfs::CCreateUncompressed7zLibrary::WriteFileName(std::ostream& out, utf8string const& filename)
vfs::size_t vfs::CCreateUncompressed7zLibrary::writeFileName(std::ostream& out, utf8string const& filename)
{
vfs::UInt32 count = 0;
::size_t count = 0;
THROWIFFALSE(filename.length(), L"zero length name");
count += szExt::WRITEBUFFER(out, &filename.c_wcs().at(0), filename.length());
count += szExt::WRITEBUFFER(out, filename.c_str(), filename.length());
count += szExt::WRITE(out, (sz::Byte)0);
count += szExt::WRITE(out, (sz::Byte)0);
return count;
return (vfs::size_t)count;
}
+18 -20
View File
@@ -1,8 +1,6 @@
#ifndef _VFS_CREATE_7Z_LIBRARY_H_
#define _VFS_CREATE_7Z_LIBRARY_H_
//#include "Interface/vfs_library_interface.h"
//#include "Interface/vfs_directory_interface.h"
#include "../Interface/vfs_file_interface.h"
#include <sstream>
#include <map>
@@ -10,30 +8,30 @@
namespace vfs
{
class CCreateUncompressed7zLibrary
class VFS_API CCreateUncompressed7zLibrary
{
public:
CCreateUncompressed7zLibrary();
virtual ~CCreateUncompressed7zLibrary();
bool AddFile(vfs::tReadableFile* pFile);
bool addFile(vfs::tReadableFile* pFile);
bool WriteLibrary(vfs::Path const& sLibName);
bool WriteLibrary(vfs::tWriteableFile* pFile);
bool writeLibrary(vfs::Path const& sLibName);
bool writeLibrary(vfs::tWritableFile* pFile);
protected:
bool WriteSignatureHeader(std::ostream& out);
bool WriteNextHeader(std::ostream& out);
bool WriteMainStreamsInfo(std::ostream &out);
bool WritePackInfo(std::ostream& out);
bool WriteUnPackInfo(std::ostream& out);
bool WriteSubStreamsInfo(std::ostream& out);
bool WriteFolder(std::ostream& out);
bool WriteFilesInfo(std::ostream& out);
bool writeSignatureHeader(std::ostream& out);
bool writeNextHeader(std::ostream& out);
bool writeMainStreamsInfo(std::ostream &out);
bool writePackInfo(std::ostream& out);
bool writeUnPackInfo(std::ostream& out);
bool writeSubStreamsInfo(std::ostream& out);
bool writeFolder(std::ostream& out);
bool writeFilesInfo(std::ostream& out);
private:
unsigned int WriteFileName(std::ostream& out, utf8string const& filename);
vfs::size_t writeFileName(std::ostream& out, utf8string const& filename);
protected:
vfs::tWriteableFile* m_pLibFile;
vfs::tWritableFile* m_pLibFile;
struct SFileInfo
{
@@ -42,10 +40,10 @@ namespace vfs
{};
//////
utf8string::str_t name;
UInt32 CRC;
UInt64 offset;
UInt64 size;
UInt64 time_creation,time_last_access,time_write;
vfs::UInt32 CRC;
vfs::UInt64 offset;
vfs::UInt64 size;
vfs::UInt64 time_creation,time_last_access,time_write;
};
typedef std::map<utf8string::str_t,SFileInfo> tDirInfo;
+420 -328
View File
@@ -1,131 +1,158 @@
#include "vfs_directory_tree.h"
#include "../File/vfs_dir_file.h"
#include "../Interface/vfs_location_aware_file_interface.h"
#include "../iteratedir.h"
#include "../os_functions.h"
#include <queue>
#include <list>
#include <set>
const utf8string::str_t CONST_EXT_TXT = L".TXT";
const utf8string::str_t CONST_EXT_LOG = L".LOG";
const utf8string::str_t CONST_EXT_INI = L".INI";
#define ERROR_FILE(msg) BuildString().add((msg)).add(L" : ").add(pFile->getPath()).get()
namespace vfs
{
class CSubDir : public vfs::IDirectory<vfs::CDirectoryTree::tWriteType>
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;
class IterImpl : public tClassType::Iterator::IImplemetation
{
public:
IterImpl(CSubDir& dir);
virtual ~IterImpl();
virtual tFileType* value();
virtual void next();
private:
CSubDir& _dir;
tFileCatalogue::iterator _iter;
};
public:
CSubDir(vfs::Path const& sMountPoint, vfs::Path const& sRealPath)
: vfs::IDirectory<vfs::CDirectoryTree::tWriteType>(sMountPoint,sRealPath)
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)
{
THROWIFFALSE(_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 ~CSubDir();
virtual bool FileExists(vfs::Path const& rFileName);
virtual vfs::IBaseFile* GetFile(vfs::Path const& rFileName);
virtual tFileType* GetFileTyped(vfs::Path const& rFileName);
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 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 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 void getSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual Iterator begin();
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);
}
/********************************************************/
vfs::CSubDir::IterImpl::IterImpl(CSubDir& dir)
: _dir(dir)
{
_iter = _dir.m_mapFiles.begin();
}
vfs::CSubDir::IterImpl::~IterImpl()
{
}
vfs::CSubDir::tFileType* vfs::CSubDir::IterImpl::value()
{
if(_iter != _dir.m_mapFiles.end())
{
return _iter->second;
}
return NULL;
}
void vfs::CSubDir::IterImpl::next()
{
if(_iter != _dir.m_mapFiles.end())
{
_iter++;
}
}
/********************************************************/
vfs::CSubDir::~CSubDir()
template<typename WriteType>
vfs::TSubDir<WriteType>::~TSubDir()
{
tFileCatalogue::iterator it = m_mapFiles.begin();
typename tFileCatalogue::iterator it = m_mapFiles.begin();
for(; it != m_mapFiles.end(); ++it)
{
if(it->second)
{
it->second->Close();
it->second->close();
delete it->second;
}
}
m_mapFiles.clear();
}
bool vfs::CSubDir::FileExists(vfs::Path const& sFileName)
template<typename WriteType>
bool vfs::TSubDir<WriteType>::fileExists(vfs::Path const& sFileName)
{
tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
typename tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
bool success = (it != m_mapFiles.end()) && (it->second != NULL);
return success;
}
vfs::IBaseFile* vfs::CSubDir::GetFile(vfs::Path const& sFileName)
template<typename WriteType>
vfs::IBaseFile* vfs::TSubDir<WriteType>::getFile(vfs::Path const& sFileName)
{
return GetFileTyped(sFileName);
return getFileTyped(sFileName);
}
vfs::CSubDir::tFileType* vfs::CSubDir::GetFileTyped(vfs::Path const& sFileName)
template<typename WriteType>
typename vfs::TSubDir<WriteType>::tFileType* vfs::TSubDir<WriteType>::getFileTyped(vfs::Path const& sFileName)
{
tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
typename tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
if(it != m_mapFiles.end())
{
CSubDir::tFileType* file = it->second;
return file;
return it->second;
}
return NULL;
}
vfs::CSubDir::tFileType* vfs::CSubDir::AddFile(vfs::Path const& sFilename, bool bDeleteOldFile)
template<>
vfs::TSubDir<vfs::IWriteType>::tFileType* vfs::TSubDir<vfs::IWriteType>::addFile(vfs::Path const& sFilename, bool bDeleteOldFile)
{
#if 1
tFileType* pFile = m_mapFiles[sFilename];
#else
CSubDir::tFileCatalogue::iterator it;
tFileType* pFile = ((it=m_mapFiles.find(sFilename)) != m_mapFiles.end()) ? it->second : NULL;
#endif
if(pFile)
{
if(!bDeleteOldFile)
@@ -135,47 +162,55 @@ vfs::CSubDir::tFileType* vfs::CSubDir::AddFile(vfs::Path const& sFilename, bool
}
delete pFile;
}
utf8string sExtension;
sFilename.Extension(sExtension);
if(sExtension == CONST_EXT_TXT || sExtension == CONST_EXT_LOG || sExtension == CONST_EXT_INI)
{
pFile = new vfs::CVFSTextFile(sFilename,this);
}
else
{
pFile = new vfs::CVFSFile(sFilename,this);
}
#if 1
pFile = new vfs::CReadOnlyDirFile(sFilename,this);
m_mapFiles[sFilename] = pFile;
#else
m_mapFiles.insert(std::make_pair(sFilename,pFile));
#endif
return pFile;
}
bool vfs::CSubDir::AddFile(tFileType* pFile, bool bDeleteOldFile)
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->GetFileName()];
tFileType* pOldFile = m_mapFiles[pFile->getName()];
if( pOldFile && (pOldFile != pFile) )
{
if(bDeleteOldFile)
{
pOldFile->Close();
pOldFile->close();
delete pOldFile;
}
}
m_mapFiles[pFile->GetFileName()] = pFile;
m_mapFiles[pFile->getName()] = pFile;
return true;
}
bool vfs::CSubDir::DeleteDirectory(vfs::Path const& sDirPath)
template<>
bool vfs::TSubDir<vfs::IWritable>::deleteDirectory(vfs::Path const& sDirPath)
{
if( !(m_sMountPoint == sDirPath) )
if( !(this->m_mountPoint == sDirPath) )
{
return false;
}
@@ -185,12 +220,12 @@ bool vfs::CSubDir::DeleteDirectory(vfs::Path const& sDirPath)
tFileType* pFile = it->second;
if(pFile)
{
pFile->Close();
if(!pFile->Delete())
pFile->close();
if(!pFile->deleteFile())
{
std::wstringstream wss;
wss << L"Could not delete file \"" << pFile->GetFullPath()() << L"\"";
THROWEXCEPTION(wss.str().c_str());
wss << L"Could not delete file \"" << pFile->getPath()() << L"\"";
THROWEXCEPTION( ERROR_FILE(L"Could not delete file") );
}
delete pFile;
}
@@ -199,7 +234,14 @@ bool vfs::CSubDir::DeleteDirectory(vfs::Path const& sDirPath)
return true;
}
bool vfs::CSubDir::DeleteFileFromDirectory(vfs::Path const& rFileName)
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() )
@@ -207,8 +249,8 @@ bool vfs::CSubDir::DeleteFileFromDirectory(vfs::Path const& rFileName)
tFileType* pFile = it->second;
if(pFile)
{
pFile->Close();
THROWIFFALSE(pFile->Delete(), L"Could not delete file");
pFile->close();
THROWIFFALSE(pFile->deleteFile(), ERROR_FILE(L"Could not delete file"));
delete pFile;
}
m_mapFiles.erase(it);
@@ -217,252 +259,68 @@ bool vfs::CSubDir::DeleteFileFromDirectory(vfs::Path const& rFileName)
return false;
}
bool vfs::CSubDir::CreateSubDirectory(vfs::Path const& sSubDirPath)
template<>
bool vfs::TSubDir<vfs::IWriteType>::deleteFileFromDirectory(vfs::Path const& rFileName)
{
return false;
}
void vfs::CSubDir::GetSubDirList(std::list<vfs::Path>& rlSubDirs)
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)
{
}
vfs::CSubDir::Iterator vfs::CSubDir::begin()
template<typename WriteType>
typename vfs::TSubDir<WriteType>::Iterator vfs::TSubDir<WriteType>::begin()
{
Iterator it;
{
it = Iterator(new IterImpl(*this));
}
return it;
return Iterator(new IterImpl(this));
}
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
vfs::CDirectoryTree::~CDirectoryTree()
template<typename WriteType>
class vfs::TDirectoryTree<WriteType>::IterImpl : public vfs::IBaseLocation::Iterator::IImplementation
{
tDirCatalogue::iterator it = m_catDirs.begin();
for(;it != m_catDirs.end(); ++it)
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()
{
delete it->second;
it->second = NULL;
IterImpl* iter = new IterImpl(_tree);
iter->_subdir_iter = _subdir_iter;
iter->_file_iter = _file_iter;
return iter;
}
m_catDirs.clear();
}
private:
void operator=(typename vfs::TDirectoryTree<WriteType>::IterImpl const& tree);
tDirTree& _tree;
typename tDirTree::tDirCatalogue::iterator _subdir_iter;
typename tDirTree::tLocationType::Iterator _file_iter;
};
bool vfs::CDirectoryTree::Init()
{
// contains local path
typedef std::pair<vfs::Path,CSubDir*> tDirs;
std::queue<tDirs> qSubDirs;
qSubDirs.push(tDirs(vfs::Path(vfs::Const::EMPTY()),new CSubDir(m_sMountPoint,m_sRealPath)));
m_catDirs[m_sMountPoint] = qSubDirs.front().second;
utf8string sFilename;
CSubDir *pCurrentDir;
vfs::Path oCurDir;
while(!qSubDirs.empty())
{
pCurrentDir = qSubDirs.front().second;
oCurDir = m_sRealPath;
if( !qSubDirs.front().first.empty())
{
oCurDir += qSubDirs.front().first;
}
try
{
os::CIterateDirectory::EFileAttribute eFA;
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 == os::CIterateDirectory::FA_DIRECTORY)
{
vfs::Path sLocal = qSubDirs.front().first + sFilename;
vfs::Path temp = m_sMountPoint+sLocal;
CSubDir *pNewDir = new CSubDir(sLocal, m_sRealPath+sLocal);
qSubDirs.push(tDirs(sLocal,pNewDir));
m_catDirs[temp] = pNewDir;
}
else
{
pCurrentDir->AddFile(vfs::Path(sFilename));
}
}
}
catch(CBasicException &ex)
{
// probably directory doesn't exist. abort or continue???
// -> abort AND continue
LogException(ex);
return false;
}
qSubDirs.pop();
}
return true;
}
vfs::CDirectoryTree::tFileType* vfs::CDirectoryTree::AddFile(vfs::Path const& sFilename, bool bDeleteOldFile)
{
vfs::Path sDir,sFile;
sFilename.SplitLast(sDir,sFile);
tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
vfs::Path sTemp,sCreateDir,sLeft,sRight = sDir;
while(sRight.SplitFirst(sLeft,sTemp))
{
sRight = sTemp;
sCreateDir += sLeft;
CreateSubDirectory(sCreateDir);
}
it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
return NULL;
}
}
if(it->second)
{
vfs::CDirectoryTree::tFileType* file = it->second->AddFile(sFile,bDeleteOldFile);
return file;
}
return NULL;
}
bool vfs::CDirectoryTree::AddFile(tFileType* pFile, bool bDeleteOldFile)
{
// no files from outside
// these files are not connected with the correct directory object
return false;
}
bool vfs::CDirectoryTree::DeleteDirectory(vfs::Path const& sDirPath)
{
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;
}
bool vfs::CDirectoryTree::DeleteFileFromDirectory(vfs::Path const& sFileName)
{
vfs::Path sDir,sFile;
sFileName.SplitLast(sDir,sFile);
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
*/
bool vfs::CDirectoryTree::FileExists(vfs::Path const& sFileName)
{
vfs::Path sDir, sFile;
sFileName.SplitLast(sDir, sFile);
tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
// no such directory
return false;
}
if(it->second)
{
bool success = it->second->FileExists(sFile);
return success;
}
return false;
}
vfs::IBaseFile* vfs::CDirectoryTree::GetFile(vfs::Path const& sFileName)
{
return GetFileTyped(sFileName);
}
vfs::CDirectoryTree::tFileType* vfs::CDirectoryTree::GetFileTyped(vfs::Path const& sFileName)
{
vfs::Path sDir, sFile;
sFileName.SplitLast(sDir, sFile);
//tDirCatalogue::iterator it = m_catDirs.find(sDir);
tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it == m_catDirs.end())
{
// no such directory
return NULL;
}
if(it->second)
{
vfs::CDirectoryTree::tFileType* file = it->second->GetFileTyped(sFile);
return file;
}
return NULL;
}
bool vfs::CDirectoryTree::CreateSubDirectory(vfs::Path const& sSubDirPath)
{
if(os::CreateRealDirecory( m_sRealPath + sSubDirPath ))
{
if( m_catDirs[sSubDirPath] == NULL)
{
CSubDir *pNewDir = new CSubDir(sSubDirPath, m_sRealPath + sSubDirPath);
m_catDirs[sSubDirPath] = pNewDir;
}
return true;
}
return false;
}
void vfs::CDirectoryTree::GetSubDirList(std::list<vfs::Path>& rlSubDirs)
{
tDirCatalogue::iterator it = m_catDirs.begin();
for(;it != m_catDirs.end(); ++it)
{
rlSubDirs.push_back(it->first);
}
}
vfs::CDirectoryTree::Iterator vfs::CDirectoryTree::begin()
{
return Iterator(new IterImpl(*this));
}
/*****************************************************************************/
/*****************************************************************************/
vfs::CDirectoryTree::IterImpl::IterImpl(CDirectoryTree& tree)
: _tree(tree)
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())
{
CSubDir *sdir = dynamic_cast<CSubDir*>(_subdir_iter->second);
CSubDir::Iterator it = sdir->begin();
TSubDir<WriteType> *sdir = dynamic_cast<TSubDir<WriteType>*>(_subdir_iter->second);
typename TSubDir<WriteType>::Iterator it = sdir->begin();
_file_iter = it;
if(_file_iter.end())
{
@@ -471,20 +329,23 @@ vfs::CDirectoryTree::IterImpl::IterImpl(CDirectoryTree& tree)
}
}
vfs::CDirectoryTree::IterImpl::~IterImpl()
template<typename WriteType>
vfs::TDirectoryTree<WriteType>::IterImpl::~IterImpl()
{
}
vfs::CDirectoryTree::tFileType* vfs::CDirectoryTree::IterImpl::value()
template<typename WriteType>
typename vfs::TDirectoryTree<WriteType>::tFileType* vfs::TDirectoryTree<WriteType>::IterImpl::value()
{
if(!_file_iter.end())
{
return static_cast<tFileType*>(_file_iter.value());
return static_cast<vfs::TDirectoryTree<WriteType>::tFileType*>(_file_iter.value());
}
return NULL;
}
void vfs::CDirectoryTree::IterImpl::next()
template<typename WriteType>
void vfs::TDirectoryTree<WriteType>::IterImpl::next()
{
if(!_file_iter.end())
{
@@ -509,5 +370,236 @@ void vfs::CDirectoryTree::IterImpl::next()
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;
utf8string 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
{
os::CIterateDirectory::EFileAttribute eFA;
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 == 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(CBasicException &ex)
{
// probably directory doesn't exist. abort or continue???
// -> abort AND continue
logException(ex);
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;
this->createSubDirectory(sCreateDir);
}
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(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
+26 -33
View File
@@ -15,55 +15,48 @@ namespace vfs
/**
* IDirectory<read,write>
*/
class CDirectoryTree : public vfs::IDirectory<vfs::IWriteable>
template<typename WriteType>
class TDirectoryTree : public vfs::TDirectory<WriteType>
{
typedef std::map<vfs::Path, vfs::IDirectory<CDirectoryTree::tWriteType>*, vfs::Path::Less> tDirCatalogue;
typedef std::map<vfs::Path, vfs::TDirectory<typename TDirectoryTree<WriteType>::tWriteType>*, vfs::Path::Less> tDirCatalogue;
class IterImpl : public tClassType::Iterator::IImplemetation
{
public:
IterImpl(CDirectoryTree& tree);
virtual ~IterImpl();
virtual tFileType* value();
virtual void next();
private:
CDirectoryTree& _tree;
tDirCatalogue::iterator _subdir_iter;
tClassType::Iterator _file_iter;
};
public:
CDirectoryTree(vfs::Path const& sMountPoint, vfs::Path const& sRealPath)
: vfs::IDirectory<vfs::IWriteable>(sMountPoint,sRealPath)
{};
virtual ~CDirectoryTree();
class IterImpl;
public:
typedef vfs::TDirectory<WriteType> tBaseClass;
typedef typename tBaseClass::tWriteType tWriteType;
typedef typename tBaseClass::tFileType tFileType;
bool Init();
typedef TIterator<vfs::IBaseFile> Iterator;
TDirectoryTree(vfs::Path const& sMountPoint, vfs::Path const& sRealPath);
virtual ~TDirectoryTree();
bool init();
/**
* IDirectory interface
* TDirectory interface
*/
virtual tFileType* AddFile(vfs::Path const& sFilename, bool bDeleteOldFile=false);
virtual bool AddFile(tFileType* pFile, bool bDeleteOldFile=false);
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 bool createSubDirectory(vfs::Path const& sSubDirPath);
virtual bool deleteDirectory(vfs::Path const& sDirPath);
virtual bool deleteFileFromDirectory(vfs::Path const& sFileName);
/**
* IVFSLocation interface
* TVFSLocation interface
*/
virtual bool FileExists(vfs::Path const& sFileName);
virtual vfs::IBaseFile* GetFile(vfs::Path const& sFileName);
virtual tFileType* GetFileTyped(vfs::Path const& sFileName);
virtual void GetSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual bool fileExists(vfs::Path const& sFileName);
virtual vfs::IBaseFile* getFile(vfs::Path const& sFileName);
virtual tFileType* getFileTyped(vfs::Path const& sFileName);
virtual void getSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual Iterator begin();
protected:
tDirCatalogue m_catDirs;
};
typedef TDirectoryTree<vfs::IWritable> CDirectoryTree;
typedef TDirectoryTree<vfs::IWriteType> CReadOnlyDirectoryTree;
} // end namespace
+117 -90
View File
@@ -1,104 +1,172 @@
#include "vfs_lib_dir.h"
#include "../Tools/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_mapFiles.begin();
for(; it != m_mapFiles.end(); ++it)
tFileCatalogue::iterator it = m_files.begin();
for(; it != m_files.end(); ++it)
{
// don't delete objects here
//delete it->second;
}
m_mapFiles.clear();
m_files.clear();
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::AddFile(vfs::Path const& sFilename, bool bDeleteOldFile)
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::addFile(vfs::Path const& filename, bool deleteOldFile)
{
return NULL;
}
bool vfs::CLibDirectory::AddFile(tFileType* pFile, bool bDeleteOldFile)
bool vfs::CLibDirectory::addFile(tFileType* file, bool deleteOldFile)
{
if(!pFile)
if(!file)
{
return false;
}
vfs::Path const& sName = pFile->GetFileName();
tFileType* pFileOld = m_mapFiles[sName];
if(pFileOld && (pFileOld != pFile) )
vfs::Path const& name = file->getName();
tFileType* oldFile = m_files[name];
if(oldFile && (oldFile != file) )
{
if(bDeleteOldFile)
if(deleteOldFile)
{
delete pFileOld;
m_mapFiles[sName] = pFile;
delete oldFile;
m_files[name] = file;
}
else
{
return false;
}
}
m_mapFiles[sName] = pFile;
m_files[name] = file;
return true;
}
bool vfs::CLibDirectory::DeleteDirectory(vfs::Path const& sDirPath)
bool vfs::CLibDirectory::deleteDirectory(vfs::Path const& dirPath)
{
if( !(m_sMountPoint == sDirPath) )
{
return false;
}
if(IsWriteable())
{
tFileCatalogue::iterator it = m_mapFiles.begin();
for(; it != m_mapFiles.end(); ++it)
{
delete it->second;
}
m_mapFiles.clear();
return true;
}
//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;
//}
CLog log(L"errors.log");
log << "called 'vfs::CLibDirectory::deleteDirectory' which doesn't implement the IWritable interface" << CLog::ENDL;
return false;
}
bool vfs::CLibDirectory::DeleteFileFromDirectory(vfs::Path const& sFileName)
bool vfs::CLibDirectory::deleteFileFromDirectory(vfs::Path const& filename)
{
if(IsWriteable())
{
tFileCatalogue::iterator it = m_mapFiles.find(sFileName);
if(it != m_mapFiles.end())
{
delete it->second;
m_mapFiles.erase(it);
return true;
}
}
//if(implementsWritable())
//{
// tFileCatalogue::iterator it = m_files.find(filename);
// if(it != m_files.end())
// {
// delete it->second;
// m_files.erase(it);
// return true;
// }
//}
CLog log(L"errors.log");
log << "called 'vfs::CLibDirectory::deleteFileFromDirectory' which doesn't implement the IWritable interface" << CLog::ENDL;
return false;
}
bool vfs::CLibDirectory::FileExists(vfs::Path const& sFileName)
bool vfs::CLibDirectory::fileExists(vfs::Path const& filename)
{
bool success = (m_mapFiles[sFileName] != NULL);
return success;
return (m_files[filename] != NULL);
}
vfs::IBaseFile* vfs::CLibDirectory::GetFile(vfs::Path const& sFileName)
vfs::IBaseFile* vfs::CLibDirectory::getFile(vfs::Path const& filename)
{
return GetFileTyped(sFileName);
return getFileTyped(filename);
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::GetFileTyped(vfs::Path const& sFileName)
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::getFileTyped(vfs::Path const& filename)
{
CLibDirectory::tFileType* file = m_mapFiles[sFileName];
return file;
tFileCatalogue::iterator it = m_files.find(filename);
if(it != m_files.end())
{
return it->second;
}
return NULL;
}
bool vfs::CLibDirectory::CreateSubDirectory(vfs::Path const& sSubDirPath)
bool vfs::CLibDirectory::createSubDirectory(vfs::Path const& subDirPath)
{
// libraries are readonly
return false;
}
void vfs::CLibDirectory::GetSubDirList(std::list<vfs::Path>& rlSubDirs)
void vfs::CLibDirectory::getSubDirList(std::list<vfs::Path>& rlSubDirs)
{
// nothing
}
vfs::CLibDirectory::Iterator vfs::CLibDirectory::begin()
@@ -108,44 +176,3 @@ vfs::CLibDirectory::Iterator vfs::CLibDirectory::begin()
/***************************************************************************/
/***************************************************************************/
vfs::CLibDirectory::IterImpl::IterImpl(CLibDirectory& lib)
: _lib(lib)
{
_iter = _lib.m_mapFiles.begin();
}
vfs::CLibDirectory::IterImpl::~IterImpl()
{
}
vfs::CLibDirectory::tFileType* vfs::CLibDirectory::IterImpl::value()
{
if(_iter != _lib.m_mapFiles.end())
{
return _iter->second;
}
return NULL;
}
void vfs::CLibDirectory::IterImpl::next()
{
if(_iter != _lib.m_mapFiles.end())
{
_iter++;
}
}
/***************************************************************************/
/***************************************************************************/
static void tesst()
{
vfs::CLibDirectory *dir = new vfs::CLibDirectory(vfs::Path("test"), vfs::Path("test2"));
vfs::CLibDirectory::Iterator it = dir->begin();
while(!it.end())
{
vfs::CLibDirectory::tFileType* file = static_cast<vfs::CLibDirectory::tFileType*>(it.value());
it.next();
}
}
+16 -27
View File
@@ -6,47 +6,36 @@
namespace vfs
{
class CLibDirectory : public vfs::IDirectory<vfs::IWriteType>
class CLibDirectory : public vfs::TDirectory<vfs::IWriteType>
{
typedef vfs::TDirectory<vfs::IWriteType> tBaseClass;
typedef std::map<vfs::Path, tFileType*, vfs::Path::Less> tFileCatalogue;
class IterImpl : public tClassType::Iterator::IImplemetation
{
public:
IterImpl(CLibDirectory& lib);
virtual ~IterImpl();
virtual tFileType* value();
virtual void next();
private:
CLibDirectory& _lib;
tFileCatalogue::iterator _iter;
};
class IterImpl;
public:
CLibDirectory(vfs::Path const& sLocalPath, vfs::Path const& sRealPath)
: vfs::IDirectory<vfs::IWriteType>(sLocalPath,sRealPath)
{};
CLibDirectory(vfs::Path const& sLocalPath, vfs::Path const& sRealPath);
virtual ~CLibDirectory();
/**
* IDirectory interface
* TDirectory interface
*/
virtual tFileType* AddFile(vfs::Path const& sFilename, bool bDeleteOldFile=false);
virtual bool AddFile(tFileType* pFile, bool bDeleteOldFile=false);
virtual bool DeleteFileFromDirectory(vfs::Path const& sFileName);
virtual bool CreateSubDirectory(vfs::Path const& sSubDirPath);
virtual bool DeleteDirectory(vfs::Path const& sDirPath);
virtual tFileType* addFile(vfs::Path const& filename, bool deleteOldFile=false);
virtual bool addFile(tFileType* file, bool deleteOldFile=false);
virtual bool deleteFileFromDirectory(vfs::Path const& filename);
virtual bool createSubDirectory(vfs::Path const& subDirPath);
virtual bool deleteDirectory(vfs::Path const& dirPath);
/**
* IVFSLocation interface
* TVFSLocation interface
*/
virtual bool FileExists(vfs::Path const& sFileName);
virtual vfs::IBaseFile* GetFile(vfs::Path const& sFileName);
virtual tFileType* GetFileTyped(vfs::Path const& sFileName);
virtual void GetSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual bool fileExists(vfs::Path const& filename);
virtual vfs::IBaseFile* getFile(vfs::Path const& filename);
virtual tFileType* getFileTyped(vfs::Path const& filename);
virtual void getSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual Iterator begin();
protected:
tFileCatalogue m_mapFiles;
tFileCatalogue m_files;
};
} // -end- namespace
+43 -47
View File
@@ -1,7 +1,11 @@
#include "vfs_slf_library.h"
#include "../Interface/vfs_directory_interface.h"
#include "vfs_lib_dir.h"
#include "../vfs_config.h"
#include "../Interface/vfs_directory_interface.h"
#include "../File/vfs_lib_file.h"
#include "../vfs_file_raii.h"
#include <cstring>
namespace slf
{
@@ -50,35 +54,31 @@ namespace slf
/********************************************************************************************/
/********************************************************************************************/
vfs::CSLFLibrary::CSLFLibrary(tReadableFile *pLibraryFile, vfs::Path const& sMountPoint, bool bOwnFile)
: vfs::CUncompressedLibraryBase(pLibraryFile,sMountPoint,bOwnFile)
{};
vfs::CSLFLibrary::~CSLFLibrary()
{
}
bool vfs::CSLFLibrary::Init()
bool vfs::CSLFLibrary::init()
{
if(m_pLibraryFile)
if(!m_libraryFile)
{
if(!m_pLibraryFile->OpenRead())
{
return false;
}
return false;
}
try
{
vfs::COpenReadFile rfile(m_libraryFile);
slf::LIBHEADER LibFileHeader;
UInt32 uiNumBytesRead;
if(!m_pLibraryFile->Read((Byte*)&LibFileHeader,sizeof( slf::LIBHEADER ), uiNumBytesRead))
{
m_pLibraryFile->Close();
return false;
}
if( uiNumBytesRead != sizeof( slf::LIBHEADER ) )
{
//Error Reading the file database header.
m_pLibraryFile->Close();
return false;
}
vfs::size_t bytesRead = m_libraryFile->read((vfs::Byte*)&LibFileHeader, sizeof( slf::LIBHEADER ));
THROWIFFALSE(bytesRead == sizeof( slf::LIBHEADER ), L"");
vfs::Path oLibPath;
//if the library has a path
if( strlen( LibFileHeader.sPathToLibrary ) != 0 )
if( strlen( (char*)LibFileHeader.sPathToLibrary ) != 0 )
{
oLibPath = vfs::Path( LibFileHeader.sPathToLibrary );
}
@@ -87,70 +87,66 @@ bool vfs::CSLFLibrary::Init()
//else the library name does not contain a path ( most likely either an error or it is the default path )
oLibPath = vfs::Path( vfs::Const::EMPTY() );
}
if(m_sMountPoint.empty())
if(m_mountPoint.empty())
{
m_sMountPoint = oLibPath;
m_mountPoint = oLibPath;
}
else
{
m_sMountPoint += oLibPath;
m_mountPoint += oLibPath;
}
//place the file pointer at the begining of the file headers ( they are at the end of the file )
m_pLibraryFile->SetReadLocation(-( LibFileHeader.iEntries * (Int32)sizeof(slf::DIRENTRY) ), vfs::IBaseFile::SD_END);
m_libraryFile->setReadPosition(-( LibFileHeader.iEntries * (vfs::Int32)sizeof(slf::DIRENTRY) ), vfs::IBaseFile::SD_END);
//loop through the library and determine the number of files that are FILE_OK
//ie. so we dont load the old or deleted files
slf::DIRENTRY DirEntry;
vfs::Path oDir, oFile;
vfs::Path oDirPath;
for(UInt32 uiLoop=0; uiLoop < (UInt32)LibFileHeader.iEntries; uiLoop++ )
for(vfs::UInt32 uiLoop=0; uiLoop < (vfs::UInt32)LibFileHeader.iEntries; uiLoop++ )
{
//read in the file header
if(!m_pLibraryFile->Read((Byte*)&DirEntry, sizeof( slf::DIRENTRY ), uiNumBytesRead))
{
m_pLibraryFile->Close();
return false;
}
bytesRead = m_libraryFile->read((Byte*)&DirEntry, sizeof( slf::DIRENTRY ));
THROWIFFALSE(bytesRead == sizeof( slf::DIRENTRY ), L"");
if( DirEntry.ubState == slf::FILE_OK )
{
vfs::Path sPath(utf8string::as_utf16(DirEntry.sFileName));
sPath.SplitLast(oDir,oFile);
oDirPath = m_sMountPoint;
sPath.splitLast(oDir,oFile);
oDirPath = m_mountPoint;
if(!oDir.empty())
{
oDirPath += oDir;
}
// get or create according directory object
vfs::IDirectory<ILibrary::tWriteType>* pLD = NULL;
tDirCatalogue::iterator it = m_catDirs.find(oDirPath);
if(it != m_catDirs.end())
vfs::TDirectory<ILibrary::tWriteType>* pLD = NULL;
tDirCatalogue::iterator it = m_dirs.find(oDirPath);
if(it != m_dirs.end())
{
pLD = it->second;
}
else
{
pLD = new vfs::CLibDirectory(oDirPath,oDirPath);
m_catDirs.insert(std::make_pair(oDirPath,pLD));
m_dirs.insert(std::make_pair(oDirPath,pLD));
}
// create file
vfs::CLibFile *pFile = vfs::CLibFile::Create(oFile,pLD,this);
vfs::CLibFile *pFile = vfs::CLibFile::create(oFile,pLD,this);
// add file to directory
if(!pLD->AddFile(pFile))
{
m_pLibraryFile->Close();
return false;
}
THROWIFFALSE(pLD->addFile(pFile), L"");
// link file data struct to file object
m_mapLibData.insert(std::make_pair(pFile,sFileData(DirEntry.uiLength, DirEntry.uiOffset)));
m_fileData.insert(std::make_pair(pFile, SFileData(DirEntry.uiLength, DirEntry.uiOffset)));
} // end if
} // end for
m_pLibraryFile->Close();
return true;
} // end if
// no library file
return false;
}
catch(CBasicException& ex)
{
logException(ex);
return false;
}
}
+3 -10
View File
@@ -1,24 +1,17 @@
#ifndef _VFS_SLF_LIBRARY_H_
#define _VFS_SLF_LIBRARY_H_
//#include "Interface/vfs_library_interface.h"
//#include "Interface/vfs_directory_interface.h"
#include "vfs_uncompressed_lib_base.h"
namespace vfs
{
class CSLFLibrary : public vfs::CUncompressedLibraryBase
class VFS_API CSLFLibrary : public vfs::CUncompressedLibraryBase
{
public:
CSLFLibrary(tReadableFile *pLibraryFile, vfs::Path const& sMountPoint, bool bOwnFile = false)
: vfs::CUncompressedLibraryBase(pLibraryFile,sMountPoint,bOwnFile)
{};
CSLFLibrary(tReadableFile *pLibraryFile, vfs::Path const& sMountPoint, bool bOwnFile = false);
virtual ~CSLFLibrary();
/**
* ILibrary interface
*/
virtual bool Init();
virtual bool init();
};
} // end namespace
+208 -181
View File
@@ -1,100 +1,167 @@
#include "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;
}
THROWEXCEPTION(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();
this->closeLibrary();
// delete sub dirs from catalogue
tDirCatalogue::iterator it = m_catDirs.begin();
for(; it != m_catDirs.end(); ++it)
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_mapLibData.clear();
m_catDirs.clear();
m_fileData.clear();
m_dirs.clear();
}
bool vfs::CUncompressedLibraryBase::CloseLibrary()
void vfs::CUncompressedLibraryBase::closeLibrary()
{
bool success = true;
tLibData::iterator it = m_mapLibData.begin();
for(; it != m_mapLibData .end(); ++it)
tFileData::iterator it = m_fileData.begin();
for(; it != m_fileData .end(); ++it)
{
success &= it->first->Close();
// what if closing of (at least) one file fails?? continue or not??
// in the end, these are not real files!
IGNOREEXCEPTION(it->first->close());
}
return success;
}
bool vfs::CUncompressedLibraryBase::FileExists(vfs::Path const& sFileName)
bool vfs::CUncompressedLibraryBase::fileExists(vfs::Path const& filename)
{
vfs::Path sDir,sFile;
sFileName.SplitLast(sDir,sFile);
tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it != m_catDirs.end())
filename.splitLast(sDir,sFile);
tDirCatalogue::iterator it = m_dirs.find(sDir);
if(it != m_dirs.end())
{
return it->second->FileExists(sFile);
return it->second->fileExists(sFile);
}
return false;
}
vfs::IBaseFile* vfs::CUncompressedLibraryBase::GetFile(vfs::Path const& sFileName)
vfs::IBaseFile* vfs::CUncompressedLibraryBase::getFile(vfs::Path const& filename)
{
return GetFileTyped(sFileName);
return getFileTyped(filename);
}
vfs::CUncompressedLibraryBase::tFileType* vfs::CUncompressedLibraryBase::GetFileTyped(vfs::Path const& sFileName)
vfs::CUncompressedLibraryBase::tFileType* vfs::CUncompressedLibraryBase::getFileTyped(vfs::Path const& filename)
{
vfs::Path sDir,sFile;
sFileName.SplitLast(sDir,sFile);
tDirCatalogue::iterator it = m_catDirs.find(sDir);
if(it != m_catDirs.end())
filename.splitLast(sDir,sFile);
tDirCatalogue::iterator it = m_dirs.find(sDir);
if(it != m_dirs.end())
{
return it->second->GetFileTyped(sFile);
return it->second->getFileTyped(sFile);
}
return NULL;
}
bool vfs::CUncompressedLibraryBase::Close(tFileType *pFileHandle)
void vfs::CUncompressedLibraryBase::close(tFileType *fileHandle)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
try
{
// wrong file handle
return false;
}
// 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
it->second.uiCurrentReadPosition = 0;
if(m_uiNumberOfOpenedFiles > 0)
{
m_uiNumberOfOpenedFiles--;
if(m_uiNumberOfOpenedFiles == 0)
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_pLibraryFile->Close();
m_numberOfOpenedFiles--;
if(m_numberOfOpenedFiles == 0)
{
m_libraryFile->close();
}
}
}
return true;
}
bool vfs::CUncompressedLibraryBase::OpenRead(tFileType *pFileHandle)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
catch(CBasicException& ex)
{
// wrong file handle
return false;
RETHROWEXCEPTION(L"", &ex);
}
m_uiNumberOfOpenedFiles++;
if(m_uiNumberOfOpenedFiles == 1)
}
bool vfs::CUncompressedLibraryBase::openRead(tFileType *fileHandle)
{
try
{
if(!m_pLibraryFile->IsOpenRead() && !m_pLibraryFile->OpenRead())
_fileDataFromHandle(fileHandle);
}
catch(CBasicException& ex)
{
RETHROWEXCEPTION(L"", &ex);
}
m_numberOfOpenedFiles++;
if(m_numberOfOpenedFiles == 1)
{
if(!m_libraryFile->isOpenRead() && !m_libraryFile->openRead())
{
return false;
}
@@ -103,127 +170,116 @@ bool vfs::CUncompressedLibraryBase::OpenRead(tFileType *pFileHandle)
return true;
}
bool vfs::CUncompressedLibraryBase::Read(tFileType *pFileHandle, Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead)
vfs::size_t vfs::CUncompressedLibraryBase::read(tFileType *fileHandle, vfs::Byte* data, vfs::size_t bytesToRead)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
try
{
// wrong file handle
return false;
}
// if(m_pLibraryFile->IsOpen()) ... we could check it (*AGAIN*),
// but the file implementation does it already,
// and the user probably called 'OpenRead' too,
// so why do it over and over again
if( (it->second.uiCurrentReadPosition + uiBytesToRead) > it->second.uiFileSize )
{
// original implementation
uiBytesRead = 0;
return false;
// or you could adjust the number of bytes to read
uiBytesToRead = it->second.uiFileSize - it->second.uiCurrentReadPosition; // +-1 ???
}
// set lib-file's read-location to match location of virtual-file
m_pLibraryFile->SetReadLocation(it->second.uiFileOffset + it->second.uiCurrentReadPosition,IBaseFile::SD_BEGIN);
bool success = m_pLibraryFile->Read(pData,uiBytesToRead,uiBytesRead)
&& (uiBytesToRead == uiBytesRead);
if(success)
{
it->second.uiCurrentReadPosition += uiBytesRead;
}
// false if read operation failed
return success;
}
SFileData& file = _fileDataFromHandle(fileHandle);
vfs::UInt32 vfs::CUncompressedLibraryBase::GetReadLocation(tFileType *pFileHandle)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
{
// wrong file handle
return -1;
}
return it->second.uiCurrentReadPosition;
}
bool vfs::CUncompressedLibraryBase::SetReadLocation(tFileType *pFileHandle, vfs::UInt32 uiPositionInBytes)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
{
// wrong file handle
return false;
}
if( (uiPositionInBytes < 0) || (uiPositionInBytes >= (vfs::Int32)(it->second.uiFileSize)) )
{
return false;
}
// uiCurrentReadPosition is offset to file-offset
it->second.uiCurrentReadPosition = uiPositionInBytes;
return true;
}
bool vfs::CUncompressedLibraryBase::SetReadLocation(tFileType *pFileHandle, Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
{
// wrong file handle
return false;
}
if( abs(uiOffsetInBytes) > (Int32)(it->second.uiFileSize) )
{
// cannot be corrent in any case
return false;
}
if(eSeekDir == IBaseFile::SD_BEGIN)
{
if(uiOffsetInBytes < 0)
if( (file._currentReadPosition + bytesToRead) > file._fileSize )
{
return false;
bytesToRead = file._fileSize - file._currentReadPosition;
}
it->second.uiCurrentReadPosition = uiOffsetInBytes;
return true;
}
else if(eSeekDir == IBaseFile::SD_CURRENT)
{
vfs::Int32 temp = it->second.uiCurrentReadPosition + uiOffsetInBytes;
if( (temp < 0) || (temp > (Int32)(it->second.uiFileSize)) )
if(bytesToRead == 0)
{
return false;
// eof
return 0;
}
it->second.uiCurrentReadPosition = temp;
return true;
// 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);
THROWIFFALSE( bytesToRead == bytesRead, L"Number of bytes doesn't match" );
file._currentReadPosition += bytesRead;
return bytesRead;
}
else if(eSeekDir == IBaseFile::SD_END)
catch(CBasicException& ex)
{
if(uiOffsetInBytes > 0)
{
return false;
}
it->second.uiCurrentReadPosition = it->second.uiFileSize - uiOffsetInBytes;
return true;
RETHROWEXCEPTION(L"", &ex);
}
return false;
}
bool vfs::CUncompressedLibraryBase::GetFileSize(tFileType *pFileHandle, UInt32& uiFileSize)
vfs::size_t vfs::CUncompressedLibraryBase::getReadPosition(tFileType *fileHandle)
{
tLibData::iterator it = m_mapLibData.find(pFileHandle);
if(it == m_mapLibData.end())
try
{
// wrong file handle
uiFileSize = 0;
return false;
return _fileDataFromHandle(fileHandle)._currentReadPosition;
}
catch(CBasicException& ex)
{
RETHROWEXCEPTION(L"", &ex);
}
uiFileSize = it->second.uiFileSize;
return true;
}
void vfs::CUncompressedLibraryBase::GetSubDirList(std::list<vfs::Path>& rlSubDirs)
void vfs::CUncompressedLibraryBase::setReadPosition(tFileType *fileHandle, vfs::size_t positionInBytes)
{
tDirCatalogue::iterator it = m_catDirs.begin();
for(;it != m_catDirs.end(); ++it)
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
THROWIFFALSE( positionInBytes < file._fileSize, L"" );
// uiCurrentReadPosition is offset to file-offset
file._currentReadPosition = positionInBytes;
}
catch(CBasicException& ex)
{
RETHROWEXCEPTION(L"", &ex);
}
}
void vfs::CUncompressedLibraryBase::setReadPosition(tFileType *fileHandle, vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir)
{
try
{
SFileData& file = _fileDataFromHandle(fileHandle);
vfs::size_t pos = (vfs::size_t) (offsetInBytes < 0 ? -offsetInBytes : offsetInBytes);
THROWIFFALSE( pos < file._fileSize, L"" );
if(seekDir == IBaseFile::SD_BEGIN)
{
THROWIFFALSE(offsetInBytes >= 0, L"");
file._currentReadPosition = offsetInBytes;
}
else if(seekDir == IBaseFile::SD_CURRENT)
{
vfs::offset_t pos = file._currentReadPosition + offsetInBytes;
THROWIFFALSE( (pos >= 0) && (pos <= (vfs::offset_t)file._fileSize), L"" );
file._currentReadPosition = (vfs::size_t) pos;
}
else if(seekDir == IBaseFile::SD_END)
{
THROWIFFALSE(offsetInBytes <= 0, L"");
file._currentReadPosition = file._fileSize + offsetInBytes;
}
else
{
THROWEXCEPTION(L"Unknown seek direction");
}
}
catch(CBasicException& ex)
{
RETHROWEXCEPTION(L"", &ex);
}
}
vfs::size_t vfs::CUncompressedLibraryBase::getSize(tFileType *fileHandle)
{
try
{
return _fileDataFromHandle(fileHandle)._fileSize;
}
catch(CBasicException& ex)
{
RETHROWEXCEPTION(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);
}
@@ -234,34 +290,5 @@ vfs::CUncompressedLibraryBase::Iterator vfs::CUncompressedLibraryBase::begin()
return Iterator(new IterImpl(*this));
}
/************************************************************************/
/************************************************************************/
vfs::CUncompressedLibraryBase::IterImpl::IterImpl(vfs::CUncompressedLibraryBase &lib)
: _lib(&lib)
{
_iter = _lib->m_mapLibData.begin();
}
vfs::CUncompressedLibraryBase::IterImpl::~IterImpl()
{
}
vfs::CUncompressedLibraryBase::tFileType* vfs::CUncompressedLibraryBase::IterImpl::value()
{
if(_iter != _lib->m_mapLibData.end())
{
return _iter->first;
}
return NULL;
}
void vfs::CUncompressedLibraryBase::IterImpl::next()
{
if(_iter != _lib->m_mapLibData.end())
{
_iter++;
}
}
+29 -39
View File
@@ -8,65 +8,55 @@
namespace vfs
{
class CUncompressedLibraryBase : public vfs::ILibrary
class VFS_API CUncompressedLibraryBase : public vfs::ILibrary
{
protected:
typedef std::map<vfs::Path, vfs::IDirectory<ILibrary::tWriteType>*, vfs::Path::Less> tDirCatalogue;
struct sFileData
typedef std::map<vfs::Path, vfs::TDirectory<vfs::ILibrary::tWriteType>*, vfs::Path::Less> tDirCatalogue;
struct SFileData
{
sFileData(UInt32 const& fileSize, UInt32 const& fileOffset)
: uiFileSize(fileSize), uiFileOffset(fileOffset), uiCurrentReadPosition(0)
SFileData(vfs::size_t const& fileSize, vfs::size_t const& fileOffset)
: _fileSize(fileSize), _fileOffset(fileOffset), _currentReadPosition(0)
{};
UInt32 uiFileSize,uiFileOffset,uiCurrentReadPosition;
vfs::size_t _fileSize, _fileOffset, _currentReadPosition;
};
typedef std::map<tFileType*, sFileData> tLibData;
typedef std::map<tFileType*, SFileData> tFileData;
class IterImpl : public tClassType::Iterator::IImplemetation
{
public:
IterImpl(CUncompressedLibraryBase& lib);
virtual ~IterImpl();
virtual tFileType* value();
virtual void next();
private:
CUncompressedLibraryBase* _lib;
tLibData::iterator _iter;
};
class IterImpl;
public:
CUncompressedLibraryBase(tReadableFile *pLibraryFile, vfs::Path const& sMountPoint, bool bOwnFile = false)
: vfs::ILibrary(pLibraryFile,sMountPoint,bOwnFile), m_uiNumberOfOpenedFiles(0)
{};
CUncompressedLibraryBase(vfs::tReadableFile *libraryFile, vfs::Path const& mountPoint, bool ownFile = false);
virtual ~CUncompressedLibraryBase();
/**
* IVFSLocation interface
* TVFSLocation interface
*/
virtual bool FileExists(vfs::Path const& sFileName);
virtual vfs::IBaseFile* GetFile(vfs::Path const& sFileName);
virtual tFileType* GetFileTyped(vfs::Path const& sFileName);
virtual void GetSubDirList(std::list<vfs::Path>& rlSubDirs);
virtual bool fileExists(vfs::Path const& filename);
virtual vfs::IBaseFile* getFile(vfs::Path const& filename);
virtual tFileType* getFileTyped(vfs::Path const& filename);
virtual void getSubDirList(std::list<vfs::Path>& rlSubDirs);
/**
* ILibrary interface
*/
virtual bool Init() = 0;
virtual bool CloseLibrary();
virtual bool init() = 0;
virtual void closeLibrary();
virtual bool Close(tFileType *pFileHandle);
virtual bool OpenRead(tFileType *pFileHandle);
virtual bool Read(tFileType *pFileHandle, Byte* pData, UInt32 uiBytesToRead, UInt32& uiBytesRead);
virtual void close(tFileType *fileHandle);
virtual bool openRead(tFileType *fileHandle);
virtual vfs::size_t read(tFileType *fileHandle, vfs::Byte* data, vfs::size_t bytesToRead);
virtual UInt32 GetReadLocation(tFileType *pFileHandle);
virtual bool SetReadLocation(tFileType *pFileHandle, UInt32 uiPositionInBytes);
virtual bool SetReadLocation(tFileType *pFileHandle, Int32 uiOffsetInBytes, IBaseFile::ESeekDir eSeekDir);
virtual vfs::size_t getReadPosition(tFileType *fileHandle);
virtual void setReadPosition(tFileType *fileHandle, vfs::size_t positionInBytes);
virtual void setReadPosition(tFileType *fileHandle, vfs::offset_t offsetInBytes, IBaseFile::ESeekDir seekDir);
virtual bool GetFileSize(tFileType *pFileHandle, UInt32& uiFileSize);
virtual vfs::size_t getSize(tFileType *fileHandle);
virtual Iterator begin();
virtual Iterator begin();
protected:
tDirCatalogue m_catDirs;
tLibData m_mapLibData;
UInt32 m_uiNumberOfOpenedFiles;
tDirCatalogue m_dirs;
tFileData m_fileData;
vfs::UInt32 m_numberOfOpenedFiles;
private:
SFileData& _fileDataFromHandle(tFileType* handle);
};
} // end namespace