2012-07-22 15:02:13 +00:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CFileInputStream.h"
|
|
|
|
|
2013-07-28 14:49:50 +00:00
|
|
|
#include "CFileInfo.h"
|
2012-07-22 15:02:13 +00:00
|
|
|
|
2014-08-21 22:26:28 +02:00
|
|
|
CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
|
2012-07-22 15:02:13 +00:00
|
|
|
{
|
2012-08-01 12:02:54 +00:00
|
|
|
open(file, start, size);
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-01 12:02:54 +00:00
|
|
|
CFileInputStream::CFileInputStream(const CFileInfo & file, si64 start, si64 size)
|
2012-07-22 15:02:13 +00:00
|
|
|
{
|
2012-08-01 12:02:54 +00:00
|
|
|
open(file.getName(), start, size);
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFileInputStream::~CFileInputStream()
|
|
|
|
{
|
2012-08-02 11:03:26 +00:00
|
|
|
fileStream.close();
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
2014-08-21 22:26:28 +02:00
|
|
|
void CFileInputStream::open(const boost::filesystem::path & file, si64 start, si64 size)
|
2012-07-22 15:02:13 +00:00
|
|
|
{
|
2014-08-21 22:26:28 +02:00
|
|
|
fileStream.open(file, std::ios::in | std::ios::binary);
|
2012-08-01 12:02:54 +00:00
|
|
|
|
2012-07-22 15:02:13 +00:00
|
|
|
if (fileStream.fail())
|
2014-08-21 22:26:28 +02:00
|
|
|
throw std::runtime_error("File " + file.string() + " isn't available.");
|
2012-07-22 15:02:13 +00:00
|
|
|
|
2012-08-01 12:02:54 +00:00
|
|
|
dataStart = start;
|
|
|
|
dataSize = size;
|
|
|
|
|
|
|
|
if (dataSize == 0)
|
|
|
|
{
|
|
|
|
fileStream.seekg(0, std::ios::end);
|
|
|
|
dataSize = tell();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileStream.seekg(start, std::ios::beg);
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::read(ui8 * data, si64 size)
|
|
|
|
{
|
2012-08-01 12:02:54 +00:00
|
|
|
si64 origin = tell();
|
|
|
|
si64 toRead = std::min(dataSize - origin, size);
|
|
|
|
fileStream.read(reinterpret_cast<char *>(data), toRead);
|
2012-07-22 15:02:13 +00:00
|
|
|
return fileStream.gcount();
|
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::seek(si64 position)
|
|
|
|
{
|
2013-02-05 23:11:48 +00:00
|
|
|
fileStream.seekg(dataStart + std::min(position, dataSize));
|
|
|
|
return tell();
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::tell()
|
|
|
|
{
|
2014-04-19 21:44:21 +04:00
|
|
|
return static_cast<si64>(fileStream.tellg()) - dataStart;
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::skip(si64 delta)
|
|
|
|
{
|
2012-08-01 12:02:54 +00:00
|
|
|
si64 origin = tell();
|
|
|
|
//ensure that we're not seeking past the end of real data
|
|
|
|
si64 toSeek = std::min(dataSize - origin, delta);
|
|
|
|
fileStream.seekg(toSeek, std::ios::cur);
|
2012-07-22 15:02:13 +00:00
|
|
|
|
2012-08-01 12:02:54 +00:00
|
|
|
return tell() - origin;
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::getSize()
|
|
|
|
{
|
2012-08-01 12:02:54 +00:00
|
|
|
return dataSize;
|
2012-07-22 15:02:13 +00:00
|
|
|
}
|