2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* CMemoryStream.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 "CMemoryStream.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2012-11-03 16:30:47 +03:00
|
|
|
CMemoryStream::CMemoryStream(const ui8 * data, si64 size) :
|
2012-11-11 15:23:31 +03:00
|
|
|
data(data), size(size), position(0)
|
2012-07-22 18:02:13 +03:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
si64 CMemoryStream::read(ui8 * data, si64 size)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
si64 toRead = std::min(this->size - tell(), size);
|
|
|
|
std::copy(this->data + position, this->data + position + toRead, data);
|
2012-07-22 18:02:13 +03:00
|
|
|
position += size;
|
2012-11-11 15:23:31 +03:00
|
|
|
return toRead;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CMemoryStream::seek(si64 position)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
si64 origin = tell();
|
|
|
|
this->position = std::min(position, size);
|
|
|
|
return tell() - origin;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CMemoryStream::tell()
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
return this->position;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CMemoryStream::skip(si64 delta)
|
|
|
|
{
|
2012-11-11 15:23:31 +03:00
|
|
|
si64 origin = tell();
|
|
|
|
this->position += std::min(size - origin, delta);
|
|
|
|
return tell() - origin;
|
2012-07-22 18:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
si64 CMemoryStream::getSize()
|
|
|
|
{
|
|
|
|
return size;
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|