This has probably been written countless times before, but I found myself needing it today and it was quick to write. It lets you read characters from a char array in C++ via the istream interface:
#include "ptrstream.h"
int main() {
ptristream in("Hello, world!\n");
char buf[31];
in.getline(buf, 32);
std::cout << buf << std::endl;
}
Handy for if you don’t want std::istringstream needlessly copying character strings.
Below is the implementation, which is in the public domain:
#include <istream>
#include <streambuf>
#include <stdlib.h>
class ptristream : public std::istream
{
class ptrinbuf : public std::streambuf
{
protected:
char * ptr;
std::size_t len;
public:
ptrinbuf(char * _ptr, std::size_t _len) : ptr(_ptr), len(_len) {
assert(ptr);
if (*ptr && len == 0)
len = std::strlen(ptr);
setg(ptr, // beginning of putback area
ptr, // read position
ptr+len); // end position
}
protected:
virtual int_type underflow() {
// is read position before end of buffer?
if (gptr() < egptr())
return traits_type::to_int_type(*gptr());
else
return EOF;
}
virtual pos_type seekoff(off_type off, ios_base::seekdir way,
ios_base::openmode mode =
ios_base::in | ios_base::out)
{
switch (way) {
case std::ios::cur:
setg(ptr, gptr()+off, ptr+len);
break;
case std::ios::beg:
setg(ptr, ptr+off, ptr+len);
break;
case std::ios::end:
setg(ptr, egptr()+off, ptr+len);
break;
default:
assert(false);
break;
}
return pos_type(gptr() - ptr);
}
};
protected:
ptrinbuf buf;
public:
ptristream(char * ptr, std::size_t len = 0)
: std::istream(0), buf(ptr, len) {
rdbuf(&buf);
}
};


typedef std::istrstream ptristream;
There was also this proposal, which solves some of the safety issues with strstreams, but unfortunately didn’t get accepted: http://www.open-std.org/JTC1/sc22/wg21/docs/papers/2006/n2065.pdf
Thank you for this code. It is interesting, and for me it sparked off some exploration of C++.
If you are using this class with const char* variables, I suppose you const_cast<char*> before passing to the ptristream constructor? You are probably leaving off other optional stuff, like only allowing stack-based construction (no heap-based allocs), non-copyable semantics, etc.
It is interesting to note that std::strstream fills your need, but it is a deprecated class.
Also I explored boost::iostreams, which also does what you want, if I understand correctly:
#include <iostream> #include <boost/iostreams/stream.hpp> int main() { const char *something = "Hello, world!\n"; { namespace bio = boost::iostreams; bio::stream<bio::array_source> in( something, strlen(something)); char buf[31]; in.getline(buf, 32); std::cout << buf << std::endl; } }However, I get 11KB for your impl, and 25KB for the my boost impl. Plus compile time increases. :-/ (gnu g++ 4.2.4, g++ -Wall && strip, boost 1.37.0)
Well, well, I did not know about std::istrstream! You learn something new about this language every day. Thanks, Jonathan.