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 "CLimitReadFile.h"
\r
7 #include "irrString.h"
\r
15 CLimitReadFile::CLimitReadFile(IReadFile* alreadyOpenedFile, long pos,
\r
16 long areaSize, const io::path& name)
\r
17 : Filename(name), AreaStart(0), AreaEnd(0), Pos(0),
\r
18 File(alreadyOpenedFile)
\r
21 setDebugName("CLimitReadFile");
\r
28 AreaEnd = AreaStart + areaSize;
\r
33 CLimitReadFile::~CLimitReadFile()
\r
40 //! returns how much was read
\r
41 size_t CLimitReadFile::read(void* buffer, size_t sizeToRead)
\r
47 long r = AreaStart + Pos;
\r
48 long toRead = core::min_(AreaEnd, r + (long)sizeToRead) - core::max_(AreaStart, r);
\r
52 r = (long)File->read(buffer, toRead);
\r
56 const long pos = File->getPos();
\r
61 if (pos + (long)sizeToRead >= AreaEnd)
\r
62 sizeToRead = AreaEnd - pos;
\r
64 return File->read(buffer, sizeToRead);
\r
69 //! changes position in file, returns true if successful
\r
70 bool CLimitReadFile::seek(long finalPos, bool relativeMovement)
\r
73 Pos = core::s32_clamp(finalPos + (relativeMovement ? Pos : 0 ), 0, AreaEnd - AreaStart);
\r
76 const long pos = File->getPos();
\r
78 if (relativeMovement)
\r
80 if (pos + finalPos > AreaEnd)
\r
81 finalPos = AreaEnd - pos;
\r
85 finalPos += AreaStart;
\r
86 if (finalPos > AreaEnd)
\r
90 return File->seek(finalPos, relativeMovement);
\r
95 //! returns size of file
\r
96 long CLimitReadFile::getSize() const
\r
98 return AreaEnd - AreaStart;
\r
102 //! returns where in the file we are.
\r
103 long CLimitReadFile::getPos() const
\r
108 return File->getPos() - AreaStart;
\r
113 //! returns name of file
\r
114 const io::path& CLimitReadFile::getFileName() const
\r
120 IReadFile* createLimitReadFile(const io::path& fileName, IReadFile* alreadyOpenedFile, long pos, long areaSize)
\r
122 return new CLimitReadFile(alreadyOpenedFile, pos, areaSize, fileName);
\r
126 } // end namespace io
\r
127 } // end namespace irr
\r