streambuf filter

Discussion in 'C++' started by steve.holle@gmail.com, Mar 30, 2006.

  1. Guest

    Can anyone provide me with a simple example of a streambuf filter for
    character substitution that would function like this :
    fout << filter << fin

    All the filter would need to do is replace a specific character with
    another.

    Thanks for your help.
     
    , Mar 30, 2006
    #1
    1. Advertising

  2. Ben Pope Guest

    wrote:
    > Can anyone provide me with a simple example of a streambuf filter for
    > character substitution that would function like this :
    > fout << filter << fin
    >
    > All the filter would need to do is replace a specific character with
    > another.


    You might try searching for some of Dietmars posts:
    http://groups.google.co.uk/groups?q=group:comp.lang.c++ author:dietmar filter

    I haven't checked them in detail.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
     
    Ben Pope, Mar 31, 2006
    #2
    1. Advertising

  3. Hello Steve,

    > Can anyone provide me with a simple example of a
    > streambuf filter for character substitution that would
    > function like this :
    > fout << filter << fin


    Try this one:

    #include <sstream>
    #include <istream>
    #include <iostream>

    class ReplaceBuf : public std::streambuf
    {
    char readBuf_; // current char
    std::streambuf* pExternBuf_; // source buffer

    // get next input char
    int_type underflow()
    {
    if(gptr() == &readBuf_)
    return traits_type::to_int_type(readBuf_);

    int_type nextChar = pExternBuf_->sbumpc();
    if(nextChar == EOF) return traits_type::eof();
    readBuf_ = traits_type::to_char_type(nextChar);

    // replace character
    if(readBuf_ == 'X') readBuf_ = 'O';

    setg(&readBuf_, &readBuf_, &readBuf_ + 1);
    return traits_type::to_int_type(readBuf_);
    }

    public:
    ReplaceBuf(std::streambuf* pExternBuf)
    : pExternBuf_(pExternBuf)
    {
    setg(0, 0, 0);
    }
    };


    int main()
    {
    std::stringstream test("HELLX WXRLD, TEST...!");
    ReplaceBuf rbuf(test.rdbuf());
    std::istream strm(&rbuf);

    std::string line;
    std::getline(strm, line);
    std::cout << line << "\n";
    }

    Best regards,
    Tilman
     
    Tilman Kuepper, Mar 31, 2006
    #3
  4. Guest

    Exactly what I was looking for.

    Thanks.
     
    , Mar 31, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davis King
    Replies:
    0
    Views:
    510
    Davis King
    Jul 23, 2003
  2. Matt Chaplain

    Problem with streambuf

    Matt Chaplain, Jul 30, 2003, in forum: C++
    Replies:
    4
    Views:
    462
    Matt Chaplain
    Jul 31, 2003
  3. Peter Jansson
    Replies:
    1
    Views:
    518
    David Rubin
    Nov 8, 2004
  4. Christopher Pisz
    Replies:
    2
    Views:
    606
    James Kanze
    Dec 12, 2007
  5. zax75
    Replies:
    1
    Views:
    1,123
Loading...

Share This Page