#ifndef SENTRY_MEMSTREAM_H
#define SENTRY_MEMSTREAM_H
#include <istream>
#include <streambuf>
struct membuf : std::streambuf {
membuf(const char *base, size_t size) {
char *p(const_cast<char *>(base));
this->setg(p, p, p + size);
}
protected:
pos_type seekoff(
off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode which = std::ios_base::in) override {
if (dir == std::ios_base::cur)
gbump(off);
else if (dir == std::ios_base::end)
setg(eback(), egptr() + off, egptr());
else if (dir == std::ios_base::beg)
setg(eback(), eback() + off, egptr());
return gptr() - eback();
}
pos_type seekpos(pos_type sp, std::ios_base::openmode which) override {
return seekoff(sp - pos_type(off_type(0)), std::ios_base::beg, which);
}
};
struct imemstream : virtual membuf, std::istream {
imemstream(char const *base, size_t size)
: membuf(base, size),
std::istream(static_cast<std::streambuf *>(this)) {
}
};
#endif