Thread: Streambuf overloading with sputbackc

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Streambuf overloading with sputbackc

    I want to have a simple way to replace a istream or ostream in order to do on-line filtering of the content (for instance: add 1 to each character when reading, remove 1 when writing). It has to support operations like unget and putback

    I think the easiest way might be to overload streambuf

    I thought this would be something very easy, but it seems most of the code I'm doing results in error when code tries to do a unget

    Could someone show me how to subclass a streambuf to do online filtering (char by char) which I can then put in a istream or ostrea?

    Here is some code I'm using

    Code:
    class FilteringBuf : public std::streambuf
    {
        char readBuf_; // current char
        std::streambuf* pExternBuf_; // source buffer
    
    
        int_type underflow()
        {
            if (gptr() == &readBuf_)
                return traits_type::to_int_type(readBuf_);
    
    
            int_type nextChar = pExternBuf_->sbumpc();
            if (nextChar == traits_type::eof())
                return traits_type::eof();
            readBuf_ = traits_type::to_char_type(nextChar);
    
    
            // replace character
            readBuf_ += 1;
    
    
            setg(&readBuf_, &readBuf_, &readBuf_ + 1);
            return traits_type::to_int_type(readBuf_);
        }
    
    
        int_type overflow(int_type ch) {
            // replace character
            ch = ch - 1;
            return pExternBuf_->sputc(ch);
        }
    
    
        int sync() {
            return pExternBuf_->pubsync();
        }
    
    
        public:
            FilteringBuf(std::streambuf* pExternBuf)
                : pExternBuf_(pExternBuf)
            {
                reset();
            }
    
    
            ~FilteringBuf() {
                sync();
            }
    
    
            void reset() {
                setg(0, 0, 0);
                setp(0, 0);
            }
    };

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    112
    I do not know, no, I'm not a code master.
    And provide all the code, I'm interested, and I'll compile your entire project, and I'll think and read.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. streambuf
    By Dae in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2009, 09:44 AM
  2. Trying to override streambuf to use with cout
    By rob_l_f in forum C++ Programming
    Replies: 6
    Last Post: 01-12-2009, 01:31 AM
  3. problem with streambuf
    By vin_pll in forum C++ Programming
    Replies: 18
    Last Post: 06-22-2008, 04:13 AM
  4. using streambuf when working with files
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2005, 11:31 AM
  5. Runtime error in streambuf
    By alvifarooq in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2004, 07:31 PM

Tags for this Thread