2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CFileInputStream.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2012-07-22 18:02:13 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CFileInputStream.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2014-08-21 23:26:28 +03:00
|
|
|
CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size)
|
2016-01-13 00:40:24 +02:00
|
|
|
: dataStart{start},
|
|
|
|
dataSize{size},
|
|
|
|
fileStream{file, std::ios::in | std::ios::binary}
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
if (fileStream.fail())
|
2014-08-21 23:26:28 +03:00
|
|
|
throw std::runtime_error("File " + file.string() + " isn't available.");
|
2012-07-22 18:02:13 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
if (dataSize == 0)
|
|
|
|
{
|
|
|
|
fileStream.seekg(0, std::ios::end);
|
|
|
|
dataSize = tell();
|
|
|
|
}
|
|
|
|
|
2016-01-13 00:40:24 +02:00
|
|
|
fileStream.seekg(dataStart, std::ios::beg);
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
2016-01-13 00:40:24 +02:00
|
|
|
|
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()
|
|
|
|
{
|
2014-04-19 20:44:21 +03:00
|
|
|
return static_cast<si64>(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
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|