So, result of discussion is something like that:
(i removed some code for simplicity)
class SubStream: public sf::InputStream
{
public:
explicit SubStream(sf::InputStream* stream, size_t begin, size_t size);
virtual ~SubStream();
virtual sf::Int64 read(void* data, sf::Int64 size) override;
virtual sf::Int64 seek(sf::Int64 position) override;
virtual sf::Int64 tell() override;
virtual sf::Int64 getSize() override;
protected:
sf::InputStream* mStream = nullptr;
size_t mBegin = 0;
size_t mSize = 0;
size_t mOffset = 0;
};
SubStream::SubStream(sf::InputStream* stream, size_t begin, size_t size):
mStream(stream),
mBegin(begin),
mSize(size),
mOffset(0)
{
mIsOk = mStream != nullptr && mSize > 0;
}
SubStream::~SubStream()
{
}
/* virtual */ sf::Int64 SubStream::read(void* data, sf::Int64 size)
{
if (mIsOk)
{
sf::Int64 readed = mStream->read(data, size);
if (readed != -1)
{
mOffset += readed;
return readed;
}
}
return -1;
}
/* virtual */ sf::Int64 SubStream::seek(sf::Int64 position)
{
if ( (size_t) position < mSize)
{
mOffset = (size_t) position;
mStream->seek(mBegin + mOffset);
return (sf::Int64) mOffset;
}
return -1;
}
/* virtual */ sf::Int64 SubStream::tell()
{
return (sf::Int64) mOffset;
}
/* virtual */ sf::Int64 SubStream::getSize()
{
return (sf::Int64) mSize;
}
It works very well, and a little bit faster than use of redundant malloc/free .