I couldn't find anything ready-made, so I came up with:
class membuf : public basic_streambuf<char>
{
public:
membuf(char* p, size_t n) {
setg(p, p, p + n);
setp(p, p + n);
}
}
Usage:
char *mybuffer;
size_t length;
// ... allocate "mybuffer", put data into it, set "length"
membuf mb(mybuffer, length);
istream reader(&mb);
// use "reader"
I know of stringstream, but it doesn't seem to be able to work with binary data of given length.
Am I inventing my own wheel here?
EDIT
- It must not copy the input data, just create something that will iterate over the data.
- It must be portable - at least it should work both under gcc and MSVC.