Archives

Creative Commons License
This blog is licensed under a Creative Commons License.

Simple class for walking char arrays as istreams

| 4 Comments | No TrackBacks

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);
  }
};

No TrackBacks

TrackBack URL: http://www.newartisans.com/mt/mt-tb.cgi/1716

4 Comments

include <strstream>

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.

Leave a comment

About this Entry

This page contains a single entry by John Wiegley published on February 3, 2009 9:46 PM.

Ready Lisp version 20090130 now available was the previous entry in this blog.

Building a better pre-commit hook for Git is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Comments

  • yoman: “Barfin”? “Slurping”? “Slime” “Hunchentoot” ??? What in the T.F. world read more
  • Bjorn Tipling: Why would you add instructions for installing an editor when read more
  • Mark Aufflick: sudo port install sbcl +threads If you previously installed sbcl read more
  • Alexander Lehmann: Thank. You. So. Much. – Clisp caused a lot of read more
  • Vetle: Btw, to get support for threading in SBCL, you have read more
  • ifade: I tried the same and get the same answer, but read more
  • Martial Boniou: Hi, I tried to install slime with MacPorts and I read more
OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.261