Commit b53e949e authored by hybrid's avatar hybrid

Added hendu's constification patch for memory read/write files. Also slightly...

Added hendu's constification patch for memory read/write files. Also slightly changed the use of global C methods to class specific static methods, which seems more appropriate

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@4471 dfc29bdd-3216-0410-991c-e03cc46cb475
parent fce326fd
...@@ -51,7 +51,7 @@ public: ...@@ -51,7 +51,7 @@ public:
The returned pointer should be dropped when no longer needed. The returned pointer should be dropped when no longer needed.
See IReferenceCounted::drop() for more information. See IReferenceCounted::drop() for more information.
*/ */
virtual IReadFile* createMemoryReadFile(void* memory, s32 len, const path& fileName, bool deleteMemoryWhenDropped=false) =0; virtual IReadFile* createMemoryReadFile(const void* memory, s32 len, const path& fileName, bool deleteMemoryWhenDropped=false) =0;
//! Creates an IReadFile interface for accessing files inside files. //! Creates an IReadFile interface for accessing files inside files.
/** This is useful e.g. for archives. /** This is useful e.g. for archives.
......
...@@ -44,12 +44,8 @@ namespace io ...@@ -44,12 +44,8 @@ namespace io
virtual const io::path& getFileName() const = 0; virtual const io::path& getFileName() const = 0;
}; };
//! Internal function, please do not use.
IReadFile* createReadFile(const io::path& fileName);
//! Internal function, please do not use. //! Internal function, please do not use.
IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize); IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize);
//! Internal function, please do not use.
IReadFile* createMemoryReadFile(void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped);
} // end namespace io } // end namespace io
} // end namespace irr } // end namespace irr
......
...@@ -40,9 +40,6 @@ namespace io ...@@ -40,9 +40,6 @@ namespace io
virtual const path& getFileName() const = 0; virtual const path& getFileName() const = 0;
}; };
//! Internal function, please do not use.
IWriteFile* createWriteFile(const io::path& fileName, bool append);
} // end namespace io } // end namespace io
} // end namespace irr } // end namespace irr
......
...@@ -19,8 +19,10 @@ ...@@ -19,8 +19,10 @@
#include "stdio.h" #include "stdio.h"
#include "os.h" #include "os.h"
#include "CAttributes.h" #include "CAttributes.h"
#include "CReadFile.h"
#include "CMemoryFile.h" #include "CMemoryFile.h"
#include "CLimitReadFile.h" #include "CLimitReadFile.h"
#include "CWriteFile.h"
#include "irrList.h" #include "irrList.h"
#if defined (_IRR_WINDOWS_API_) #if defined (_IRR_WINDOWS_API_)
...@@ -117,19 +119,19 @@ IReadFile* CFileSystem::createAndOpenFile(const io::path& filename) ...@@ -117,19 +119,19 @@ IReadFile* CFileSystem::createAndOpenFile(const io::path& filename)
// Create the file using an absolute path so that it matches // Create the file using an absolute path so that it matches
// the scheme used by CNullDriver::getTexture(). // the scheme used by CNullDriver::getTexture().
return createReadFile(getAbsolutePath(filename)); return CReadFile::createReadFile(getAbsolutePath(filename));
} }
//! Creates an IReadFile interface for treating memory like a file. //! Creates an IReadFile interface for treating memory like a file.
IReadFile* CFileSystem::createMemoryReadFile(void* memory, s32 len, IReadFile* CFileSystem::createMemoryReadFile(const void* memory, s32 len,
const io::path& fileName, bool deleteMemoryWhenDropped) const io::path& fileName, bool deleteMemoryWhenDropped)
{ {
if (!memory) if (!memory)
return 0; return 0;
else else
return new CMemoryFile(memory, len, fileName, deleteMemoryWhenDropped); return new CMemoryReadFile(memory, len, fileName, deleteMemoryWhenDropped);
} }
//! Creates an IReadFile interface for reading files inside files //! Creates an IReadFile interface for reading files inside files
...@@ -150,14 +152,14 @@ IWriteFile* CFileSystem::createMemoryWriteFile(void* memory, s32 len, ...@@ -150,14 +152,14 @@ IWriteFile* CFileSystem::createMemoryWriteFile(void* memory, s32 len,
if (!memory) if (!memory)
return 0; return 0;
else else
return new CMemoryFile(memory, len, fileName, deleteMemoryWhenDropped); return new CMemoryWriteFile(memory, len, fileName, deleteMemoryWhenDropped);
} }
//! Opens a file for write access. //! Opens a file for write access.
IWriteFile* CFileSystem::createAndWriteFile(const io::path& filename, bool append) IWriteFile* CFileSystem::createAndWriteFile(const io::path& filename, bool append)
{ {
return createWriteFile(filename, append); return CWriteFile::createWriteFile(filename, append);
} }
......
...@@ -34,7 +34,7 @@ public: ...@@ -34,7 +34,7 @@ public:
virtual IReadFile* createAndOpenFile(const io::path& filename); virtual IReadFile* createAndOpenFile(const io::path& filename);
//! Creates an IReadFile interface for accessing memory like a file. //! Creates an IReadFile interface for accessing memory like a file.
virtual IReadFile* createMemoryReadFile(void* memory, s32 len, const io::path& fileName, bool deleteMemoryWhenDropped = false); virtual IReadFile* createMemoryReadFile(const void* memory, s32 len, const io::path& fileName, bool deleteMemoryWhenDropped = false);
//! Creates an IReadFile interface for accessing files inside files //! Creates an IReadFile interface for accessing files inside files
virtual IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize); virtual IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize);
......
...@@ -168,7 +168,7 @@ CGUIEnvironment::~CGUIEnvironment() ...@@ -168,7 +168,7 @@ CGUIEnvironment::~CGUIEnvironment()
void CGUIEnvironment::loadBuiltInFont() void CGUIEnvironment::loadBuiltInFont()
{ {
io::IReadFile* file = io::createMemoryReadFile((void *) BuiltInFontData, io::IReadFile* file = FileSystem->createMemoryReadFile(BuiltInFontData,
BuiltInFontDataSize, DefaultFontName, false); BuiltInFontDataSize, DefaultFontName, false);
CGUIFont* font = new CGUIFont(this, DefaultFontName ); CGUIFont* font = new CGUIFont(this, DefaultFontName );
......
...@@ -11,16 +11,16 @@ namespace io ...@@ -11,16 +11,16 @@ namespace io
{ {
CMemoryFile::CMemoryFile(void* memory, long len, const io::path& fileName, bool d) CMemoryReadFile::CMemoryReadFile(const void* memory, long len, const io::path& fileName, bool d)
: Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d) : Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
{ {
#ifdef _DEBUG #ifdef _DEBUG
setDebugName("CMemoryFile"); setDebugName("CMemoryReadFile");
#endif #endif
} }
CMemoryFile::~CMemoryFile() CMemoryReadFile::~CMemoryReadFile()
{ {
if (deleteMemoryWhenDropped) if (deleteMemoryWhenDropped)
delete [] (c8*)Buffer; delete [] (c8*)Buffer;
...@@ -28,7 +28,7 @@ CMemoryFile::~CMemoryFile() ...@@ -28,7 +28,7 @@ CMemoryFile::~CMemoryFile()
//! returns how much was read //! returns how much was read
s32 CMemoryFile::read(void* buffer, u32 sizeToRead) s32 CMemoryReadFile::read(void* buffer, u32 sizeToRead)
{ {
s32 amount = static_cast<s32>(sizeToRead); s32 amount = static_cast<s32>(sizeToRead);
if (Pos + amount > Len) if (Pos + amount > Len)
...@@ -45,8 +45,88 @@ s32 CMemoryFile::read(void* buffer, u32 sizeToRead) ...@@ -45,8 +45,88 @@ s32 CMemoryFile::read(void* buffer, u32 sizeToRead)
return amount; return amount;
} }
//! returns how much was written //! changes position in file, returns true if successful
s32 CMemoryFile::write(const void* buffer, u32 sizeToWrite) //! if relativeMovement==true, the pos is changed relative to current pos,
//! otherwise from begin of file
bool CMemoryReadFile::seek(long finalPos, bool relativeMovement)
{
if (relativeMovement)
{
if (Pos + finalPos > Len)
return false;
Pos += finalPos;
}
else
{
if (finalPos > Len)
return false;
Pos = finalPos;
}
return true;
}
//! returns size of file
long CMemoryReadFile::getSize() const
{
return Len;
}
//! returns where in the file we are.
long CMemoryReadFile::getPos() const
{
return Pos;
}
//! returns name of file
const io::path& CMemoryReadFile::getFileName() const
{
return Filename;
}
CMemoryWriteFile::CMemoryWriteFile(void* memory, long len, const io::path& fileName, bool d)
: Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
{
#ifdef _DEBUG
setDebugName("CMemoryWriteFile");
#endif
}
CMemoryWriteFile::~CMemoryWriteFile()
{
if (deleteMemoryWhenDropped)
delete [] (c8*)Buffer;
}
//! returns how much was read
s32 CMemoryWriteFile::read(void* buffer, u32 sizeToRead)
{
s32 amount = static_cast<s32>(sizeToRead);
if (Pos + amount > Len)
amount -= Pos + amount - Len;
if (amount <= 0)
return 0;
c8* p = (c8*)Buffer;
memcpy(buffer, p + Pos, amount);
Pos += amount;
return amount;
}
//! returns how much was written
s32 CMemoryWriteFile::write(const void* buffer, u32 sizeToWrite)
{ {
s32 amount = static_cast<s32>(sizeToWrite); s32 amount = static_cast<s32>(sizeToWrite);
if (Pos + amount > Len) if (Pos + amount > Len)
...@@ -68,7 +148,7 @@ s32 CMemoryFile::write(const void* buffer, u32 sizeToWrite) ...@@ -68,7 +148,7 @@ s32 CMemoryFile::write(const void* buffer, u32 sizeToWrite)
//! changes position in file, returns true if successful //! changes position in file, returns true if successful
//! if relativeMovement==true, the pos is changed relative to current pos, //! if relativeMovement==true, the pos is changed relative to current pos,
//! otherwise from begin of file //! otherwise from begin of file
bool CMemoryFile::seek(long finalPos, bool relativeMovement) bool CMemoryWriteFile::seek(long finalPos, bool relativeMovement)
{ {
if (relativeMovement) if (relativeMovement)
{ {
...@@ -90,29 +170,35 @@ bool CMemoryFile::seek(long finalPos, bool relativeMovement) ...@@ -90,29 +170,35 @@ bool CMemoryFile::seek(long finalPos, bool relativeMovement)
//! returns size of file //! returns size of file
long CMemoryFile::getSize() const long CMemoryWriteFile::getSize() const
{ {
return Len; return Len;
} }
//! returns where in the file we are. //! returns where in the file we are.
long CMemoryFile::getPos() const long CMemoryWriteFile::getPos() const
{ {
return Pos; return Pos;
} }
//! returns name of file //! returns name of file
const io::path& CMemoryFile::getFileName() const const io::path& CMemoryWriteFile::getFileName() const
{ {
return Filename; return Filename;
} }
IReadFile* createMemoryReadFile(void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped) IReadFile* createMemoryReadFile(const void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped)
{
CMemoryReadFile* file = new CMemoryReadFile(memory, size, fileName, deleteMemoryWhenDropped);
return file;
}
IWriteFile* createMemoryWriteFile(void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped)
{ {
CMemoryFile* file = new CMemoryFile(memory, size, fileName, deleteMemoryWhenDropped); CMemoryWriteFile* file = new CMemoryWriteFile(memory, size, fileName, deleteMemoryWhenDropped);
return file; return file;
} }
......
...@@ -15,18 +15,55 @@ namespace irr ...@@ -15,18 +15,55 @@ namespace irr
namespace io namespace io
{ {
/*!
Class for reading from memory.
*/
class CMemoryReadFile : public IReadFile
{
public:
//! Constructor
CMemoryReadFile(const void* memory, long len, const io::path& fileName, bool deleteMemoryWhenDropped);
//! Destructor
virtual ~CMemoryReadFile();
//! returns how much was read
virtual s32 read(void* buffer, u32 sizeToRead);
//! changes position in file, returns true if successful
virtual bool seek(long finalPos, bool relativeMovement = false);
//! returns size of file
virtual long getSize() const;
//! returns where in the file we are.
virtual long getPos() const;
//! returns name of file
virtual const io::path& getFileName() const;
private:
const void *Buffer;
long Len;
long Pos;
io::path Filename;
bool deleteMemoryWhenDropped;
};
/*! /*!
Class for reading and writing from memory. Class for writing to memory.
*/ */
class CMemoryFile : public IReadFile, public IWriteFile class CMemoryWriteFile : public IWriteFile
{ {
public: public:
//! Constructor //! Constructor
CMemoryFile(void* memory, long len, const io::path& fileName, bool deleteMemoryWhenDropped); CMemoryWriteFile(void* memory, long len, const io::path& fileName, bool deleteMemoryWhenDropped);
//! Destructor //! Destructor
virtual ~CMemoryFile(); virtual ~CMemoryWriteFile();
//! returns how much was read //! returns how much was read
virtual s32 read(void* buffer, u32 sizeToRead); virtual s32 read(void* buffer, u32 sizeToRead);
......
...@@ -155,7 +155,7 @@ IReadFile* CMountPointReader::createAndOpenFile(u32 index) ...@@ -155,7 +155,7 @@ IReadFile* CMountPointReader::createAndOpenFile(u32 index)
if (index >= Files.size()) if (index >= Files.size())
return 0; return 0;
return createReadFile(RealFileNames[Files[index].ID]); return CReadFile::createReadFile(RealFileNames[Files[index].ID]);
} }
//! opens a file by file name //! opens a file by file name
......
...@@ -97,8 +97,7 @@ const io::path& CReadFile::getFileName() const ...@@ -97,8 +97,7 @@ const io::path& CReadFile::getFileName() const
} }
IReadFile* CReadFile::createReadFile(const io::path& fileName)
IReadFile* createReadFile(const io::path& fileName)
{ {
CReadFile* file = new CReadFile(fileName); CReadFile* file = new CReadFile(fileName);
if (file->isOpen()) if (file->isOpen())
......
...@@ -47,6 +47,9 @@ namespace io ...@@ -47,6 +47,9 @@ namespace io
//! returns name of file //! returns name of file
virtual const io::path& getFileName() const; virtual const io::path& getFileName() const;
//! create read file on disk.
static IReadFile* createReadFile(const io::path& fileName);
private: private:
//! opens the file //! opens the file
......
...@@ -107,7 +107,7 @@ const io::path& CWriteFile::getFileName() const ...@@ -107,7 +107,7 @@ const io::path& CWriteFile::getFileName() const
IWriteFile* createWriteFile(const io::path& fileName, bool append) IWriteFile* CWriteFile::createWriteFile(const io::path& fileName, bool append)
{ {
CWriteFile* file = new CWriteFile(fileName, append); CWriteFile* file = new CWriteFile(fileName, append);
if (file->isOpen()) if (file->isOpen())
......
...@@ -41,6 +41,9 @@ namespace io ...@@ -41,6 +41,9 @@ namespace io
//! returns if file is open //! returns if file is open
bool isOpen() const; bool isOpen() const;
//! creator method
static IWriteFile* createWriteFile(const io::path& fileName, bool append);
private: private:
//! opens the file //! opens the file
......
...@@ -111,7 +111,7 @@ IFileArchive* CArchiveLoaderZIP::createArchive(io::IReadFile* file, bool ignoreC ...@@ -111,7 +111,7 @@ IFileArchive* CArchiveLoaderZIP::createArchive(io::IReadFile* file, bool ignoreC
bool isGZip = (sig == 0x8b1f); bool isGZip = (sig == 0x8b1f);
archive = new CZipReader(file, ignoreCase, ignorePaths, isGZip); archive = new CZipReader(FileSystem, file, ignoreCase, ignorePaths, isGZip);
} }
return archive; return archive;
} }
...@@ -137,8 +137,8 @@ bool CArchiveLoaderZIP::isALoadableFileFormat(io::IReadFile* file) const ...@@ -137,8 +137,8 @@ bool CArchiveLoaderZIP::isALoadableFileFormat(io::IReadFile* file) const
// zip archive // zip archive
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
CZipReader::CZipReader(IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip) CZipReader::CZipReader(IFileSystem* fs, IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip)
: CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), File(file), IsGZip(isGZip) : CFileList((file ? file->getFileName() : io::path("")), ignoreCase, ignorePaths), FileSystem(fs), File(file), IsGZip(isGZip)
{ {
#ifdef _DEBUG #ifdef _DEBUG
setDebugName("CZipReader"); setDebugName("CZipReader");
...@@ -586,7 +586,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index) ...@@ -586,7 +586,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index)
delete [] decryptedBuf; delete [] decryptedBuf;
return 0; return 0;
} }
decrypted = io::createMemoryReadFile(decryptedBuf, decryptedSize, Files[index].FullName, true); decrypted = FileSystem->createMemoryReadFile(decryptedBuf, decryptedSize, Files[index].FullName, true);
actualCompressionMethod = (e.header.Sig & 0xffff); actualCompressionMethod = (e.header.Sig & 0xffff);
#if 0 #if 0
if ((e.header.Sig & 0xff000000)==0x01000000) if ((e.header.Sig & 0xff000000)==0x01000000)
...@@ -680,7 +680,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index) ...@@ -680,7 +680,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index)
return 0; return 0;
} }
else else
return io::createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true); return FileSystem->createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true);
#else #else
return 0; // zlib not compiled, we cannot decompress the data. return 0; // zlib not compiled, we cannot decompress the data.
...@@ -751,7 +751,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index) ...@@ -751,7 +751,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index)
return 0; return 0;
} }
else else
return io::createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true); return FileSystem->createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true);
#else #else
os::Printer::log("bzip2 decompression not supported. File cannot be read.", ELL_ERROR); os::Printer::log("bzip2 decompression not supported. File cannot be read.", ELL_ERROR);
...@@ -814,7 +814,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index) ...@@ -814,7 +814,7 @@ IReadFile* CZipReader::createAndOpenFile(u32 index)
return 0; return 0;
} }
else else
return io::createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true); return FileSystem->createMemoryReadFile(pBuf, uncompressedSize, Files[index].FullName, true);
#else #else
os::Printer::log("lzma decompression not supported. File cannot be read.", ELL_ERROR); os::Printer::log("lzma decompression not supported. File cannot be read.", ELL_ERROR);
......
...@@ -180,7 +180,7 @@ namespace io ...@@ -180,7 +180,7 @@ namespace io
public: public:
//! constructor //! constructor
CZipReader(IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip=false); CZipReader(IFileSystem* fs, IReadFile* file, bool ignoreCase, bool ignorePaths, bool isGZip=false);
//! destructor //! destructor
virtual ~CZipReader(); virtual ~CZipReader();
...@@ -210,6 +210,7 @@ namespace io ...@@ -210,6 +210,7 @@ namespace io
bool scanCentralDirectoryHeader(); bool scanCentralDirectoryHeader();
io::IFileSystem* FileSystem;
IReadFile* File; IReadFile* File;
// holds extended info about files // holds extended info about files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment