1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "CMemoryFile.h"
\r
6 #include "irrString.h"
\r
14 CMemoryReadFile::CMemoryReadFile(const void* memory, long len, const io::path& fileName, bool d)
\r
15 : Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
\r
18 setDebugName("CMemoryReadFile");
\r
23 CMemoryReadFile::~CMemoryReadFile()
\r
25 if (deleteMemoryWhenDropped)
\r
26 delete [] (c8*)Buffer;
\r
30 //! returns how much was read
\r
31 size_t CMemoryReadFile::read(void* buffer, size_t sizeToRead)
\r
33 long amount = static_cast<long>(sizeToRead);
\r
34 if (Pos + amount > Len)
\r
35 amount -= Pos + amount - Len;
\r
40 c8* p = (c8*)Buffer;
\r
41 memcpy(buffer, p + Pos, amount);
\r
45 return static_cast<size_t>(amount);
\r
48 //! changes position in file, returns true if successful
\r
49 //! if relativeMovement==true, the pos is changed relative to current pos,
\r
50 //! otherwise from begin of file
\r
51 bool CMemoryReadFile::seek(long finalPos, bool relativeMovement)
\r
53 if (relativeMovement)
\r
55 if (Pos + finalPos < 0 || Pos + finalPos > Len)
\r
62 if (finalPos < 0 || finalPos > Len)
\r
72 //! returns size of file
\r
73 long CMemoryReadFile::getSize() const
\r
79 //! returns where in the file we are.
\r
80 long CMemoryReadFile::getPos() const
\r
86 //! returns name of file
\r
87 const io::path& CMemoryReadFile::getFileName() const
\r
93 CMemoryWriteFile::CMemoryWriteFile(void* memory, long len, const io::path& fileName, bool d)
\r
94 : Buffer(memory), Len(len), Pos(0), Filename(fileName), deleteMemoryWhenDropped(d)
\r
97 setDebugName("CMemoryWriteFile");
\r
102 CMemoryWriteFile::~CMemoryWriteFile()
\r
104 if (deleteMemoryWhenDropped)
\r
105 delete [] (c8*)Buffer;
\r
109 //! returns how much was written
\r
110 size_t CMemoryWriteFile::write(const void* buffer, size_t sizeToWrite)
\r
112 long amount = (long)sizeToWrite;
\r
113 if (Pos + amount > Len)
\r
114 amount -= Pos + amount - Len;
\r
119 c8* p = (c8*)Buffer;
\r
120 memcpy(p + Pos, buffer, amount);
\r
124 return (size_t)amount;
\r
129 //! changes position in file, returns true if successful
\r
130 //! if relativeMovement==true, the pos is changed relative to current pos,
\r
131 //! otherwise from begin of file
\r
132 bool CMemoryWriteFile::seek(long finalPos, bool relativeMovement)
\r
134 if (relativeMovement)
\r
136 if (Pos + finalPos < 0 || Pos + finalPos > Len)
\r
143 if (finalPos < 0 || finalPos > Len)
\r
153 //! returns where in the file we are.
\r
154 long CMemoryWriteFile::getPos() const
\r
160 //! returns name of file
\r
161 const io::path& CMemoryWriteFile::getFileName() const
\r
166 bool CMemoryWriteFile::flush()
\r
168 return true; // no buffering, so nothing to do
\r
171 IReadFile* createMemoryReadFile(const void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped)
\r
173 CMemoryReadFile* file = new CMemoryReadFile(memory, size, fileName, deleteMemoryWhenDropped);
\r
178 IWriteFile* createMemoryWriteFile(void* memory, long size, const io::path& fileName, bool deleteMemoryWhenDropped)
\r
180 CMemoryWriteFile* file = new CMemoryWriteFile(memory, size, fileName, deleteMemoryWhenDropped);
\r
185 } // end namespace io
\r
186 } // end namespace irr
\r