2012-07-22 18:02:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CFileInputStream.h"
|
|
|
|
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "CFileInfo.h"
|
2012-07-22 18:02:13 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
CFileInputStream::CFileInputStream(const std::string & file, si64 start, si64 size)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
open(file, start, size);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
CFileInputStream::CFileInputStream(const CFileInfo & file, si64 start, si64 size)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
open(file.getName(), start, size);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CFileInputStream::~CFileInputStream()
|
|
|
|
{
|
2012-08-02 14:03:26 +03:00
|
|
|
fileStream.close();
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
void CFileInputStream::open(const std::string & file, si64 start, si64 size)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
fileStream.open(file.c_str(), std::ios::in | std::ios::binary);
|
2012-08-01 15:02:54 +03:00
|
|
|
|
2012-07-22 18:02:13 +03:00
|
|
|
if (fileStream.fail())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("File " + file + " isn't available.");
|
|
|
|
}
|
|
|
|
|
2012-08-01 15:02:54 +03: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 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::read(ui8 * data, si64 size)
|
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
si64 origin = tell();
|
|
|
|
si64 toRead = std::min(dataSize - origin, size);
|
|
|
|
fileStream.read(reinterpret_cast<char *>(data), toRead);
|
2012-07-22 18:02:13 +03:00
|
|
|
return fileStream.gcount();
|
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::seek(si64 position)
|
|
|
|
{
|
2013-02-06 02:11:48 +03:00
|
|
|
fileStream.seekg(dataStart + std::min(position, dataSize));
|
|
|
|
return tell();
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::tell()
|
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
return fileStream.tellg() - dataStart;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::skip(si64 delta)
|
|
|
|
{
|
2012-08-01 15:02:54 +03: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 18:02:13 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
return tell() - origin;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CFileInputStream::getSize()
|
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
return dataSize;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|